Skip to content

Commit ecf381a

Browse files
TensorFlow Datasets Teamcopybara-github
authored andcommitted
Adds Caltech-101. Includes only the image-level labels, and not the bounding boxes.
PiperOrigin-RevId: 239160178
1 parent f56435d commit ecf381a

File tree

12 files changed

+333
-0
lines changed

12 files changed

+333
-0
lines changed

tensorflow_datasets/image/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
"""Image datasets."""
1717

18+
from tensorflow_datasets.image.caltech import Caltech101
1819
from tensorflow_datasets.image.cats_vs_dogs import CatsVsDogs
1920
from tensorflow_datasets.image.celeba import CelebA
2021
from tensorflow_datasets.image.celebahq import CelebAHq
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
accordion
2+
airplanes
3+
anchor
4+
ant
5+
background_google
6+
barrel
7+
bass
8+
beaver
9+
binocular
10+
bonsai
11+
brain
12+
brontosaurus
13+
buddha
14+
butterfly
15+
camera
16+
cannon
17+
car_side
18+
ceiling_fan
19+
cellphone
20+
chair
21+
chandelier
22+
cougar_body
23+
cougar_face
24+
crab
25+
crayfish
26+
crocodile
27+
crocodile_head
28+
cup
29+
dalmatian
30+
dollar_bill
31+
dolphin
32+
dragonfly
33+
electric_guitar
34+
elephant
35+
emu
36+
euphonium
37+
ewer
38+
faces
39+
faces_easy
40+
ferry
41+
flamingo
42+
flamingo_head
43+
garfield
44+
gerenuk
45+
gramophone
46+
grand_piano
47+
hawksbill
48+
headphone
49+
hedgehog
50+
helicopter
51+
ibis
52+
inline_skate
53+
joshua_tree
54+
kangaroo
55+
ketch
56+
lamp
57+
laptop
58+
leopards
59+
llama
60+
lobster
61+
lotus
62+
mandolin
63+
mayfly
64+
menorah
65+
metronome
66+
minaret
67+
motorbikes
68+
nautilus
69+
octopus
70+
okapi
71+
pagoda
72+
panda
73+
pigeon
74+
pizza
75+
platypus
76+
pyramid
77+
revolver
78+
rhino
79+
rooster
80+
saxophone
81+
schooner
82+
scissors
83+
scorpion
84+
sea_horse
85+
snoopy
86+
soccer_ball
87+
stapler
88+
starfish
89+
stegosaurus
90+
stop_sign
91+
strawberry
92+
sunflower
93+
tick
94+
trilobite
95+
umbrella
96+
watch
97+
water_lilly
98+
wheelchair
99+
wild_cat
100+
windsor_chair
101+
wrench
102+
yin_yang
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 flowers data loading."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
from tensorflow_datasets import testing
22+
from tensorflow_datasets.image import caltech
23+
24+
25+
class Caltech101Test(testing.DatasetBuilderTestCase):
26+
DATASET_CLASS = caltech.Caltech101
27+
28+
SPLITS = {
29+
'train': 6
30+
}
31+
32+
if __name__ == '__main__':
33+
testing.test_main()
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
r"""Script to generate Caltech101 like files with random data for testing.
17+
18+
"""
19+
20+
from __future__ import absolute_import
21+
from __future__ import division
22+
from __future__ import print_function
23+
24+
import os
25+
26+
from absl import app
27+
from absl import flags
28+
29+
import numpy as np
30+
import tensorflow as tf
31+
32+
from tensorflow_datasets.core import utils
33+
from tensorflow_datasets.core.utils import py_utils
34+
from tensorflow_datasets.image import caltech
35+
import tensorflow_datasets.public_api as tfds
36+
from tensorflow_datasets.testing import fake_data_utils
37+
38+
flags.DEFINE_string("tfds_dir", py_utils.tfds_dir(),
39+
"Path to tensorflow_datasets directory")
40+
41+
FLAGS = flags.FLAGS
42+
43+
NUM_CLASSES = 3
44+
IMAGES_PER_CLASS = 2
45+
MIN_EDGE_LENGTH = 150
46+
MAX_EDGE_LENGTH = 350
47+
48+
49+
def _output_dir():
50+
"""Returns output directory."""
51+
return os.path.join(FLAGS.tfds_dir, "testing", "test_data", "fake_examples",
52+
"caltech101", "{}_ObjectCategories".format(NUM_CLASSES))
53+
54+
55+
def _save_image(jpeg, label, image_idx):
56+
"""Saves jpeg."""
57+
dirname = os.path.join(_output_dir(), label)
58+
if not os.path.exists(dirname):
59+
os.makedirs(dirname)
60+
path = os.path.join(dirname, "image_{:04d}.jpg".format(image_idx))
61+
with open(path, "wb") as out_file:
62+
out_file.write(jpeg)
63+
64+
65+
def _get_jpeg(height, width):
66+
"""Returns jpeg picture."""
67+
image = fake_data_utils.get_random_picture(height, width)
68+
jpeg = tf.image.encode_jpeg(image)
69+
with utils.nogpu_session() as sess:
70+
res = sess.run(jpeg)
71+
return res
72+
73+
74+
def _generate_images():
75+
"""Generates training images."""
76+
names_file = tfds.core.get_tfds_path(caltech._LABELS_FNAME) # pylint: disable=protected-access
77+
label_names = tfds.features.ClassLabel(
78+
names_file=names_file).names[:NUM_CLASSES]
79+
for label in label_names:
80+
for i in range(IMAGES_PER_CLASS):
81+
height = np.random.randint(low=MIN_EDGE_LENGTH, high=MAX_EDGE_LENGTH)
82+
width = np.random.randint(low=MIN_EDGE_LENGTH, high=MAX_EDGE_LENGTH)
83+
jpeg = _get_jpeg(height=height, width=width)
84+
_save_image(jpeg, label, i + 1)
85+
86+
87+
def main(argv):
88+
if len(argv) > 1:
89+
raise app.UsageError("Too many command-line arguments.")
90+
_generate_images()
91+
92+
93+
if __name__ == "__main__":
94+
app.run(main)
103 KB
Loading
56.3 KB
Loading
67.6 KB
Loading
59.2 KB
Loading
103 KB
Loading

0 commit comments

Comments
 (0)