def train(epoch): logging.info('\nEpoch: ' + str(epoch)) net.train() lr = get_lr(epoch) for param_group in optimizer.param_groups: param_group['lr'] = lr train_loss = 0 correct = 0 total = 0 trainfeat = np.zeros( (len(trfnamelst), 2560 + CROPSIZE * CROPSIZE * CROPSIZE + 1)) if debug: print('trainfeat.shape', trainfeat.shape) #(912, 35329) trainlabel = np.zeros((len(trfnamelst), )) idx = 0 for batch_idx, (inputs, targets, feat) in enumerate(trainloader): # print('batch_idx',batch_idx) if use_cuda: # print(len(inputs), len(targets), len(feat), type(inputs[0]), type(targets[0]), type(feat[0]))#16 16 16 <class 'torch.Tensor'> <class 'torch.Tensor'> <class 'torch.Tensor'> # print(type(targets), type(inputs), len(targets)) # targetarr = np.zeros((len(targets),)) # for idx in xrange(len(targets)): # targetarr[idx] = targets[idx] # print((Variable(torch.from_numpy(targetarr)).data).cpu().numpy().shape) inputs, targets = inputs.cuda(), targets.cuda() optimizer.zero_grad() inputs, targets = Variable(inputs, requires_grad=True), Variable(targets) outputs, dfeat = net(inputs) # add feature into the array # print(torch.stack(targets).data.numpy().shape, torch.stack(feat).data.numpy().shape) # print((dfeat.data).cpu().numpy().shape)#(16,2560) trainfeat[idx:idx + len(targets), :2560] = np.array( (dfeat.data).cpu().numpy()) #[4,2560] for i in range(len(targets)): trainfeat[idx + i, 2560:] = np.array( (Variable(feat[i]).data).cpu().numpy()) trainlabel[idx + i] = np.array((targets[i].data).cpu().numpy()) idx += len(targets) # print('outputs.shape, targets.shape',outputs.shape, targets.shape)#torch.Size([16, 2]) torch.Size([16]) loss = criterion(outputs, targets) loss.backward() optimizer.step() train_loss += loss.item() _, predicted = torch.max(outputs.data, 1) total += targets.size(0) correct += predicted.eq(targets.data).cpu().sum() accout = round(correct.data.cpu().numpy() / total, 4) # print('accout',correct.data.cpu().numpy(),total, accout) print('TrainLoss: %.3f | Acc: %.3f%% (%d/%d)' % (train_loss / (batch_idx + 1), 100. * accout, correct, total)) m = gbt(max_depth=gbtdepth, random_state=0) m.fit(trainfeat, trainlabel) gbttracc = round(np.mean(m.predict(trainfeat) == trainlabel), 4) # print('accout1',accout) print('ep ' + str(epoch) + ' tracc ' + str(accout) + ' lr ' + str(lr) + ' gbtacc ' + str(gbttracc)) logging.info('ep ' + str(epoch) + ' tracc ' + str(accout) + ' lr ' + str(lr) + ' gbtacc ' + str(gbttracc)) return m
def train(epoch): logging.info('\nEpoch: ' + str(epoch)) net.train() lr = get_lr(epoch) for param_group in optimizer.param_groups: param_group['lr'] = lr train_loss = 0 correct = 0 total = 0 trainfeat = np.zeros( (len(trfnamelst), 2560 + CROPSIZE * CROPSIZE * CROPSIZE + 1)) trainlabel = np.zeros((len(trfnamelst), )) idx = 0 pbar = tqdm(total=len(trainloader), unit="batchs") for batch_idx, (inputs, targets, feat) in enumerate(trainloader): if use_cuda: # print(len(inputs), len(targets), len(feat), type(inputs[0]), type(targets[0]), type(feat[0])) # print(type(targets), type(inputs), len(targets)) # targetarr = np.zeros((len(targets),)) # for idx in xrange(len(targets)): # targetarr[idx] = targets[idx] # print((Variable(torch.from_numpy(targetarr)).data).cpu().numpy().shape) inputs, targets = inputs.cuda(), targets.cuda() optimizer.zero_grad() inputs, targets = Variable(inputs), Variable(targets) outputs, dfeat = net(inputs) # add feature into the array # print(torch.stack(targets).data.numpy().shape, torch.stack(feat).data.numpy().shape) # print((dfeat.data).cpu().numpy().shape) trainfeat[idx:idx + len(targets), :2560] = np.array( (dfeat.data).cpu().numpy()) for i in xrange(len(targets)): trainfeat[idx + i, 2560:] = np.array( (Variable(feat[i]).data).cpu().numpy()) trainlabel[idx + i] = np.array((targets[i].data).cpu().numpy()) idx += len(targets) loss = criterion(outputs, targets) loss.backward() optimizer.step() train_loss += loss.data[0] _, 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)' # % (train_loss/(batch_idx+1), 100.*correct/total, correct, total)) pbar.update(1) pbar.set_description( 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (train_loss / (batch_idx + 1), 100. * correct / total, correct, total)) m = gbt(max_depth=gbtdepth, random_state=0) m.fit(trainfeat, trainlabel) gbttracc = np.mean(m.predict(trainfeat) == trainlabel) print('ep ' + str(epoch) + ' tracc ' + str(correct / float(total)) + ' lr ' + str(lr) + ' gbtacc ' + str(gbttracc)) logging.info('ep ' + str(epoch) + ' tracc ' + str(correct / float(total)) + ' lr ' + str(lr) + ' gbtacc ' + str(gbttracc)) return m
def gbtfunc(dep): m = gbt(max_depth=dep, random_state=0) m.fit(traindata, trainlabel) predtrain = m.predict(traindata) predtest = m.predict_proba(testdata) # print predtest.shape, predtest[1,:] return np.sum(predtrain == trainlabel) / float(traindata.shape[0]), \ np.mean((predtest[:,1]>0.5).astype(int) == testlabel), predtest # / float(testdata.shape[0]),
def train(self, epoch): logging.info('\nEpoch: ' + str(epoch)) self.net.train() lr = self.get_lr(epoch) for param_group in self.optimizer.param_groups: param_group['lr'] = lr train_loss = 0 correct = 0 total = 0 trainfeat = np.zeros( (len(self.trfnamelst), 2560 + CROPSIZE * CROPSIZE * CROPSIZE + 1)) trainlabel = np.zeros((len(self.trfnamelst), )) idx = 0 pbar = tqdm(total=len(self.trainloader), unit="batchs") for batch_idx, (inputs, targets, feat) in enumerate(self.trainloader): if self.use_cuda: inputs, targets = inputs.cuda(), targets.cuda() self.optimizer.zero_grad() inputs, targets = Variable(inputs), Variable(targets) outputs, dfeat = self.net(inputs) # add feature into the array trainfeat[idx:idx + len(targets), :2560] = np.array( (dfeat.data).cpu().numpy()) for i in xrange(len(targets)): trainfeat[idx + i, 2560:] = np.array( (Variable(feat[i]).data).cpu().numpy()) trainlabel[idx + i] = np.array((targets[i].data).cpu().numpy()) idx += len(targets) loss = self.criterion(outputs, targets) loss.backward() self.optimizer.step() train_loss += loss.data.item() _, predicted = torch.max(outputs.data, 1) total += targets.size(0) correct += predicted.eq(targets.data).cpu().sum() pbar.update(1) pbar.set_description( 'Training: epoch: %d | Loss: %.3f | Acc: %.3f%% (%d/%d)' % (epoch, train_loss / (batch_idx + 1), 100. * correct / total, correct, total)) pbar.close() m = gbt(max_depth=gbtdepth, random_state=0) m.fit(trainfeat, trainlabel) gbttracc = np.mean(m.predict(trainfeat) == trainlabel) #print('ep '+str(epoch)+' tracc '+str(correct/float(total))+' lr '+str(lr)+' gbtacc '+str(gbttracc)) logging.info('ep ' + str(epoch) + ' tracc ' + str(correct / float(total)) + ' lr ' + str(lr) + ' gbtacc ' + str(gbttracc)) return m
def main(): args = parse_args() preprocesspath = '/data/preprocess/luna_preprocess/' croppath = './crop/' anno_csv = './data/annotationdetclsconvfnl_v3.csv' global best_acc, best_acc_gbt, start_epoch, fold if args.crop: crop2(preprocesspath, croppath, anno_csv, config) return # prepare Model net = DPN92_3D() savemodelpath = args.save_dir if not os.path.exists(savemodelpath): os.makedirs(savemodelpath) if args.checkpoint: # Load checkpoint. logging.info('==> Resuming from {}'.format(args.checkpoint)) checkpoint = torch.load(os.path.join(savemodelpath, args.checkpoint)) net.load_state_dict(checkpoint['state_dict']) m = pickle.load(open(os.path.join(savemodelpath, 'gbtmodel.sav'), 'rb')) best_acc = checkpoint['best_acc'] best_acc_gbt = checkpoint['best_acc_gbt'] start_epoch = checkpoint['epoch'] + 1 else: logging.info('==> Building model..') m = gbt(max_depth=gbtdepth, random_state=0) # Prepare train and test data splits pixmean, pixstd = get_mean_and_std(croppath, blklst) transform_train, transform_test = get_transform(pixmean, pixstd) trainloader, testloadter = get_train_test_loader(anno_csv, croppath, fold, transform_train, transform_test) # Define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) if use_cuda: net.cuda() net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count())) # use all GPU criterion = criterion.cuda() cudnn.benchmark = False # Train and test(validate) for epoch in range(start_epoch, start_epoch + 350*neptime): m = train(net, m, criterion, optimizer, trainloader, epoch) test(net, m, criterion, testloadter, epoch, args, savemodelpath)