Skip to content

Commit 08f91c8

Browse files
Merge pull request #1209 from hannesrichter:master
PiperOrigin-RevId: 282059203
2 parents 733e736 + c2291f9 commit 08f91c8

File tree

13 files changed

+43
-17
lines changed

13 files changed

+43
-17
lines changed

tensorflow_datasets/image/deep_weeds.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
from __future__ import division
2020
from __future__ import print_function
2121

22+
import csv
2223
import os
2324

2425
import tensorflow as tf
2526
import tensorflow_datasets.public_api as tfds
2627

27-
2828
_URL = "https://nextcloud.qriscloud.org.au/index.php/s/a3KxPawpqkiorST/download"
29+
_URL_LABELS = "https://raw.githubusercontent.com/AlexOlsen/DeepWeeds/master/labels/labels.csv"
2930

3031
_DESCRIPTION = (
3132
"""The DeepWeeds dataset consists of 17,509 images capturing eight different weed species native to Australia """
@@ -34,11 +35,6 @@
3435
""" "Cluden", "Douglas", "Hervey Range", "Kelso", "McKinlay" and "Paluma"."""
3536
)
3637

37-
_NAMES = [
38-
"Chinee apple", "Snake weed", "Lantana", "Prickly acacia", "Siam weed",
39-
"Parthenium", "Rubber vine", "Parkinsonia", "Negative"
40-
]
41-
4238
_IMAGE_SHAPE = (256, 256, 3)
4339

4440
_CITATION = """\
@@ -73,7 +69,7 @@
7369
class DeepWeeds(tfds.core.GeneratorBasedBuilder):
7470
"""DeepWeeds Image Dataset Class."""
7571

76-
VERSION = tfds.core.Version("1.0.0")
72+
VERSION = tfds.core.Version("2.0.0")
7773

7874
def _info(self):
7975
"""Define Dataset Info."""
@@ -83,7 +79,7 @@ def _info(self):
8379
description=(_DESCRIPTION),
8480
features=tfds.features.FeaturesDict({
8581
"image": tfds.features.Image(shape=_IMAGE_SHAPE),
86-
"label": tfds.features.ClassLabel(names=_NAMES),
82+
"label": tfds.features.ClassLabel(num_classes=9),
8783
}),
8884
supervised_keys=("image", "label"),
8985
homepage="https://github.com/AlexOlsen/DeepWeeds",
@@ -93,24 +89,39 @@ def _info(self):
9389
def _split_generators(self, dl_manager):
9490
"""Define Splits."""
9591
# The file is in ZIP format, but URL doesn't mention it.
96-
path = dl_manager.download_and_extract(
97-
tfds.download.Resource(
92+
paths = dl_manager.download_and_extract({
93+
"image": tfds.download.Resource(
9894
url=_URL,
99-
extract_method=tfds.download.ExtractMethod.ZIP))
95+
extract_method=tfds.download.ExtractMethod.ZIP),
96+
"label": _URL_LABELS})
10097

10198
return [
10299
tfds.core.SplitGenerator(
103100
name="train",
104101
gen_kwargs={
105-
"data_dir_path": path,
102+
"data_dir_path": paths["image"],
103+
"label_path": paths["label"],
106104
},
107105
),
108106
]
109107

110-
def _generate_examples(self, data_dir_path):
108+
def _generate_examples(self, data_dir_path, label_path):
111109
"""Generate images and labels for splits."""
112110

111+
with tf.io.gfile.GFile(label_path) as f:
112+
# Convert to list to reuse the iterator multiple times
113+
reader = list(csv.DictReader(f))
114+
115+
# Extract the mapping int -> str and save the label name string to the
116+
# feature
117+
label_id_to_name = {int(row["Label"]): row["Species"] for row in reader}
118+
self.info.features["label"].names = [
119+
v for _, v in sorted(label_id_to_name.items())
120+
]
121+
122+
filename_to_label = {row["Filename"]: row["Species"] for row in reader}
113123
for file_name in tf.io.gfile.listdir(data_dir_path):
114-
image = os.path.join(data_dir_path, file_name)
115-
label = _NAMES[int(file_name.split("-")[2].split(".")[0])]
116-
yield file_name, {"image": image, "label": label}
124+
yield file_name, {
125+
"image": os.path.join(data_dir_path, file_name),
126+
"label": filename_to_label[file_name]
127+
}

tensorflow_datasets/image/deep_weeds_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
class DeepWeedsTest(tfds_test.DatasetBuilderTestCase):
2424
DATASET_CLASS = deep_weeds.DeepWeeds
2525
SPLITS = {
26-
"train": 4,
26+
"train": 9,
27+
}
28+
DL_EXTRACT_RESULT = {
29+
"image": "images",
30+
"label": "labels.csv",
2731
}
2832

2933

Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)