target3D_var = torch.autograd.Variable(xyz).float() output = model(input_var) reg = output[-1] loss = criterion(reg, target3D_var) Loss3D.update(loss.data[0], input1.size(0)) print(len(output)) for k in range(0, 3): loss += criterion(output[k], target2D_var) Loss.update(loss.data[0], input1.size(0)) Acc.update( Accuracy((output[-2].data).cpu().numpy(), (target2D_var.data).numpy())) mpjpe, num3D = MPJPE((output[-2].data).cpu().numpy(), (reg[:, 32:].data).numpy(), meta) if num3D > 0: Mpjpe.update(mpjpe, num3D) optimizer.zero_grad() loss.backward() optimizer.step() Bar.suffix = 'Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Loss3D {loss3d.avg:.6f} | Acc {Acc.avg:.6f} | Mpjpe {Mpjpe.avg:.6f}'.format( epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss,
def step(split, epoch, opt, dataLoader, model, criterion, optimizer=None): if split == 'train': model.train() else: model.eval() Loss, Acc, Mpjpe, Loss3D = AverageMeter(), AverageMeter(), AverageMeter( ), AverageMeter() nIters = len(dataLoader) bar = Bar('==>', max=nIters) for i, (input, target2D, target3D, meta) in enumerate(dataLoader): input_var = torch.autograd.Variable(input).float().cuda() target2D_var = torch.autograd.Variable(target2D).float().cuda() target3D_var = torch.autograd.Variable(target3D).float().cuda() output = model(input_var) reg = output[opt.nStack] if opt.DEBUG >= 2: gt = getPreds(target2D.cpu().numpy()) * 4 pred = getPreds((output[opt.nStack - 1].data).cpu().numpy()) * 4 debugger = Debugger() debugger.addImg( (input[0].numpy().transpose(1, 2, 0) * 256).astype(np.uint8)) debugger.addPoint2D(pred[0], (255, 0, 0)) debugger.addPoint2D(gt[0], (0, 0, 255)) debugger.showImg() debugger.saveImg('debug/{}.png'.format(i)) loss = FusionCriterion(opt.regWeight, opt.varWeight)(reg, target3D_var) Loss3D.update(loss.data[0], input.size(0)) for k in range(opt.nStack): loss += criterion(output[k], target2D_var) Loss.update(loss.data[0], input.size(0)) Acc.update( Accuracy((output[opt.nStack - 1].data).cpu().numpy(), (target2D_var.data).cpu().numpy())) mpjpe, num3D = MPJPE((output[opt.nStack - 1].data).cpu().numpy(), (reg.data).cpu().numpy(), meta) if num3D > 0: Mpjpe.update(mpjpe, num3D) if split == 'train': optimizer.zero_grad() loss.backward() optimizer.step() Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Loss3D {loss3d.avg:.6f} | Acc {Acc.avg:.6f} | Mpjpe {Mpjpe.avg:.6f} ({Mpjpe.val:.6f})'.format( epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss, Acc=Acc, split=split, Mpjpe=Mpjpe, loss3d=Loss3D) bar.next() bar.finish() return Loss.avg, Acc.avg, Mpjpe.avg, Loss3D.avg
def step_gan(split, epoch, opt, realDataLoader, fakeDataLoader, generator, discriminator, criterion_2d, optimizer_G=None, optimizer_D=None): if split == 'train': generator.train() discriminator.train() else: generator.eval() discriminator.eval() LossG, LossD, Loss2D, Acc, Mpjpe = AverageMeter(), AverageMeter( ), AverageMeter(), AverageMeter(), AverageMeter() nIters = len(dataLoader) bar = Bar('==>', max=nIters) for i, (real, meta) in enumerate(realdataLoader): real_3d = torch.autograd.Variable(real).float().cuda() # generate a batch of fake 3d pose (z, z_target2D, z_target3D, meta) = next(iter((fakeDataLoader))) z_var = Variable(z).float().cuda() z_target2d_var = Variable(z_target2D).float().cuda() z_target3d_var = Variable(z_target3D).float().cuda() fake_3d = generator(z_var)[opt.nStack + 3].detach() # Adversarial loss loss_D = -torch.mean(discriminator(real_3d)) + torch.mean( discriminator(fake_3d)) LossD.update(loss_D) if split == 'train': optimizer_D.zero_grad() loss_D.backward() optimizer_D.step() for p in discriminator.parameters(): p.data.clamp_(-opt.clip_value, opt.clip_value) if i % opt.n_critic == 0: # train generator output = generator(z_var) gen_3d = output[opt.nStack + 3] loss_G = -torch.mean(discriminator(gen_3d)) LossG.update(loss_G) loss_2d = 0 for k in range(opt.nStack): loss_2d += criterion_2d(output[k], z_target2D_var) Loss2D.update(loss_2d.data.item(), z.size(0)) Acc.update( Accuracy((output[opt.nStack - 1].data).cpu().numpy(), (z_target2D_var.data).cpu().numpy())) mpjpe, num3D = pointMPJPE(gen_3d.data.cpu().numpy(), meta) if num3D > 0: Mpjpe.update(mpjpe, num3D) if split == 'train': optimizer_G.zero_grad() loss_G.backward() optimizer_G.step() Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | LossG {lossG.avg:.6f} | LossD {lossD.avg:.6f} | Loss2D {loss2D.avg:.6f} | Acc {Acc.avg:.6f} | Mpjpe {Mpjpe.avg:.6f} ({Mpjpe.val:.6f})'.format( epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, lossG=LossG, lossD=LossD, loss2D=Loss2D, Acc=Acc, split=split, Mpjpe=Mpjpe) bar.next() bar.finish() return LossG.avg, LossD.avg, Loss2D.avg, Acc.avg, Mpjpe.avg
def step(split, epoch, opt, dataLoader, model, criterion, optimizer=None): if split == 'train': model.train() else: model.eval() Loss, Acc = AverageMeter(), AverageMeter() preds = [] nIters = len(dataLoader) bar = Bar('{}'.format(opt.expID), max=nIters) for i, (input, target, target2, meta) in enumerate(dataLoader): input_var = torch.autograd.Variable(input).float().cuda() target_var = torch.autograd.Variable(target).float().cuda() target_var2 = torch.autograd.Variable(target2).float().cuda() #print( input_var) output = model(input_var) #print(output[-1].size()) if opt.DEBUG >= 2: gt = getPreds(target.cpu().numpy()) * 4 pred = getPreds((output[opt.nStack - 1].data).cpu().numpy()) * 4 debugger = Debugger() img = (input[0].numpy().transpose(1, 2, 0) * 256).astype( np.uint8).copy() debugger.addImg(img) debugger.addPoint2D(pred[0], (255, 0, 0)) debugger.addPoint2D(gt[0], (0, 0, 255)) debugger.showAllImg(pause=True) loss = criterion(output[0], target_var) for k in range(1, opt.nStack): loss += criterion(output[k], target_var) Loss.update(loss.data[0], input.size(0)) Acc.update( Accuracy((output[opt.nStack - 1].data).cpu().numpy(), (target_var.data).cpu().numpy())) if split == 'train': optimizer.zero_grad() loss.backward() optimizer.step() else: input_ = input.cpu().numpy() input_[0] = Flip(input_[0]).copy() inputFlip_var = torch.autograd.Variable( torch.from_numpy(input_).view(1, input_.shape[1], ref.inputRes, ref.inputRes)).float().cuda( opt.GPU) outputFlip = model(inputFlip_var) outputFlip = ShuffleLR( Flip((outputFlip[opt.nStack - 1].data).cpu().numpy()[0])).reshape( 1, ref.nJoints, 64, 64) output_ = ( (output[opt.nStack - 1].data).cpu().numpy() + outputFlip) / 2 preds.append( finalPreds(output_, meta['center'], meta['scale'], meta['rotate'])[0]) Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Acc {Acc.avg:.6f} ({Acc.val:.6f})'.format( epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss, Acc=Acc, split=split) bar.next() bar.finish() return {'Loss': Loss.avg, 'Acc': Acc.avg}, preds
def train(epoch): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() Loss, Acc, Mpjpe, Loss3D = AverageMeter(), AverageMeter(), AverageMeter( ), AverageMeter() # switch to train mode model.train() end = time.time() for i, data in enumerate(train_loader): input, target2D, target3D, meta = data input_var = torch.autograd.Variable(input).float().cuda() target2D_var = torch.autograd.Variable(target2D).float().cuda() target3D_var = torch.autograd.Variable(target3D).float().cuda() # measure data loading time data_time.update(time.time() - end) #if args.gpu is not None: # input = input.cuda(args.gpu, non_blocking=True) #target = target.cuda(args.gpu, non_blocking=True) # compute output #output = model(input) #loss = criterion(output, target) output = model(input_var) reg = output[args.nStack] # measure accuracy and record loss #prec1, prec5 = accuracy(output, target, topk=(1, 5)) #losses.update(loss.item(), input.size(0)) #top1.update(prec1[0], input.size(0)) #top5.update(prec5[0], input.size(0)) #print(reg) #print(reg.float()) #print(reg.type(torch.FloatTensor)) reg = reg.float().cuda() #print(reg.type()) #loss = FusionCriterion(args.regWeight, args.varWeight)(reg, target3D_var) #Loss3D.update(loss.data[0], input.size(0)) loss = 0 for k in range(args.nStack): loss += criterion(output[k], target2D_var) Loss.update(loss.data[0], input.size(0)) Acc.update( Accuracy((output[args.nStack - 1].data).cpu().numpy(), (target2D_var.data).cpu().numpy())) mpjpe, num3D = MPJPE((output[args.nStack - 1].data).cpu().numpy(), (reg.data).cpu().numpy(), meta) if num3D > 0: Mpjpe.update(mpjpe, num3D) # compute gradient and do SGD step optimizer.zero_grad() loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() loss_train, acc_train, mpjpe_train, loss3d_train = Loss.avg, Acc.avg, Mpjpe.avg, Loss3D.avg if i % args.print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Acc {acc.val:.3f} ({acc.avg:.3f})\t' 'Mpjpe {mpjpe.val:.3f} ({mpjpe.avg:.3f})\t' 'Loss3d {loss3d.val:.3f} ({loss3d.avg:.3f})\t'.format( epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=Loss, acc=Acc, mpjpe=Mpjpe, loss3d=Loss3D))
def step(split, epoch, opt, dataLoader, model, criterion, optimizer=None): if split == 'train': model.train() else: model.eval() Loss, Acc = AverageMeter(), AverageMeter() nIters = len(dataLoader) # bar = Bar('==>', max=nIters) start_time = time.time() for i, (input, target) in enumerate(dataLoader): input_var = torch.autograd.Variable(input).float().cuda() target_var = torch.autograd.Variable( target.cuda(async=True)).float().cuda() if (model.hgType == 'vae'): output, latentspace = model(input_var) else: output = model(input_var) if opt.DEBUG >= 2: gt = getPreds(target.cpu().numpy()) * 4 pred = getPreds((output[opt.nStack - 1].data).cpu().numpy()) * 4 # init = getPreds(input.numpy()[:, 3:]) debugger = Debugger() img = (input[0].numpy()[:3].transpose(1, 2, 0) * 256).astype( np.uint8).copy() debugger.addImg(img) debugger.addPoint2D(pred[0], (255, 0, 0)) debugger.addPoint2D(gt[0], (0, 0, 255)) # debugger.addPoint2D(init[0], (0, 255, 0)) debugger.showAllImg(pause=True) #debugger.saveImg('debug/{}.png'.format(i)) loss = criterion(output[0], target_var) for k in range(1, opt.nStack): loss += criterion(output[k], target_var) if (model.hgType == 'vae'): for k in range(0, opt.nStack): loss += ref.vaeloss_wt * _compute_kl(latentspace[k]) Loss.update(loss.data[0], input.size(0)) Acc.update( Accuracy((output[opt.nStack - 1].data).cpu().numpy(), (target_var.data).cpu().numpy())) if split == 'train': optimizer.zero_grad() loss.backward() optimizer.step() # Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Acc {Acc.avg:.6f} ({Acc.val:.6f})'.format(epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss, Acc=Acc, split = split) # bar.next() curr_time = time.time() print( '{split} Epoch: [{0}][{1}/{2}]| Total: {total:f} | ETA: {eta:f} | Loss {loss.avg:.6f} | Acc {Acc.avg:.6f} ({Acc.val:.6f})' .format(epoch, i, nIters, total=curr_time - start_time, eta=(curr_time - start_time) * (nIters - i + 1) / (i + 1), loss=Loss, Acc=Acc, split=split)) # bar.finish() return Loss.avg, Acc.avg
def step(split, epoch, opt, dataLoader, model, criterion, optimizer=None): if split == 'train': model.train() else: model.eval() Loss, Acc = AverageMeter(), AverageMeter() preds = [] nIters = len(dataLoader) bar = Bar('{}'.format(opt.expID), max=nIters) for i, (input, targets, action, meta) in enumerate(dataLoader): input_var = torch.autograd.Variable(input).float().cuda(opt.GPU) target_var = [] for t in range(len(targets)): target_var.append( torch.autograd.Variable(targets[t]).float().cuda(opt.GPU)) z = [] for k in range(opt.numNoise): noise = torch.autograd.Variable( torch.randn((input_var.shape[0], 1, 64, 64))).cuda(opt.GPU) z.append(noise) output, samples = model(input_var, z, action) pred_sample = maximumExpectedUtility(samples, criterion) target = maximumExpectedUtility(target_var, criterion) if opt.DEBUG >= 2: gt = getPreds(target.cpu().numpy()) * 4 pred = getPreds((pred_sample.data).cpu().numpy()) * 4 debugger = Debugger() img = (input[0].numpy().transpose(1, 2, 0) * 256).astype( np.uint8).copy() debugger.addImg(img) debugger.addPoint2D(pred[0], (255, 0, 0)) debugger.addPoint2D(gt[0], (0, 0, 255)) debugger.showAllImg(pause=True) loss = DiscoLoss(output, samples, target_var, criterion) Loss.update(loss.item(), input.size(0)) Acc.update( Accuracy((pred_sample.data).cpu().numpy(), (target.data).cpu().numpy())) if split == 'train': optimizer.zero_grad() loss.backward() optimizer.step() else: input_ = input.cpu().numpy() input_[0] = Flip(input_[0]).copy() inputFlip_var = torch.autograd.Variable( torch.from_numpy(input_).view(1, input_.shape[1], ref.inputRes, ref.inputRes)).float().cuda( opt.GPU) _, samplesFlip = model(inputFlip_var, z, action) pred_sample_flip = maximumExpectedUtility(samplesFlip, criterion) outputFlip = ShuffleLR( Flip((pred_sample_flip.data).cpu().numpy()[0])).reshape( 1, ref.nJoints, ref.outputRes, ref.outputRes) output_ = old_div(((pred_sample.data).cpu().numpy() + outputFlip), 2) preds.append( finalPreds(output_, meta['center'], meta['scale'], meta['rotate'])[0]) Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Acc {Acc.avg:.6f} ({Acc.val:.6f})'.format( epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss, Acc=Acc, split=split) bar.next() bar.finish() return {'Loss': Loss.avg, 'Acc': Acc.avg}, preds
inputs = input.float().cuda() targets_2D = target2D.float().cuda() targets_3D = target3D.float().cuda() #print(target2D,targets_3D) output = model(inputs) reg = output[opt.nStack] loss = FusionCriterion(opt.regWeight, opt.varWeight)(reg, targets_3D) Loss3D.update(loss.item(), input.size(0)) for j in range(1, opt.nStack): loss += criterion(output[j], targets_2D) Loss.update(loss.item(), input.size(0)) acc = Accuracy((output[opt.nStack - 1].data).cpu().numpy(), (targets_2D.data).cpu().numpy()) Acc.update(acc) mpjpe, num3D = MPJPE((output[opt.nStack - 1].data).cpu().numpy(), (reg.data).cpu().numpy(), meta) if num3D > 0: Mpjpe.update(mpjpe, num3D) optimizer.zero_grad() loss.backward() optimizer.step() print(epoch, i, loss.data, acc) if epoch % opt.valIntervals == 0: torch.save(model, os.path.join(opt.saveDir, 'model_{}.pth'.format(epoch))) adjust_learning_rate(optimizer, epoch, opt.dropLR, opt.LR)
def main(): opt = opts().parse() now = datetime.datetime.now() logger = Logger(opt.saveDir, now.isoformat()) model = MSSH().cuda() optimizer = torch.optim.RMSprop(model.parameters(), opt.LR, alpha = ref.alpha, eps = ref.epsilon, weight_decay = ref.weightDecay, momentum = ref.momentum) criterion = torch.nn.MSELoss().cuda() # if opt.GPU > -1: # print('Using GPU', opt.GPU) # model = model.cuda(opt.GPU) # criterion = criterion.cuda(opt.GPU) val_loader = torch.utils.data.DataLoader( MPII(opt, 'val'), batch_size = 1, shuffle = False, num_workers = int(ref.nThreads) ) if opt.test: log_dict_train, preds = val(0, opt, val_loader, model, criterion) sio.savemat(os.path.join(opt.saveDir, 'preds.mat'), mdict = {'preds': preds}) return train_loader = torch.utils.data.DataLoader( MPII(opt, 'train'), batch_size = opt.trainBatch, shuffle = True if opt.DEBUG == 0 else False, num_workers = int(ref.nThreads) ) for epoch in range(1): model.train() Loss, Acc = AverageMeter(), AverageMeter() preds = [] nIters = len(train_loader) bar = Bar('{}'.format(opt.expID), max=nIters) for i, (input, target, meta) in enumerate(train_loader): input_var = torch.autograd.Variable(input).float().cuda() target_var = torch.autograd.Variable(target).float().cuda() #print( input_var) output = model(input_var) loss = criterion(output, target_var) Loss.update(loss.data[0], input.size(0)) Acc.update(Accuracy((output.data).cpu().numpy(), (target_var.data).cpu().numpy())) optimizer.zero_grad() loss.backward() optimizer.step() Bar.suffix = '{split} Epoch: [{0}][{1}/{2}]| Total: {total:} | ETA: {eta:} | Loss {loss.avg:.6f} | Acc {Acc.avg:.6f} ({Acc.val:.6f})'.format(epoch, i, nIters, total=bar.elapsed_td, eta=bar.eta_td, loss=Loss, Acc=Acc, split = "train") bar.next() bar.finish()