|
| 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 | +"""Caltech images dataset.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import os |
| 23 | +import tensorflow as tf |
| 24 | +import tensorflow_datasets.public_api as tfds |
| 25 | + |
| 26 | +_CITATION = """\ |
| 27 | +@article{FeiFei2004LearningGV, |
| 28 | + title={Learning Generative Visual Models from Few Training Examples: An Incremental Bayesian Approach Tested on 101 Object Categories}, |
| 29 | + author={Li Fei-Fei and Rob Fergus and Pietro Perona}, |
| 30 | + journal={Computer Vision and Pattern Recognition Workshop}, |
| 31 | + year={2004}, |
| 32 | +} |
| 33 | +""" |
| 34 | +_DESCRIPTION = """\ |
| 35 | +Caltech-101 consists of pictures of objects belonging to 101 classes, plus |
| 36 | +one `background clutter` class. Each image is labelled with a single object. |
| 37 | +Each class contains roughly 40 to 800 images, totalling around 9k images. |
| 38 | +Images are of variable sizes, with typical edge lengths of 200-300 pixels. |
| 39 | +This version contains image-level labels only. The original dataset also |
| 40 | +contains bounding boxes. |
| 41 | +""" |
| 42 | +_LABELS_FNAME = "image/caltech101_labels.txt" |
| 43 | +_URL = "http://www.vision.caltech.edu/Image_Datasets/Caltech101/" |
| 44 | +_IMAGES_FNAME = "101_ObjectCategories.tar.gz" |
| 45 | + |
| 46 | + |
| 47 | +class Caltech101(tfds.core.GeneratorBasedBuilder): |
| 48 | + """Caltech-101.""" |
| 49 | + |
| 50 | + VERSION = tfds.core.Version("1.0.0") |
| 51 | + |
| 52 | + def _info(self): |
| 53 | + names_file = tfds.core.get_tfds_path(_LABELS_FNAME) |
| 54 | + return tfds.core.DatasetInfo( |
| 55 | + builder=self, |
| 56 | + description=_DESCRIPTION, |
| 57 | + features=tfds.features.FeaturesDict({ |
| 58 | + "image": tfds.features.Image(), |
| 59 | + "label": tfds.features.ClassLabel(names_file=names_file), |
| 60 | + "image/file_name": tfds.features.Text(), # E.g. 'image_0001.jpg'. |
| 61 | + }), |
| 62 | + supervised_keys=("image", "label"), |
| 63 | + urls=[_URL], |
| 64 | + citation=_CITATION |
| 65 | + ) |
| 66 | + |
| 67 | + def _split_generators(self, dl_manager): |
| 68 | + path = dl_manager.download_and_extract(os.path.join(_URL, _IMAGES_FNAME)) |
| 69 | + # There is no predefined train/val/test split for this dataset. |
| 70 | + return [ |
| 71 | + tfds.core.SplitGenerator( |
| 72 | + name=tfds.Split.TRAIN, |
| 73 | + num_shards=5, |
| 74 | + gen_kwargs={ |
| 75 | + "images_dir_path": path |
| 76 | + }), |
| 77 | + ] |
| 78 | + |
| 79 | + def _generate_examples(self, images_dir_path): |
| 80 | + """Generates images and labels given the image directory path. |
| 81 | +
|
| 82 | + Args: |
| 83 | + images_dir_path: path to the directory where the images are stored. |
| 84 | +
|
| 85 | + Yields: |
| 86 | + The image path, and its corresponding label and filename. |
| 87 | + """ |
| 88 | + parent_dir = tf.io.gfile.listdir(images_dir_path)[0] |
| 89 | + walk_dir = os.path.join(images_dir_path, parent_dir) |
| 90 | + dirs = tf.io.gfile.listdir(walk_dir) |
| 91 | + |
| 92 | + for d in dirs: |
| 93 | + if tf.io.gfile.isdir(os.path.join(walk_dir, d)): |
| 94 | + for full_path, _, fname in tf.io.gfile.walk(os.path.join(walk_dir, d)): |
| 95 | + for image_file in fname: |
| 96 | + if image_file.endswith(".jpg"): |
| 97 | + image_path = os.path.join(full_path, image_file) |
| 98 | + yield { |
| 99 | + "image": image_path, |
| 100 | + "label": d.lower(), |
| 101 | + "image/file_name": image_file, |
| 102 | + } |
0 commit comments