Datasets:

Modalities:
Image
Formats:
parquet
Languages:
English
DOI:
Libraries:
Datasets
Dask
License:
File size: 6,216 Bytes
dfd73db
8608511
 
 
07602f9
dfd73db
 
 
 
 
 
 
 
 
 
 
 
8608511
8c398be
8608511
4ab310f
 
 
 
 
 
8608511
 
dfd73db
 
8c398be
dfd73db
 
 
3d18b90
dfd73db
 
3d18b90
dfd73db
 
 
 
3d18b90
dfd73db
 
 
 
 
 
 
 
 
 
 
3d18b90
4ab310f
dfd73db
 
3d18b90
 
 
 
 
 
 
dfd73db
 
 
3d18b90
01880af
 
 
4ab310f
3d18b90
 
 
 
01880af
 
 
 
 
dfd73db
 
 
8608511
 
 
 
 
 
3d18b90
8608511
 
 
 
3d18b90
 
 
 
8c398be
3d18b90
 
 
 
 
 
 
 
8608511
 
 
3d18b90
8608511
dfd73db
 
 
 
8608511
dfd73db
8608511
dfd73db
 
 
 
 
8608511
dfd73db
8608511
dfd73db
 
 
8608511
 
 
 
 
 
01880af
8608511
 
 
 
 
 
 
 
 
dfd73db
8608511
 
8c398be
 
8608511
 
 
 
 
 
 
01880af
 
 
 
2aba420
01880af
 
 
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
import datasets
import pandas as pd
import glob
from pathlib import Path
from PIL import Image, ImageOps

_DESCRIPTION = """Photos of various plants with their major, above ground organs labeled. Includes labels for stem, leafs, fruits and flowers."""

_HOMEPAGE = "https://huggingface.co/datasets/jpodivin/plantorgans"

_CITATION = """"""

_LICENSE = "MIT"

_BASE_URL = "https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/"
_TRAIN_URLS = [_BASE_URL + f"sourcedata_labeled.tar.{i:02}" for i in range(0, 8)]
_TEST_URLS = [_BASE_URL + f"sourcedata_labeled.tar.{i:02}" for i in range(8, 12)]
_MASKS_URLS = [_BASE_URL + f"masks.tar.0{i}" for i in range(0, 2)]
_SEMANTIC_MASKS_URLS = "semantic_masks.tar.gz"

_SEMANTIC_METADATA_URLS = {
    'train': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_semantic_train.csv',
    'test': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_semantic_test.csv'
}

_PANOPTIC_METADATA_URLS = {
    'train': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_train.csv',
    'test': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_test.csv'
}


class PlantOrgansConfig(datasets.BuilderConfig):
    """Builder Config for PlantOrgans"""
 
    def __init__(self, data_urls, metadata_urls, splits, **kwargs):
        """BuilderConfig for PlantOrgans.
        Args:
          data_urls: list of `string`s, urls to download the zip files from.
          metadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs
          **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(version=datasets.Version("1.0.0"), **kwargs)
        self.data_urls = data_urls
        self.metadata_urls = metadata_urls
        self.splits = splits


class PlantOrgans(datasets.GeneratorBasedBuilder):
    """Plantorgans dataset
    """
    BUILDER_CONFIGS = [
        PlantOrgansConfig(
            name="semantic_segmentation_full",
            description="This configuration contains segmentation masks.",
            data_urls=_BASE_URL,
            metadata_urls=_SEMANTIC_METADATA_URLS,
            splits=['train', 'test'],
        ),
        PlantOrgansConfig(
            name="instance_segmentation_full",
            description="This configuration contains segmentation masks.",
            data_urls=_BASE_URL,
            metadata_urls=_PANOPTIC_METADATA_URLS,
            splits=['train', 'test'],
        ),
    ]

    def _info(self):
        features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "mask": datasets.Image(),
                    "image_name": datasets.Value(dtype="string"),
                })
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=("image", "mask"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE,
        )


    def _split_generators(self, dl_manager):
        
        train_archives_paths = dl_manager.download_and_extract(_TRAIN_URLS)
        test_archives_paths = dl_manager.download_and_extract(_TEST_URLS)
      
        train_paths = []
        test_paths = []

        for p in train_archives_paths:
            train_paths.extend(glob.glob(str(p)+'/sourcedata/labeled/**.jpg'))
        for p in test_archives_paths:
            test_paths.extend(glob.glob(str(p)+'/sourcedata/labeled/**.jpg'))
        
        if self.config.name == 'instance_segmentation_full':
            metadata_urls = _PANOPTIC_METADATA_URLS
            mask_urls = _MASKS_URLS
            mask_glob = '/masks/**.png'
        else:
            metadata_urls = _SEMANTIC_METADATA_URLS
            mask_urls = _SEMANTIC_MASKS_URLS
            mask_glob = '/semantic_masks/**.png'

        split_metadata_paths = dl_manager.download(metadata_urls)

        mask_archives_paths = dl_manager.download_and_extract(mask_urls)
            
        mask_paths = []
        for p in mask_archives_paths:
            mask_paths.extend(glob.glob(str(p)+mask_glob))

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": train_paths,
                    "metadata_path": split_metadata_paths["train"],
                    "masks_path": mask_paths,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images": test_paths,
                    "metadata_path": split_metadata_paths["test"],
                    "masks_path": mask_paths,
                },
            ),
        ]


    def _generate_examples(self, images, metadata_path, masks_path):
        """
        images: path to image directory
        metadata_path: path to metadata csv
        masks_path: path to masks
        """

        # Get local image paths
        image_paths = pd.DataFrame(
            [(str(Path(*Path(e).parts[-3:])), e) for e in images], columns=['image', 'image_path'])

        # Get local mask paths
        masks_paths = pd.DataFrame(
            [(str(Path(*Path(e).parts[-2:])), e) for e in masks_path], columns=['mask', 'mask_path'])
        
        # Get all common about images and masks from csv
        metadata = pd.read_csv(metadata_path)
        metadata['image'] = metadata['image_path'].apply(lambda x: str(Path(x).parts[-1]))
        metadata['mask'] = metadata['mask_path'].apply(lambda x: str(Path(x).parts[-1]))

        # Merge dataframes
        metadata = metadata.merge(masks_paths, on='mask', how='inner')
        metadata = metadata.merge(image_paths, on='image', how='inner')

        # Make examples and yield
        for i, r in metadata.iterrows():
            # Example contains paths to mask, source image, certainty of label,
            # and name of source image.
            example = {
                'mask': r['mask_path'],
                'image': r['image_path'],
                'image_name': Path(r['image_path']).parts[-1],
            }
            yield i, example