Removing download script
Browse filesSigned-off-by: Jiri Podivin <jpodivin@gmail.com>
- plantorgans.py +0 -168
plantorgans.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
import datasets
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import glob
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
from PIL import Image, ImageOps
|
| 6 |
-
|
| 7 |
-
_DESCRIPTION = """Photos of various plants with their major, above ground organs labeled. Includes labels for stem, leafs, fruits and flowers."""
|
| 8 |
-
|
| 9 |
-
_HOMEPAGE = "https://huggingface.co/datasets/jpodivin/plantorgans"
|
| 10 |
-
|
| 11 |
-
_CITATION = """"""
|
| 12 |
-
|
| 13 |
-
_LICENSE = "MIT"
|
| 14 |
-
|
| 15 |
-
_BASE_URL = "https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/"
|
| 16 |
-
_TRAIN_URLS = [_BASE_URL + f"sourcedata_labeled.tar.{i:02}" for i in range(0, 8)]
|
| 17 |
-
_TEST_URLS = [_BASE_URL + f"sourcedata_labeled.tar.{i:02}" for i in range(8, 12)]
|
| 18 |
-
_MASKS_URLS = [_BASE_URL + f"masks.tar.0{i}" for i in range(0, 2)]
|
| 19 |
-
_SEMANTIC_MASKS_URLS = "semantic_masks.tar.gz"
|
| 20 |
-
|
| 21 |
-
_SEMANTIC_METADATA_URLS = {
|
| 22 |
-
'train': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_semantic_train.csv',
|
| 23 |
-
'test': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_semantic_test.csv'
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
_PANOPTIC_METADATA_URLS = {
|
| 27 |
-
'train': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_train.csv',
|
| 28 |
-
'test': 'https://huggingface.co/datasets/jpodivin/plantorgans/resolve/main/metadata_test.csv'
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
class PlantOrgansConfig(datasets.BuilderConfig):
|
| 33 |
-
"""Builder Config for PlantOrgans"""
|
| 34 |
-
|
| 35 |
-
def __init__(self, data_urls, metadata_urls, splits, **kwargs):
|
| 36 |
-
"""BuilderConfig for PlantOrgans.
|
| 37 |
-
Args:
|
| 38 |
-
data_urls: list of `string`s, urls to download the zip files from.
|
| 39 |
-
metadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs
|
| 40 |
-
**kwargs: keyword arguments forwarded to super.
|
| 41 |
-
"""
|
| 42 |
-
super().__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 43 |
-
self.data_urls = data_urls
|
| 44 |
-
self.metadata_urls = metadata_urls
|
| 45 |
-
self.splits = splits
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
class PlantOrgans(datasets.GeneratorBasedBuilder):
|
| 49 |
-
"""Plantorgans dataset
|
| 50 |
-
"""
|
| 51 |
-
BUILDER_CONFIGS = [
|
| 52 |
-
PlantOrgansConfig(
|
| 53 |
-
name="semantic_segmentation_full",
|
| 54 |
-
description="This configuration contains segmentation masks.",
|
| 55 |
-
data_urls=_BASE_URL,
|
| 56 |
-
metadata_urls=_SEMANTIC_METADATA_URLS,
|
| 57 |
-
splits=['train', 'test'],
|
| 58 |
-
),
|
| 59 |
-
PlantOrgansConfig(
|
| 60 |
-
name="instance_segmentation_full",
|
| 61 |
-
description="This configuration contains segmentation masks.",
|
| 62 |
-
data_urls=_BASE_URL,
|
| 63 |
-
metadata_urls=_PANOPTIC_METADATA_URLS,
|
| 64 |
-
splits=['train', 'test'],
|
| 65 |
-
),
|
| 66 |
-
]
|
| 67 |
-
|
| 68 |
-
def _info(self):
|
| 69 |
-
features=datasets.Features(
|
| 70 |
-
{
|
| 71 |
-
"image": datasets.Image(),
|
| 72 |
-
"mask": datasets.Image(),
|
| 73 |
-
"image_name": datasets.Value(dtype="string"),
|
| 74 |
-
})
|
| 75 |
-
return datasets.DatasetInfo(
|
| 76 |
-
description=_DESCRIPTION,
|
| 77 |
-
features=features,
|
| 78 |
-
supervised_keys=("image", "mask"),
|
| 79 |
-
homepage=_HOMEPAGE,
|
| 80 |
-
citation=_CITATION,
|
| 81 |
-
license=_LICENSE,
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def _split_generators(self, dl_manager):
|
| 86 |
-
|
| 87 |
-
train_archives_paths = dl_manager.download_and_extract(_TRAIN_URLS)
|
| 88 |
-
test_archives_paths = dl_manager.download_and_extract(_TEST_URLS)
|
| 89 |
-
|
| 90 |
-
train_paths = []
|
| 91 |
-
test_paths = []
|
| 92 |
-
|
| 93 |
-
for p in train_archives_paths:
|
| 94 |
-
train_paths.extend(glob.glob(str(p)+'/sourcedata/labeled/**.jpg'))
|
| 95 |
-
for p in test_archives_paths:
|
| 96 |
-
test_paths.extend(glob.glob(str(p)+'/sourcedata/labeled/**.jpg'))
|
| 97 |
-
|
| 98 |
-
if self.config.name == 'instance_segmentation_full':
|
| 99 |
-
metadata_urls = _PANOPTIC_METADATA_URLS
|
| 100 |
-
mask_urls = _MASKS_URLS
|
| 101 |
-
mask_glob = '/masks/**.png'
|
| 102 |
-
else:
|
| 103 |
-
metadata_urls = _SEMANTIC_METADATA_URLS
|
| 104 |
-
mask_urls = _SEMANTIC_MASKS_URLS
|
| 105 |
-
mask_glob = '/semantic_masks/**.png'
|
| 106 |
-
|
| 107 |
-
split_metadata_paths = dl_manager.download(metadata_urls)
|
| 108 |
-
|
| 109 |
-
mask_archives_paths = dl_manager.download_and_extract(mask_urls)
|
| 110 |
-
|
| 111 |
-
mask_paths = []
|
| 112 |
-
for p in mask_archives_paths:
|
| 113 |
-
mask_paths.extend(glob.glob(str(p)+mask_glob))
|
| 114 |
-
|
| 115 |
-
return [
|
| 116 |
-
datasets.SplitGenerator(
|
| 117 |
-
name=datasets.Split.TRAIN,
|
| 118 |
-
gen_kwargs={
|
| 119 |
-
"images": train_paths,
|
| 120 |
-
"metadata_path": split_metadata_paths["train"],
|
| 121 |
-
"masks_path": mask_paths,
|
| 122 |
-
},
|
| 123 |
-
),
|
| 124 |
-
datasets.SplitGenerator(
|
| 125 |
-
name=datasets.Split.TEST,
|
| 126 |
-
gen_kwargs={
|
| 127 |
-
"images": test_paths,
|
| 128 |
-
"metadata_path": split_metadata_paths["test"],
|
| 129 |
-
"masks_path": mask_paths,
|
| 130 |
-
},
|
| 131 |
-
),
|
| 132 |
-
]
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
def _generate_examples(self, images, metadata_path, masks_path):
|
| 136 |
-
"""
|
| 137 |
-
images: path to image directory
|
| 138 |
-
metadata_path: path to metadata csv
|
| 139 |
-
masks_path: path to masks
|
| 140 |
-
"""
|
| 141 |
-
|
| 142 |
-
# Get local image paths
|
| 143 |
-
image_paths = pd.DataFrame(
|
| 144 |
-
[(str(Path(*Path(e).parts[-3:])), e) for e in images], columns=['image', 'image_path'])
|
| 145 |
-
|
| 146 |
-
# Get local mask paths
|
| 147 |
-
masks_paths = pd.DataFrame(
|
| 148 |
-
[(str(Path(*Path(e).parts[-2:])), e) for e in masks_path], columns=['mask', 'mask_path'])
|
| 149 |
-
|
| 150 |
-
# Get all common about images and masks from csv
|
| 151 |
-
metadata = pd.read_csv(metadata_path)
|
| 152 |
-
metadata['image'] = metadata['image_path'].apply(lambda x: str(Path(x).parts[-1]))
|
| 153 |
-
metadata['mask'] = metadata['mask_path'].apply(lambda x: str(Path(x).parts[-1]))
|
| 154 |
-
|
| 155 |
-
# Merge dataframes
|
| 156 |
-
metadata = metadata.merge(masks_paths, on='mask', how='inner')
|
| 157 |
-
metadata = metadata.merge(image_paths, on='image', how='inner')
|
| 158 |
-
|
| 159 |
-
# Make examples and yield
|
| 160 |
-
for i, r in metadata.iterrows():
|
| 161 |
-
# Example contains paths to mask, source image, certainty of label,
|
| 162 |
-
# and name of source image.
|
| 163 |
-
example = {
|
| 164 |
-
'mask': r['mask_path'],
|
| 165 |
-
'image': r['image_path'],
|
| 166 |
-
'image_name': Path(r['image_path']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