Commit
·
fc7f7bd
1
Parent(s):
6f5d8e9
Create image-classification-yenthienviet.py
Browse files
image-classification-yenthienviet.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
"""TODO: Add a description here."""
|
| 3 |
+
import csv
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import datasets
|
| 8 |
+
from datasets.tasks import ImageClassification
|
| 9 |
+
|
| 10 |
+
_DESCRIPTION = """\
|
| 11 |
+
This dataset contains all THIENVIET products images split in training,
|
| 12 |
+
validation and testing
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
_URLS = {
|
| 16 |
+
"train": "https://huggingface.co/datasets/chanelcolgate/image-classification-yenthienviet/resolve/main/data/train.zip",
|
| 17 |
+
"val": "https://huggingface.co/datasets/chanelcolgate/image-classification-yenthienviet/resolve/main/data/val.zip",
|
| 18 |
+
"test": "https://huggingface.co/datasets/chanelcolgate/image-classification-yenthienviet/resolve/main/data/test.zip"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
_CATEGORIES = ['botkhi','thuytinh','ocvit','ban','contrung','kimloai','toc']
|
| 22 |
+
|
| 23 |
+
class YenthienvietConfig(datasets.BuilderConfig):
|
| 24 |
+
"""Builder Config for image-classification-yenthienviet"""
|
| 25 |
+
def __init__(self, name, data_urls, **kwargs):
|
| 26 |
+
"""
|
| 27 |
+
BuilderConfig for image-classification-yenthienviet.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
data_urls: `dict`, name to url to download the zip file from.
|
| 31 |
+
**kwargs: keyword arguments forwared to super.
|
| 32 |
+
"""
|
| 33 |
+
super().__init__(version=datasets.Version("1.0.0", **kwargs))
|
| 34 |
+
self.name
|
| 35 |
+
self.data_urls = data_urls
|
| 36 |
+
|
| 37 |
+
# TODO: Name of the dataset usually matches the script name
|
| 38 |
+
class YenthienvietClassification(datasets.GeneratorBasedBuilder):
|
| 39 |
+
""" Builder for image-classification-yenthienviet"""
|
| 40 |
+
VERSION = datasets.Version("1.0.0")
|
| 41 |
+
|
| 42 |
+
BUILDER_CONFIG_CLASS = YenthienvietConfig
|
| 43 |
+
BUILDER_CONFIGS = [
|
| 44 |
+
YenthienvietConfig(
|
| 45 |
+
name="version-10/10",
|
| 46 |
+
description="Version 10/10 of image-classification-yenthienviet dataset.",
|
| 47 |
+
data_urls=_URLS,
|
| 48 |
+
)
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
def _info(self):
|
| 52 |
+
features = datasets.Features(
|
| 53 |
+
{
|
| 54 |
+
"image_file_path": datasets.Value("string"),
|
| 55 |
+
"image": datasets.Image(),
|
| 56 |
+
"labels": datasets.features.ClassLabel(names=_CATEGORIES)
|
| 57 |
+
}
|
| 58 |
+
)
|
| 59 |
+
return datasets.DatasetInfo(
|
| 60 |
+
description=_DESCRIPTION,
|
| 61 |
+
features=features,
|
| 62 |
+
supervised_keys=("image", "label"),
|
| 63 |
+
task_templates=[ImageClassification(image_column="image", label_column="labels")]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
def _split_generators(self, dl_manager):
|
| 67 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 68 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 69 |
+
|
| 70 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and exract URLS
|
| 71 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 72 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 73 |
+
data_files = dl_manager.download_and_extract(self.config.data_urls)
|
| 74 |
+
return [
|
| 75 |
+
datasets.SplitGenerator(
|
| 76 |
+
name=datasets.Split.TRAIN,
|
| 77 |
+
gen_kwargs={
|
| 78 |
+
"files": dl_manager.iter_files([data_files["train"]]),
|
| 79 |
+
},
|
| 80 |
+
),
|
| 81 |
+
datasets.SplitGenerator(
|
| 82 |
+
name=datasets.Split.VALIDATION,
|
| 83 |
+
gen_kwargs={
|
| 84 |
+
"files": dl_manager.iter_files([data_files["val"]]),
|
| 85 |
+
},
|
| 86 |
+
),
|
| 87 |
+
datasets.SplitGenerator(
|
| 88 |
+
name=datasets.Split.TEST,
|
| 89 |
+
gen_kwargs={
|
| 90 |
+
"files": dl_manager.iter_files([data_files["test"]]),
|
| 91 |
+
},
|
| 92 |
+
),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
def _generate_examples(self, files):
|
| 96 |
+
for i, path in enumerate(files):
|
| 97 |
+
file_name = os.path.basename(path)
|
| 98 |
+
if file_name.endswith((".jpg", ".png", ".jpeg", ".bmp", ".tif", ".tiff")):
|
| 99 |
+
yield i, {
|
| 100 |
+
"image_file_path": path,
|
| 101 |
+
"image": path,
|
| 102 |
+
"labels": os.path.basename(os.path.dirname(path)),
|
| 103 |
+
}
|