def main(args): global device best_acc = 0.0 # create checkpoint dir and log dir if not isdir(args.checkpoint): print("Create new checkpoint folder " + args.checkpoint) mkdir_p(args.checkpoint) if not args.resume: if isdir(args.logdir): shutil.rmtree(args.logdir) mkdir_p(args.logdir) # create model model = ROOTNET() model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # optionally resume from a checkpoint if args.resume: if isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_acc = checkpoint['best_acc'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) lr = optimizer.param_groups[0]['lr'] print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True print(' Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0)) train_loader = DataLoader(GraphDataset(root=args.train_folder), batch_size=args.train_batch, shuffle=True, follow_batch=['joints']) val_loader = DataLoader(GraphDataset(root=args.val_folder), batch_size=args.test_batch, shuffle=False, follow_batch=['joints']) test_loader = DataLoader(GraphDataset(root=args.test_folder), batch_size=args.test_batch, shuffle=False, follow_batch=['joints']) if args.evaluate: print('\nEvaluation only') test_loss, test_acc = test(test_loader, model) print('test_loss {:.8f}. test_acc: {:.6f}'.format(test_loss, test_acc)) return scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.schedule, gamma=args.gamma) logger = SummaryWriter(log_dir=args.logdir) for epoch in range(args.start_epoch, args.epochs): lr = scheduler.get_last_lr() print('\nEpoch: %d | LR: %.8f' % (epoch + 1, lr[0])) train_loss = train(train_loader, model, optimizer, args) val_loss, val_acc = test(val_loader, model) test_loss, test_acc = test(test_loader, model) scheduler.step() print('Epoch{:d}. train_loss: {:.6f}.'.format(epoch + 1, train_loss)) print('Epoch{:d}. val_loss: {:.6f}. val_acc: {:.6f}'.format( epoch + 1, val_loss, val_acc)) print('Epoch{:d}. test_loss: {:.6f}. test_acc: {:.6f}'.format( epoch + 1, test_loss, test_acc)) # remember best acc and save checkpoint is_best = val_acc > best_acc best_acc = max(val_acc, best_acc) save_checkpoint( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_acc': best_acc, 'optimizer': optimizer.state_dict() }, is_best, checkpoint=args.checkpoint) info = { 'train_loss': train_loss, 'val_loss': val_loss, 'val_accuracy': val_acc, 'test_loss': test_loss, 'test_accuracy': test_acc } for tag, value in info.items(): logger.add_scalar(tag, value, epoch + 1) print("=> loading checkpoint '{}'".format( os.path.join(args.checkpoint, 'model_best.pth.tar'))) checkpoint = torch.load(os.path.join(args.checkpoint, 'model_best.pth.tar')) best_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( os.path.join(args.checkpoint, 'model_best.pth.tar'), best_epoch)) test_loss, test_acc = test(test_loader, model) print('Best epoch:\n test_loss {:8f} test_acc {:8f}'.format( test_loss, test_acc))
def main(args): global device lowest_loss = 1e20 # create checkpoint dir and log dir if not isdir(args.checkpoint): print("Create new checkpoint folder " + args.checkpoint) mkdir_p(args.checkpoint) if not args.resume: if isdir(args.logdir): shutil.rmtree(args.logdir) mkdir_p(args.logdir) # create model model = PairCls() model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # optionally resume from a checkpoint if args.resume: if isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) args.start_epoch = checkpoint['epoch'] lowest_loss = checkpoint['lowest_loss'] print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True print(' Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0)) train_loader = DataLoader(GraphDataset(root=args.train_folder), batch_size=args.train_batch, shuffle=True) val_loader = DataLoader(GraphDataset(root=args.val_folder), batch_size=args.test_batch, shuffle=False) test_loader = DataLoader(GraphDataset(root=args.test_folder), batch_size=args.test_batch, shuffle=False) if args.evaluate: print('\nEvaluation only') test_loss = test(test_loader, model, args, save_result=True, best_epoch=args.start_epoch) print('test_loss {:8f}'.format(test_loss)) return lr = args.lr logger = SummaryWriter(log_dir=args.logdir) for epoch in range(args.start_epoch, args.epochs): print('\nEpoch: %d | LR: %.8f' % (epoch + 1, lr)) lr = adjust_learning_rate(optimizer, epoch, lr, args.schedule, args.gamma) train_loss = train(train_loader, model, optimizer, args) val_loss = test(val_loader, model, args) test_loss = test(test_loader, model, args, best_epoch=epoch + 1) print('Epoch{:d}. train_loss: {:.6f}.'.format(epoch, train_loss)) print('Epoch{:d}. val_loss: {:.6f}.'.format(epoch, val_loss)) print('Epoch{:d}. test_loss: {:.6f}.'.format(epoch, test_loss)) # remember best acc and save checkpoint is_best = val_loss < lowest_loss lowest_loss = min(val_loss, lowest_loss) save_checkpoint( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'lowest_loss': lowest_loss, 'optimizer': optimizer.state_dict() }, is_best, checkpoint=args.checkpoint) info = { 'train_loss': train_loss, 'val_loss': val_loss, 'test_loss': test_loss } for tag, value in info.items(): logger.add_scalar(tag, value, epoch + 1) print("=> loading checkpoint '{}'".format( os.path.join(args.checkpoint, 'model_best.pth.tar'))) checkpoint = torch.load(os.path.join(args.checkpoint, 'model_best.pth.tar')) best_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( os.path.join(args.checkpoint, 'model_best.pth.tar'), best_epoch)) test_loss = test(test_loader, model, args, save_result=True, best_epoch=best_epoch) print('Best epoch:\n test_loss {:8f}'.format(test_loss))
def main(args): global device lowest_loss = 1e20 # create checkpoint dir and log dir if not isdir(args.checkpoint): print("Create new checkpoint folder " + args.checkpoint) mkdir_p(args.checkpoint) if not args.resume: if isdir(args.logdir): shutil.rmtree(args.logdir) mkdir_p(args.logdir) # create model model = models.__dict__["skinnet"](args.nearest_bone, args.Dg, args.Lf) model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # optionally resume from a checkpoint lr = args.lr if args.resume: if isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] lowest_loss = checkpoint['lowest_loss'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) lr = optimizer.param_groups[0]['lr'] print("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True print(' Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0)) train_loader = DataLoader(SkinDataset(root=args.train_folder), batch_size=args.train_batch, shuffle=True) val_loader = DataLoader(SkinDataset(root=args.val_folder), batch_size=args.test_batch, shuffle=False) test_loader = DataLoader(SkinDataset(root=args.test_folder), batch_size=args.test_batch, shuffle=False) if args.evaluate: print('\nEvaluation only') test_loss = test(test_loader, model, args, save_result=True) print('test_loss {:6f}'.format(test_loss)) return scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.schedule, gamma=args.gamma) logger = SummaryWriter(log_dir=args.logdir) for epoch in range(args.start_epoch, args.epochs): lr = scheduler.get_last_lr() print('\nEpoch: %d | LR: %.8f' % (epoch + 1, lr[0])) train_loss = train(train_loader, model, optimizer, args) val_loss = test(val_loader, model, args) test_loss = test(test_loader, model, args) scheduler.step() print('Epoch{:d}. train_loss: {:.6f}.'.format(epoch + 1, train_loss)) print('Epoch{:d}. val_loss: {:.6f}.'.format(epoch + 1, val_loss)) print('Epoch{:d}. test_loss: {:.6f}.'.format(epoch + 1, test_loss)) # remember best acc and save checkpoint is_best = val_loss < lowest_loss lowest_loss = min(val_loss, lowest_loss) save_checkpoint({'epoch': epoch + 1, 'state_dict': model.state_dict(), 'lowest_loss': lowest_loss, 'optimizer': optimizer.state_dict()}, is_best, checkpoint=args.checkpoint) info = {'train_loss': train_loss, 'val_loss': val_loss, 'test_loss': test_loss} for tag, value in info.items(): logger.add_scalar(tag, value, epoch + 1)