File size: 1,928 Bytes
5ab1cc0
 
 
d148b96
 
 
 
135cb72
d148b96
 
135cb72
 
 
d148b96
 
135cb72
d148b96
 
135cb72
 
d148b96
 
135cb72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d148b96
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets
from datasets.tasks import ImageClassification
from .classes import FRUITS30_CLASSES

class fruits30(datasets.GeneratorBasedBuilder):
    
    def _info(self):
        assert len(FRUITS30_CLASSES) == 30
        return datasets.DatasetInfo(
            description="The fruits-30 is an image dataset for fruit image classification task."
                        " It contains high-quality images of 30 types of fruit with annotation using segmentation.",
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(names=list(FRUITS30_CLASSES.values())),
                }
            ),
            homepage="https://github.com/VinayHajare/Fruit-Image-Dataset",
            task_templates=[ImageClassification(image_column="image", label_column="label")],
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        data_dir = "./FruitImageDataset"

        # Only return the "train" split
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_dir": data_dir,
                    "split": "train",
                },
            )
        ]

    def _generate_examples(self, archives, split):
        """Yields examples."""
        idx = 0
        for archive in archives:
            for path, file in archive:
                if path.endswith(".JPG"):
                    # image filepath format: <IMAGE_FILENAME>_<SYNSET_ID>.JPEG
                    root, _ = os.path.splitext(path)
                    _, synset_id = os.path.basename(root).rsplit("_", 1)
                    label = FRUITS30_CLASSES[synset_id]
                    ex = {"image": {"path": path, "bytes": file.read()}, "label": label}
                    yield idx, ex
                    idx += 1