-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_bce.py
173 lines (119 loc) · 4.52 KB
/
train_bce.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
from model import UNet
from dataset import SegmentationDataset
import torch.optim as optim
import sys
from torch import manual_seed, cuda
from torch.utils.data import random_split, DataLoader
import torch
import numpy as np
from glob import glob
import os
from loss_fns import DiceLoss
dataset_root = 'data/dataset-sample/'
img_dir = dataset_root + 'image-chips/'
label_dir = dataset_root + 'label-chips/'
epochs = 1
network_width_param = 64
test_set_portion = .2
gpu_cuda = torch.cuda.is_available()
device = torch.device('cuda' if gpu_cuda else 'cpu')
#OPTIMIZER
lr = .001
momentum = .9
nesterov = True
weight_decay = 5e-4
#LR_SCHEDULER - for MultiStepLR Parameters
milestones = [2,4,6]
#list of epoch indeces, must be increasing
gamma = .1
#DATALOADER
batch_size = 2
num_workers = 0
out_channels = 6
save = True
def main():
print("Using CUDA: {}".format(gpu_cuda))
model = lambda: UNet(in_channels=3, out_channels=out_channels, features=network_width_param)
optimizer = lambda m: optim.Adam(m.parameters(), lr=lr, weight_decay=weight_decay)
lr_scheduler = lambda o: optim.lr_scheduler.MultiStepLR(o, milestones=milestones, gamma=gamma)
loss_fn = torch.nn.BCEWithLogitsLoss()
model = model()
if gpu_cuda:
model = model.cuda()
optimizer = optimizer(model)
lr_scheduler = lr_scheduler(optimizer)
best_metrics = dict()
best_metrics['loss'] = sys.maxsize
for item in ('precision', 'recall', 'f1_score', 'pixel_acc'):
best_metrics[item] = 0.0
dataset = SegmentationDataset(img_dir, label_dir, scale=1, mode='nearest')
n_test = int(len(dataset) * test_set_portion)
n_train = len(dataset) - n_test
manual_seed(101)
train_set, test_set = random_split(dataset, [n_train, n_test])
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, \
num_workers=num_workers, pin_memory=torch.cuda.is_available())
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False, \
num_workers=num_workers, pin_memory=torch.cuda.is_available())
#TRAIN
for epoch in range(epochs):
print('------- EPOCH [{} / {}] -------'.format(epoch + 1, epochs))
#train, store metrics
train_loss = train(model, optimizer, train_loader, loss_fn, device)
print("Average Loss = {}".format(train_loss))
#test, store metrics
test_loss = test(model, test_loader, loss_fn, device)
print("Test Loss = {}".format(test_loss))
lr_scheduler.step()
#update best metrics
# if best metrics improved, or it is the first epoch, save model
# display best metrics
#Save Model
path = 'saved_models/model-'
if(save == True):
path += str(len(glob(os.path.join('saved_models/', '*.pth'))))
path += '.pth'
torch.save(model, path)
print("Train Complete: Model Saved as " + path)
def train(model, optimizer, loader, loss_fn, device):
model.train()
n_batches = len(loader)
running_loss = 0.
with torch.set_grad_enabled(True):
for batch_idx, (imgs, labels) in enumerate(loader):
imgs, labels = map(lambda x: x.to(device, dtype=torch.float32), (imgs, labels))
if gpu_cuda:
logits = model(imgs).cuda()
else:
logits = model(imgs)
loss = loss_fn.forward(logits, labels)
running_loss += loss.item()
# Backprop
optimizer.zero_grad()
loss.backward()
optimizer.step()
logits.detach()
print("Batch: {}/{} | Loss: {} | LR: {}".format(batch_idx + 1, n_batches, loss, get_lr(optimizer)))
return running_loss / (n_batches)
def test(model, loader, loss_fn, device):
model.eval()
n_batches = len(loader)
running_loss = 0.
with torch.set_grad_enabled(False):
for batch_idx, (imgs, labels) in enumerate(loader):
imgs, labels = map(lambda x: x.to(device, dtype=torch.float32), (imgs, labels))
if gpu_cuda:
logits = model(imgs).cuda()
else:
logits = model(imgs)
try:
loss = loss_fn.forward(logits, labels)
running_loss += loss.item()
except:
print("EXCEPTION CAUGHT: Test Batch Skipped")
return running_loss / (n_batches)
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
if(__name__ == "__main__"):
main()