|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""A dataset consisting of svgs, their png representations, and various masks""" |
|
|
|
|
|
import csv |
|
import json |
|
import os |
|
from math import floor |
|
|
|
import datasets |
|
|
|
|
|
_CITATION = """\ |
|
@InProceedings{huggingface:dataset, |
|
title = {A dataset for understanding vector graphics}, |
|
author={eezy, Inc. |
|
}, |
|
year={2023} |
|
} |
|
""" |
|
|
|
|
|
_DESCRIPTION = """\ |
|
This new dataset is designed to provide a corpus for training machine vision tasks on the understanding of basic vector graphics |
|
""" |
|
|
|
_HOMEPAGE = "https://eezy.com" |
|
|
|
|
|
_LICENSE = "CC-BY" |
|
|
|
|
|
|
|
|
|
_URLS = { |
|
"circles": "https://eezy-data-bucket.s3.amazonaws.com/public-datasets/basic_shapes_10k_v1/circles.tgz", |
|
"squares": "https://eezy-data-bucket.s3.amazonaws.com/public-datasets/basic_shapes_10k_v1/squares.tgz", |
|
"squares_and_circles": "https://eezy-data-bucket.s3.amazonaws.com/public-datasets/basic_shapes_10k_v1/squares_and_circles.tgz", |
|
"scer": "https://eezy-data-bucket.s3.amazonaws.com/public-datasets/basic_shapes_10k_v1/scer.tgz" |
|
} |
|
|
|
|
|
|
|
class BasicShapes10K(datasets.GeneratorBasedBuilder): |
|
"""A dataset consisting of simple vector shapes and various kinds of masks""" |
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
SPLIT_COUNTS = { |
|
'train': (0, 8000), |
|
'dev': (8000, 9000), |
|
'test': (9000, 10000) |
|
} |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig(name="mixed", version=VERSION, description="These images are a mixture of all the other datasets"), |
|
datasets.BuilderConfig(name="circles", version=VERSION, description="These images only contain circles"), |
|
datasets.BuilderConfig(name="squares", version=VERSION, description="These images only contain squares"), |
|
datasets.BuilderConfig(name="squares_and_circles", version=VERSION, description="These images contain circles and squares"), |
|
datasets.BuilderConfig(name="scer", version=VERSION, description="These images contain circles, squares, rectangles, and ellipses"), |
|
] |
|
|
|
DEFAULT_CONFIG_NAME = "mixed" |
|
|
|
def _info(self): |
|
features = datasets.Features( |
|
{ |
|
"svg": datasets.Value("string"), |
|
"png": datasets.Image(), |
|
"layer_mask": datasets.Image(), |
|
"object_mask": datasets.Image(), |
|
} |
|
) |
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
supervised_keys=("png", "layer_mask"), |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.config.name == 'mixed': |
|
urls = _URLS |
|
else: |
|
urls = {self.config.name: _URLS[self.config.name]} |
|
|
|
data_dir = dl_manager.download_and_extract(urls) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"split": "train", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"split": "dev", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"split": "test" |
|
}, |
|
), |
|
] |
|
|
|
|
|
def _generate_examples(self, data_dir, split): |
|
|
|
start, stop = self.SPLIT_COUNTS[split] |
|
domains = [self.config.name] |
|
|
|
if self.config.name == 'mixed': |
|
start = start * 4 |
|
stop = stop * 4 |
|
domains = [ |
|
'circles', |
|
'squares', |
|
'squares_and_circles', |
|
'scer' |
|
] |
|
|
|
divisions = len(domains) |
|
|
|
for key in range(start, stop): |
|
domain = domains[key % divisions] |
|
idx = floor(key / divisions) |
|
yield f'{self.config.name}_{str(key).zfill(6)}', \ |
|
self._example_for_domain(data_dir, domain, idx, split) |
|
|
|
|
|
def _example_for_domain(self, data_dir, domain, idx, split): |
|
data = {} |
|
svg_path = os.path.join(data_dir[domain], domain, 'svg', str(idx).zfill(6) + '.svg') |
|
with open(svg_path, 'r') as file: |
|
data['svg'] = file.read() |
|
|
|
png_path = os.path.join(data_dir[domain], domain, 'png', str(idx).zfill(6) + '.png') |
|
with open(png_path, 'rb') as file: |
|
data['png'] = {"path": png_path, "bytes": file.read()} |
|
|
|
if split != "test": |
|
layer_mask_path = os.path.join(data_dir[domain], domain, 'layer_mask', str(idx).zfill(6) + '.png') |
|
with open(layer_mask_path, 'rb') as file: |
|
data['layer_mask'] = {"path": layer_mask_path, "bytes": file.read()} |
|
object_mask_path = os.path.join(data_dir[domain], domain, 'obj_mask', str(idx).zfill(6) + '.png') |
|
with open(object_mask_path, 'rb') as file: |
|
data['object_mask'] = {"path": object_mask_path, "bytes": file.read()} |
|
else: |
|
data['layer_mask'] = '' |
|
data['object_mask'] = '' |
|
return data |
|
|