def main(): TTA2_preprocess = [preprocess, preprocess_hflip] TTA10_preprocess = [preprocess_tencrop] TTA12_preprocess = [preprocess, preprocess_hflip, preprocess_tencrop] id = 0 print("testing {}.....".format(ckp_path)) for trans in TTA10_preprocess: print("id is: {}".format(id)) test_dataset = McDataset(test_root, test_source, transform=trans) test_loader = DataLoader(test_dataset, batch_size, shuffle=False, pin_memory=False) print("test loading....") model = get_model('senet154', pretrained=False) # model.cuda() model = torch.nn.DataParallel(model).cuda() checkpoint = load_checkpoint(ckp_path) model.load_state_dict(checkpoint['state_dict']) tester = TenCropTester(model) # if id == 2: # tester = TenCropTester(model) # else: # tester = BaseTester(model) pred = tester.extract(test_loader) np.save("./rst/prob_dense{}.npy".format(id), pred) id += 1
def main(): # tta_preprocess = [preprocess, preprocess_hflip] tta_preprocess = [preprocess_tencrop] id = 0 print("testing {}.....".format(ckp_path)) for trans in tta_preprocess: print("id is: {}".format(id)) test_dataset = McDataset(test_root, test_source, transform=trans) test_loader = DataLoader(test_dataset, batch_size, shuffle=False, pin_memory=False) print("test loading....") model = models.__dict__[arch]() model = FineTuneModel(model, arch, 128) # model.cuda() model = torch.nn.DataParallel(model).cuda() checkpoint = load_checkpoint(ckp_path) model.load_state_dict(checkpoint['state_dict']) # tester = BaseTester(model) tester = TenCropTester(model) pred = tester.extract(test_loader) np.save("./rst/prob_dense{}.npy".format(id), pred) id += 1
def main(): normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) test_dataset = McDataset( test_root, test_source, transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(input_size), transforms.ToTensor(), normalize, ])) test_loader = DataLoader(test_dataset, batch_size, shuffle=False, pin_memory=False) model = models.__dict__[arch]() model = FineTuneModel(model, arch, 128) # model.cuda() model = torch.nn.DataParallel(model).cuda() checkpoint = load_checkpoint(ckp_path) model.load_state_dict(checkpoint['state_dict']) tester = BaseTester(model) pred = tester.extract(test_loader) np.save("./rst/prob_dense.npy", pred)
def main(): global args, best_prec1, min_loss args = parser.parse_args() rank, world_size = dist_init(args.port) print("world_size is: {}".format(world_size)) assert (args.batch_size % world_size == 0) assert (args.workers % world_size == 0) args.batch_size = args.batch_size // world_size args.workers = args.workers // world_size # create model print("=> creating model '{}'".format("inceptionv4")) print("save_path is: {}".format(args.save_path)) image_size = 341 input_size = 299 model = get_model('inceptionv4', pretrained=True) # print("model is: {}".format(model)) model.cuda() model = DistModule(model) # optionally resume from a checkpoint if args.load_path: if args.resume_opt: best_prec1, start_epoch = load_state(args.load_path, model, optimizer=optimizer) else: # print('load weights from', args.load_path) load_state(args.load_path, model) cudnn.benchmark = True # Data loading code normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) train_dataset = McDataset( args.train_root, args.train_source, transforms.Compose([ transforms.Resize(image_size), transforms.RandomCrop(input_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ColorAugmentation(), normalize, ])) val_dataset = McDataset( args.val_root, args.val_source, transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(input_size), transforms.ToTensor(), normalize, ])) train_sampler = DistributedSampler(train_dataset) val_sampler = DistributedSampler(val_dataset) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False, sampler=train_sampler) val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False, sampler=val_sampler) # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss() lr = 0 patience = 0 for epoch in range(args.start_epoch, args.epochs): # adjust_learning_rate(optimizer, epoch) train_sampler.set_epoch(epoch) if epoch == 1: lr = 0.00003 if patience == 2: patience = 0 checkpoint = load_checkpoint(args.save_path + '_best.pth.tar') model.load_state_dict(checkpoint['state_dict']) print("Loading checkpoint_best.............") # model.load_state_dict(torch.load('checkpoint_best.pth.tar')) lr = lr / 10.0 if epoch == 0: lr = 0.001 for name, param in model.named_parameters(): # print("name is: {}".format(name)) if (name not in last_layer_names): param.requires_grad = False optimizer = torch.optim.RMSprop(filter(lambda p: p.requires_grad, model.parameters()), lr=lr) # optimizer = torch.optim.Adam( # filter(lambda p: p.requires_grad, model.parameters()), lr=lr) else: for param in model.parameters(): param.requires_grad = True optimizer = torch.optim.RMSprop(model.parameters(), lr=lr, weight_decay=0.0001) # optimizer = torch.optim.Adam( # model.parameters(), lr=lr, weight_decay=0.0001) print("lr is: {}".format(lr)) # train for one epoch train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set val_prec1, val_losses = validate(val_loader, model, criterion) print("val_losses is: {}".format(val_losses)) # remember best prec@1 and save checkpoint if rank == 0: # remember best prec@1 and save checkpoint if val_losses < min_loss: is_best = True save_checkpoint( { 'epoch': epoch + 1, 'arch': 'inceptionv4', 'state_dict': model.state_dict(), 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, is_best, args.save_path) # torch.save(model.state_dict(), 'best_val_weight.pth') print( 'val score improved from {:.5f} to {:.5f}. Saved!'.format( min_loss, val_losses)) min_loss = val_losses patience = 0 else: patience += 1 if rank == 1 or rank == 2 or rank == 3 or rank == 4 or rank == 5 or rank == 6 or rank == 7: if val_losses < min_loss: min_loss = val_losses patience = 0 else: patience += 1 print("patience is: {}".format(patience)) print("min_loss is: {}".format(min_loss)) print("min_loss is: {}".format(min_loss))
def main(): global args, config, best_prec1 args = parser.parse_args() with open(args.config) as f: config = yaml.load(f) config = EasyDict(config['common']) config.save_path = os.path.dirname(args.config) rank, world_size = dist_init() # create model bn_group_size = config.model.kwargs.bn_group_size bn_var_mode = config.model.kwargs.get('bn_var_mode', 'L2') if bn_group_size == 1: bn_group = None else: assert world_size % bn_group_size == 0 bn_group = simple_group_split(world_size, rank, world_size // bn_group_size) config.model.kwargs.bn_group = bn_group config.model.kwargs.bn_var_mode = (link.syncbnVarMode_t.L1 if bn_var_mode == 'L1' else link.syncbnVarMode_t.L2) model = model_entry(config.model) if rank == 0: print(model) model.cuda() if config.optimizer.type == 'FP16SGD' or config.optimizer.type == 'FusedFP16SGD': args.fp16 = True else: args.fp16 = False if args.fp16: # if you have modules that must use fp32 parameters, and need fp32 input # try use link.fp16.register_float_module(your_module) # if you only need fp32 parameters set cast_args=False when call this # function, then call link.fp16.init() before call model.half() if config.optimizer.get('fp16_normal_bn', False): print('using normal bn for fp16') link.fp16.register_float_module(link.nn.SyncBatchNorm2d, cast_args=False) link.fp16.register_float_module(torch.nn.BatchNorm2d, cast_args=False) link.fp16.init() model.half() model = DistModule(model, args.sync) # create optimizer opt_config = config.optimizer opt_config.kwargs.lr = config.lr_scheduler.base_lr if config.get('no_wd', False): param_group, type2num = param_group_no_wd(model) opt_config.kwargs.params = param_group else: opt_config.kwargs.params = model.parameters() optimizer = optim_entry(opt_config) # optionally resume from a checkpoint last_iter = -1 best_prec1 = 0 if args.load_path: if args.recover: best_prec1, last_iter = load_state(args.load_path, model, optimizer=optimizer) else: load_state(args.load_path, model) cudnn.benchmark = True # Data loading code normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # augmentation aug = [ transforms.RandomResizedCrop(config.augmentation.input_size), transforms.RandomHorizontalFlip() ] for k in config.augmentation.keys(): assert k in [ 'input_size', 'test_resize', 'rotation', 'colorjitter', 'colorold' ] rotation = config.augmentation.get('rotation', 0) colorjitter = config.augmentation.get('colorjitter', None) colorold = config.augmentation.get('colorold', False) if rotation > 0: aug.append(transforms.RandomRotation(rotation)) if colorjitter is not None: aug.append(transforms.ColorJitter(*colorjitter)) aug.append(transforms.ToTensor()) if colorold: aug.append(ColorAugmentation()) aug.append(normalize) # train train_dataset = McDataset(config.train_root, config.train_source, transforms.Compose(aug), fake=args.fake) # val val_dataset = McDataset( config.val_root, config.val_source, transforms.Compose([ transforms.Resize(config.augmentation.test_resize), transforms.CenterCrop(config.augmentation.input_size), transforms.ToTensor(), normalize, ]), args.fake) train_sampler = DistributedGivenIterationSampler( train_dataset, config.lr_scheduler.max_iter, config.batch_size, last_iter=last_iter) val_sampler = DistributedSampler(val_dataset, round_up=False) train_loader = DataLoader(train_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.workers, pin_memory=True, sampler=train_sampler) val_loader = DataLoader(val_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.workers, pin_memory=True, sampler=val_sampler) config.lr_scheduler['optimizer'] = optimizer.optimizer if isinstance( optimizer, FP16SGD) else optimizer config.lr_scheduler['last_iter'] = last_iter lr_scheduler = get_scheduler(config.lr_scheduler) if rank == 0: tb_logger = SummaryWriter(config.save_path + '/events') logger = create_logger('global_logger', config.save_path + '/log.txt') logger.info('args: {}'.format(pprint.pformat(args))) logger.info('config: {}'.format(pprint.pformat(config))) else: tb_logger = None if args.evaluate: if args.fusion_list is not None: validate(val_loader, model, fusion_list=args.fusion_list, fuse_prob=args.fuse_prob) else: validate(val_loader, model) link.finalize() return train(train_loader, val_loader, model, optimizer, lr_scheduler, last_iter + 1, tb_logger) link.finalize()
def main(): global args, best_prec1, timer args = parser.parse_args() rank, world_size = dist_init(args.port) assert (args.batch_size % world_size == 0) assert (args.workers % world_size == 0) args.batch_size = args.batch_size // world_size args.workers = args.workers // world_size # step1: create model print("=> creating model '{}'".format(args.arch)) if args.arch.startswith('inception_v3'): print('inception_v3 without aux_logits!') image_size = 341 input_size = 299 model = models.__dict__[args.arch](aux_logits=False) elif args.arch.startswith('ir18'): image_size = 640 input_size = 448 model = IR18() else: image_size = 256 input_size = 224 model = models.__dict__[args.arch]() if args.pretrained: print("=> using pre-trained model '{}'".format(args.arch)) if os.path.isfile(args.pretrained): print("=> loading pretrained_model '{}'".format(args.pretrained)) pretrained_model = torch.load(args.pretrained) model.load_state_dict(pretrained_model['state_dict'], strict=False) print("=> loaded pretrained_model '{}'".format(args.pretrained)) else: print("=> no checkpoint found at '{}'".format(args.pretrained)) model.cuda() model = DistModule(model) # step2: define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True # step3: Data loading code normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_dataset = McDataset( args.train_root, args.train_source, transforms.Compose([ transforms.RandomResizedCrop(input_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # ColorAugmentation(), # normalize, ])) val_dataset = McDataset( args.val_root, args.val_source, transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(input_size), transforms.ToTensor(), # normalize, ])) train_sampler = DistributedSampler(train_dataset) val_sampler = DistributedSampler(val_dataset) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False, sampler=train_sampler) val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False, sampler=val_sampler) if args.evaluate: validate(val_loader, model, criterion) return timer = Timer( len(train_loader) + len(val_loader), args.epochs - args.start_epoch) for epoch in range(args.start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch) train_sampler.set_epoch(epoch) # train for one epoch train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set prec1 = validate(val_loader, model, criterion) if rank == 0: # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) save_checkpoint( { 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, is_best, args.save_path) print('* Best Prec 1: {best:.3f}'.format(best=best_prec1))
def main(): global writer, best_prec1 if not os.path.exists('{}/checkpoints'.format(args.ckpt)): os.makedirs('{}/checkpoints'.format(args.ckpt)) if not os.path.exists('{}/images'.format(args.ckpt)): os.makedirs('{}/images'.format(args.ckpt)) if not os.path.exists('{}/logs'.format(args.ckpt)): os.makedirs('{}/logs'.format(args.ckpt)) if not os.path.exists('{}/plots'.format(args.ckpt)): os.makedirs('{}/plots'.format(args.ckpt)) if os.path.exists('{}/runs'.format(args.ckpt)): shutil.rmtree('{}/runs'.format(args.ckpt)) os.makedirs('{}/runs'.format(args.ckpt)) logger = create_logger('global_logger', '{}/logs/{}.txt'.format(args.ckpt,time.time())) logger.info('{}'.format(args)) writer = SummaryWriter('{}/runs'.format(args.ckpt)) #net = nn.parallel.distributed.DistributedDataParallel(net) # build dataset data_dir = args.data_dir data_list = args.data_list val_data_dir = args.val_data_dir val_data_list = args.val_data_list train_dataset = McDataset(data_dir, data_list) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=args.bs, shuffle=True, num_workers=1, pin_memory=True, collate_fn=fast_collate) val_dataset = McDataset(val_data_dir, val_data_list) val_loader = torch.utils.data.DataLoader( val_dataset, batch_size=args.bs, shuffle=True, num_workers=4, pin_memory=True, collate_fn=fast_collate) # create model print("=> creating model '{}'".format(args.net)) net = model.Net(classnum=args.classnum, feature_dim=2, head=args.loss_type, radius=args.radius, sample_feat=args.sample_feat) net = net.cuda() print(net) # build optimizer optimizer = torch.optim.SGD(net.parameters(), lr=args.base_lr, momentum=0.9, weight_decay=5e-4) if args.loss_type == 'a-softmax': criterion = model.AngleLoss(LambdaMax=args.LambdaMax, gamma=args.gamma, power=args.power).cuda() if args.loss_type == 'softmax' or args.loss_type == 'gaussian': criterion = torch.nn.CrossEntropyLoss() start_epoch = 0 best_prec1 = 0 # optionally resume from a pretrained model if args.evaluate: model_path = os.path.join(args.ckpt, 'checkpoints', args.evaluate) checkpoint = torch.load(model_path) epoch = int(checkpoint['epoch']) net.load_state_dict(checkpoint['state_dict']) val_feat, val_target, prec1 = validate(net, val_loader, criterion, epoch) print('Prec1: {}'.format(prec1)) return if args.resume: model_path = os.path.join(args.ckpt, 'checkpoints', args.resume) checkpoint = torch.load(model_path) start_epoch = int(checkpoint['epoch']) best_prec1 = float(checkpoint['best_prec1']) net.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) if args.pretrained: start_epoch = 0 ## start training net.train() freq = args.print_freq end = time.time() for epoch in range(start_epoch,args.epochs): train_feat, train_target, train_prec1 = train(net, epoch, train_loader, args, criterion, optimizer) val_feat, val_target, prec1 = validate(net, val_loader, criterion, epoch) #pdb.set_trace() if (epoch+1) % args.vis_freq == 0: visualize(train_feat, train_target, val_feat, val_target, epoch, args, train_prec1, prec1) is_best = prec1>best_prec1 best_prec1 = max(prec1, best_prec1) save_checkpoint({ 'epoch': epoch+1, 'state_dict': net.state_dict(), 'optimizer': optimizer.state_dict(), 'best_prec1': best_prec1 },args, is_best) print('Best Prec1: {}'.format(best_prec1)) writer.close()