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