Skip to content

Commit e12e4c8

Browse files
Merge pull request #613 from ChanchalKumarMaji:Downsampled-ImageNet
PiperOrigin-RevId: 250990035
2 parents 2dae77d + 9e46a30 commit e12e4c8

File tree

13 files changed

+192
-0
lines changed

13 files changed

+192
-0
lines changed

docs/release_notes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@
22

33
## Nightly
44

5+
### New datasets
6+
7+
* Image:
8+
[downsampled_imagenet](https://github.com/tensorflow/datasets/tree/master/docs/datasets.md#downsampled_imagenet)
9+
10+
### Features
11+
512
* It is now possible to add arbitrary metadata to `tfds.core.DatasetInfo`
613
which will be stored/restored with the dataset. See `tfds.core.Metadata`.

tensorflow_datasets/image/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from tensorflow_datasets.image.colorectal_histology import ColorectalHistologyLarge
3232
from tensorflow_datasets.image.cycle_gan import CycleGAN
3333
from tensorflow_datasets.image.diabetic_retinopathy_detection import DiabeticRetinopathyDetection
34+
from tensorflow_datasets.image.downsampled_imagenet import DownsampledImagenet
3435
from tensorflow_datasets.image.dsprites import Dsprites
3536
from tensorflow_datasets.image.dtd import Dtd
3637
from tensorflow_datasets.image.eurosat import Eurosat
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
"""Downsampled Imagenet 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+
26+
from tensorflow_datasets.core import api_utils
27+
import tensorflow_datasets.public_api as tfds
28+
29+
_CITATION = """\
30+
@article{DBLP:journals/corr/OordKK16,
31+
author = {A{\"{a}}ron van den Oord and
32+
Nal Kalchbrenner and
33+
Koray Kavukcuoglu},
34+
title = {Pixel Recurrent Neural Networks},
35+
journal = {CoRR},
36+
volume = {abs/1601.06759},
37+
year = {2016},
38+
url = {http://arxiv.org/abs/1601.06759},
39+
archivePrefix = {arXiv},
40+
eprint = {1601.06759},
41+
timestamp = {Mon, 13 Aug 2018 16:46:29 +0200},
42+
biburl = {https://dblp.org/rec/bib/journals/corr/OordKK16},
43+
bibsource = {dblp computer science bibliography, https://dblp.org}
44+
}
45+
"""
46+
47+
_DESCRIPTION = """\
48+
Dataset with images of 2 resolutions (see config name for information on the resolution).
49+
It is used for density estimation and generative modeling experiments.
50+
"""
51+
52+
_DL_URL = "http://image-net.org/small/"
53+
54+
_DATA_OPTIONS = ["32x32", "64x64"]
55+
56+
57+
class DownsampledImagenetConfig(tfds.core.BuilderConfig):
58+
"""BuilderConfig for Downsampled Imagenet."""
59+
60+
@api_utils.disallow_positional_args
61+
def __init__(self, data=None, **kwargs):
62+
"""Constructs a DownsampledImagenetConfig.
63+
64+
Args:
65+
data: `str`, one of `_DATA_OPTIONS`.
66+
**kwargs: keyword arguments forwarded to super.
67+
"""
68+
if data not in _DATA_OPTIONS:
69+
raise ValueError("data must be one of %s" % _DATA_OPTIONS)
70+
71+
super(DownsampledImagenetConfig, self).__init__(**kwargs)
72+
self.data = data
73+
74+
75+
class DownsampledImagenet(tfds.core.GeneratorBasedBuilder):
76+
"""Downsampled Imagenet dataset."""
77+
78+
BUILDER_CONFIGS = [
79+
DownsampledImagenetConfig( # pylint: disable=g-complex-comprehension
80+
name=config_name,
81+
description=(
82+
"A dataset consisting of Train and Validation images of " +
83+
config_name + " resolution."),
84+
version="0.1.0",
85+
data=config_name,
86+
) for config_name in _DATA_OPTIONS
87+
]
88+
89+
def _info(self):
90+
return tfds.core.DatasetInfo(
91+
builder=self,
92+
description=_DESCRIPTION,
93+
features=tfds.features.FeaturesDict({
94+
"image": tfds.features.Image(),
95+
}),
96+
supervised_keys=None,
97+
urls=["http://image-net.org/small/download.php"],
98+
)
99+
100+
def _split_generators(self, dl_manager):
101+
"""Returns SplitGenerators."""
102+
103+
train_url = _DL_URL + "train_" + self.builder_config.name + ".tar"
104+
valid_url = _DL_URL + "valid_" + self.builder_config.name + ".tar"
105+
106+
extracted_paths = dl_manager.download_and_extract({
107+
"train_images": train_url,
108+
"valid_images": valid_url,
109+
})
110+
111+
return [
112+
tfds.core.SplitGenerator(
113+
name=tfds.Split.TRAIN,
114+
num_shards=10,
115+
gen_kwargs={
116+
"path":
117+
os.path.join(extracted_paths["train_images"],
118+
"train_" + self.builder_config.name),
119+
}),
120+
tfds.core.SplitGenerator(
121+
name=tfds.Split.VALIDATION,
122+
num_shards=1,
123+
gen_kwargs={
124+
"path":
125+
os.path.join(extracted_paths["valid_images"],
126+
"valid_" + self.builder_config.name),
127+
}),
128+
]
129+
130+
def _generate_examples(self, path):
131+
images = tf.io.gfile.listdir(path)
132+
133+
for image in images:
134+
yield {
135+
"image": os.path.join(path, image),
136+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
"""Tests for downsampled_imagenet dataset module."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
23+
import tensorflow_datasets as tfds
24+
from tensorflow_datasets import testing
25+
from tensorflow_datasets.image import downsampled_imagenet
26+
27+
28+
class DownsampledImagenetTest(testing.DatasetBuilderTestCase):
29+
DATASET_CLASS = downsampled_imagenet.DownsampledImagenet
30+
BUILDER_CONFIG_NAMES_TO_TEST = ["32x32", "64x64"]
31+
32+
SPLITS = {
33+
tfds.Split.TRAIN: 2,
34+
tfds.Split.VALIDATION: 2,
35+
}
36+
37+
DL_EXTRACT_RESULT = {
38+
"train_images": "train_images",
39+
"valid_images": "valid_images",
40+
}
41+
42+
43+
if __name__ == "__main__":
44+
testing.test_main()

0 commit comments

Comments
 (0)