|
2 | 2 |
|
3 | 3 | import tensorflow_datasets.public_api as tfds
|
4 | 4 |
|
| 5 | +_TRAIN_IMAGE_IDS = [9270406, 9270356, 9270408, 9270367, 9270349, 9270351, 9270390, 9270375, 9270387, 9270370, 9270396, 9270340, 9270411, 9270369, 9270357, 9270378, 9270386, 9270376, 9270341, 9270392, 9270334, 9270404, 9270330, 9270321, 9270364, 9270380, 9270343, 9270335, 9270412, 9270362, 9270339, 9270331, 9270399, 9270410, 9270393, 9270325, 9270346, 9270337, 9270391, 9270361, 9270363, 9270372, 9270326, 9270322, 9270329, 9270381, 9270338, 9270397, 9270405, 9270379, 9270352, 9270400, 9270384, 9270383, 9270388, 9270324, 9270407, 9270348, 9270347, 9270371, 9270358, 9270350, 9270323, 9270401, 9270368, 9270360, 9270328, 9270327, 9270382, 9270332, 9270394, 9270409, 9270345, 9270342, 9270353, 9270403, 9270398, 9270402, 9270395, 9270333, 9270373, 9270336, 9270385, 9270320, 9270366, 9270374, 9270377, 9270354, 9270344, 9270359] |
| 6 | + |
| 7 | +_URLS = { |
| 8 | + 'train_images': [ |
| 9 | + tfds.download.Resource( |
| 10 | + url=f'https://dataverse.harvard.edu/api/access/datafile/{id}?format=original', |
| 11 | + extract_method=tfds.download.ExtractMethod.GZIP, |
| 12 | + ) |
| 13 | + for id in _TRAIN_IMAGE_IDS |
| 14 | + ], |
| 15 | + 'validation_images': tfds.download.Resource( |
| 16 | + url='https://dataverse.harvard.edu/api/access/datafile/9270355?format=original', |
| 17 | + extract_method=tfds.download.ExtractMethod.GZIP, |
| 18 | + ), |
| 19 | + 'test_images': tfds.download.Resource( |
| 20 | + url='https://dataverse.harvard.edu/api/access/datafile/9270389?format=original', |
| 21 | + extract_method=tfds.download.ExtractMethod.GZIP, |
| 22 | + ), |
| 23 | + 'train_image_metadata': 'https://dataverse.harvard.edu/api/access/datafile/9844933?format=original', |
| 24 | + 'train_bbox_metadata': 'https://dataverse.harvard.edu/api/access/datafile/9844934?format=original', |
| 25 | + 'validation_metadata': 'https://dataverse.harvard.edu/api/access/datafile/9844936?format=original', |
| 26 | + 'test_metadata': 'https://dataverse.harvard.edu/api/access/datafile/9844935?format=original', |
| 27 | +} |
5 | 28 |
|
6 | 29 | class Builder(tfds.core.GeneratorBasedBuilder):
|
7 | 30 | """DatasetBuilder for wake_vision dataset."""
|
8 | 31 |
|
9 | 32 | VERSION = tfds.core.Version('1.0.0')
|
10 | 33 | RELEASE_NOTES = {
|
11 |
| - '1.0.0': 'Initial release.', |
| 34 | + '1.0.0': 'Initial TensorFlow Datasets release. Note that this is based on the 2.0 version of Wake Vision on Harvard Dataverse.', |
12 | 35 | }
|
13 | 36 |
|
14 | 37 | def _info(self) -> tfds.core.DatasetInfo:
|
15 | 38 | """Returns the dataset metadata."""
|
16 |
| - # TODO(wake_vision): Specifies the tfds.core.DatasetInfo object |
17 | 39 | return self.dataset_info_from_configs(
|
| 40 | + description= |
| 41 | + """ |
| 42 | + The Wake Vision dataset for person detection. |
| 43 | +
|
| 44 | + The dataset contains images with annotations of whether each image contains a person. Additional annotations about perceived gender, perceived age, subject distance, lighting conditions, depictions, and specific body parts are also available for some subsets of the dataset. |
| 45 | +
|
| 46 | + We publish the annotations of this dataset under a CC BY 4.0 license. All images in the dataset are from the Open Images v7 dataset, which sourced images from Flickr listed as having a CC BY 2.0 license. |
| 47 | + """ |
| 48 | + , |
18 | 49 | features=tfds.features.FeaturesDict({
|
19 | 50 | # These are the features of your dataset like images, labels ...
|
20 | 51 | 'image': tfds.features.Image(shape=(None, None, 3)),
|
21 |
| - 'label': tfds.features.ClassLabel(names=['no', 'yes']), |
| 52 | + 'filename': tfds.features.Text(), |
| 53 | + 'person': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 54 | + 'depiction': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 55 | + 'body_part': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 56 | + 'predominantly_female': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 57 | + 'predominantly_male': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 58 | + 'gender_unknown': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 59 | + 'young': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 60 | + 'middle_age': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 61 | + 'older': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 62 | + 'age_unknown': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 63 | + 'near': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 64 | + 'medium_distance': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 65 | + 'far': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 66 | + 'dark': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 67 | + 'normal_lighting': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 68 | + 'bright': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 69 | + 'person_depiction': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 70 | + 'non-person_depiction': tfds.features.ClassLabel(names=['No', 'Yes']), |
| 71 | + 'non-person_non-depiction': tfds.features.ClassLabel(names=['No', 'Yes']), |
22 | 72 | }),
|
23 | 73 | # If there's a common (input, target) tuple from the
|
24 | 74 | # features, specify them here. They'll be used if
|
25 | 75 | # `as_supervised=True` in `builder.as_dataset`.
|
26 |
| - supervised_keys=('image', 'label'), # Set to `None` to disable |
27 |
| - homepage='https://dataset-homepage/', |
| 76 | + supervised_keys=('image', 'person'), # Set to `None` to disable |
| 77 | + homepage='https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910%2FDVN%2F1HOPXC', |
| 78 | + license='See homepage for license information.', |
28 | 79 | )
|
29 | 80 |
|
30 | 81 | def _split_generators(self, dl_manager: tfds.download.DownloadManager):
|
31 | 82 | """Returns SplitGenerators."""
|
32 |
| - # TODO(wake_vision): Downloads the data and defines the splits |
33 |
| - path = dl_manager.download_and_extract('https://todo-data-url') |
| 83 | + paths = dl_manager.download_and_extract(_URLS) |
34 | 84 |
|
35 |
| - # TODO(wake_vision): Returns the Dict[split names, Iterator[Key, Example]] |
36 | 85 | return {
|
37 |
| - 'train': self._generate_examples(path / 'train_imgs'), |
| 86 | + 'train_image': self._generate_examples(paths['train_images'], paths['train_image_metadata']), |
| 87 | + 'train_bbox': self._generate_examples(paths['train_images'], paths['train_bbox_metadata']), |
| 88 | + 'validation' : self._generate_examples(paths['validation_images'], paths['validation_metadata']), |
| 89 | + 'test' : self._generate_examples(paths['test_images'], paths['test_metadata']), |
38 | 90 | }
|
39 | 91 |
|
40 |
| - def _generate_examples(self, path): |
| 92 | + def _generate_examples(self, image_paths, metadata_path): |
41 | 93 | """Yields examples."""
|
42 |
| - # TODO(wake_vision): Yields (key, example) tuples from the dataset |
43 |
| - for f in path.glob('*.jpeg'): |
44 |
| - yield 'key', { |
45 |
| - 'image': f, |
46 |
| - 'label': 'yes', |
47 |
| - } |
| 94 | + metadata = tfds.core.lazy_imports.pandas.read_csv(metadata_path, index_col='filename') |
| 95 | + |
| 96 | + for tar_file in image_paths: |
| 97 | + for sample_path, sample_object in tfds.download.iter_archive(tar_file, tfds.download.ExtractMethod.TAR_STREAM): |
| 98 | + file_name = sample_path |
| 99 | + |
| 100 | + sample_metadata = metadata.loc[file_name] |
| 101 | + |
| 102 | + yield file_name, { |
| 103 | + 'image': sample_object, |
| 104 | + 'filename': file_name, |
| 105 | + 'person': sample_metadata['person'], |
| 106 | + 'depiction': sample_metadata['depiction'], |
| 107 | + 'body_part': sample_metadata['body_part'], |
| 108 | + 'predominantly_female': sample_metadata['predominantly_female'], |
| 109 | + 'predominantly_male': sample_metadata['predominantly_male'], |
| 110 | + 'gender_unknown': sample_metadata['gender_unknown'], |
| 111 | + 'young': sample_metadata['young'], |
| 112 | + 'middle_age': sample_metadata['middle_age'], |
| 113 | + 'older': sample_metadata['older'], |
| 114 | + 'age_unknown': sample_metadata['age_unknown'], |
| 115 | + 'near': sample_metadata['near'], |
| 116 | + 'medium_distance': sample_metadata['medium_distance'], |
| 117 | + 'far': sample_metadata['far'], |
| 118 | + 'dark': sample_metadata['dark'], |
| 119 | + 'normal_lighting': sample_metadata['normal_lighting'], |
| 120 | + 'bright': sample_metadata['bright'], |
| 121 | + 'person_depiction': sample_metadata['person_depiction'], |
| 122 | + 'non-person_depiction': sample_metadata['non-person_depiction'], |
| 123 | + 'non-person_non-depiction': sample_metadata['non-person_non-depiction'], |
| 124 | + } |
0 commit comments