-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.py
324 lines (285 loc) · 11.5 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
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
319
320
321
322
323
324
'''Train CIFAR10 with PyTorch.'''
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import transforms as transforms
from dataloader import lunanod
import os
import argparse
import time
from models.cnn_res import *
# from utils import progress_bar
from torch.autograd import Variable
import logging
import numpy as np
import ast
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr', default=0.0002, type=float, help='learning rate')
parser.add_argument('--batch_size', default=1, type=int, help='batch size ')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
parser.add_argument('--savemodel', type=str, default='', help='resume from checkpoint model')
parser.add_argument("--gpuids", type=str, default='all', help='use which gpu')
parser.add_argument('--num_epochs', type=int, default=700)
parser.add_argument('--num_epochs_decay', type=int, default=70)
parser.add_argument('--num_workers', type=int, default=24)
parser.add_argument('--beta1', type=float, default=0.5) # momentum1 in Adam
parser.add_argument('--beta2', type=float, default=0.999) # momentum2 in Adam
parser.add_argument('--lamb', type=float, default=1, help="lambda for loss2")
parser.add_argument('--fold', type=int, default=5, help="fold")
args = parser.parse_args()
CROPSIZE = 32
gbtdepth = 1
fold = args.fold
blklst = [] # ['1.3.6.1.4.1.14519.5.2.1.6279.6001.121993590721161347818774929286-388', \
# '1.3.6.1.4.1.14519.5.2.1.6279.6001.121993590721161347818774929286-389', \
# '1.3.6.1.4.1.14519.5.2.1.6279.6001.132817748896065918417924920957-660']
logging.basicConfig(filename='log-' + str(fold), level=logging.INFO)
use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
best_acc_gbt = 0
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
# Cal mean std
# preprocesspath = '/media/data1/wentao/tianchi/luna16/cls/crop_v3/'
preprocesspath = '/data/xxx/LUNA/cls/crop_v3/'
# preprocesspath = '/media/jehovah/Work/data/LUNA/cls/crop_v3/'
pixvlu, npix = 0, 0
for fname in os.listdir(preprocesspath):
# print(fname)
if fname.endswith('.npy'):
if fname[:-4] in blklst: continue
data = np.load(os.path.join(preprocesspath, fname))
pixvlu += np.sum(data)
# print("data.shape = " + str(data.shape))
npix += np.prod(data.shape)
pixmean = pixvlu / float(npix)
pixvlu = 0
for fname in os.listdir(preprocesspath):
if fname.endswith('.npy'):
if fname[:-4] in blklst: continue
data = np.load(os.path.join(preprocesspath, fname)) - pixmean
pixvlu += np.sum(data * data)
pixstd = np.sqrt(pixvlu / float(npix))
# pixstd /= 255
print(pixmean, pixstd)
logging.info('mean ' + str(pixmean) + ' std ' + str(pixstd))
# Datatransforms
logging.info('==> Preparing data..') # Random Crop, Zero out, x z flip, scale,
transform_train = transforms.Compose([
# transforms.RandomScale(range(28, 38)),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomYFlip(),
transforms.RandomZFlip(),
transforms.ZeroOut(4),
transforms.ToTensor(),
transforms.Normalize((pixmean), (pixstd)), # need to cal mean and std, revise norm func
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((pixmean), (pixstd)),
])
# load data list
trfnamelst = []
trlabellst = []
trfeatlst = []
tefnamelst = []
telabellst = []
tefeatlst = []
import pandas as pd
dataframe = pd.read_csv('./data/annotationdetclsconvfnl_v3.csv',
names=['seriesuid', 'coordX', 'coordY', 'coordZ', 'diameter_mm', 'malignant'])
alllst = dataframe['seriesuid'].tolist()[1:]
labellst = dataframe['malignant'].tolist()[1:]
crdxlst = dataframe['coordX'].tolist()[1:]
crdylst = dataframe['coordY'].tolist()[1:]
crdzlst = dataframe['coordZ'].tolist()[1:]
dimlst = dataframe['diameter_mm'].tolist()[1:]
# test id
teidlst = []
for fname in os.listdir('/data/xxx/LUNA/rowfile/subset' + str(fold) + '/'):
# for fname in os.listdir('/media/jehovah/Work/data/LUNA/rowfile/subset' + str(fold) + '/'):
if fname.endswith('.mhd'):
teidlst.append(fname[:-4])
mxx = mxy = mxz = mxd = 0
for srsid, label, x, y, z, d in zip(alllst, labellst, crdxlst, crdylst, crdzlst, dimlst):
mxx = max(abs(float(x)), mxx)
mxy = max(abs(float(y)), mxy)
mxz = max(abs(float(z)), mxz)
mxd = max(abs(float(d)), mxd)
if srsid in blklst: continue
# crop raw pixel as feature
data = np.load(os.path.join(preprocesspath, srsid + '.npy'))
bgx = int(data.shape[0] / 2 - CROPSIZE / 2)
bgy = int(data.shape[1] / 2 - CROPSIZE / 2)
bgz = int(data.shape[2] / 2 - CROPSIZE / 2)
data = np.array(data[bgx:bgx + CROPSIZE, bgy:bgy + CROPSIZE, bgz:bgz + CROPSIZE])
# feat = np.hstack((np.reshape(data, (-1,)) / 255, float(d)))
y, x, z = np.ogrid[-CROPSIZE / 2:CROPSIZE / 2, -CROPSIZE / 2:CROPSIZE / 2, -CROPSIZE / 2:CROPSIZE / 2]
mask = abs(y ** 3 + x ** 3 + z ** 3) <= abs(float(d)) ** 3
feat = np.zeros((CROPSIZE, CROPSIZE, CROPSIZE), dtype=float)
feat[mask] = 1
# print(feat.shape)
if srsid.split('-')[0] in teidlst:
tefnamelst.append(srsid + '.npy')
telabellst.append(int(label))
tefeatlst.append(feat)
else:
trfnamelst.append(srsid + '.npy')
trlabellst.append(int(label))
trfeatlst.append(feat)
for idx in range(len(trfeatlst)):
# trfeatlst[idx][0] /= mxx
# trfeatlst[idx][1] /= mxy
# trfeatlst[idx][2] /= mxz
trfeatlst[idx][-1] /= mxd
for idx in range(len(tefeatlst)):
# tefeatlst[idx][0] /= mxx
# tefeatlst[idx][1] /= mxy
# tefeatlst[idx][2] /= mxz
tefeatlst[idx][-1] /= mxd
trainset = lunanod(preprocesspath, trfnamelst, trlabellst, trfeatlst, train=True, download=True,
transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True, num_workers=20)
testset = lunanod(preprocesspath, tefnamelst, telabellst, tefeatlst, train=False, download=True,
transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=args.batch_size, shuffle=False, num_workers=20)
savemodelpath = './checkpoint-' + str(fold) + '/'
# Model
print(args.resume)
if args.resume:
print('==> Resuming from checkpoint..')
print(args.savemodel)
if args.savemodel == '':
logging.info('==> Resuming from checkpoint..')
assert os.path.isdir(savemodelpath), 'Error: no checkpoint directory found!'
checkpoint = torch.load(savemodelpath + 'ckpt.t7')
else:
logging.info('==> Resuming from checkpoint..')
assert os.path.isdir(savemodelpath), 'Error: no checkpoint directory found!'
checkpoint = torch.load(args.savemodel)
net = checkpoint['net']
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']
print(savemodelpath + " load success")
print(start_epoch)
else:
logging.info('==> Building model..')
logging.info('args.savemodel : ' + args.savemodel)
net = ConvRes([[64, 64, 64], [128, 128, 256], [256, 256, 256, 512]])
if args.savemodel != "":
# args.savemodel = '/home/xxx/DeepLung-master/nodcls/checkpoint-5/ckpt.t7'
checkpoint = torch.load(args.savemodel)
finenet = checkpoint
Low_rankmodel_dic = net.state_dict()
finenet = {k: v for k, v in finenet.items() if k in Low_rankmodel_dic}
Low_rankmodel_dic.update(finenet)
net.load_state_dict(Low_rankmodel_dic)
print("net_loaded")
lr = args.lr
def get_lr(epoch):
global lr
if (epoch + 1) > (args.num_epochs - args.num_epochs_decay):
lr -= (lr / float(args.num_epochs_decay))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
print('Decay learning rate to lr: {}.'.format(lr))
if use_cuda:
net.cuda()
if args.gpuids == 'all':
device_ids = range(torch.cuda.device_count())
else:
device_ids = map(int, list(filter(str.isdigit, args.gpuids)))
print('gpu use' + str(device_ids))
net = torch.nn.DataParallel(net, device_ids=device_ids)
cudnn.benchmark = False # True
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=args.lr, betas=(args.beta1, args.beta2))
# L2Loss = torch.nn.MSELoss()
# Training
def train(epoch):
logging.info('\nEpoch: ' + str(epoch))
net.train()
get_lr(epoch)
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets, feat) in enumerate(trainloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
optimizer.zero_grad()
inputs, targets = Variable(inputs), Variable(targets)
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.data.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
# progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
print('ep ' + str(epoch) + ' tracc ' + str(correct.data.item() / float(total)) + ' lr ' + str(lr))
logging.info(
'ep ' + str(epoch) + ' tracc ' + str(correct.data.item() / float(total)) + ' lr ' + str(lr))
def test(epoch):
epoch_start_time = time.time()
global best_acc
global best_acc_gbt
net.eval()
test_loss = 0
correct = 0
total = 0
TP = FP = FN = TN = 0
for batch_idx, (inputs, targets, feat) in enumerate(testloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs, requires_grad=False), Variable(targets)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.data.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
TP += ((predicted == 1) & (targets.data == 1)).cpu().sum()
TN += ((predicted == 0) & (targets.data == 0)).cpu().sum()
FN += ((predicted == 0) & (targets.data == 1)).cpu().sum()
FP += ((predicted == 1) & (targets.data == 0)).cpu().sum()
# Save checkpoint.
acc = 100. * correct.data.item() / total
if acc > best_acc:
logging.info('Saving..')
state = {
'net': net.module if use_cuda else net,
'acc': acc,
'epoch': epoch,
}
if not os.path.isdir(savemodelpath):
os.mkdir(savemodelpath)
torch.save(state, savemodelpath + 'ckpt.t7')
best_acc = acc
logging.info('Saving..')
state = {
'net': net.module if use_cuda else net,
'acc': acc,
'epoch': epoch,
}
if not os.path.isdir(savemodelpath):
os.mkdir(savemodelpath)
if epoch % 50 == 0:
torch.save(state, savemodelpath + 'ckpt' + str(epoch) + '.t7')
# best_acc = acc
tpr = 100. * TP.data.item() / (TP.data.item() + FN.data.item())
fpr = 100. * FP.data.item() / (FP.data.item() + TN.data.item())
print('teacc ' + str(acc) + ' bestacc ' + str(best_acc))
print('tpr ' + str(tpr) + ' fpr ' + str(fpr))
print('Time Taken: %d sec' % (time.time() - epoch_start_time))
logging.info(
'teacc ' + str(acc) + ' bestacc ' + str(best_acc))
logging.info(
'tpr ' + str(tpr) + ' fpr ' + str(fpr))
if __name__ == '__main__':
for epoch in range(start_epoch + 1, start_epoch + args.num_epochs + 1): # 200):
train(epoch)
test(epoch)