-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
299 lines (231 loc) · 8.44 KB
/
helper.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
from collections import defaultdict
from glob import glob
import cv2
import helper
import numpy as np
import pandas as pd
import timm
import torch
import torchvision.transforms as T
from albumentations.pytorch import ToTensorV2
from scipy.signal import convolve2d
from sklearn.metrics import accuracy_score, f1_score
from torchvision.datasets import ImageFolder
from tqdm import tqdm
GAUSSIAN_3X3_WEIGHT = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]])
GAUSSIAN_3X3_WEIGHT = np.divide(GAUSSIAN_3X3_WEIGHT, 16)
def load_image(directory, size=(224, 224)):
image = cv2.imread(directory)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if not size is None:
image = cv2.resize(image, size)
return np.uint16(image)
def save_image(image, save_path):
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if not cv2.imwrite(save_path, image):
raise ValueError(f"Unable to save to {save_path}!")
def clip(image):
return np.uint16(np.clip(image, 0, 255))
def convolve(image, weight):
args = dict(mode="same", boundary="symm")
size = np.shape(image)
if len(size) < 3:
image = convolve2d(image, weight, **args)
return clip(image)
for i in range(size[-1]):
image[..., i] = convolve2d(image[..., i], weight, **args)
return clip(image)
def gaussian_blur(image, num_convolve):
image_copy = np.copy(image)
for _ in range(num_convolve):
image_copy = convolve(image_copy, GAUSSIAN_3X3_WEIGHT)
return image_copy
def gaussian_pixel_noise(image, std):
size = np.shape(image)
noise = np.random.normal(scale=std, size=size)
return clip(image + noise)
def scale_contrast(image, scale):
return clip(image * scale)
def change_brightness(image, value):
return clip(image + value)
def occlusion(image, edge_length):
image_copy = np.copy(image)
if edge_length > 0:
size = np.shape(image)
h_start = np.random.randint(size[0] - edge_length)
h_end = h_start + edge_length
w_start = np.random.randint(size[1] - edge_length)
w_end = w_start + edge_length
mask = np.zeros([edge_length] * 2).astype(np.int16)
if len(size) > 2:
mask = np.expand_dims(mask, -1)
image_copy[h_start:h_end, w_start:w_end] = mask
return clip(image_copy)
def salt_and_pepper(image, rate, salt_ratio=0.5):
size = np.shape(image)
mask = np.random.random(size)
pepper = mask < rate
salt = mask < rate * salt_ratio
image_copy = np.copy(image)
image_copy[pepper] = 0
image_copy[salt] = 255
return clip(image_copy)
def f1_macro(outputs, targets):
outputs = outputs.argmax(-1).cpu().numpy()
targets = targets.cpu().numpy()
return f1_score(targets, outputs, average="macro")
def accuracy(outputs, targets):
outputs = outputs.argmax(-1).cpu().numpy()
targets = targets.cpu().numpy()
return accuracy_score(targets, outputs)
# Dataloader hyperparameter
def construct_dataset(batch_size):
transforms = [
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
transforms_train = T.Compose([T.RandAugment(), *transforms])
transforms_eval = T.Compose(transforms)
dataset = {
"train": ImageFolder("dataset/train", transform=transforms_train),
"valid": ImageFolder("dataset/valid", transform=transforms_eval),
"test": ImageFolder("dataset/test", transform=transforms_eval),
}
dataloader = {
"train": torch.utils.data.DataLoader(
dataset["train"], batch_size, shuffle=True, pin_memory=True
),
"valid": torch.utils.data.DataLoader(
dataset["valid"], batch_size, pin_memory=True
),
"test": torch.utils.data.DataLoader(
dataset["test"], batch_size, pin_memory=True
),
}
return dataset, dataloader
def pretrained(backbone):
backbone = timm.create_model(backbone, pretrained=True)
for p in backbone.parameters():
p.requires_grad = False
return backbone
def generate_classifier(backbone, num_classes, p_dropout=0):
dropout = torch.nn.Identity()
if p_dropout > 0:
dropout = torch.nn.Dropout(p_dropout)
if isinstance(backbone, timm.models.resnet.ResNet):
in_features = backbone.fc.in_features
backbone.fc = torch.nn.Sequential(
dropout, torch.nn.Linear(in_features, num_classes)
)
if isinstance(backbone, timm.models.ConvNeXt):
in_features = backbone.head.fc.in_features
backbone.head.drop = dropout
backbone.head.fc = torch.nn.Linear(in_features, num_classes)
if isinstance(backbone, timm.models.maxxvit.MaxxVit):
in_features = backbone.head.fc.in_features
backbone.head.fc = torch.nn.Sequential(
dropout, torch.nn.Linear(in_features, num_classes)
)
if isinstance(backbone, timm.models.mlp_mixer.MlpMixer):
in_features = backbone.head.in_features
backbone.head = torch.nn.Sequential(
dropout, torch.nn.Linear(in_features, num_classes)
)
if isinstance(backbone, timm.models.densenet.DenseNet):
in_features = backbone.classifier.in_features
backbone.classifier = torch.nn.Sequential(
dropout, torch.nn.Linear(in_features, num_classes)
)
return backbone
def train_one_epoch(
dataloader,
model,
criterion,
optimizer,
scheduler=None,
scaler=None,
ema=None,
subset="train",
):
record = defaultdict(float)
record["Subset"] = subset.title()
with torch.autocast("cuda"):
model.train()
for inputs, targets in dataloader["train"]:
inputs = inputs.cuda()
targets = targets.cuda()
outputs = model(inputs)
loss = criterion(outputs, targets)
optimizer.zero_grad()
if scaler is None:
loss.backward()
optimizer.step()
else:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
if not scheduler is None:
scheduler.step()
if not ema is None:
ema.update(model)
record["Loss"] += loss.item()
record["Accuracy"] += accuracy(outputs, targets)
record["F1-Macro"] += f1_macro(outputs, targets)
record["Loss"] /= len(dataloader["train"])
record["Accuracy"] /= len(dataloader["train"])
record["F1-Macro"] /= len(dataloader["train"])
return dict(record)
def evaluate(dataloader, model, criterion, subset):
record = defaultdict(float)
record["Subset"] = subset.title()
with torch.autocast("cuda"), torch.inference_mode():
model.eval()
for inputs, targets in dataloader[subset]:
inputs = inputs.cuda()
targets = targets.cuda()
outputs = model(inputs)
loss = criterion(outputs, targets)
record["Loss"] += loss.item()
record["Accuracy"] += accuracy(outputs, targets)
record["F1-Macro"] += f1_macro(outputs, targets)
record["Loss"] /= len(dataloader[subset])
record["Accuracy"] /= len(dataloader[subset])
record["F1-Macro"] /= len(dataloader[subset])
return dict(record)
def train(backbone):
torch.cuda.empty_cache()
torch.manual_seed(2022)
epochs = 200
dataset, dataloader = construct_dataset(512)
num_classes = len(dataset["train"].classes)
model = generate_classifier(pretrained(backbone), num_classes).cuda()
criterion = torch.nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(
model.parameters(),
lr=0.001 * dataloader["train"].batch_size / 256,
momentum=0.9,
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, 50 * len(dataloader["train"]), 1e-6
)
scaler = torch.cuda.amp.GradScaler()
history = []
with tqdm(total=epochs) as pbar:
for _ in range(pbar.total):
history.append(
train_one_epoch(
dataloader,
model,
criterion,
optimizer,
scheduler,
scaler,
)
)
history.append(evaluate(dataloader, model, criterion, "valid"))
pbar.set_postfix(history[-1])
pbar.update()
torch.save(model.state_dict(), f"weights/{backbone}.pt")
del model
return pd.DataFrame(history)