Santiago Hincapie-Potes commited on
Commit ·
8503957
1
Parent(s): e1c0506
feat: add dataset script
Browse files- imvision.py +147 -0
imvision.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and Santiago Hincapie Potes.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""TODO: Add a description here."""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import csv
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# TODO: Add BibTeX citation
|
| 24 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@InProceedings{yu2019lytnet,
|
| 27 |
+
title = {LYTNet: A Convolutional Neural Network for Real-Time Pedestrian Traffic Lights and Zebra Crossing Recognition for the Visually Impaired},
|
| 28 |
+
author = {Yu, Samuel and Lee, Heon and Kim, John},
|
| 29 |
+
booktitle = {Computer Analysis of Images and Patterns (CAIP)},
|
| 30 |
+
month = {Aug},
|
| 31 |
+
year = {2019}
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# TODO: Add description of the dataset here
|
| 36 |
+
# You can copy an official description
|
| 37 |
+
_DESCRIPTION = """\
|
| 38 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
# TODO: Add a link to an official homepage for the dataset here
|
| 42 |
+
_HOMEPAGE = "https://github.com/samuelyu2002/ImVisible"
|
| 43 |
+
|
| 44 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 45 |
+
_LICENSE = ""
|
| 46 |
+
|
| 47 |
+
# TODO: Add link to the official dataset URLs here
|
| 48 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 49 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 50 |
+
_URLS = {
|
| 51 |
+
"imags": "ptl_dataset.tar.gz",
|
| 52 |
+
"train": "training_file.csv",
|
| 53 |
+
"validation": "validation_file.csv",
|
| 54 |
+
"test": "testing_file.csv",
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
| 59 |
+
class ImVision(datasets.GeneratorBasedBuilder):
|
| 60 |
+
"""TODO: Short description of my dataset."""
|
| 61 |
+
|
| 62 |
+
VERSION = datasets.Version("1.1.0")
|
| 63 |
+
|
| 64 |
+
def _info(self):
|
| 65 |
+
features = datasets.Features(
|
| 66 |
+
{
|
| 67 |
+
"img": datasets.Image(),
|
| 68 |
+
"boxes": datasets.features.Sequence({
|
| 69 |
+
"label": datasets.Value("int8"),
|
| 70 |
+
"occluded": datasets.Value("bool"),
|
| 71 |
+
"x_max": datasets.Value("float"),
|
| 72 |
+
"x_min": datasets.Value("float"),
|
| 73 |
+
"y_max": datasets.Value("float"),
|
| 74 |
+
"y_min": datasets.Value("float"),
|
| 75 |
+
}),
|
| 76 |
+
}
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return datasets.DatasetInfo(
|
| 80 |
+
# This is the description that will appear on the datasets page.
|
| 81 |
+
description=_DESCRIPTION,
|
| 82 |
+
# This defines the different columns of the dataset and their types
|
| 83 |
+
features=features, # Here we define them above because they are different between the two configurations
|
| 84 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 85 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 86 |
+
# supervised_keys=("sentence", "label"),
|
| 87 |
+
# Homepage of the dataset for documentation
|
| 88 |
+
homepage=_HOMEPAGE,
|
| 89 |
+
# License for the dataset if available
|
| 90 |
+
license=_LICENSE,
|
| 91 |
+
# Citation for the dataset
|
| 92 |
+
citation=_CITATION,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
def _split_generators(self, dl_manager):
|
| 96 |
+
urls = _URLS
|
| 97 |
+
data_dir = dl_manager.download_and_extract(urls)
|
| 98 |
+
return [
|
| 99 |
+
datasets.SplitGenerator(
|
| 100 |
+
name=datasets.Split.TRAIN,
|
| 101 |
+
gen_kwargs={
|
| 102 |
+
"img_folder": data_dir["imgs"],
|
| 103 |
+
"labels": data_dir["train"],
|
| 104 |
+
},
|
| 105 |
+
),
|
| 106 |
+
datasets.SplitGenerator(
|
| 107 |
+
name=datasets.Split.TEST,
|
| 108 |
+
# These kwargs will be passed to _generate_examples
|
| 109 |
+
gen_kwargs={
|
| 110 |
+
"img_folder": data_dir["imgs"],
|
| 111 |
+
"labels": data_dir["test"],
|
| 112 |
+
},
|
| 113 |
+
),
|
| 114 |
+
datasets.SplitGenerator(
|
| 115 |
+
name=datasets.Split.VALIDATION,
|
| 116 |
+
# These kwargs will be passed to _generate_examples
|
| 117 |
+
gen_kwargs={
|
| 118 |
+
"img_folder": data_dir["imgs"],
|
| 119 |
+
"labels": data_dir["validation"],
|
| 120 |
+
},
|
| 121 |
+
),
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 125 |
+
def _generate_examples(self, img_folder, labels):
|
| 126 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
| 127 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 128 |
+
with open(labels, encoding="utf-8") as f:
|
| 129 |
+
reader = csv.reader(f)
|
| 130 |
+
for key, row in enumerate(reader):
|
| 131 |
+
if key == 0:
|
| 132 |
+
continue
|
| 133 |
+
|
| 134 |
+
fname, label, x_min, y_min, x_max, y_max, occluded = row
|
| 135 |
+
yield key - 1, {
|
| 136 |
+
"img": os.path.join(img_folder, fname),
|
| 137 |
+
"boxes": [
|
| 138 |
+
{
|
| 139 |
+
"label": int(label),
|
| 140 |
+
"occluded": occluded != "not_blocked",
|
| 141 |
+
"x_max": float(x_max),
|
| 142 |
+
"x_min": float(x_min),
|
| 143 |
+
"y_max": float(y_max),
|
| 144 |
+
"y_min": float(y_min),
|
| 145 |
+
}
|
| 146 |
+
]
|
| 147 |
+
}
|