-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet_train.py
executable file
·167 lines (140 loc) · 5.92 KB
/
unet_train.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 21 11:48:17 2022
@author: sen
"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import DataLoader
from unet_model import VGGNET
from camvid_dataloader import CamVidDataset
from camvid_utils import iou, pixel_acc
import numpy as np
import os
from tqdm import tqdm
from torchvision.models import vgg16_bn
__all__ = ['vgg16bn_unet']
nworkers = 2
nclasses = 32
batch_size = 8
epochs = 100
lr = 0.0002
weight_decay = 0.0005
device = "cuda" if torch.cuda.is_available() else "cpu"
root_dir = "CamVid/"
train_file = os.path.join(root_dir, "train.txt")
val_file = os.path.join(root_dir, "val.txt")
path_cpt_file = 'cpts/unet.cpt'
save_model = True
def train (train_loader, model, optimizer, loss_f):
loop = tqdm(train_loader, leave = True)
model.train()
total_ious = []
pixel_accs = []
for batch_idx, batch in enumerate(loop):
x, y = Variable(batch['X']).to(device), Variable(batch['Y']).to(device)
out = model(x)
N, _, h, w = out.shape
pred = out.data.cpu().numpy().transpose(0, 2, 3, 1).reshape(-1, nclasses).argmax(axis=1).reshape(N, h, w)
del x
loss_val = loss_f(out, y)
target = batch['l'].cpu().numpy().reshape(N, h, w)
for p, t in zip(pred, target):
total_ious.append(iou(p, t))
pixel_accs.append(pixel_acc(p, t))
del y
del out
optimizer.zero_grad()
loss_val.backward()
optimizer.step()
# update progress bar
loop.set_postfix(loss = loss_val.item())
total_ious = np.array(total_ious).T
ious = np.nanmean(total_ious, axis=1)
pixel_accs = np.array(pixel_accs).mean()
return (float(loss_val.item()), np.nanmean(ious), pixel_accs)
def test (test_loader, model, loss_f):
loop = tqdm(test_loader, leave = True)
model.eval()
total_ious = []
pixel_accs = []
with torch.no_grad():
for batch_idx, batch in enumerate(loop):
x, y = Variable(batch['X']).to(device), Variable(batch['Y']).to(device)
out = model(x)
N, _, h, w = out.shape
pred = out.data.cpu().numpy().transpose(0, 2, 3, 1).reshape(-1, nclasses).argmax(axis=1).reshape(N, h, w)
del x
loss_val = loss_f(out, y)
target = batch['l'].cpu().numpy().reshape(N, h, w)
for p, t in zip(pred, target):
total_ious.append(iou(p, t))
pixel_accs.append(pixel_acc(p, t))
del y
del out
# update progress bar
loop.set_postfix(loss = loss_val.item())
total_ious = np.array(total_ious).T
ious = np.nanmean(total_ious, axis=1)
pixel_accs = np.array(pixel_accs).mean()
return (float(loss_val.item()), np.nanmean(ious), pixel_accs)
def vgg16bn_unet(nclasses, pretrained = False):
return VGGNET(vgg16_bn, pretrained=pretrained, nclasses=nclasses)
def main():
unet_model = vgg16bn_unet(nclasses = nclasses, pretrained=True).to(device)
optimizer = optim.Adam(unet_model.parameters(), lr = lr, weight_decay = weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=4,
T_mult=2,
eta_min=0.000002,
last_epoch=-1)
loss_f = nn.BCEWithLogitsLoss()
train_dataset = CamVidDataset(file = train_file, phase ='train')
test_dataset = CamVidDataset(file = val_file, phase ='val')
train_loader = DataLoader(dataset = train_dataset, batch_size = batch_size,
num_workers = nworkers, shuffle = True)
test_loader = DataLoader(dataset = test_dataset, batch_size = batch_size,
num_workers = nworkers, shuffle = True)
loss_lst = []
iou_avg = []
pixel_avg_acc = []
loss_lst_test = []
iou_avg_test = []
pixel_avg_acc_test = []
for epoch in range(epochs):
loss_value, iou_avg_val, pixel_avg_val = train(train_loader, unet_model, optimizer, loss_f)
loss_lst.append(loss_value)
iou_avg.append(iou_avg_val)
pixel_avg_acc.append(pixel_avg_val)
loss_value_test, iou_avg_val_test, pixel_avg_val_test = test(test_loader, unet_model, loss_f)
loss_lst_test.append(loss_value_test)
iou_avg_test.append(iou_avg_val_test)
pixel_avg_acc_test.append(pixel_avg_val_test)
print(f"Epoch:{epoch} Train[Loss:{loss_value} Avg_IoU:{iou_avg[-1]} Pix_acc:{pixel_avg_acc[-1]}]")
print(f"Epoch:{epoch} Test[Loss:{loss_value_test} Avg_IoU:{iou_avg_test[-1]} Pix_acc:{pixel_avg_acc_test[-1]}]")
scheduler.step()
if epoch == epochs - 1:
with open('results/unet_loss.txt','w') as values:
values.write(str(loss_lst))
with open('results/unet_iou.txt','w') as values:
values.write(str(iou_avg))
with open('results/unet_pixel_acc.txt','w') as values:
values.write(str(pixel_avg_acc))
with open('results/unet_loss_test.txt','w') as values:
values.write(str(loss_lst_test))
with open('results/unet_iou_test.txt','w') as values:
values.write(str(iou_avg_test))
with open('results/unet_pixel_acc_test.txt','w') as values:
values.write(str(pixel_avg_acc_test))
torch.save({
'model_state_dict': unet_model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}, path_cpt_file)
save_model = False
print("Results and Model Stores!")
break
if __name__ == "__main__":
main()