Skip to content

Commit 4d7308c

Browse files
Merge pull request #238 from captain-pool:issue_203
PiperOrigin-RevId: 264108693
2 parents fb82e5d + 8bf4cbd commit 4d7308c

File tree

12 files changed

+124
-0
lines changed

12 files changed

+124
-0
lines changed

tensorflow_datasets/image/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
from tensorflow_datasets.image.smallnorb import Smallnorb
7171
from tensorflow_datasets.image.so2sat import So2sat
7272
from tensorflow_datasets.image.stanford_dogs import StanfordDogs
73+
from tensorflow_datasets.image.stanford_online_products import StanfordOnlineProducts
7374
from tensorflow_datasets.image.sun import Sun397
7475
from tensorflow_datasets.image.svhn import SvhnCropped
7576
from tensorflow_datasets.image.uc_merced import UcMerced
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
"""Stanford Online Products Dataset."""
17+
import csv
18+
import os
19+
import tensorflow as tf
20+
import tensorflow_datasets.public_api as tfds
21+
22+
_DOWNLOAD_LINK = "ftp://cs.stanford.edu/cs/cvgl/Stanford_Online_Products.zip"
23+
_SPLITS = {tfds.Split.TRAIN: "Ebay_train", tfds.Split.TEST: "Ebay_test"}
24+
25+
_SUPER_CLASSES = [
26+
"bicycle", "cabinet", "chair", "coffee_maker", "fan", "kettle", "lamp",
27+
"mug", "sofa", "stapler", "table", "toaster"
28+
]
29+
_CITATION = """\
30+
@inproceedings{song2016deep,
31+
author = {Song, Hyun Oh and Xiang, Yu and Jegelka, Stefanie and Savarese, Silvio},
32+
title = {Deep Metric Learning via Lifted Structured Feature Embedding},
33+
booktitle = {IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
34+
year = {2016}
35+
}
36+
"""
37+
38+
39+
class StanfordOnlineProducts(tfds.core.GeneratorBasedBuilder):
40+
"""Stanford Online Products Dataset."""
41+
42+
VERSION = tfds.core.Version("1.0.0")
43+
44+
def _info(self):
45+
return tfds.core.DatasetInfo(
46+
description=("Stanford Online Products Dataset"),
47+
builder=self,
48+
citation=_CITATION,
49+
urls=["http://cvgl.stanford.edu/projects/lifted_struct/"],
50+
features=tfds.features.FeaturesDict({
51+
"class_id":
52+
tfds.features.ClassLabel(num_classes=22634),
53+
"super_class_id/num":
54+
tfds.features.ClassLabel(num_classes=len(_SUPER_CLASSES)),
55+
"super_class_id":
56+
tfds.features.ClassLabel(names=_SUPER_CLASSES),
57+
"image":
58+
tfds.features.Image()
59+
}))
60+
61+
def _split_generators(self, dl_manager):
62+
dl_path = dl_manager.download_and_extract(_DOWNLOAD_LINK)
63+
folder_path = os.path.join(dl_path, "Stanford_Online_Products")
64+
return [ # pylint:disable=g-complex-comprehension
65+
tfds.core.SplitGenerator(
66+
name=k,
67+
gen_kwargs={"file_path": os.path.join(folder_path, "%s.txt" % v)})
68+
for k, v in _SPLITS.items()
69+
]
70+
71+
def _generate_examples(self, file_path):
72+
"""Images of Product from the Data Directory.
73+
74+
Args:
75+
file_path: str, path to the Ebay_(train/test/info).txt file. Having
76+
Columns ['class_id', 'super_class_id', 'path']
77+
Yields:
78+
Dataset examples.
79+
"""
80+
with tf.io.gfile.GFile(file_path, "r") as file_:
81+
dataset = csv.DictReader(file_, delimiter=" ")
82+
for i, row in enumerate(dataset):
83+
yield i, {
84+
"class_id": int(row["class_id"]) - 1,
85+
"super_class_id/num": int(row["super_class_id"]) - 1,
86+
"super_class_id": _SUPER_CLASSES[int(row["super_class_id"]) - 1],
87+
"image": os.path.join(os.path.dirname(file_path), row["path"])
88+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
from tensorflow_datasets.image import stanford_online_products
17+
import tensorflow_datasets.testing as tfds_test
18+
19+
20+
class StanfordOnlineProductsTest(tfds_test.DatasetBuilderTestCase):
21+
DATASET_CLASS = stanford_online_products.StanfordOnlineProducts
22+
SPLITS = {"train": 3, "test": 3}
23+
24+
25+
if __name__ == "__main__":
26+
tfds_test.test_main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class_id super_class_id path
2+
115308 21635 11 table_final/381052627539_0.JPG
3+
80418 14815 4 coffee_maker_final/371344103996_3.JPG
4+
82898 15277 5 fan_final/321823124835_0.JPG
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class_id super_class_id path
2+
2607 365 1 bicycle_final/181415767003_7.JPG
3+
16154 2727 4 coffee_maker_final/111737519223_7.JPG
4+
25971 4540 6 kettle_final/151781232926_10.JPG
25.9 KB
Loading
15.9 KB
Loading
17.4 KB
Loading
19 KB
Loading
13.5 KB
Loading

0 commit comments

Comments
 (0)