|
| 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 | + } |
0 commit comments