def show_features(load, encoder=0, xslice=20): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = init_forknet(load, device) activation = {} def get_activation(name): def hook(model, input, output): activation[name] = output.detach() return hook # encode[0] is the convolution module of the encoder model.encoders[encoder].encode[0].register_forward_hook( get_activation('enc1')) dataset = MICCAI18(base_dir="./data/miccai18/training", case_list=['4'], slice_axis=2) loader = torch.utils.data.DataLoader(dataset, num_workers=2, batch_size=48, shuffle=True) batch_dict = iter(loader).next() for tensor in batch_dict: batch_dict[tensor] = batch_dict[tensor].to(device) data = batch_dict['t1w'][xslice] data.unsqueeze_(0) output = model(data) act = activation['enc1'].squeeze() num_plots = 2 fig, axarr = plt.subplots(ncols=num_plots) for idx in range(num_plots): axarr[idx].imshow(act[idx].detach().cpu()) plt.show()
def train(epoch): model.train() train_loss, train_prior_loss, train_likelihood = 0, 0, 0 # for batch_idx, (data, _) in enumerate(train_loader): for batch_idx, data in enumerate(train_loader): data.transpose_(0, 1) data.unsqueeze_(2) data = Variable(data[0]) if args.cuda: data = data.cuda() optimizer.zero_grad() recon_batch, mu, logvar = model(data) loss, prior, likelihood = loss_function(recon_batch, data, mu, logvar) loss.backward() train_loss += loss.data[0] train_prior_loss += prior.data[0] train_likelihood += likelihood.data[0] optimizer.step() if batch_idx % args.log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.data[0])) train_loss /= (len(train_loader.dataset) / args.batch_size) train_prior_loss /= (len(train_loader.dataset) / args.batch_size) train_likelihood /= (len(train_loader.dataset) / args.batch_size) print('====> Epoch: {} Average loss: {:.4f}'.format( epoch, train_loss)) return train_loss, train_prior_loss, train_likelihood
def train(epoch): model.train() total_loss = 0.0 global optimizer global prev_loss for batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data = data.unsqueeze_(1) data, target = Variable(data), Variable(target) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) total_loss += loss.item() # loss.data[0] loss.backward() optimizer.step() if batch_idx % args.log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) # loss.data[0])) print("Total loss = %.6f" % (total_loss / len(train_loader.dataset))) dev_loss = 0. model.eval() f = open('dev-eer', 'w') for data, devtarget in devdata_loader: data = data.unsqueeze_(1) data, target = Variable(data), Variable(devtarget) if args.cuda: data, target = data.cuda(), target.cuda() output = model(data) if args.cuda: op = output.cpu() tgt = target.cpu() else: op = output tgt = target output_arr = op.data.numpy().reshape((-1, 2)) tgt = tgt.data.numpy() for i in range(tgt.shape[0]): itgt = int(tgt[i]) scr = -output_arr[i, 0] + output_arr[i, 1] if itgt == 0: f.write('%f %s\n' % (scr, 'nontarget')) else: f.write('%f %s\n' % (scr, 'target')) dev_loss += F.nll_loss(output, target, size_average=False).item() # sum up batch loss f.close() dev_loss /= len(devdata_loader.dataset) print("Dev loss is %.6f" % dev_loss)
def test(): model.eval() test_loss = 0 correct = 0 f = open('eer-file', 'w') for data, target in test_loader: if args.cuda: data, target = data.cuda(), target.cuda() data = data.unsqueeze_(1) data, target = Variable(data, volatile=True), Variable(target) output = model(data) output_arr = output.data.numpy().reshape((-1, 2)) tgt = target.data.numpy() for i in range(tgt.shape[0]): itgt = int(tgt[i]) scr = -output_arr[i, 0] + output_arr[i, 1] if itgt == 0: f.write('%f %s\n' % (scr, 'nontarget')) else: f.write('%f %s\n' % (scr, 'target')) test_loss += F.nll_loss(output, target, size_average=False).item() # sum up batch loss pred = output.data.max( 1, keepdim=True)[1] # get the index of the max log-probability correct += pred.eq(target.data.view_as(pred)).cpu().sum() f.close() test_loss /= len(test_loader.dataset) print( '\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset)))
def train(epochs, train_loader, dev_loader, lr, seed, log_interval, output_dir): """Train the model. Store snapshot models in the output_dir alongside evaluations on the dev set after each epoch """ model = Net() optimizer = optim.Adam(model.parameters(), lr=lr) measure_size(model) use_cuda = torch.cuda.is_available() device = torch.device("cuda:0" if use_cuda else "cpu") print("Using device: ", device) if use_cuda: torch.cuda.manual_seed(seed) else: torch.manual_seed(seed) #torch.backends.cudnn.benchmark = False #torch.backends.cudnn.deterministic = True model.to(device) for epoch in range(1, epochs): model.train() total_loss = 0.0 for batch_idx, (data, target) in enumerate(train_loader): if use_cuda: data, target = data.to(device), target.to(device) data = data.unsqueeze_(1) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) total_loss += loss.item() loss.backward() optimizer.step() if batch_idx % log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) print("Total loss = %.6f" % (total_loss / len(train_loader.dataset))) test(model, dev_loader, os.path.join(output_dir, 'dev-eer-' + str(epoch))) torch.save(model, os.path.join(output_dir, 'iter' + str(epoch) + '.mdl'))
def test(model, test_loader, report_filename): """Test the model on a provided dataset Save test output to a `report_filename`. Print a summary of performance to stdout. """ model.eval() test_loss = 0 correct = 0 scores = [] targets = [] use_cuda = torch.cuda.is_available() device = torch.device("cuda:0" if use_cuda else "cpu") with open(report_filename, 'w') as f: for data, target in test_loader: if use_cuda: data, target = data.to(device), target.to(device) data = data.unsqueeze_(1) output = model(data) if use_cuda: op = output.cpu() tgt = target.cpu() else: op = output tgt = target output_arr = op.data.numpy().reshape((-1, 2)) tgt = tgt.data.numpy() for i in range(tgt.shape[0]): itgt = int(tgt[i]) scr = -output_arr[i, 0] + output_arr[i, 1] t = "nontarget" if itgt == 0 else "target" f.write('%f %s\n' % (scr, t)) scores.append(scr) targets.append(t) test_loss += F.nll_loss( output, target, reduction='sum').item() # sum up batch loss pred = output.data.max( 1, keepdim=True)[1] # get the index of the max log-probability correct += pred.eq(target.data.view_as(pred)).cpu().sum() test_eer = eer(targets, scores) test_loss /= len(test_loader.dataset) print( '\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%), EER: {}%\n' .format(test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset), test_eer * 100))
def test(epoch): model.eval() test_loss, test_prior_loss, test_likelihood = 0, 0, 0 # for data, _ in test_loader: for data in test_loader: data.transpose_(0, 1) data.unsqueeze_(2) if args.cuda: data = data.cuda() data = Variable(data[0], volatile=True) recon_batch, mu, logvar = model(data) loss, prior, likelihood = loss_function(recon_batch, data, mu, logvar) test_loss += loss.data[0] test_prior_loss += prior.data[0] test_likelihood += likelihood.data[0] test_loss /= (len(test_loader.dataset) / args.batch_size) test_prior_loss /= (len(test_loader.dataset) / args.batch_size) test_likelihood /= (len(test_loader.dataset) / args.batch_size) print('====> Test set loss: {:.4f}'.format(test_loss)) return test_loss, test_prior_loss, test_likelihood
def train(model, trainData, testData, optimizer, criterion): trainLoss = 0 correct = 0 for idx, (data, target) in enumerate(trainData): model.train() data, target = data.cuda(), target.long().cuda() optimizer.zero_grad() output = model(data.unsqueeze_(1)) loss = criterion(output, target) pred = output.argmax(dim=1, keepdim=True) correct += pred.eq(target.view_as(pred)).sum().item() trainLoss += loss.item() loss.backward() optimizer.step() return trainLoss, 100. * (correct / len(trainData.dataset))
def predict(arc): args = parser.parse_args() args.arch = arc f_out = open('submission/round2_%s_log_submission.csv' % (arc), 'w') for i in range(len(task_list)): task = task_list[i] args.task = task model, input_size = get_model(args) input_size = 512 model = nn.DataParallel(model, device_ids=CUDDEVICE).cuda() checkpoint_name = os.path.join('./round2_checkpoint', arc + '_' + task + '_best.pkl') checkpoint = torch.load(checkpoint_name) # print(task + ' : ' + str(checkpoint['best_prec1'])) # continue model.module.load_state_dict(checkpoint['state_dict']) #model.cpu() criterion = nn.CrossEntropyLoss().cuda() #model = model.cuda() model.eval() # if arc == 'resnet50' or arc =='inceptionv4' or arc =='resnet152': # # model._modules.module.module.fc = model._modules.module.module.last_linear # model.module.module.fc = model.module.module.last_linear # if arc == 'densenet169': # model.classifier = model.last_linear # if arc == 'xception': # model.last_linear = model.fc normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transform = transforms.Compose([ transforms.Resize((input_size, input_size)), transforms.ToTensor(), normalize, ]) # f_out = open('submission/%s_%s.csv' % (arc, task), 'w') with open('data/week-rank/Tests/question.csv', 'r') as f_in: lines = f_in.readlines() tokens = [l.rstrip().split(',') for l in lines] task_tokens = [t for t in tokens if t[1] == task] n = len(task_tokens) cnt = 0 for path, task, _ in task_tokens: img_path = os.path.join('data/week-rank', path) img = Image.open(img_path) data = transform(img) data = data.unsqueeze_(0) data = data.cuda(async=True) data = Variable(data, volatile=True) output = model(data) # softmax = torch.nn.Softmax(dim=-1) softmax = torch.nn.LogSoftmax(dim=-1) output = softmax(output) output = output.view(-1) output = output.cpu().data.numpy().tolist() print(output, path) pred_out = ';'.join(["%.8f" % (o) for o in output]) line_out = ','.join([path, task, pred_out]) f_out.write(line_out + '\n') cnt += 1 progressbar(cnt, n) f_out.close()
for epoch in range(1, args.epochs + 1): train_loss, train_prior_loss, train_likelihood = train(epoch) test_loss, test_prior_loss, test_likelihood = test(epoch) log_values = (epoch, train_loss, train_prior_loss, train_prior_loss, train_likelihood, test_loss, test_prior_loss, test_likelihood) format_string = ",".join(["{:.8e}"]*len(log_values)) logging.debug(format_string.format(*log_values)) z = Variable(torch.randn(args.batch_size, args.hidden_dim), volatile=True) if args.cuda: z = z.cuda() # pdb.set_trace() generations = model.decode(z)[0] generations = generations.resize(generations.size(0), 28, 28) for batch_idx, data in enumerate(train_loader): data.transpose_(0, 1) data.unsqueeze_(2) data = Variable(data[0], volatile=True) if args.cuda: data = data.cuda() recon_batch, mu, logvar = model(data) recon_batch = recon_batch[0].data recon_batch.resize_(recon_batch.size(0), 28, 28) save_tensors_image("{}/recons{}.png".format(args.save, epoch), [g.expand(3, 28, 28) for g in recon_batch]) break save_tensors_image("{}/generations{}.png".format(args.save, epoch), [g.expand(3, 28, 28) for g in generations])
def main(): parser = argparse.ArgumentParser() parser.add_argument('--workers', type=int, help='number of data loading workers', default=8) parser.add_argument('--batchSize', type=int, default=64, help='input batch size') parser.add_argument('--nz', type=int, default=8, help='size of the latent z vector') parser.add_argument('--niter', type=int, default=25, help='number of epochs to train for') parser.add_argument('--lr', type=float, default=0.00005, help='learning rate, default=0.0002') parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5') parser.add_argument('--outf', default='./out/', help='folder to output images and model checkpoints') parser.add_argument('--manualSeed', type=int, help='manual seed') opt = parser.parse_args() print(opt) ngpu = 1 try: os.makedirs(opt.outf) except OSError: pass if opt.manualSeed is None: opt.manualSeed = random.randint(1, 10000) print("Random Seed: ", opt.manualSeed) random.seed(opt.manualSeed) torch.manual_seed(opt.manualSeed) dataset = ds.MimicData() assert dataset dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize, shuffle=True, num_workers=int(opt.workers)) device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") print(device) nz = int(opt.nz) ngf = 64 ndf = 64 nc = 1 # custom weights initialization called on netG and netD # custom weights initialization called on netG and netD def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: nn.init.normal_(m.weight.data, 0, 0.02) elif classname.find('BatchNorm') != -1: nn.init.normal_(m.weight.data, 1, 0.02) nn.init.constant_(m.bias.data, 0) class Generator(nn.Module): def __init__(self): super(Generator, self).__init__() self.main = nn.Sequential( nn.ConvTranspose2d(nz, ngf * 4, (1, 2), (1, 1), 0, bias=False), nn.BatchNorm2d(ngf * 4), nn.ReLU(True), # state size. (ngf*8) x 4 x 4 nn.ConvTranspose2d(ngf * 4, ngf * 2, (1, 2), (1, 2), 0, bias=False), nn.BatchNorm2d(ngf * 2), nn.ReLU(True), # state size. (ngf*4) x 8 x 8 nn.ConvTranspose2d(ngf * 2, ngf * 2, (1, 2), (1, 2), 0, bias=False), nn.BatchNorm2d(ngf * 2), nn.ReLU(True), # state size. (ngf*2) x 16 x 16 nn.ConvTranspose2d(ngf * 2, ngf * 2, (1, 7), (1, 2), 0, bias=False), nn.BatchNorm2d(ngf * 2), nn.ReLU(True), # state size. (ngf*2) x 16 x 16 nn.ConvTranspose2d(ngf * 2, ngf, (6, 1), (1, 1), 0, bias=False), nn.BatchNorm2d(ngf), nn.ReLU(True), # state size. (ngf) x 32 x 32 nn.ConvTranspose2d(ngf, nc, (10, 1), (8, 1), 0, bias=False), nn.Tanh() # state size. (nc) x 64 x 64 ) def forward(self, input): output = self.main(input) return output netG = Generator().to(device) # Handle multi-gpu if desired if (device.type == 'cuda') and (ngpu > 1): netG = nn.DataParallel(netG, list(range(ngpu))) netG.apply(weights_init) class Discriminator(nn.Module): def __init__(self): super(Discriminator, self).__init__() self.main = nn.Sequential( # input is (nc) x 64 x 64 nn.Conv2d(nc, ndf, (10, 1), (8, 1), 0, bias=False), nn.BatchNorm2d(ndf), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 nn.Conv2d(ndf, ndf * 2, (6, 1), (6, 1), 0, bias=False), nn.BatchNorm2d(ndf * 2), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 16 x 16 nn.Conv2d(ndf * 2, ndf * 2, (1, 2), (1, 2), 0, bias=False), nn.BatchNorm2d(ndf * 2), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*4) x 8 x 8 nn.Conv2d(ndf * 2, ndf * 4, (1, 4), (1, 2), 0, bias=False), nn.BatchNorm2d(ndf * 4), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*4) x 8 x 8 nn.Conv2d(ndf * 4, 1, (1, 3), (1, 2), 0, bias=False), nn.Sigmoid() ) def forward(self, input): output = self.main(input) return output.view(-1, 1).squeeze(1) netD = Discriminator().to(device) if (device.type == 'cuda') and (ngpu > 1): netD = nn.DataParallel(netD, list(range(ngpu))) netD.apply(weights_init) criterion = nn.BCELoss() fixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device) real_label = 1 fake_label = 0 # setup optimizer optimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) ldl = [] lgl = [] dxl = [] dgz1l = [] dgz2l = [] for epoch in range(opt.niter): for i, data in enumerate(dataloader, 0): data = data[2] data.unsqueeze_(1) data.add_(-0.5).mul_(2) # train with real netD.zero_grad() real_cpu = data.to(device) batch_size = real_cpu.size(0) label = torch.full((batch_size,), real_label, device=device) output = netD(real_cpu) errD_real = criterion(output, label) errD_real.backward() D_x = output.mean().item() # train with fake noise = torch.randn(batch_size, nz, 1, 1, device=device) fake = netG(noise) label.fill_(fake_label) output = netD(fake.detach()) errD_fake = criterion(output, label) errD_fake.backward() D_G_z1 = output.mean().item() errD = errD_real + errD_fake optimizerD.step() # Update G network: maximize log(D(G(z))) netG.zero_grad() label.fill_(real_label) # fake labels are real for generator cost output = netD(fake) errG = criterion(output, label) errG.backward() D_G_z2 = output.mean().item() optimizerG.step() ldl.append(errD.item()) lgl.append(errG.item()) dxl.append(D_x) dgz1l.append(D_G_z1) dgz2l.append(D_G_z2) plot_gan(ldl, lgl, dxl, dgz1l, dgz2l, opt.outf) print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f' % (epoch, opt.niter, i, len(dataloader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2)) if i % 5 == 0: vutils.save_image(real_cpu, '%s/real_samples.png' % opt.outf, normalize=True,pad_value=0.5) fake = netG(fixed_noise) vutils.save_image(fake.detach().apply_(lambda x: 1 if x > 0 else 0), '%s/fake_samples_epoch_%03d.png' % (opt.outf, epoch), normalize=True,pad_value=0.5)
loss = [total_val_loss, p_l2_total_loss, div_l2_total_loss, \ p_l1_total_loss, div_l1_total_loss] filename = 'figures/fig_' + str(it) + '.png' #if (it % 8 == 0): # plotField(out_list, target, flags, loss, mconf, filename) data[:, 1:3] = out_U.clone() return data, target, loss print('Dataset in ' + str(conf['dataDir'])) print('Plotting results at epoch ' + str(state['epoch'])) it = 0 it_8dt = 0 max_iter = 8000 data, _ = te.__getitem__(0) data.unsqueeze_(0) data_loss_plot = np.empty((0, 2)) fig = plt.gcf() fig.show() plt.legend() plt.xlabel('Iteration') plt.ylabel('Divergence') fig.canvas.draw() while (it < max_iter): print('It = ' + str(it)) if (it % 8 == 0): _, target = te.__getitem__(it_8dt) target.unsqueeze_(0) it_8dt += 1