forked from dsp-uga/team-linden-p2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainTiramisu.py
166 lines (132 loc) · 6.56 KB
/
TrainTiramisu.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
from src.tiramisu.datasets import cilia, joint_transforms
from src.tiramisu.utils import training
from src.tiramisu.models import tiramisu
import adabound
import torch
from torchvision import transforms
from torch.utils import data
from imageio import imwrite, imread
from pathlib import Path
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import time
import argparse
def main(args):
args.rootDir = os.path.normpath(args.rootDir)
# ensure the root directory has expected subdirectories
if not os.path.exists(args.rootDir):
raise Exception("ERROR: The dir '"+args.rootDir+"' doesn't exist")
if not os.path.exists(args.rootDir+"/test/data"):
raise Exception("ERROR: The dir '"+args.rootDir+"/test/data' " + \
"doesn't exist")
if not os.path.exists(args.rootDir+"/results"):
os.mkdir(args.rootDir+"/results")
if not os.path.exists(args.rootDir+"/weights"):
os.mkdir(args.rootDir+"/weights")
training.RESULTS_PATH = Path(args.rootDir+"/results/")
training.WEIGHTS_PATH = Path(args.rootDir+"/weights/")
train_joint_transformer = transforms.Compose([
joint_transforms.JointRandomSizedCrop(args.randCrop),
joint_transforms.JointRandomHorizontalFlip()
])
train_cilia = cilia.Cilia(args.rootDir, joint_transform \
= train_joint_transformer)
train_loader = data.DataLoader(train_cilia, batch_size = args.batchSize, \
shuffle = True)
val_cilia = cilia.Cilia(args.rootDir, 'validate')
val_loader = torch.utils.data.DataLoader(val_cilia, \
batch_size=args.batchSize, \
shuffle=True)
print("Train: %d" %len(train_loader.dataset.imgs))
print("Val: %d" %len(val_loader.dataset.imgs))
inputs, targets = next(iter(train_loader))
print("Inputs: ", inputs.size())
print("Targets: ", targets.size())
figure, subplot = plt.subplots(1,2)
subplot[0].imshow(inputs[0, 0, :, :], cmap = 'gray')
subplot[1].imshow(targets[0, :, :], cmap = 'gray')
# Define constants for later reference
LR = args.lr
LR_DECAY = args.lrDecay
DECAY_EVERY_N_EPOCHS = args.decayOverEpochs
N_EPOCHS = args.nEpochs
## define the model
model = tiramisu.FCDenseNet103(n_classes=3, in_channels=1).cuda()
model.apply(training.weights_init)
if args.adaBound:
optimizer = adabound.AdaBound(model.parameters(), lr=args.lr, \
final_lr=args.finalLr)
if args.torchAdam:
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, \
weight_decay=args.weightDecay)
criterion = torch.nn.NLLLoss().cuda()
## Now run through the epochs
train_acc, val_acc = [], []
for epoch in range(1, N_EPOCHS+1):
since = time.time()
### Train ###
trn_loss, trn_err = training.train(model, train_loader, optimizer, \
criterion, epoch)
print('Epoch {:d}\nTrain - Loss: {:.4f}, Acc: {:.4f}'.format(epoch, \
trn_loss, 1 - trn_err))
time_elapsed = time.time() - since
print('Train Time {:.0f}m {:.0f}s'.format(time_elapsed // 60, \
time_elapsed % 60))
### Validate ###
val_loss, val_err = training.test(model, val_loader, criterion, epoch)
print('Val - Loss: {:.4f} | Acc: {:.4f}'.format(val_loss, 1 - val_err))
time_elapsed = time.time() - since
print('Total Time {:.0f}m {:.0f}s\n'.format(time_elapsed // 60, \
time_elapsed % 60))
train_acc.append(1 - trn_err)
val_acc.append(1 - val_err)
### Checkpoint ###
training.save_weights(model, epoch, val_loss, val_err)
### Adjust Lr ###
training.adjust_learning_rate(LR, LR_DECAY, optimizer, epoch,
DECAY_EVERY_N_EPOCHS)
## Write final summary to output
output_accu = np.zeros((len(train_acc), 3))
output_accu[:, 0] = np.arange(1, len(train_acc)+1)
output_accu[:, 1] = np.array(train_acc)
output_accu[:, 2] = np.array(val_acc)
np.savetxt(args.rootDir + "/" + 'accuracy.txt', output_accu)
print('----Trainig done successfully----')
print('Generated weights have been saved in ',args.rootDir+"/weights/")
print('accuracy.txt has been saved in ',args.rootDir)
# Done!
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='This ' + \
'is part of the UGA CSCI 8360 Project 2 - . Please visit our ' + \
'GitHub project at https://github.com/dsp-uga/team-linden-p2 ' + \
'for more information regarding data organization ' + \
'expectations and examples on how to execute our scripts.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-ab', '--adaBound', action='store_true',
help='Use the adaBound CNN')
group.add_argument('-ta', '--torchAdam', action='store_true',
help='Use the torch Adam CNN')
parser.add_argument('-r','--rootDir', required=True,
help='The base directory storing files and ' + \
'directories conforming with organization ' + \
'expectations, please visit out GitHub website')
parser.add_argument('-bs', '--batchSize', type=int, default=1,
help='Size of batch between CNN weight adjustment')
parser.add_argument('-rc', '--randCrop', type=int, default=256,
help='Random sized crop value for torchvision')
parser.add_argument('-lr', '--lr', type=float, default=1e-4,
help='LR value for CNN')
parser.add_argument('-flr', '--finalLr', type=float, default=1e-4,
help='Final LR value for AdaBound CNN')
parser.add_argument('-de', '--decayOverEpochs', type=int, default=1,
help='Number of epochs crossed for decay')
parser.add_argument('-lrd', '--lrDecay', type=float, default=0.995,
help='LR Decay Rate')
parser.add_argument('-ne', '--nEpochs', type=int, default=1000,
help='Number of epochs to run')
parser.add_argument('-wd', '--weightDecay', type=float, default=1e-4,
help='Weight decay in tourch Adam CNN')
args = parser.parse_args()
main(args)