File size: 5,988 Bytes
2a92119
 
 
 
 
 
 
 
 
 
e345d5d
 
2a92119
 
 
 
3bd1229
 
 
0670536
 
 
 
2a92119
 
 
 
 
13bcdb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a92119
bb43d96
2a92119
 
 
 
 
 
 
 
fdda653
2a92119
 
 
 
 
 
b5ed5dc
2a92119
 
 
 
 
 
0670536
 
2a92119
 
0b998a2
 
2a92119
0670536
 
 
 
 
 
 
 
 
 
 
 
2a92119
0670536
2a92119
 
 
0670536
 
 
 
 
 
 
2a92119
0670536
7a8e3b7
b5ed5dc
0670536
2a92119
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
Loading script for the Food Vision 199 classes dataset.

See the template: https://github.com/huggingface/datasets/blob/main/templates/new_dataset_script.py
See the example for Food101: https://huggingface.co/datasets/food101/blob/main/food101.py 
See another example: https://huggingface.co/datasets/davanstrien/encyclopedia_britannica/blob/main/encyclopedia_britannica.py
"""

import datasets
import os
import requests

import pandas as pd

from datasets.tasks import ImageClassification

# Print datasets version
print(f"Datasets version: {datasets.__version__}")

# Set verbosity to 10
datasets.logging.set_verbosity(10)
print(f"Verbosity level: {datasets.logging.get_verbosity()}")

_HOMEPAGE = "https://www.nutrify.app"
_LICENSE = "TODO"
_CITATION = "TODO"
_DESCRIPTION = "Images of 199 food classes from the Nutrify app."

# # Download class_names.txt and read it
# url = "https://huggingface.co/datasets/mrdbourke/food_vision_199_classes/blob/main/class_names.txt"
# r = requests.get(url, allow_redirects=True)
# open("class_names.txt", "wb").write(r.content)
# with open("class_names.txt", "r") as f:
#     _NAMES = f.read().splitlines()

# Create list of class names
_NAMES = ['almond_butter',
 'almonds',
 'apple',
 'apricot',
 'asparagus',
 'avocado',
 'bacon',
 'bacon_and_egg_burger',
 'bagel',
 'baklava',
 'banana',
 'banana_bread',
 'barbecue_sauce',
 'beans',
 'beef',
 'beef_curry',
 'beef_mince',
 'beef_stir_fry',
 'beer',
 'beetroot',
 'biltong',
 'blackberries',
 'blueberries',
 'bok_choy',
 'bread',
 'broccoli',
 'broccolini',
 'brownie',
 'brussel_sprouts',
 'burrito',
 'butter',
 'cabbage',
 'calamari',
 'candy',
 'capsicum',
 'carrot',
 'cashews',
 'cauliflower',
 'celery',
 'cheese',
 'cheeseburger',
 'cherries',
 'chicken_breast',
 'chicken_thighs',
 'chicken_wings',
 'chilli',
 'chimichurri',
 'chocolate',
 'chocolate_cake',
 'coconut',
 'coffee',
 'coleslaw',
 'cookies',
 'coriander',
 'corn',
 'corn_chips',
 'cream',
 'croissant',
 'crumbed_chicken',
 'cucumber',
 'cupcake',
 'daikon_radish',
 'dates',
 'donuts',
 'dragonfruit',
 'eggplant',
 'eggs',
 'enoki_mushroom',
 'fennel',
 'figs',
 'french_toast',
 'fried_rice',
 'fries',
 'fruit_juice',
 'garlic',
 'garlic_bread',
 'ginger',
 'goji_berries',
 'granola',
 'grapefruit',
 'grapes',
 'green_beans',
 'green_onion',
 'guacamole',
 'guava',
 'gyoza',
 'ham',
 'honey',
 'hot_chocolate',
 'ice_coffee',
 'ice_cream',
 'iceberg_lettuce',
 'jerusalem_artichoke',
 'kale',
 'karaage_chicken',
 'kimchi',
 'kiwi_fruit',
 'lamb_chops',
 'leek',
 'lemon',
 'lentils',
 'lettuce',
 'lime',
 'mandarin',
 'mango',
 'maple_syrup',
 'mashed_potato',
 'mayonnaise',
 'milk',
 'miso_soup',
 'mushrooms',
 'nectarines',
 'noodles',
 'nuts',
 'olive_oil',
 'olives',
 'omelette',
 'onion',
 'orange',
 'orange_juice',
 'oysters',
 'pain_au_chocolat',
 'pancakes',
 'papaya',
 'parsley',
 'parsnips',
 'passionfruit',
 'pasta',
 'pawpaw',
 'peach',
 'pear',
 'peas',
 'pickles',
 'pineapple',
 'pizza',
 'plum',
 'pomegranate',
 'popcorn',
 'pork_belly',
 'pork_chop',
 'pork_loins',
 'porridge',
 'potato_bake',
 'potato_chips',
 'potato_scallop',
 'potatoes',
 'prawns',
 'pumpkin',
 'radish',
 'ramen',
 'raspberries',
 'red_onion',
 'red_wine',
 'rhubarb',
 'rice',
 'roast_beef',
 'roast_pork',
 'roast_potatoes',
 'rockmelon',
 'rosemary',
 'salad',
 'salami',
 'salmon',
 'salsa',
 'salt',
 'sandwich',
 'sardines',
 'sausage_roll',
 'sausages',
 'scrambled_eggs',
 'seaweed',
 'shallots',
 'snow_peas',
 'soda',
 'soy_sauce',
 'spaghetti_bolognese',
 'spinach',
 'sports_drink',
 'squash',
 'starfruit',
 'steak',
 'strawberries',
 'sushi',
 'sweet_potato',
 'tacos',
 'tamarillo',
 'taro',
 'tea',
 'toast',
 'tofu',
 'tomato',
 'tomato_chutney',
 'tomato_sauce',
 'turnip',
 'watermelon',
 'white_onion',
 'white_wine',
 'yoghurt',
 'zucchini']

# Create Food199 class
class Food199(datasets.GeneratorBasedBuilder):
    """Food199 Images dataset"""

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(names=_NAMES)
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE
        )

    def _split_generators(self, dl_manager):
        """
        This function returns the logic to split the dataset into different splits as well as labels.
        """
        annotations_csv = dl_manager.download("annotations_with_links.csv")
        print(annotations_csv)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "annotations": annotations_csv,
                    "split": "train"
                }
            ),
        #     datasets.SplitGenerator(
        #         name=datasets.Split.TEST,
        #         gen_kwargs={
        #             "annotations": annotations_csv,
        #             "split": "test"
        #         }
        #     )
        ]

    def _generate_examples(self, annotations, split):
        """
        This function takes in the kwargs from the _split_generators method and can then yield information from them.
        """
        annotations_df = pd.read_csv(annotations, low_memory=False)

        if split == "train":
            annotations = annotations_df[["image", "label"]][annotations_df["split"] == "train"].to_dict(orient="records")
        elif split == "test":
            annotations = annotations_df[["image", "label"]][annotations_df["split"] == "test"].to_dict(orient="records")

        for id_, row in enumerate(annotations):
            # print(row["image"])
            row["image"] = str(row.pop("image"))
            row["label"] = row.pop("label")
            # print(id_, row)
            yield id_, row