def main(): global args, best_mAP args = parser.parse_args() useCuda = torch.cuda.is_available() and (args.gpu_id is not None or args.multiGPU) torch.manual_seed(args.seed) if useCuda: torch.cuda.manual_seed(args.seed) # Check the save_dir exists or not if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # model = torch.nn.DataParallel(resnet.__dict__[args.arch]()) model = resnet.resnet20(useRRSVM=False) # optionally resume from a checkpoint if args.resume: pretrained_file = os.path.join(default_model_directory, '{:s}.th'.format(args.arch)) if os.path.isfile(pretrained_file): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(pretrained_file) # args.start_epoch = checkpoint['epoch'] if 'epoch' in checkpoint best_prec1 = checkpoint['best_prec1'] if 'epoch' in checkpoint else 0 state_dict = {k.replace('module.', ''): v for k, v in checkpoint['state_dict'].items()} model.load_state_dict(state_dict, strict=False) print("=> loaded checkpoint {:s}".format(pretrained_file)) else: print("=> no checkpoint found at '{}'".format(args.resume)) model = cuda_model.convertModel2Cuda(model, args.gpu_id, args.multiGpu) # train_loader, val_loader = Cifar10.get_cifar10_datasets(args, train_portion=1.0) # using 10% of all data normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # train_loader = torch.utils.data.DataLoader( datasets.CIFAR10(root=dataset_root, train=True, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, 4), transforms.ToTensor(), normalize, ]), download=True), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader( datasets.CIFAR10(root=dataset_root, train=False, transform=transforms.Compose([ transforms.ToTensor(), normalize, ])), batch_size=128, shuffle=False, num_workers=args.workers, pin_memory=True) # # define loss function (criterion) and pptimizer criterion = nn.CrossEntropyLoss().cuda() # if args.half: # model.half() # criterion.half() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) args.start_epoch = 0 lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[10, 20, 40]) # lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, # milestones=[10, 20, 40], last_epoch=args.start_epoch - 1) if args.arch in ['resnet1202', 'resnet110']: # for resnet1202 original paper uses lr=0.01 for first 400 minibatches for warm-up # then switch back. In this implementation it will correspond for first epoch. for param_group in optimizer.param_groups: param_group['lr'] = args.lr*0.1 if args.evaluate: validate(val_loader, model, criterion) return is_best=False for epoch in range(args.start_epoch, args.epochs): # train for one epoch lr_scheduler.step(epoch) print('current lr {:.5e}'.format(optimizer.param_groups[0]['lr'])) train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set prec1 = validate(val_loader, model, criterion) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) if epoch > 0 and epoch % args.save_every == 0: save_checkpoint({ 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, }, is_best, filename=os.path.join(args.save_dir, 'checkpoint-{:04d}.th'.format(epoch))) save_checkpoint({ 'state_dict': model.state_dict(), 'best_prec1': best_prec1, }, is_best, filename=os.path.join(args.save_dir, 'checkpoint-{:04d}.th'.format(epoch)))
x = F.relu(x) x = x.view(-1, self.d2b) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) # return x return F.log_softmax(x) model = Net(pool_method=args.pool_method) optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) loss_fn = torch.nn.CrossEntropyLoss() if args.cuda: model = cuda_model.convertModel2Cuda(model, args) loss_fn = loss_fn.cuda() print("Number of Params:\t{:d}".format( sum([p.data.nelement() for p in model.parameters()]))) def train(): model.train() start_time = time.time() n_train_exs = 0 total_loss = 0 for batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) optimizer.zero_grad()
siamese_net = SiameseNet(single_pass_net) ckpt = torch.load(ckpt_file, map_location=lambda storage, loc: storage) model_state_dict = ckpt['state_dict'] siamese_net.load_state_dict(model_state_dict) else: single_pass_net = CompositionNet(pretrained=True, LinearSize1=args.l1, LinearSize2=args.l2) siamese_net = SiameseNet(single_pass_net) print("Number of Params in {:s}\t{:d}".format( identifier, sum([p.data.nelement() for p in single_pass_net.parameters()]))) useCuda = cuda_model.ifUseCuda(args.gpu_id, args.multiGpu) single_pass_net = cuda_model.convertModel2Cuda(single_pass_net, args.gpu_id, args.multiGpu) single_pass_net.eval() pdefined_anchors = get_pdefined_anchors() t_transform = data_transforms.get_val_transform(224) image_list = get_test_list() image_list = image_list[0:5] n_images = len(image_list) print("Number of Images:\t{:d}".format(len(image_list))) image_annotation = {} topN = 3