|
| 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 | +"""PetFinder 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 pandas as pd |
| 24 | +import tensorflow as tf |
| 25 | +import tensorflow_datasets.public_api as tfds |
| 26 | + |
| 27 | +# petfinder: BibTeX citation |
| 28 | +_CITATION = """ |
| 29 | +@ONLINE {kaggle-petfinder-adoption-prediction, |
| 30 | + author = "Kaggle and PetFinder.my", |
| 31 | + title = "PetFinder.my Adoption Prediction", |
| 32 | + month = "april", |
| 33 | + year = "2019", |
| 34 | + url = "https://www.kaggle.com/c/petfinder-adoption-prediction/data/" |
| 35 | +} |
| 36 | +""" |
| 37 | + |
| 38 | +_URL = ("https://storage.googleapis.com/petfinder_dataset/") |
| 39 | +_DATA_OPTIONS = [ |
| 40 | + "test_metadata", "test_images", "test_sentiment", "train_metadata", |
| 41 | + "train_images", "train_sentiment" |
| 42 | +] |
| 43 | +_LABEL_OPTIONS = [ |
| 44 | + "test", "train", "breed_labels", "state_labels", "color_labels" |
| 45 | +] |
| 46 | + |
| 47 | +_DL_URLS = {name: _URL + name + ".zip" for name in _DATA_OPTIONS} |
| 48 | +_DL_URLS.update({label: _URL + label + ".csv" for label in _LABEL_OPTIONS}) |
| 49 | + |
| 50 | +_INT_FEATS = [ |
| 51 | + "Type", "Age", "Breed1", "Breed2", "Gender", "Color1", "Color2", "Color3", |
| 52 | + "MaturitySize", "FurLength", "Vaccinated", "Dewormed", "Sterilized", |
| 53 | + "Health", "Quantity", "Fee", "State", "VideoAmt" |
| 54 | +] |
| 55 | +_FLOAT_FEATS = ["PhotoAmt"] |
| 56 | +_OBJ_FEATS = ["name", "Type", "PetID", "RescurID"] |
| 57 | +_DESCRIPTION = (( |
| 58 | + "A large set of images of cats and dogs." |
| 59 | + "Together with the metadata information of sentiment information.")) |
| 60 | + |
| 61 | + |
| 62 | +class PetFinder(tfds.core.GeneratorBasedBuilder): |
| 63 | + """Pet Finder.""" |
| 64 | + VERSION = tfds.core.Version("1.0.0") |
| 65 | + SUPPORTED_VERSIONS = [ |
| 66 | + tfds.core.Version("1.0.0", experiments={tfds.core.Experiment.S3: True}), |
| 67 | + ] |
| 68 | + |
| 69 | + def _info(self): |
| 70 | + return tfds.core.DatasetInfo( |
| 71 | + builder=self, |
| 72 | + description="Dataset with images from 5 classes (see config name for " |
| 73 | + "information on the specific class)", |
| 74 | + features=tfds.features.FeaturesDict({ |
| 75 | + "image": tfds.features.Image(), |
| 76 | + "image/filename": tfds.features.Text(), |
| 77 | + "PetID": tfds.features.Text(), |
| 78 | + "attributes": {name: tf.int64 for name in _INT_FEATS}, |
| 79 | + "label": tfds.features.ClassLabel(num_classes=5), |
| 80 | + }), |
| 81 | + supervised_keys=("attributes", "label"), |
| 82 | + urls=[_URL], |
| 83 | + citation=_CITATION, |
| 84 | + ) |
| 85 | + |
| 86 | + def _split_generators(self, dl_manager): |
| 87 | + """Returns SplitGenerators.""" |
| 88 | + # petfinder: Downloads the data and defines the splits |
| 89 | + # dl_manager is a tfds.download.DownloadManager that can be used to |
| 90 | + # download and extract URLs |
| 91 | + # dl_paths = dl_manager.download_kaggle_data(url) |
| 92 | + dl_paths = dl_manager.download_and_extract(_DL_URLS) |
| 93 | + |
| 94 | + return [ |
| 95 | + tfds.core.SplitGenerator( |
| 96 | + name=tfds.Split.TRAIN, |
| 97 | + num_shards=10, |
| 98 | + gen_kwargs={ |
| 99 | + "csv_name": "train.csv", |
| 100 | + "csv_paths": dl_paths["train"], |
| 101 | + "img_paths": dl_paths["train_images"], |
| 102 | + }, |
| 103 | + ), |
| 104 | + tfds.core.SplitGenerator( |
| 105 | + name=tfds.Split.TEST, |
| 106 | + num_shards=10, |
| 107 | + gen_kwargs={ |
| 108 | + "csv_name": "test.csv", |
| 109 | + "csv_paths": dl_paths["test"], |
| 110 | + "img_paths": dl_paths["test_images"], |
| 111 | + }, |
| 112 | + ), |
| 113 | + ] |
| 114 | + |
| 115 | + def _generate_examples(self, csv_name, csv_paths, img_paths): |
| 116 | + """Yields examples. |
| 117 | +
|
| 118 | + Args: |
| 119 | + csv_name: file name for the csv file used in the split |
| 120 | + csv_paths: Path to csv files containing the label and attributes |
| 121 | + information. |
| 122 | + img_paths: Path to images. |
| 123 | + """ |
| 124 | + if not tf.io.gfile.exists(csv_paths): |
| 125 | + raise AssertionError("{} not exist".format(csv_name)) |
| 126 | + with tf.io.gfile.GFile(csv_paths) as csv_file: |
| 127 | + dataframe = pd.read_csv(csv_file) |
| 128 | + # add a dummy label for test set |
| 129 | + if csv_name == "test.csv": |
| 130 | + dataframe["AdoptionSpeed"] = -1 |
| 131 | + |
| 132 | + images = tf.io.gfile.listdir(img_paths) |
| 133 | + for image in images: |
| 134 | + pet_id = image.split("-")[0] |
| 135 | + image_path = os.path.join(img_paths, image) |
| 136 | + attr_dict = dataframe.loc[dataframe["PetID"] == pet_id] |
| 137 | + record = { |
| 138 | + "image": image_path, |
| 139 | + "image/filename": image, |
| 140 | + "PetID": pet_id, |
| 141 | + "attributes": attr_dict[_INT_FEATS].to_dict("records")[0], |
| 142 | + "label": attr_dict["AdoptionSpeed"].values[0] |
| 143 | + } |
| 144 | + if self.version.implements(tfds.core.Experiment.S3): |
| 145 | + yield image, record |
| 146 | + else: |
| 147 | + yield record |
0 commit comments