|
|
from datasets import DatasetInfo, GeneratorBasedBuilder, SplitGenerator, Split, Features, Value, ClassLabel, Image, Sequence |
|
|
import csv |
|
|
import datasets |
|
|
import ast |
|
|
|
|
|
class CAFOSatConfig(datasets.BuilderConfig): |
|
|
def __init__(self, split_column="cafosat_set1_training_train", **kwargs): |
|
|
super().__init__(**kwargs) |
|
|
self.split_column = split_column |
|
|
|
|
|
class CAFOSat(datasets.GeneratorBasedBuilder): |
|
|
BUILDER_CONFIGS = [ |
|
|
CAFOSatConfig(name="set1_train", split_column="cafosat_set1_training_train", description="Set 1 training split"), |
|
|
CAFOSatConfig(name="set1_val", split_column="cafosat_set1_training_val", description="Set 1 validation split"), |
|
|
CAFOSatConfig(name="verified_train", split_column="cafosat_verified_training_train", description="Verified training split"), |
|
|
CAFOSatConfig(name="all_train", split_column="cafosat_all_training_train", description="Verified training split"), |
|
|
] |
|
|
DEFAULT_CONFIG_NAME = "all_train" |
|
|
|
|
|
def _info(self): |
|
|
return DatasetInfo( |
|
|
description="CAFOSat: Remote sensing CAFO dataset with bounding boxes and infrastructure tags.", |
|
|
features=Features({ |
|
|
"patch_file": Image(), |
|
|
"label": ClassLabel( |
|
|
names=["Negative", "Swine", "Dairy", "Beef", "Poultry", "Horses", "Sheep/Goats"] |
|
|
), |
|
|
"barn": Value("float32"), |
|
|
"manure_pond": Value("float32"), |
|
|
"grazing_area": Value("float32"), |
|
|
"others": Value("float32"), |
|
|
"geom_bbox": Sequence(Value("float32")), |
|
|
"category": Value("string"), |
|
|
"state": Value("string"), |
|
|
"image_type": Value("string"), |
|
|
"CAFO_UNIQUE_ID": Value("string"), |
|
|
"verified_label": Value("string"), |
|
|
"patch_res": Value("string") |
|
|
}), |
|
|
supervised_keys=None, |
|
|
homepage="https://huggingface.co/datasets/oishee3003/CAFOSat/", |
|
|
license="cc-by-4.0" |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
csv_path = dl_manager.download_and_extract("cafosat.csv") |
|
|
return [ |
|
|
SplitGenerator(name=Split.TRAIN, gen_kwargs={"csv_path": csv_path, "split_flag": self.config.split_column}) |
|
|
] |
|
|
|
|
|
def _generate_examples(self, csv_path, split_flag): |
|
|
with open(csv_path, encoding="utf-8") as f: |
|
|
reader = csv.DictReader(f) |
|
|
for idx, row in enumerate(reader): |
|
|
if row.get(split_flag, "0") != "1": |
|
|
continue |
|
|
|
|
|
|
|
|
try: |
|
|
bbox = ast.literal_eval(row.get("geom_bbox", "[5.0, 5.0, 700.0, 700.0]")) |
|
|
except: |
|
|
bbox = [5.0, 5.0, 700.0, 700.0] |
|
|
|
|
|
yield idx, { |
|
|
"patch_file": row["patch_file"], |
|
|
"label": int(row["label"]), |
|
|
"barn": float(row.get("barn", 0)), |
|
|
"manure_pond": float(row.get("manure_pond", 0)), |
|
|
"grazing_area": float(row.get("grazing_area", 0)), |
|
|
"others": float(row.get("others", 0)), |
|
|
"geom_bbox": bbox, |
|
|
"category": row.get("category", ""), |
|
|
"state": row.get("state", ""), |
|
|
"image_type": row.get("image_type", ""), |
|
|
"CAFO_UNIQUE_ID": row.get("CAFO_UNIQUE_ID", ""), |
|
|
"verified_label": row.get("verified_label", ""), |
|
|
"patch_res": row.get("patch_res", "") |
|
|
"refine_x": row.get("refine_x", "") |
|
|
"refine_y": row.get("refine_y", "") |
|
|
} |
|
|
|