|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2019 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Dataset class for DeepWeeds dataset.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import os |
| 23 | + |
| 24 | +import tensorflow as tf |
| 25 | +import tensorflow_datasets.public_api as tfds |
| 26 | + |
| 27 | + |
| 28 | +_URL = "https://nextcloud.qriscloud.org.au/index.php/s/a3KxPawpqkiorST/download" |
| 29 | + |
| 30 | +_DESCRIPTION = ( |
| 31 | + """The DeepWeeds dataset consists of 17,509 images capturing eight different weed species native to Australia """ |
| 32 | + """in situ with neighbouring flora.The selected weed species are local to pastoral grasslands across the state of Queensland.""" |
| 33 | + """The images were collected from weed infestations at the following sites across Queensland: "Black River", "Charters Towers", """ |
| 34 | + """ "Cluden", "Douglas", "Hervey Range", "Kelso", "McKinlay" and "Paluma".""" |
| 35 | +) |
| 36 | + |
| 37 | +_NAMES = [ |
| 38 | + "Chinee apple", "Snake weed", "Lantana", "Prickly acacia", "Siam weed", |
| 39 | + "Parthenium", "Rubber vine", "Parkinsonia", "Negative" |
| 40 | +] |
| 41 | + |
| 42 | +_IMAGE_SHAPE = (256, 256, 3) |
| 43 | + |
| 44 | +_CITATION = """\ |
| 45 | + @article{DeepWeeds2019, |
| 46 | + author = {Alex Olsen and |
| 47 | + Dmitry A. Konovalov and |
| 48 | + Bronson Philippa and |
| 49 | + Peter Ridd and |
| 50 | + Jake C. Wood and |
| 51 | + Jamie Johns and |
| 52 | + Wesley Banks and |
| 53 | + Benjamin Girgenti and |
| 54 | + Owen Kenny and |
| 55 | + James Whinney and |
| 56 | + Brendan Calvert and |
| 57 | + Mostafa {Rahimi Azghadi} and |
| 58 | + Ronald D. White}, |
| 59 | + title = {{DeepWeeds: A Multiclass Weed Species Image Dataset for Deep Learning}}, |
| 60 | + journal = {Scientific Reports}, |
| 61 | + year = 2019, |
| 62 | + number = 2058, |
| 63 | + month = 2, |
| 64 | + volume = 9, |
| 65 | + issue = 1, |
| 66 | + day = 14, |
| 67 | + url = "https://doi.org/10.1038/s41598-018-38343-3", |
| 68 | + doi = "10.1038/s41598-018-38343-3" |
| 69 | +} |
| 70 | +""" |
| 71 | + |
| 72 | + |
| 73 | +class DeepWeeds(tfds.core.GeneratorBasedBuilder): |
| 74 | + """DeepWeeds Image Dataset Class.""" |
| 75 | + |
| 76 | + VERSION = tfds.core.Version("1.0.0") |
| 77 | + |
| 78 | + def _info(self): |
| 79 | + """Define Dataset Info.""" |
| 80 | + |
| 81 | + return tfds.core.DatasetInfo( |
| 82 | + builder=self, |
| 83 | + description=(_DESCRIPTION), |
| 84 | + features=tfds.features.FeaturesDict({ |
| 85 | + "image": tfds.features.Image(shape=_IMAGE_SHAPE), |
| 86 | + "label": tfds.features.ClassLabel(names=_NAMES), |
| 87 | + }), |
| 88 | + supervised_keys=("image", "label"), |
| 89 | + urls=[_URL], |
| 90 | + citation=_CITATION, |
| 91 | + ) |
| 92 | + |
| 93 | + def _split_generators(self, dl_manager): |
| 94 | + """Define Splits.""" |
| 95 | + path = dl_manager.download_and_extract(_URL) |
| 96 | + |
| 97 | + return [ |
| 98 | + tfds.core.SplitGenerator( |
| 99 | + name="train", |
| 100 | + gen_kwargs={ |
| 101 | + "data_dir_path": path, |
| 102 | + }, |
| 103 | + ), |
| 104 | + ] |
| 105 | + |
| 106 | + def _generate_examples(self, data_dir_path): |
| 107 | + """Generate images and labels for splits.""" |
| 108 | + |
| 109 | + for file_name in tf.io.gfile.listdir(data_dir_path): |
| 110 | + image = os.path.join(data_dir_path, file_name) |
| 111 | + label = _NAMES[int(file_name.split("-")[2].split(".")[0])] |
| 112 | + yield file_name, {"image": image, "label": label} |
0 commit comments