-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
195 lines (161 loc) · 7.77 KB
/
main.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import os
import argparse
from tqdm import tqdm as tqdm
import time, os
import datasets
from models import *
#from models import *
from train import *
from test import *
from visualization import *
model_list = {'unet': unet(), 'fcnvgg16': fcnvgg16(), 'fcnresnet50' : fcnresnet50(), 'fcnresnet101': fcnresnet101()}
parser = argparse.ArgumentParser(description='PyTorch Chair Segments Training')
parser.add_argument('--data', metavar='DIR', default='data',
help='path to dataset')
parser.add_argument('--dataset', metavar='DIR', default='Chair',
help='"Chair" for ChairSegments')
parser.add_argument('--lr', default=1e-4, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true',
help='resume from checkpoint')
parser.add_argument('--arch', '-a', metavar='ARCH', default='unet',
choices=model_list,
help='model architecture: ' +
' | '.join(model_list) +
' (default: unet) other options: fcnvgg16, fcnresnet50, fcnresnet101')
parser.add_argument('--batchSize', '-b', default=10, type=int,
metavar='N',
help='mini-batch size (default: 10), this is the total ')
parser.add_argument('--criterion', '-crit', default='BCE',
help='criterion')
parser.add_argument('--optimizer', '-opt', default='Adam',
help='optimizer Adam or SGD')
parser.add_argument('--momentum', '-mome', default=0.9, type=float,
help='momentum')
parser.add_argument('--weight_decay', '-wdecay', default=1e-5, type=float,
help='weight_decay')
parser.add_argument('--lines', '-lines', default=3,
help='lines, 3 by default')
parser.add_argument('--size', '-s', default=10,
help='images per line, 10 by default')
parser.add_argument('--epochs', '-e', default=1, type=int,
help='epochs , 10 by default')
parser.add_argument('--resol', '-re', default=64, type=int,
help='Resolution of the Chair image (default:64)')
args = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
resolution = '0'+ str(args.resol) if (int(args.resol) < 100) else str(args.resol)
# Transform
rsizet = transforms.Compose([
transforms.Resize([int(resolution), int(resolution)]),
transforms.ToTensor()
])
# Data
dspath = args.data + '/' + args.dataset
trainset = datasets.CHAIRS2020(dspath + resolution + '/', split = 'train', transform = rsizet)
valset = datasets.CHAIRS2020(dspath + resolution + '/', split = 'val', transform = rsizet)
trainLoader = torch.utils.data.DataLoader(dataset = trainset,
batch_size = args.batchSize,
shuffle = True)
valLoader = torch.utils.data.DataLoader(dataset = valset,
batch_size = args.batchSize,
shuffle = False) # No need.
print('==> Generating Images and Ground Truth..')
visualizeImages(args.dataset, 'results/' + args.dataset + resolution, args.size, args.lines, valset, rsizet)
# Model
print('==> Building ' + args.arch +' model..')
model = model_list[args.arch]
model = model.to(device)
#criterion
criterion = nn.BCEWithLogitsLoss() if args.criterion=='BCE' else nn.CrossEntropyLoss()
#optimizer
print('optimizer: ' + args.optimizer)
if (args.optimizer == 'Adam'):
optimizer = torch.optim.Adam(model.parameters(), args.lr)
elif(args.optimizer == 'SGD'):
optimizer = optim.SGD(model.parameters(), args.lr, momentum = args.momentum, weight_decay = args.weight_decay)
else:
optimizer = optim.RMSprop(model.parameters(), args.lr, momentum = args.momentum, weight_decay = args.weight_decay)
type_train = 'scratch'
start_epoch = 0
if args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
assert os.path.isdir('logs'), 'Error: no checkpoint directory found!'
modelname = args.dataset + resolution + '_' + args.arch + '_' + type_train + '.pth'
checkpoint = torch.load(os.path.join('logs', modelname))
model.load_state_dict(checkpoint['state_dict'])
msg = '\n' + modelname
msg += ', best_loss: ' + str(checkpoint['best_loss'])
msg += ', best_iou: ' + str(checkpoint['iou'])
msg += ', best_dice: ' + str(checkpoint['dice'])
msg += ', best_prec: ' + str(checkpoint['prec'])
msg += ', best_epoch: ' + str(checkpoint['epoch'])
print(msg)
file1 = open('resumefile.txt',"a")
file1.write(msg)
file1.close()
start_epoch = checkpoint['epoch']
model.cuda()
criterion.cuda()
train_losses, train_iou, train_dice, train_prec = [], [], [], []
val_losses, val_iou, val_dice, val_prec = [], [], [], []
best_loss, best_epoch = 1, 0
max_iou, max_dice, max_prec = 0, 0, 0
epochs = args.epochs
best_acc = 0 # best test accuracy
dataname = args.dataset + resolution + '_' + args.arch + '_' + type_train
print(dataname)
tt = 0
st = time.time()
for epoch in range(start_epoch, epochs + 1): # Number of epochs.
cumloss, totaliou, totaldice, totalprec = 0, 0, 0, 0
# Perform a round of training. ##########################################################
cumloss, totaliou, totaldice, totalprec = trainRound(args.arch, model, args.dataset,
trainLoader, optimizer, criterion, epoch)
print('cumloss: ' + str(cumloss) + ', iou: ' + str(totaliou) + ', dice: ' + str(totaldice) + ', prec: ' + str(totalprec) )
train_losses.append(cumloss)
train_iou.append(totaliou)
train_dice.append(totaldice)
train_prec.append(totalprec)
# Perform a round of validation. ##########################################################
cumloss, totaliou, totaldice, totalprec = valRound(args.arch, model, args.dataset,
valLoader, criterion, epoch)
print('cumloss: ' + str(cumloss) + ', iou: ' + str(totaliou) + ', dice: ' + str(totaldice) + ', prec: ' + str(totalprec) )
iou = totaliou
dice = totaldice
prec = totalprec
val_losses.append(cumloss)
val_iou.append(totaliou)
val_dice.append(totaldice)
val_prec.append(totalprec)
# remember best acc@1 and save checkpoint
is_best = best_loss > cumloss
if(is_best):
tt = time.time() - st
best_epoch = epoch
max_iou, max_dice, max_prec, best_loss = max(iou, max_iou), max(dice, max_dice), max(prec, max_prec), min(cumloss, best_loss)
plots('results/'+ dataname, val_losses, train_losses, val_iou, train_iou, val_dice, train_dice, val_prec, train_prec)
state = {
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_loss': best_loss,
'optimizer' : optimizer.state_dict(),
'iou': max_iou,
'dice': max_dice,
'prec': max_prec
}
if not os.path.isdir('logs'):
os.mkdir('logs')
torch.save(state, os.path.join('logs', dataname ) + ".pth")
visualizeResults(args.dataset, model, args.arch, resolution, args.size, args.lines, valset)
###### Saving time and numbers in respective file:
save_files(dataname, tt, best_epoch, max_iou, max_dice, max_prec, val_losses, val_iou, val_dice, val_prec)
plots('results/'+ dataname, val_losses, train_losses, val_iou, train_iou, val_dice, train_dice, val_prec, train_prec)