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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Segment Dataset"""
|
| 2 |
+
|
| 3 |
+
from typing import List
|
| 4 |
+
from functools import partial
|
| 5 |
+
|
| 6 |
+
import datasets
|
| 7 |
+
|
| 8 |
+
import pandas
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
VERSION = datasets.Version("1.0.0")
|
| 12 |
+
|
| 13 |
+
_ENCODING_DICS = {}
|
| 14 |
+
|
| 15 |
+
DESCRIPTION = "Segment dataset."
|
| 16 |
+
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification"
|
| 17 |
+
_URLS = ("https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification")
|
| 18 |
+
_CITATION = """
|
| 19 |
+
@misc{misc_statlog_(image_segmentation)_147,
|
| 20 |
+
title = {{Statlog (Image Segmentation)}},
|
| 21 |
+
year = {1990},
|
| 22 |
+
howpublished = {UCI Machine Learning Repository},
|
| 23 |
+
note = {{DOI}: \\url{10.24432/C5P01G}}
|
| 24 |
+
}
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Dataset info
|
| 28 |
+
urls_per_split = {
|
| 29 |
+
"train": "https://huggingface.co/datasets/mstz/segment/raw/main/segment.csv"
|
| 30 |
+
}
|
| 31 |
+
features_types_per_config = {
|
| 32 |
+
"segment": {
|
| 33 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 34 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 35 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 36 |
+
"short_line_density": datasets.Value("float64"),
|
| 37 |
+
"vedge_mean": datasets.Value("float64"),
|
| 38 |
+
"vedge_std": datasets.Value("float64"),
|
| 39 |
+
"hedge_mean": datasets.Value("float64"),
|
| 40 |
+
"hedge_std": datasets.Value("float64"),
|
| 41 |
+
"intensity_mean": datasets.Value("float64"),
|
| 42 |
+
"rawred_mean": datasets.Value("float64"),
|
| 43 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 44 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 45 |
+
"exred_mean": datasets.Value("float64"),
|
| 46 |
+
"exblue_mean": datasets.Value("float64"),
|
| 47 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 48 |
+
"value_mean": datasets.Value("float64"),
|
| 49 |
+
"saturation_mean": datasets.Value("float64"),
|
| 50 |
+
"hue_mean": datasets.Value("float64"),
|
| 51 |
+
"class": datasets.ClassLabel(num_classes=7,
|
| 52 |
+
names=("brickface", "sky", "foliage", "cement", "window", "path", "grass")),
|
| 53 |
+
},
|
| 54 |
+
"brickface": {
|
| 55 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 56 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 57 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 58 |
+
"short_line_density": datasets.Value("float64"),
|
| 59 |
+
"vedge_mean": datasets.Value("float64"),
|
| 60 |
+
"vedge_std": datasets.Value("float64"),
|
| 61 |
+
"hedge_mean": datasets.Value("float64"),
|
| 62 |
+
"hedge_std": datasets.Value("float64"),
|
| 63 |
+
"intensity_mean": datasets.Value("float64"),
|
| 64 |
+
"rawred_mean": datasets.Value("float64"),
|
| 65 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 66 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 67 |
+
"exred_mean": datasets.Value("float64"),
|
| 68 |
+
"exblue_mean": datasets.Value("float64"),
|
| 69 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 70 |
+
"value_mean": datasets.Value("float64"),
|
| 71 |
+
"saturation_mean": datasets.Value("float64"),
|
| 72 |
+
"hue_mean": datasets.Value("float64"),
|
| 73 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 74 |
+
},
|
| 75 |
+
"sky": {
|
| 76 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 77 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 78 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 79 |
+
"short_line_density": datasets.Value("float64"),
|
| 80 |
+
"vedge_mean": datasets.Value("float64"),
|
| 81 |
+
"vedge_std": datasets.Value("float64"),
|
| 82 |
+
"hedge_mean": datasets.Value("float64"),
|
| 83 |
+
"hedge_std": datasets.Value("float64"),
|
| 84 |
+
"intensity_mean": datasets.Value("float64"),
|
| 85 |
+
"rawred_mean": datasets.Value("float64"),
|
| 86 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 87 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 88 |
+
"exred_mean": datasets.Value("float64"),
|
| 89 |
+
"exblue_mean": datasets.Value("float64"),
|
| 90 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 91 |
+
"value_mean": datasets.Value("float64"),
|
| 92 |
+
"saturation_mean": datasets.Value("float64"),
|
| 93 |
+
"hue_mean": datasets.Value("float64"),
|
| 94 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 95 |
+
},
|
| 96 |
+
"foliage": {
|
| 97 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 98 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 99 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 100 |
+
"short_line_density": datasets.Value("float64"),
|
| 101 |
+
"vedge_mean": datasets.Value("float64"),
|
| 102 |
+
"vedge_std": datasets.Value("float64"),
|
| 103 |
+
"hedge_mean": datasets.Value("float64"),
|
| 104 |
+
"hedge_std": datasets.Value("float64"),
|
| 105 |
+
"intensity_mean": datasets.Value("float64"),
|
| 106 |
+
"rawred_mean": datasets.Value("float64"),
|
| 107 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 108 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 109 |
+
"exred_mean": datasets.Value("float64"),
|
| 110 |
+
"exblue_mean": datasets.Value("float64"),
|
| 111 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 112 |
+
"value_mean": datasets.Value("float64"),
|
| 113 |
+
"saturation_mean": datasets.Value("float64"),
|
| 114 |
+
"hue_mean": datasets.Value("float64"),
|
| 115 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 116 |
+
},
|
| 117 |
+
"cement": {
|
| 118 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 119 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 120 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 121 |
+
"short_line_density": datasets.Value("float64"),
|
| 122 |
+
"vedge_mean": datasets.Value("float64"),
|
| 123 |
+
"vedge_std": datasets.Value("float64"),
|
| 124 |
+
"hedge_mean": datasets.Value("float64"),
|
| 125 |
+
"hedge_std": datasets.Value("float64"),
|
| 126 |
+
"intensity_mean": datasets.Value("float64"),
|
| 127 |
+
"rawred_mean": datasets.Value("float64"),
|
| 128 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 129 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 130 |
+
"exred_mean": datasets.Value("float64"),
|
| 131 |
+
"exblue_mean": datasets.Value("float64"),
|
| 132 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 133 |
+
"value_mean": datasets.Value("float64"),
|
| 134 |
+
"saturation_mean": datasets.Value("float64"),
|
| 135 |
+
"hue_mean": datasets.Value("float64"),
|
| 136 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 137 |
+
},
|
| 138 |
+
"window": {
|
| 139 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 140 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 141 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 142 |
+
"short_line_density": datasets.Value("float64"),
|
| 143 |
+
"vedge_mean": datasets.Value("float64"),
|
| 144 |
+
"vedge_std": datasets.Value("float64"),
|
| 145 |
+
"hedge_mean": datasets.Value("float64"),
|
| 146 |
+
"hedge_std": datasets.Value("float64"),
|
| 147 |
+
"intensity_mean": datasets.Value("float64"),
|
| 148 |
+
"rawred_mean": datasets.Value("float64"),
|
| 149 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 150 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 151 |
+
"exred_mean": datasets.Value("float64"),
|
| 152 |
+
"exblue_mean": datasets.Value("float64"),
|
| 153 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 154 |
+
"value_mean": datasets.Value("float64"),
|
| 155 |
+
"saturation_mean": datasets.Value("float64"),
|
| 156 |
+
"hue_mean": datasets.Value("float64"),
|
| 157 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 158 |
+
},
|
| 159 |
+
"path": {
|
| 160 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 161 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 162 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 163 |
+
"short_line_density": datasets.Value("float64"),
|
| 164 |
+
"vedge_mean": datasets.Value("float64"),
|
| 165 |
+
"vedge_std": datasets.Value("float64"),
|
| 166 |
+
"hedge_mean": datasets.Value("float64"),
|
| 167 |
+
"hedge_std": datasets.Value("float64"),
|
| 168 |
+
"intensity_mean": datasets.Value("float64"),
|
| 169 |
+
"rawred_mean": datasets.Value("float64"),
|
| 170 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 171 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 172 |
+
"exred_mean": datasets.Value("float64"),
|
| 173 |
+
"exblue_mean": datasets.Value("float64"),
|
| 174 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 175 |
+
"value_mean": datasets.Value("float64"),
|
| 176 |
+
"saturation_mean": datasets.Value("float64"),
|
| 177 |
+
"hue_mean": datasets.Value("float64"),
|
| 178 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 179 |
+
},
|
| 180 |
+
"grass": {
|
| 181 |
+
"region_centroid_col": datasets.Value("float64"),
|
| 182 |
+
"region_centroid_row": datasets.Value("float64"),
|
| 183 |
+
"region_centroid_pixel_count": datasets.Value("float64"),
|
| 184 |
+
"short_line_density": datasets.Value("float64"),
|
| 185 |
+
"vedge_mean": datasets.Value("float64"),
|
| 186 |
+
"vedge_std": datasets.Value("float64"),
|
| 187 |
+
"hedge_mean": datasets.Value("float64"),
|
| 188 |
+
"hedge_std": datasets.Value("float64"),
|
| 189 |
+
"intensity_mean": datasets.Value("float64"),
|
| 190 |
+
"rawred_mean": datasets.Value("float64"),
|
| 191 |
+
"rawblue_mean": datasets.Value("float64"),
|
| 192 |
+
"rawgreen_mean": datasets.Value("float64"),
|
| 193 |
+
"exred_mean": datasets.Value("float64"),
|
| 194 |
+
"exblue_mean": datasets.Value("float64"),
|
| 195 |
+
"exgreen_mean": datasets.Value("float64"),
|
| 196 |
+
"value_mean": datasets.Value("float64"),
|
| 197 |
+
"saturation_mean": datasets.Value("float64"),
|
| 198 |
+
"hue_mean": datasets.Value("float64"),
|
| 199 |
+
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
|
| 200 |
+
},
|
| 201 |
+
}
|
| 202 |
+
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class SegmentConfig(datasets.BuilderConfig):
|
| 206 |
+
def __init__(self, **kwargs):
|
| 207 |
+
super(SegmentConfig, self).__init__(version=VERSION, **kwargs)
|
| 208 |
+
self.features = features_per_config[kwargs["name"]]
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class Segment(datasets.GeneratorBasedBuilder):
|
| 212 |
+
# dataset versions
|
| 213 |
+
DEFAULT_CONFIG = "segment"
|
| 214 |
+
BUILDER_CONFIGS = [
|
| 215 |
+
SegmentConfig(name="segment", description="Segment for multiclass classification."),
|
| 216 |
+
SegmentConfig(name="brickface", description="Segment for binary classification."),
|
| 217 |
+
SegmentConfig(name="sky", description="Segment for binary classification."),
|
| 218 |
+
SegmentConfig(name="foliage", description="Segment for binary classification."),
|
| 219 |
+
SegmentConfig(name="cement", description="Segment for binary classification."),
|
| 220 |
+
SegmentConfig(name="window", description="Segment for binary classification."),
|
| 221 |
+
SegmentConfig(name="path", description="Segment for binary classification."),
|
| 222 |
+
SegmentConfig(name="grass", description="Segment for binary classification.")
|
| 223 |
+
]
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def _info(self):
|
| 227 |
+
info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
|
| 228 |
+
features=features_per_config[self.config.name])
|
| 229 |
+
|
| 230 |
+
return info
|
| 231 |
+
|
| 232 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 233 |
+
downloads = dl_manager.download_and_extract(urls_per_split)
|
| 234 |
+
|
| 235 |
+
return [
|
| 236 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
|
| 237 |
+
]
|
| 238 |
+
|
| 239 |
+
def _generate_examples(self, filepath: str):
|
| 240 |
+
data = pandas.read_csv(filepath)
|
| 241 |
+
data = self.preprocess(data)
|
| 242 |
+
|
| 243 |
+
for row_id, row in data.iterrows():
|
| 244 |
+
data_row = dict(row)
|
| 245 |
+
|
| 246 |
+
yield row_id, data_row
|
| 247 |
+
|
| 248 |
+
def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
|
| 249 |
+
data["class"] = data["class"].apply(lambda x: x - 1)
|
| 250 |
+
data = data.reset_index()
|
| 251 |
+
data.drop("index", axis="columns", inplace=True)
|
| 252 |
+
|
| 253 |
+
if self.config.name == "brickface":
|
| 254 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0)
|
| 255 |
+
if self.config.name == "sky":
|
| 256 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0)
|
| 257 |
+
if self.config.name == "foliage":
|
| 258 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0)
|
| 259 |
+
if self.config.name == "cement":
|
| 260 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0)
|
| 261 |
+
if self.config.name == "window":
|
| 262 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0)
|
| 263 |
+
if self.config.name == "path":
|
| 264 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0)
|
| 265 |
+
if self.config.name == "grass":
|
| 266 |
+
data["class"] = data["class"].apply(lambda x: 1 if x == 6 else 0)
|
| 267 |
+
|
| 268 |
+
for feature in _ENCODING_DICS:
|
| 269 |
+
encoding_function = partial(self.encode, feature)
|
| 270 |
+
data.loc[:, feature] = data[feature].apply(encoding_function)
|
| 271 |
+
|
| 272 |
+
return data[list(features_types_per_config[self.config.name].keys())]
|
| 273 |
+
|
| 274 |
+
def encode(self, feature, value):
|
| 275 |
+
if feature in _ENCODING_DICS:
|
| 276 |
+
return _ENCODING_DICS[feature][value]
|
| 277 |
+
raise ValueError(f"Unknown feature: {feature}")
|