refactor: data and script
Browse files- data/images.zip +3 -0
- face_masks.py +97 -54
data/images.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:16db8eda5522a7451e803be4f636b44ab8efc5d5c7a2bcec3c479f65a1ec4712
|
3 |
+
size 100867838
|
face_masks.py
CHANGED
@@ -1,21 +1,23 @@
|
|
1 |
import datasets
|
|
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
_CITATION = """\
|
5 |
@InProceedings{huggingface:dataset,
|
6 |
-
title = {
|
7 |
author = {TrainingDataPro},
|
8 |
year = {2023}
|
9 |
}
|
10 |
"""
|
11 |
|
12 |
_DESCRIPTION = """\
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
All personal information from the document is hidden.
|
17 |
"""
|
18 |
-
_NAME = '
|
19 |
|
20 |
_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
|
21 |
|
@@ -24,46 +26,89 @@ _LICENSE = "cc-by-nc-nd-4.0"
|
|
24 |
_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
|
25 |
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def _info(self):
|
31 |
-
return datasets.DatasetInfo(
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
'selfie_12': datasets.Image(),
|
48 |
-
'selfie_13': datasets.Image(),
|
49 |
-
'user_id': datasets.Value('string'),
|
50 |
-
'set_id': datasets.Value('string'),
|
51 |
-
'user_race': datasets.Value('string'),
|
52 |
-
'name': datasets.Value('string'),
|
53 |
-
'age': datasets.Value('int8'),
|
54 |
-
'country': datasets.Value('string'),
|
55 |
-
'gender': datasets.Value('string')
|
56 |
-
}),
|
57 |
-
supervised_keys=None,
|
58 |
-
homepage=_HOMEPAGE,
|
59 |
-
citation=_CITATION,
|
60 |
-
license=_LICENSE
|
61 |
-
)
|
62 |
|
63 |
def _split_generators(self, dl_manager):
|
64 |
-
images = dl_manager.
|
65 |
annotations = dl_manager.download(f"{_DATA}{_NAME}.csv")
|
66 |
-
images = dl_manager.
|
67 |
return [
|
68 |
datasets.SplitGenerator(name=datasets.Split.TRAIN,
|
69 |
gen_kwargs={
|
@@ -73,24 +118,22 @@ class SelfiesAndId(datasets.GeneratorBasedBuilder):
|
|
73 |
]
|
74 |
|
75 |
def _generate_examples(self, images, annotations):
|
76 |
-
annotations_df = pd.read_csv(annotations, sep='
|
77 |
-
images_data = pd.DataFrame(columns=['
|
78 |
-
for idx,
|
79 |
-
|
|
|
80 |
|
81 |
annotations_df = pd.merge(annotations_df,
|
82 |
images_data,
|
83 |
how='left',
|
84 |
-
on=['
|
85 |
-
for idx, worker_id in enumerate(pd.unique(annotations_df['
|
86 |
-
annotation = annotations_df.loc[
|
87 |
-
|
88 |
-
annotation = annotation.sort_values(['
|
89 |
data = {
|
90 |
-
row[
|
91 |
-
'path': row[6],
|
92 |
-
'bytes': row[10]
|
93 |
-
} for row in annotation.itertuples()
|
94 |
}
|
95 |
|
96 |
age = annotation.loc[annotation['FName'] ==
|
|
|
1 |
import datasets
|
2 |
+
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
import PIL.Image
|
5 |
+
import PIL.ImageOps
|
6 |
|
7 |
_CITATION = """\
|
8 |
@InProceedings{huggingface:dataset,
|
9 |
+
title = {face_masks},
|
10 |
author = {TrainingDataPro},
|
11 |
year = {2023}
|
12 |
}
|
13 |
"""
|
14 |
|
15 |
_DESCRIPTION = """\
|
16 |
+
Dataset includes 250 000 images, 4 types of mask worn on 28 000 unique faces.
|
17 |
+
All images were collected using the Toloka.ai crowdsourcing service and
|
18 |
+
validated by TrainingData.pro
|
|
|
19 |
"""
|
20 |
+
_NAME = 'face_masks'
|
21 |
|
22 |
_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
|
23 |
|
|
|
26 |
_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
|
27 |
|
28 |
|
29 |
+
def exif_transpose(img):
|
30 |
+
if not img:
|
31 |
+
return img
|
32 |
+
|
33 |
+
exif_orientation_tag = 274
|
34 |
+
|
35 |
+
# Check for EXIF data (only present on some files)
|
36 |
+
if hasattr(img, "_getexif") and isinstance(
|
37 |
+
img._getexif(), dict) and exif_orientation_tag in img._getexif():
|
38 |
+
exif_data = img._getexif()
|
39 |
+
orientation = exif_data[exif_orientation_tag]
|
40 |
+
|
41 |
+
# Handle EXIF Orientation
|
42 |
+
if orientation == 1:
|
43 |
+
# Normal image - nothing to do!
|
44 |
+
pass
|
45 |
+
elif orientation == 2:
|
46 |
+
# Mirrored left to right
|
47 |
+
img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
|
48 |
+
elif orientation == 3:
|
49 |
+
# Rotated 180 degrees
|
50 |
+
img = img.rotate(180)
|
51 |
+
elif orientation == 4:
|
52 |
+
# Mirrored top to bottom
|
53 |
+
img = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT)
|
54 |
+
elif orientation == 5:
|
55 |
+
# Mirrored along top-left diagonal
|
56 |
+
img = img.rotate(-90,
|
57 |
+
expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
|
58 |
+
elif orientation == 6:
|
59 |
+
# Rotated 90 degrees
|
60 |
+
img = img.rotate(-90, expand=True)
|
61 |
+
elif orientation == 7:
|
62 |
+
# Mirrored along top-right diagonal
|
63 |
+
img = img.rotate(90,
|
64 |
+
expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
|
65 |
+
elif orientation == 8:
|
66 |
+
# Rotated 270 degrees
|
67 |
+
img = img.rotate(90, expand=True)
|
68 |
+
|
69 |
+
return img
|
70 |
+
|
71 |
+
|
72 |
+
def load_image_file(file, mode='RGB'):
|
73 |
+
# Load the image with PIL
|
74 |
+
img = PIL.Image.open(file)
|
75 |
+
|
76 |
+
if hasattr(PIL.ImageOps, 'exif_transpose'):
|
77 |
+
# Very recent versions of PIL can do exit transpose internally
|
78 |
+
img = PIL.ImageOps.exif_transpose(img)
|
79 |
+
else:
|
80 |
+
# Otherwise, do the exif transpose ourselves
|
81 |
+
img = exif_transpose(img)
|
82 |
+
|
83 |
+
img = img.convert(mode)
|
84 |
+
|
85 |
+
return np.array(img)
|
86 |
+
|
87 |
+
|
88 |
+
class FaceMasks(datasets.GeneratorBasedBuilder):
|
89 |
|
90 |
def _info(self):
|
91 |
+
return datasets.DatasetInfo(description=_DESCRIPTION,
|
92 |
+
features=datasets.Features({
|
93 |
+
'photo_1': datasets.Image(),
|
94 |
+
'photo_2': datasets.Image(),
|
95 |
+
'photo_3': datasets.Image(),
|
96 |
+
'photo_4': datasets.Image(),
|
97 |
+
'selfie_5': datasets.Image(),
|
98 |
+
'worker_id': datasets.Value('string'),
|
99 |
+
'age': datasets.Value('int8'),
|
100 |
+
'country': datasets.Value('string'),
|
101 |
+
'sex': datasets.Value('string')
|
102 |
+
}),
|
103 |
+
supervised_keys=None,
|
104 |
+
homepage=_HOMEPAGE,
|
105 |
+
citation=_CITATION,
|
106 |
+
license=_LICENSE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
def _split_generators(self, dl_manager):
|
109 |
+
images = dl_manager.download_and_extract(f"{_DATA}images.zip")
|
110 |
annotations = dl_manager.download(f"{_DATA}{_NAME}.csv")
|
111 |
+
images = dl_manager.iter_files(images)
|
112 |
return [
|
113 |
datasets.SplitGenerator(name=datasets.Split.TRAIN,
|
114 |
gen_kwargs={
|
|
|
118 |
]
|
119 |
|
120 |
def _generate_examples(self, images, annotations):
|
121 |
+
annotations_df = pd.read_csv(annotations, sep=',')
|
122 |
+
images_data = pd.DataFrame(columns=['Link', 'Image'])
|
123 |
+
for idx, image_path in enumerate(images):
|
124 |
+
image = load_image_file(image_path)
|
125 |
+
images_data.loc[idx] = {'Link': image_path, 'Image': image}
|
126 |
|
127 |
annotations_df = pd.merge(annotations_df,
|
128 |
images_data,
|
129 |
how='left',
|
130 |
+
on=['Link'])
|
131 |
+
for idx, worker_id in enumerate(pd.unique(annotations_df['WorkerId'])):
|
132 |
+
annotation: pd.DataFrame = annotations_df.loc[
|
133 |
+
annotations_df['WorkerId'] == worker_id]
|
134 |
+
annotation = annotation.sort_values(['Link'])
|
135 |
data = {
|
136 |
+
f'photo_{row[0]}': row[6] for row in annotation.itertuples()
|
|
|
|
|
|
|
137 |
}
|
138 |
|
139 |
age = annotation.loc[annotation['FName'] ==
|