forked from bcaitech1/p1-img-Jo-ad-fontes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
172 lines (126 loc) · 4.61 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import random
import numpy as np
import torch
import torch.utils.data as data
from PIL import Image
from glob import glob
from torch.utils.data import Dataset
from albumentations import *
from albumentations.pytorch import ToTensorV2
from RandAugment import RandAugment
def seed_everything(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
seed_everything(42)
class eval_data(Dataset):
def __init__(self, root="./input/data/eval/images", transform=None):
super(eval_data, self).__init__()
self.root = root
self.images = sorted(glob(root + "/*"))
self.transform = transform
def __getitem__(self, idx):
image_name = self.images[idx]
image = Image.open(image_name)
img_w, img_h = image.size
image_transform = self.transform(image=np.array(image))["image"]
return image_name, image_transform
def __len__(self):
return len(self.images)
def get_eval_loader():
transform = get_transforms()
eval_dataset = eval_data(transform=transform["val"])
eval_loader = data.DataLoader(
eval_dataset, batch_size=90, num_workers=2, shuffle=False
)
return eval_loader
class Img_data(Dataset):
def __init__(self, root="/opt/ml/input/data/train/images", transform=None):
super(Img_data, self).__init__()
self.root = root
self.transform = transform
self.images = sorted(glob(root + "/**/*"))
def set_transform(self, transform):
self.transform = transform
def __getitem__(self, idx):
image_name = self.images[idx]
image = Image.open(image_name)
img_w, img_h = image.size
label = self.label_attach(image_name)
# albumentations을 사용하기 위한 형식
image_transform = self.transform(image=np.array(image))["image"]
return image_transform, label
def __len__(self):
return len(self.images)
def label_attach(self, info):
label = None
arr = info.split("/")[-2:]
attr = arr[0].split("_")
img_id, gender, area, age = attr
mask = arr[1]
age = int(age)
# label 0~17 쉽게 하는 방법이 있을 것 같긴한데...
mask_labeling = {"m": 0, "i": 1, "n": 2}
gender_labeling = {"male": 0, "female": 1}
age_labeling = lambda x: 0 if int(x) < 30 else 1 if int(x) < 58 else 2
mask_label = mask_labeling[mask[0]]
gender_label = gender_labeling[gender]
age_label = age_labeling(age)
label = self.encode_multi_class(mask_label, gender_label, age_label)
return label
@staticmethod
def encode_multi_class(mask_label, gender_label, age_label):
return mask_label * 6 + gender_label * 3 + age_label
@staticmethod
def decode_multi_class(multi_class_label):
mask_label = (multi_class_label // 6) % 3
gender_label = (multi_class_label // 3) % 2
age_label = multi_class_label % 3
return mask_label, gender_label, age_label
def get_loader(BATCH_SIZE):
data_set = Img_data()
indices = [i for i in range(0, len(data_set))]
train_data = data.Subset(data_set, indices[:15120])
val_data = data.Subset(data_set, indices[15120:])
transform = get_transforms()
train_data.dataset.set_transform(transform["train"])
val_data.dataset.set_transform(transform["val"])
train_loader = data.DataLoader(
train_data, batch_size=BATCH_SIZE, num_workers=2, shuffle=True, pin_memory=True
)
val_loader = data.DataLoader(
val_data, batch_size=BATCH_SIZE, num_workers=2, shuffle=False, pin_memory=True
)
return train_loader, val_loader
def get_transforms(
need=("train", "val"),
img_size=(384, 384),
mean=(0.5601, 0.5241, 0.5015),
std=(0.233, 0.243, 0.24567),
):
transformations = {}
if "train" in need:
transformations["train"] = Compose(
[
Resize(img_size[0], img_size[1], p=1.0),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0,
)
if "val" in need:
transformations["val"] = Compose(
[
Resize(img_size[0], img_size[1]),
Normalize(mean=mean, std=std, max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
],
p=1.0,
)
return transformations
if __name__ == "__main__":
pass