-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsweep_SegFormer.py
318 lines (262 loc) · 13.3 KB
/
sweep_SegFormer.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from scripts.dataloader_SegFormer import CustomDataset
from scripts.metrics_filtered_cls import compute_map_cls, compute_IoU
from transformers import SegformerModel, SegformerConfig, SegformerForSemanticSegmentation, SegformerImageProcessor
from torch.optim import SGD, Adam, Adagrad, AdamW
from torch.utils.data import DataLoader
import torch.optim.lr_scheduler as lr_scheduler
import torch.nn.functional as F
from torchsummary import summary
import albumentations as A
import torch.nn as nn
import cv2
import torch
import numpy as np
import os
import wandb
from tqdm import tqdm
import time
import copy
def get_image_4_wandb(path, input_size = [224,224]):
transform_img = A.Compose([
A.Resize(height=input_size[0], width=input_size[1], interpolation=cv2.INTER_NEAREST),
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
A.pytorch.ToTensorV2(p=1.0),
])
image = cv2.imread(next((os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))), None))
image = transform_img(image=image)['image']
image = image.unsqueeze(0)
image = image.cpu()
return image
def wandb_init(num_epochs, lr, batch_size, outputs, optimizer, scheduler, model):
wandb.init(
project="RailNet",
config={
"learning_rate": lr,
"batch_size": batch_size,
"epochs": num_epochs,
"outputs": outputs,
"optimizer": optimizer,
"scheduler": scheduler,
"model": model,
}
)
LIGHT = False
WANDB = True
if not LIGHT:
PATH_JPGS = "RailNet_DT/rs19_val/jpgs/rs19_val"
PATH_MASKS = "RailNet_DT/rs19_val/uint8/rs19_val" # /rails
else:
PATH_JPGS = "RailNet_DT/rs19_val_light/jpgs/rs19_val"
PATH_MASKS = "RailNet_DT/rs19_val_light/uint8/rs19_val"
PATH_MODELS = "RailNet_DT/models"
PATH_LOGS = "RailNet_DT/logs"
def create_model(output_channels=1):
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-1024-1024",
num_labels=output_channels,
ignore_mismatched_sizes=True)
model.train()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
return model
def load_model(model_path):
model = torch.load(model_path, map_location=torch.device('cpu'))
model.train()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
return model
def train(model, num_epochs, batch_size, image_size, optimizer, criterion, scheduler, config):
start = time.time()
best_model = copy.deepcopy(model.state_dict())
best_loss = 1e10
loss = 0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
for epoch in range(num_epochs):
print('-' * 20)
print('Epoch {}/{}'.format(epoch+1, num_epochs))
# Epoch
train_loss = 0 # for the wandb logging
val_loss = 0 # --||--
val_MmAP, val_mAP, val_IoU, val_MIoU = list(), list(), list(), list()
classes_MAP, classes_AP, classes_IoU, classes_MIoU = {},{},{},{}
dl_lentrain = 0
dl_lenval = 0
for phase in ['Train', 'Valid']:
image_processor = SegformerImageProcessor(reduce_labels=False)
dataset = CustomDataset(PATH_JPGS, PATH_MASKS, image_processor, image_size, subset=phase, val_fraction=0.5)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True)
if phase == 'Train':
model.train()
dl_lentrain = len(dataloader)
for inputs, masks in tqdm(dataloader):
inputs = inputs.to(device)
masks = masks.to(device)
# zero the parameter gradients
optimizer.zero_grad()
outputs = model(inputs)
logits = outputs.logits
upsampled_logits = nn.functional.interpolate(
logits,
size=masks.shape[-2:],
mode="bilinear",
align_corners=False
)
upsampled_logits = upsampled_logits.float()
loss = criterion(upsampled_logits, masks)
loss.backward() # gradients
optimizer.step() # update parameters
train_loss += loss
elif phase == 'Valid':
model.eval()
dl_lenval = len(dataloader)
with torch.no_grad():
for inputs, masks in tqdm(dataloader):
inputs = inputs.to(device)
masks = masks.to(device)
outputs = model(inputs)
logits = outputs.logits
upsampled_logits = nn.functional.interpolate(
logits,
size=masks.shape[-2:],
mode="bilinear",
align_corners=False
)
upsampled_logits = upsampled_logits.float()
loss = criterion(upsampled_logits, masks)
val_loss += loss
predicted_masks = upsampled_logits
gt_masks = masks.cpu().detach().numpy().squeeze()
for prediction, gt in zip(predicted_masks, gt_masks):
prediction = F.softmax(prediction, dim=0).cpu().detach().numpy().squeeze()
prediction = np.argmax(prediction, axis=0).astype(np.uint8)
mAP,classes_AP = compute_map_cls(gt, prediction, classes_AP)
Mmap,classes_MAP = compute_map_cls(gt, prediction, classes_MAP, major = True)
IoU,_,_,_,classes_IoU = compute_IoU(gt, prediction, classes_IoU)
MIoU,_,_,_,classes_MIoU = compute_IoU(gt, prediction, classes_MIoU, major=True)
val_mAP.append(mAP)
val_MmAP.append(Mmap)
val_IoU.append(IoU)
val_MIoU.append(MIoU)
# Compute the epoch mAP and IoU
val_MmAP, val_mAP = np.nanmean(val_MmAP), np.nanmean(val_mAP)
val_MIoU, val_IoU = np.nanmean(val_MIoU), np.nanmean(val_IoU)
for cls, value in classes_MAP.items():
classes_MAP[cls] = np.divide(value[0], value[1])
classes_MmAP_all= np.mean(np.array(list(classes_MAP.values())), axis=0)
for cls, value in classes_AP.items():
classes_AP[cls] = np.divide(value[0], value[1])
classes_mAP_all= np.mean(np.array(list(classes_AP.values())), axis=0)
for cls, value in classes_IoU.items():
classes_IoU[cls] = np.divide(value[0], value[1])
classes_IoU_all= np.mean(np.array(list(classes_IoU.values()))[:, :4], axis=0)
for cls, value in classes_MIoU.items():
classes_MIoU[cls] = np.divide(value[0], value[1])
classes_MIoU_all= np.mean(np.array(list(classes_MIoU.values()))[:, :4], axis=0)
if config.scheduler == 'LinearLR':
if epoch > 50:
scheduler.step()
current_lr = scheduler.get_last_lr()[0]
elif config.scheduler == 'ReduceLROnPlateau':
scheduler.step(classes_MIoU_all[0])
current_lr = scheduler._last_lr[0]
# Print epoch summary
print('Epoch {}/{}: Train loss: {:.4f} | Val loss: {:.4f} | lr: {:.4f} | mAP: {:.4f} | MmAP: {:.4f} | IoU: {:.4f} | MIoU: {:.4f}'.format(epoch + 1,num_epochs,train_loss/dl_lentrain,val_loss/dl_lenval,current_lr,classes_mAP_all,classes_MmAP_all,classes_IoU_all[0],classes_MIoU_all[0]))
with open(os.path.join(PATH_LOGS, 'log_{}_{}.txt'.format(num_epochs, config.learning_rate)), 'a') as log_file:
log_file.write('Epoch {}/{}: Train loss: {:.4f} | Val loss: {:.4f} | lr: {:.4f} | mAP: {:.4f} | MmAP: {:.4f} | IoU: {:.4f} | MIoU: {:.4f}'.format(epoch + 1,num_epochs,train_loss/dl_lentrain,val_loss/dl_lenval,current_lr,classes_mAP_all,classes_MmAP_all,classes_IoU_all[0],classes_MIoU_all[0]))
# Save model checkpoint every X epochs
if epoch > 1 and epoch % 10 == 0 and phase == 'Valid':
torch.save(model, os.path.join(PATH_MODELS,'modelchp_{}_{}_{:3f}.pth'.format(wandb.run.name, epoch, classes_MIoU_all[0])))
print('Saving checkpoint as: modelchp_{}_{}_{:3f}.pth'.format(wandb.run.name, epoch, classes_MIoU_all[0]))
# Save the best model based on validation loss
if phase == 'Valid' and (val_loss/dl_lenval) < best_loss:
best_loss = (val_loss/dl_lenval)
best_model = copy.deepcopy(model.state_dict())
print('Saving model as the best so far: modelb_{}_{}_{:3f}.pth'.format(wandb.run.name, epoch, classes_MIoU_all[0]))
if WANDB:
normalized_results = upsampled_logits[0].softmax(dim=0).cpu().detach().numpy().squeeze()
id_map = np.argmax(normalized_results, axis=0).astype(np.uint8)
id_map = np.divide(id_map,np.max(id_map))
im_classes = []
for class_id in range(config.outs-1):
im_classes.append(wandb.Image((upsampled_logits[0][class_id].cpu()).detach().numpy(), caption="Prediction of a class {}".format(class_id+1)))
im_classes.append(wandb.Image((upsampled_logits[0][-1].cpu()).detach().numpy(), caption="Background"))
id_log = wandb.Image(id_map, caption="Predicted ID map")
mask_log = masks[0].cpu().detach().numpy() + 1
mask_log[mask_log==256] = 0
mask_log = (mask_log / config.outs)
mask_log = wandb.Image(mask_log, caption="Input mask")
wandb.log({
"train_loss" : train_loss,
"val_loss" : val_loss,
"lr" : current_lr,
"mAP" : classes_mAP_all,
"MmAP" : classes_MmAP_all,
"IoU" : classes_IoU_all[0],
"MIoU" : classes_MIoU_all[0],
"Input, predicted mask, background" : [mask_log, id_log, im_classes[-1]],
"Classes": im_classes[0:-2]
})
time_elapsed = time.time() - start
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Lowest Loss: {:4f}'.format(best_loss))
final_model = model
model.load_state_dict(best_model)
return final_model, model
sweep_config = {
'method': 'random', # 'bayes', 'grid'
'metric': {
'name': 'MIoU',
'goal': 'maximize'
},
'parameters': {
'epochs': {
'value': 50
},
'learning_rate': {
'distribution': 'uniform',
'min': 0.00001,
'max': 0.002
},
'optimizer': {
'values': ['adam', 'adagrad'] # Different optimizers to sweep over
},
'scheduler': {
'values': ['ReduceLROnPlateau', 'LinearLR'] # Different schedulers to sweep over
},
'batch_size': {
'distribution': 'q_log_uniform_values',
'q': 4,
'min': 4,
'max': 32
},
'image_size': {
'value': 1024 # Fixed image size
},
'outs': {
'value': 13 # Fixed number of outputs
}
}
}
def sweep_train():
with wandb.init() as run:
config = wandb.config
model = create_model(config.outs)
# Define optimizer
if config.optimizer == 'adam':
optimizer = Adam(model.parameters(), lr=config.learning_rate)
elif config.optimizer == 'adagrad':
optimizer = Adagrad(model.parameters(), lr=config.learning_rate)
elif config.optimizer == 'adamw':
optimizer = AdamW(model.parameters(), lr=config.learning_rate)
# Define scheduler
if config.scheduler == 'ReduceLROnPlateau':
scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=5, verbose=True, threshold=0.005, threshold_mode='abs')
elif config.scheduler == 'LinearLR':
scheduler = lr_scheduler.LinearLR(optimizer, start_factor=1.0, end_factor=0.5, total_iters=2)
loss_function = nn.CrossEntropyLoss(ignore_index=255)
model_final, best_model = train(model, config.epochs, config.batch_size, [config.image_size,config.image_size], optimizer, loss_function, scheduler, config)
torch.save(model_final, os.path.join(PATH_MODELS, 'model_{}.pth'.format(wandb.run.name)))
torch.save(best_model, os.path.join(PATH_MODELS, 'modelb_{}.pth'.format(wandb.run.name)))
print('Saved as: model_{}.pth'.format(wandb.run.name))
if __name__ == "__main__":
sweep_id = wandb.sweep(sweep_config, project="DP_train_full")
wandb.agent(sweep_id, sweep_train, count=10)