-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuna.py
223 lines (185 loc) · 6.6 KB
/
tuna.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
# import module
import albumentations as A
import optuna
import torch
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data
from albumentations.pytorch import ToTensorV2
from optuna.samplers import TPESampler
from optuna.trial import TrialState
from segmentation_models_pytorch import UnetPlusPlus
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
from src.data.dataset import XRayDataset
from src.loss import DiceBCELoss
from src.scheduler import CosineAnnealingWarmUpRestarts
from src.utils import set_seed
def data_loader():
tf = A.Compose(
[
A.CLAHE(
p=1.0,
clip_limit=(1, 4),
tile_grid_size=(8, 8),
),
A.Resize(512, 512),
A.Normalize(
mean=(0.5, 0.5, 0.5),
std=(0.5, 0.5, 0.5),
max_pixel_value=255.0,
always_apply=True,
),
ToTensorV2(always_apply=True),
]
)
train_dataset = XRayDataset(
data_path="/opt/ml/level2_cv_semanticsegmentation-cv-15/data",
transforms=tf,
split="train",
)
val_dataset = XRayDataset(
data_path="/opt/ml/level2_cv_semanticsegmentation-cv-15/data",
transforms=tf,
split="val",
)
train_loader = DataLoader(
train_dataset,
batch_size=2,
shuffle=True,
num_workers=2,
prefetch_factor=2,
persistent_workers=True,
pin_memory=True,
)
val_loader = DataLoader(
val_dataset,
batch_size=1,
num_workers=1,
prefetch_factor=2,
persistent_workers=True,
pin_memory=True,
)
return train_loader, val_loader
# objective
def objective(trial):
# Generate the model.
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = UnetPlusPlus(
encoder_name="tu-hrnet_w64",
encoder_depth=5,
encoder_weights="imagenet",
in_channels=3,
classes=29,
).to(DEVICE)
# Generate the optimizers.
# 하이퍼 파라미터들 값 지정
# optimizer_name = trial.suggest_categorical("optimizer", ["Adam", "SGD", "AdamW"])
# lr = trial.suggest_float("lr", 1e-5, 1e-3, log=True)
T_0 = trial.suggest_int("T_0", 1000, 3000)
eta_max = trial.suggest_float("eta_max", 0.001, 0.005)
gamma = trial.suggest_float("gamma", 0.1, 0.5)
seed = trial.suggest_int("seed", 11, 20)
set_seed(seed)
optimizer = getattr(optim, "AdamW")(model.parameters(), lr=0.0001)
# scheduler
scheduler = CosineAnnealingWarmUpRestarts(
optimizer,
T_0=T_0,
T_mult=1,
eta_max=eta_max,
T_up=600,
gamma=gamma,
)
criterion = DiceBCELoss()
for epoch in range(30):
model.train()
epoch_loss = 0.0
for step, (images, masks) in tqdm(
enumerate(train_loader), total=len(train_loader)
):
# gpu 연산을 위해 device 할당
images, masks = images.cuda(), masks.cuda()
model = model.cuda()
# inference
outputs = model(images)
# loss 계산
loss = criterion(outputs, masks)
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss.item()
scheduler.step()
# Validation of the model.
# Validation에서 얻어진 Score를 활용하여 설정된 Hyperparameter 평가!
model.eval()
if (epoch + 1) % 30 == 0:
dices = []
with torch.no_grad():
total_loss = 0
cnt = 0
for step, (images, masks) in tqdm(
enumerate(valid_loader), total=len(valid_loader)
):
images, masks = images.cuda(), masks.cuda()
model = model.cuda()
outputs = model(images)
output_h, output_w = outputs.size(-2), outputs.size(-1)
mask_h, mask_w = masks.size(-2), masks.size(-1)
# restore original size
if output_h != mask_h or output_w != mask_w:
outputs = F.interpolate(
outputs, size=(mask_h, mask_w), mode="bilinear"
)
loss = criterion(outputs, masks)
total_loss += loss
cnt += 1
outputs = torch.sigmoid(outputs)
outputs = (outputs > 0.5).detach().cpu()
masks = masks.detach().cpu()
y_true_f = masks.flatten(2)
y_pred_f = outputs.flatten(2)
intersection = torch.sum(y_true_f * y_pred_f, -1)
eps = 0.0001
dices.append(
(2.0 * intersection + eps)
/ (torch.sum(y_true_f, -1) + torch.sum(y_pred_f, -1) + eps)
)
dices = torch.cat(dices, 0)
dices_per_class = torch.mean(dices, 0)
dice_str = [
f"{c:<12}: {d.item():.4f}"
for c, d in zip(
XRayDataset(
"/opt/ml/level2_cv_semanticsegmentation-cv-15/data"
).classes,
dices_per_class,
)
]
dice_str = "\n".join(dice_str)
print(dice_str)
avg_dice = torch.mean(dices_per_class).item()
# Handle pruning based on the intermediate value.
trial.report(avg_dice, epoch)
if trial.should_prune():
raise optuna.exceptions.TrialPruned()
return avg_dice
if __name__ == "__main__":
# load data
train_loader, valid_loader = data_loader()
# avg_dice 최대가 되는 방향으로 학습을 진행
study = optuna.create_study(direction="maximize", sampler=TPESampler())
# n_trials 지정없으면 무한 반복
study.optimize(objective, n_trials=10)
pruned_trials = study.get_trials(deepcopy=False, states=[TrialState.PRUNED])
complete_trials = study.get_trials(deepcopy=False, states=[TrialState.COMPLETE])
print("Study statistics: ")
print(f"Number of finished trials: {len(study.trials)}")
print(f"Number of pruned trials: {len(pruned_trials)}")
print(f"Number of complete trials: {len(complete_trials)}")
print("Best trial:")
trial = study.best_trial
print(" Value:", {trial.value})
print("Params:")
for key, value in trial.params.items():
print(f" {key}: {value}")