Update renovation.py
Browse files- renovation.py +56 -53
renovation.py
CHANGED
@@ -1,56 +1,37 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
-
# you may not use this file except in compliance with the License.
|
6 |
-
# You may obtain a copy of the License at
|
7 |
-
#
|
8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
-
#
|
10 |
-
# Unless required by applicable law or agreed to in writing, software
|
11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
-
# See the License for the specific language governing permissions and
|
14 |
-
# limitations under the License.
|
15 |
-
"""Beans leaf dataset with images of diseased and health leaves."""
|
16 |
-
|
17 |
import os
|
18 |
|
19 |
-
import
|
|
|
20 |
from datasets.tasks import ImageClassification
|
21 |
|
22 |
|
23 |
-
_HOMEPAGE = "https://
|
24 |
|
25 |
_CITATION = """\
|
26 |
-
@ONLINE {
|
27 |
-
author="
|
28 |
-
title="
|
29 |
-
month="
|
30 |
-
year="
|
31 |
-
url="https://
|
32 |
}
|
33 |
"""
|
34 |
|
35 |
_DESCRIPTION = """\
|
36 |
-
|
37 |
-
cameras. It consists of 3 classes: 2 disease classes and the healthy class.
|
38 |
-
Diseases depicted include Angular Leaf Spot and Bean Rust. Data was annotated
|
39 |
-
by experts from the National Crops Resources Research Institute (NaCRRI) in
|
40 |
-
Uganda and collected by the Makerere AI research lab.
|
41 |
"""
|
42 |
|
43 |
-
|
44 |
-
"train": "https://huggingface.co/datasets/beans/resolve/main/data/train.zip",
|
45 |
-
"validation": "https://huggingface.co/datasets/beans/resolve/main/data/validation.zip",
|
46 |
-
"test": "https://huggingface.co/datasets/beans/resolve/main/data/test.zip",
|
47 |
-
}
|
48 |
|
49 |
-
_NAMES = ["
|
50 |
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
"""Beans plant leaf images dataset."""
|
54 |
|
55 |
def _info(self):
|
56 |
return datasets.DatasetInfo(
|
@@ -59,44 +40,66 @@ class Beans(datasets.GeneratorBasedBuilder):
|
|
59 |
{
|
60 |
"image_file_path": datasets.Value("string"),
|
61 |
"image": datasets.Image(),
|
62 |
-
"
|
63 |
}
|
64 |
),
|
65 |
-
supervised_keys=("image", "
|
66 |
homepage=_HOMEPAGE,
|
67 |
citation=_CITATION,
|
68 |
-
task_templates=[ImageClassification(image_column="image", label_column="
|
69 |
)
|
70 |
|
71 |
def _split_generators(self, dl_manager):
|
72 |
-
|
73 |
return [
|
74 |
datasets.SplitGenerator(
|
75 |
name=datasets.Split.TRAIN,
|
76 |
gen_kwargs={
|
77 |
-
"
|
|
|
78 |
},
|
79 |
),
|
80 |
datasets.SplitGenerator(
|
81 |
name=datasets.Split.VALIDATION,
|
82 |
gen_kwargs={
|
83 |
-
"
|
|
|
84 |
},
|
85 |
),
|
86 |
datasets.SplitGenerator(
|
87 |
name=datasets.Split.TEST,
|
88 |
gen_kwargs={
|
89 |
-
"
|
|
|
90 |
},
|
91 |
),
|
92 |
]
|
93 |
|
94 |
-
def _generate_examples(self,
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import datasets
|
3 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import os
|
5 |
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
from datasets.tasks import ImageClassification
|
9 |
|
10 |
|
11 |
+
_HOMEPAGE = "https://huggingface.co/datasets/rshrott/renovation"
|
12 |
|
13 |
_CITATION = """\
|
14 |
+
@ONLINE {renovationquality,
|
15 |
+
author="Your Name",
|
16 |
+
title="Renovation Quality Dataset",
|
17 |
+
month="Your Month",
|
18 |
+
year="Your Year",
|
19 |
+
url="https://huggingface.co/datasets/rshrott/renovation"
|
20 |
}
|
21 |
"""
|
22 |
|
23 |
_DESCRIPTION = """\
|
24 |
+
This dataset contains images of various properties, along with labels indicating the quality of renovation - 'cheap', 'average', 'expensive'.
|
|
|
|
|
|
|
|
|
25 |
"""
|
26 |
|
27 |
+
_URL = "https://huggingface.co/datasets/rshrott/renovation/raw/main/labels.csv"
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
_NAMES = ["cheap", "average", "expensive"]
|
30 |
|
31 |
+
class RenovationQualityDataset(datasets.GeneratorBasedBuilder):
|
32 |
+
"""Renovation Quality Dataset."""
|
33 |
|
34 |
+
VERSION = datasets.Version("1.0.0")
|
|
|
35 |
|
36 |
def _info(self):
|
37 |
return datasets.DatasetInfo(
|
|
|
40 |
{
|
41 |
"image_file_path": datasets.Value("string"),
|
42 |
"image": datasets.Image(),
|
43 |
+
"label": datasets.features.ClassLabel(names=_NAMES),
|
44 |
}
|
45 |
),
|
46 |
+
supervised_keys=("image", "label"),
|
47 |
homepage=_HOMEPAGE,
|
48 |
citation=_CITATION,
|
49 |
+
task_templates=[ImageClassification(image_column="image", label_column="label")],
|
50 |
)
|
51 |
|
52 |
def _split_generators(self, dl_manager):
|
53 |
+
csv_path = dl_manager.download(_URL)
|
54 |
return [
|
55 |
datasets.SplitGenerator(
|
56 |
name=datasets.Split.TRAIN,
|
57 |
gen_kwargs={
|
58 |
+
"filepath": csv_path,
|
59 |
+
"split": "train",
|
60 |
},
|
61 |
),
|
62 |
datasets.SplitGenerator(
|
63 |
name=datasets.Split.VALIDATION,
|
64 |
gen_kwargs={
|
65 |
+
"filepath": csv_path,
|
66 |
+
"split": "validation",
|
67 |
},
|
68 |
),
|
69 |
datasets.SplitGenerator(
|
70 |
name=datasets.Split.TEST,
|
71 |
gen_kwargs={
|
72 |
+
"filepath": csv_path,
|
73 |
+
"split": "test",
|
74 |
},
|
75 |
),
|
76 |
]
|
77 |
|
78 |
+
def _generate_examples(self, filepath, split):
|
79 |
+
def url_to_image(url):
|
80 |
+
response = requests.get(url)
|
81 |
+
img = Image.open(BytesIO(response.content))
|
82 |
+
return img
|
83 |
+
|
84 |
+
with open(filepath, "r") as f:
|
85 |
+
reader = csv.reader(f)
|
86 |
+
next(reader) # skip header
|
87 |
+
rows = list(reader)
|
88 |
+
if split == 'train':
|
89 |
+
rows = rows[:int(0.8 * len(rows))]
|
90 |
+
elif split == 'validation':
|
91 |
+
rows = rows[int(0.8 * len(rows)):int(0.9 * len(rows))]
|
92 |
+
else: # test
|
93 |
+
rows = rows[int(0.9 * len(rows)):]
|
94 |
+
|
95 |
+
for id_, row in enumerate(rows):
|
96 |
+
if len(row) < 2:
|
97 |
+
print(f"Row with id {id_} has less than 2 elements: {row}")
|
98 |
+
else:
|
99 |
+
image_file_path = str(row[0])
|
100 |
+
image = url_to_image(image_file_path)
|
101 |
+
yield id_, {
|
102 |
+
'image_file_path': image_file_path,
|
103 |
+
'image': image,
|
104 |
+
'label': row[1],
|
105 |
+
}
|