def main(): test_seen_loader = torch.utils.data.DataLoader(AttributeDataset( args.data_dir, args.dataset, features_path=args.gan_path, mode='test_seen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) test_unseen_loader = torch.utils.data.DataLoader( AttributeDataset(args.data_dir, args.dataset, features_path=args.gan_path, mode='test_unseen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) # instanciate the models if args.mlp: mlp = MLP(args.dim_input, [args.nhidden * 2], args.nhidden) else: mlp = LinearProjection(args.dim_input, args.nhidden) embed = LinearProjection(args.nhidden, args.dim_embed) if args.sentences: cam_key = 'sentences' else: cam_key = 'emb' if args.gan_path is not None: cam_key = 'full_' + cam_key cam = torch.from_numpy(test_seen_loader.dataset.data[cam_key].T) proxies = ProxyNet(args.n_classes, args.dim_embed, proxies=cam) model = Base(mlp, embed, proxies) criterion = ProxyLoss(temperature=args.temp) if args.cuda: mlp.cuda() embed.cuda() model.cuda() proxies.cuda() # loading checkpoint = torch.load(args.model_path) model.load_state_dict(checkpoint['state_dict']) txt = ("=> loaded checkpoint '{}' (epoch {})".format( args.model_path, checkpoint['epoch'])) print(txt) compute_scores(test_seen_loader, test_unseen_loader, model, criterion)
import cv2 import os import numpy as np import torch from torch.utils.data.dataloader import DataLoader from dataset import KittiRoadTestDataset from transform import Resize from torchvision.transforms import ToTensor from models import Vgg11, Fcn, ProxyNet if __name__ == '__main__': ITERATION, BATCH_SIZE, CLASSES_NUM = 50, 1, 2 HEIGHT, WIDTH = 288, 800 vgg11 = Vgg11() fineTuringNet = ProxyNet('vgg11', vgg11.features) model = Fcn(scale=8, featureProxyNet=fineTuringNet, classesNum=CLASSES_NUM) # 加载模型 state_dict = torch.load('fcn_road_segment.pth') model.load_state_dict(state_dict) testSet = KittiRoadTestDataset( path='./data_road/training', type='um', transforms=[ Resize(HEIGHT, WIDTH, cv2.INTER_LINEAR), # HistogramNormalize(), # Mixup(shadowImg, random_translation=False, random_rotation=False), ToTensor(), ]) testLoader = DataLoader(testSet, batch_size=1, shuffle=True, num_workers=0)
def train_proxy(opt): logging.info(opt) # Set random seed mx.random.seed(opt.seed) np.random.seed(opt.seed) # Setup computation context context = get_context(opt.gpus, logging) run_results = [] # Adjust batch size to each compute context batch_size = opt.batch_size * len(context) # Prepare feature extractor if opt.model == 'inception-bn': feature_net, feature_params = get_feature_model(opt.model, ctx=context) data_shape = 224 scale_image_data = False elif opt.model == 'resnet50_v2': feature_net = mx.gluon.model_zoo.vision.resnet50_v2( pretrained=True, ctx=context).features data_shape = 224 scale_image_data = True else: raise RuntimeError('Unsupported model: %s' % opt.model) # Prepare datasets train_dataset, val_dataset = get_dataset(opt.dataset, opt.data_path, data_shape=data_shape, use_crops=opt.use_crops, use_aug=True, with_proxy=True, scale_image_data=scale_image_data) logging.info('Training with %d classes, validating with %d classes' % (train_dataset.num_classes(), val_dataset.num_classes())) if opt.iteration_per_epoch > 0: train_dataset, _ = get_dataset_iterator( opt.dataset, opt.data_path, batch_k=(opt.batch_size // 3) if opt.loss == 'xentropy' else opt.batch_k, batch_size=opt.batch_size, data_shape=data_shape, use_crops=opt.use_crops, scale_image_data=scale_image_data, batchify=False) train_dataloader = mx.gluon.data.DataLoader( DatasetIterator(train_dataset, opt.iteration_per_epoch, 'next_proxy_sample', call_params={ 'sampled_classes': (opt.batch_size // opt.batch_k) if (opt.batch_k is not None) else None, 'chose_classes_randomly': True, }), batch_size=1, shuffle=False, num_workers=opt.num_workers, last_batch='keep') else: train_dataloader = mx.gluon.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, num_workers=opt.num_workers, last_batch='rollover') val_dataloader = mx.gluon.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=opt.num_workers, last_batch='keep') # Prepare proxy model net = ProxyNet(feature_net, opt.embed_dim, num_classes=train_dataset.num_classes()) if opt.lr is None: logging.info('Using variable learning rate') opt.lr = max([opt.lr_proxynca, opt.lr_embedding, opt.lr_inception]) for p, v in net.encoder.collect_params().items(): v.lr_mult = opt.lr_embedding / opt.lr for p, v in net.base_net.collect_params().items(): v.lr_mult = opt.lr_inception / opt.lr for p, v in net.proxies.collect_params().items(): v.lr_mult = opt.lr_proxynca / opt.lr else: logging.info('Using single learning rate: %f' % opt.lr) for run in range(1, opt.number_of_runs + 1): logging.info('Starting run %d/%d' % (run, opt.number_of_runs)) # reset networks if opt.model == 'inception-bn': net.base_net.collect_params().load(feature_params, ctx=context, ignore_extra=True) if opt.dataset == 'CUB': for v in net.base_net.collect_params().values(): if v.name in ['batchnorm', 'bn_']: v.grad_req = 'null' elif opt.model == 'resnet50_v2': logging.info('Lowering LR for Resnet backbone') net.base_net = mx.gluon.model_zoo.vision.resnet50_v2( pretrained=True, ctx=context).features # Use a smaller learning rate for pre-trained convolutional layers. for v in net.base_net.collect_params().values(): if 'conv' in v.name: setattr(v, 'lr_mult', 0.01) else: raise NotImplementedError('Unknown model: %s' % opt.model) if opt.loss == 'triplet': net.encoder.initialize(mx.init.Xavier(magnitude=0.2), ctx=context, force_reinit=True) net.proxies.initialize(mx.init.Xavier(magnitude=0.2), ctx=context, force_reinit=True) else: net.init(TruncNorm(stdev=0.001), ctx=context, init_basenet=False) if not opt.disable_hybridize: net.hybridize() run_result = train(net, opt, train_dataloader, val_dataloader, context, run) run_results.append(run_result) logging.info('Run %d finished with %f' % (run, run_result[0][1])) logging.info( 'Average validation of %d runs:\n%s' % (opt.number_of_runs, format_results(average_results(run_results))))
def train_dreml(opt): logging.info(opt) # Set random seed mx.random.seed(opt.seed) np.random.seed(opt.seed) # Setup computation context context = get_context(opt.gpus, logging) cpu_ctx = mx.cpu() # Adjust batch size to each compute context batch_size = opt.batch_size * len(context) if opt.model == 'inception-bn': scale_image_data = False elif opt.model in ['resnet50_v2', 'resnet18_v2']: scale_image_data = True else: raise RuntimeError('Unsupported model: %s' % opt.model) # Prepare datasets train_dataset, val_dataset = get_dataset(opt.dataset, opt.data_path, data_shape=opt.data_shape, use_crops=opt.use_crops, use_aug=True, with_proxy=True, scale_image_data=scale_image_data, resize_img=int(opt.data_shape * 1.1)) # Create class mapping mapping = np.random.randint(0, opt.D, (opt.L, train_dataset.num_classes())) # Train embedding functions one by one trained_models = [] best_results = [] # R@1, NMI for ens in tqdm(range(opt.L), desc='Training model in ensemble'): train_dataset.set_class_mapping(mapping[ens], opt.D) train_dataloader = mx.gluon.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=opt.num_workers, last_batch='rollover') if opt.model == 'inception-bn': feature_net, feature_params = get_feature_model(opt.model, ctx=context) elif opt.model == 'resnet50_v2': feature_net = mx.gluon.model_zoo.vision.resnet50_v2(pretrained=True, ctx=context).features elif opt.model == 'resnet18_v2': feature_net = mx.gluon.model_zoo.vision.resnet18_v2(pretrained=True, ctx=context).features else: raise RuntimeError('Unsupported model: %s' % opt.model) if opt.static_proxies: net = EmbeddingNet(feature_net, opt.D, normalize=False) else: net = ProxyNet(feature_net, opt.D, num_classes=opt.D) # Init loss function if opt.static_proxies: logging.info('Using static proxies') proxyloss = StaticProxyLoss(opt.D) elif opt.loss == 'nca': logging.info('Using NCA loss') proxyloss = ProxyNCALoss(opt.D, exclude_positives=True, label_smooth=opt.label_smooth, multiplier=opt.embedding_multiplier) elif opt.loss == 'triplet': logging.info('Using triplet loss') proxyloss = ProxyTripletLoss(opt.D) elif opt.loss == 'xentropy': logging.info('Using NCA loss without excluding positives') proxyloss = ProxyNCALoss(opt.D, exclude_positives=False, label_smooth=opt.label_smooth, multiplier=opt.embedding_multiplier) else: raise RuntimeError('Unknown loss function: %s' % opt.loss) # Init optimizer opt_options = {'learning_rate': opt.lr, 'wd': opt.wd} if opt.optimizer == 'sgd': opt_options['momentum'] = 0.9 elif opt.optimizer == 'adam': opt_options['epsilon'] = opt.epsilon elif opt.optimizer == 'rmsprop': opt_options['gamma1'] = 0.9 opt_options['epsilon'] = opt.epsilon # Calculate decay steps steps = parse_steps(opt.steps, opt.epochs, logger=logging) # reset networks if opt.model == 'inception-bn': net.base_net.collect_params().load(feature_params, ctx=context, ignore_extra=True) elif opt.model in ['resnet18_v2', 'resnet50_v2']: net.base_net = mx.gluon.model_zoo.vision.get_model(opt.model, pretrained=True, ctx=context).features else: raise NotImplementedError('Unknown model: %s' % opt.model) if opt.static_proxies: net.init(mx.init.Xavier(magnitude=0.2), ctx=context, init_basenet=False) elif opt.loss == 'triplet': net.encoder.initialize(mx.init.Xavier(magnitude=0.2), ctx=context, force_reinit=True) net.proxies.initialize(mx.init.Xavier(magnitude=0.2), ctx=context, force_reinit=True) else: net.init(TruncNorm(stdev=0.001), ctx=context, init_basenet=False) if not opt.disable_hybridize: net.hybridize() trainer = mx.gluon.Trainer(net.collect_params(), opt.optimizer, opt_options, kvstore=opt.kvstore) smoothing_constant = .01 # for tracking moving losses moving_loss = 0 for epoch in range(1, opt.epochs + 1): p_bar = tqdm(enumerate(train_dataloader), total=len(train_dataloader), desc=('[Model %d/%d] Epoch %d' % (ens + 1, opt.L, epoch))) new_lr = get_lr(opt.lr, epoch, steps, opt.factor) logging.info('Setting LR to %f' % new_lr) trainer.set_learning_rate(new_lr) for i, batch in p_bar: data = mx.gluon.utils.split_and_load(batch[0], ctx_list=context, batch_axis=0, even_split=False) label = mx.gluon.utils.split_and_load(batch[1], ctx_list=context, batch_axis=0, even_split=False) negative_labels = mx.gluon.utils.split_and_load(batch[2], ctx_list=context, batch_axis=0, even_split=False) with ag.record(): losses = [] for x, y, nl in zip(data, label, negative_labels): if opt.static_proxies: embs = net(x) losses.append(proxyloss(embs, y)) else: embs, positive_proxy, negative_proxies, proxies = net(x, y, nl) if opt.loss in ['nca', 'xentropy']: losses.append(proxyloss(embs, proxies, y, nl)) else: losses.append(proxyloss(embs, positive_proxy, negative_proxies)) for l in losses: l.backward() trainer.step(data[0].shape[0]) ########################## # Keep a moving average of the losses ########################## curr_loss = mx.nd.mean(mx.nd.maximum(mx.nd.concatenate(losses), 0)).asscalar() moving_loss = (curr_loss if ((i == 0) and (epoch == 1)) # starting value else (1 - smoothing_constant) * moving_loss + smoothing_constant * curr_loss) p_bar.set_postfix_str('Moving loss: %.4f' % moving_loss) logging.info('Moving loss: %.4f' % moving_loss) # move model to CPU mx.nd.waitall() net.collect_params().reset_ctx(cpu_ctx) trained_models.append(net) del train_dataloader # Run ensemble validation logging.info('Running validation with %d models in the ensemble' % len(trained_models)) val_dataloader = mx.gluon.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=opt.num_workers, last_batch='keep') validation_results = validate(val_dataloader, trained_models, context, opt.static_proxies) for name, val_acc in validation_results: logging.info('Validation: %s=%f' % (name, val_acc)) if (len(best_results) == 0) or (validation_results[0][1] > best_results[0][1]): best_results = validation_results logging.info('New best validation: R@1: %f NMI: %f' % (best_results[0][1], best_results[-1][1]))
def main(): # data normalization input_size = 224 normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # data loaders kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {} if args.da: train_transforms = transforms.Compose([ random_transform, transforms.ToPILImage(), transforms.Resize((input_size, input_size)), transforms.ToTensor(), normalize ]) else: train_transforms = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((input_size, input_size)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize ]) test_transforms = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((input_size, input_size)), transforms.ToTensor(), normalize ]) train_loader = torch.utils.data.DataLoader(DataLoader(df_train, train_transforms, root=args.data_dir, mode=args.mode), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader(DataLoader(df_gal, test_transforms, root=args.data_dir, mode=args.mode), batch_size=args.batch_size, shuffle=False, **kwargs) # instanciate the models output_shape, backbone = get_backbone(args) embed = LinearProjection(output_shape, args.dim_embed) model = ConvNet(backbone, embed) # instanciate the proxies fsem = get_semantic_fname(args.word) path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem) train_proxies = get_proxies(path_semantic, df_train['cat'].cat.categories) test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories) train_proxynet = ProxyNet(args.n_classes, args.dim_embed, proxies=torch.from_numpy(train_proxies)) test_proxynet = ProxyNet(args.n_classes_gal, args.dim_embed, proxies=torch.from_numpy(test_proxies)) # criterion criterion = ProxyLoss(args.temperature) if args.multi_gpu: model = nn.DataParallel(model) if args.cuda: backbone.cuda() embed.cuda() model.cuda() train_proxynet.cuda() test_proxynet.cuda() parameters_set = [] low_layers = [] upper_layers = [] for c in backbone.children(): low_layers.extend(list(c.parameters())) for c in embed.children(): upper_layers.extend(list(c.parameters())) parameters_set.append({ 'params': low_layers, 'lr': args.lr * args.factor_lower }) parameters_set.append({'params': upper_layers, 'lr': args.lr * 1.}) optimizer = optim.SGD(parameters_set, lr=args.lr, momentum=0.9, nesterov=True, weight_decay=args.wd) n_parameters = sum([p.data.nelement() for p in model.parameters()]) print(' + Number of params: {}'.format(n_parameters)) scheduler = CosineAnnealingLR(optimizer, args.epochs * len(train_loader), eta_min=3e-6) print('Starting training...') for epoch in range(args.start_epoch, args.epochs + 1): # update learning rate scheduler.step() # train for one epoch train(train_loader, model, train_proxynet.proxies.weight, criterion, optimizer, epoch, scheduler) val_acc = evaluate(test_loader, model, test_proxynet.proxies.weight, criterion) # saving if epoch == args.epochs: save_checkpoint({'epoch': epoch, 'state_dict': model.state_dict()}) print('\nResults on test set (end of training)') write_logs('\nResults on test set (end of training)') test_acc = evaluate(test_loader, model, test_proxynet.proxies.weight, criterion)
def main(): # data normalization input_size = 224 normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # data loaders kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {} test_transforms = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((input_size, input_size)), transforms.ToTensor(), normalize ]) feats = {} labels = {} for domain in ['im', 'sk']: key = '_'.join([domain, 'model_path']) dirname = os.path.dirname(args.__dict__[key]) fpath = os.path.join(dirname, 'features.npz') results_path = os.path.join(dirname, 'results.txt') if os.path.isfile(fpath) and args.rewrite is False: data = np.load(fpath) feats[domain] = data['features'] labels[domain] = data['labels'] txt = ('Domain (%s): Acc %.2f' % (domain, data['acc'] * 100.)) print(txt) write_logs(txt, results_path) df_gal = splits[domain]['gal'] fsem = get_semantic_fname(args.word) path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem) test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories) else: df_gal = splits[domain]['gal'] test_loader = torch.utils.data.DataLoader( DataLoader(df_gal, test_transforms, root=args.data_dir, mode=domain), batch_size=args.batch_size * 10, shuffle=False, **kwargs) # instanciate the models output_shape, backbone = get_backbone(args) embed = LinearProjection(output_shape, args.dim_embed) model = ConvNet(backbone, embed) # instanciate the proxies fsem = get_semantic_fname(args.word) path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem) test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories) test_proxynet = ProxyNet(args.n_classes_gal, args.dim_embed, proxies=torch.from_numpy(test_proxies)) # criterion criterion = ProxyLoss(args.temperature) if args.multi_gpu: model = nn.DataParallel(model) # loading checkpoint = torch.load(args.__dict__[key]) model.load_state_dict(checkpoint['state_dict']) txt = ("\n=> loaded checkpoint '{}' (epoch {})".format( args.__dict__[key], checkpoint['epoch'])) print(txt) write_logs(txt, results_path) if args.cuda: backbone.cuda() embed.cuda() model.cuda() test_proxynet.cuda() txt = 'Extracting testing set (%s)...' % (domain) print(txt) x, y, acc = extract_predict(test_loader, model, test_proxynet.proxies.weight, criterion) feats[domain] = x labels[domain] = y np.savez(fpath, features=feats[domain], labels=labels[domain], acc=acc) fpath_train = os.path.join(dirname, 'features_train.npz') if args.train and not os.path.isfile(fpath_train): df_train = splits[domain]['train'] train_loader = torch.utils.data.DataLoader( DataLoader(df_train, test_transforms, root=args.data_dir, mode=domain), batch_size=args.batch_size * 10, shuffle=False, **kwargs) train_proxies = get_proxies(path_semantic, df_train['cat'].cat.categories) train_proxynet = ProxyNet( args.n_classes_gal, args.dim_embed, proxies=torch.from_numpy(train_proxies)) train_proxynet.cuda() txt = 'Extracting training set (%s)...' % (domain) print(txt) x, y, _ = extract_predict(train_loader, model, train_proxynet.proxies.weight, criterion) fpath = os.path.join(dirname, 'features_train.npz') np.savez(fpath, features=feats[domain], features_train=x, labels=labels[domain], labels_train=y, acc=acc) txt = ('Domain (%s): Acc %.2f' % (domain, acc * 100.)) print(txt) write_logs(txt, results_path) if args.shape: print('\nRetrieval per model') new_feat_im, new_labels_im = average_views(splits['im']['test'], feats['im'], labels['im']) idx = retrieve(feats['sk'], new_feat_im) metrics = score_shape(labels['sk'], new_labels_im, idx) names = ['NN', 'FT', 'ST', 'E', 'nDCG', 'mAP'] txt = [('%s %.3f' % (name, value)) for name, value in zip(names, metrics)] txt = '\t'.join(txt) print(txt) write_logs(txt, results_path) print('\nRetrieval per model with refinement') alpha = 0.4 g_sk_x = KNN(feats['sk'], new_feat_im, K=1, mode='ones') new_sk_x = slerp(alpha, L2norm(feats['sk']), L2norm(g_sk_x)) idx = retrieve(new_sk_x, new_feat_im) metrics = score_shape(labels['sk'], new_labels_im, idx) names = ['NN', 'FT', 'ST', 'E', 'nDCG', 'mAP'] txt = [('%s %.3f' % (name, value)) for name, value in zip(names, metrics)] txt = '\t'.join(txt) print(txt) write_logs(txt, results_path) else: print('\nRetrieval') txt = evaluate(feats['im'], labels['im'], feats['sk'], labels['sk']) print(txt) write_logs(txt, results_path) print('\nRetrieval with refinement') if args.overwrite: alpha = 0.7 else: alpha = 0.4 g_sk_x = KNN(feats['sk'], feats['im'], K=1, mode='ones') new_sk_x = slerp(alpha, L2norm(feats['sk']), L2norm(g_sk_x)) txt = evaluate(feats['im'], labels['im'], new_sk_x, labels['sk']) print(txt) write_logs(txt, results_path)
def main(): # data normalization normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # data loaders kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {} test_transforms = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((224, 224)), transforms.ToTensor(), normalize ]) feats = {} labels = {} for domain in ['im', 'sk']: key = '_'.join([domain, 'model_path']) dirname = os.path.dirname(args.__dict__[key]) fpath = os.path.join(dirname, 'features.npz') results_path = os.path.join(dirname, 'results.txt') if os.path.isfile(fpath) and args.rewrite is False: data = np.load(fpath) feats[domain] = data['features'] labels[domain] = data['labels'] txt = ('Domain (%s): Acc %.2f' % (domain, data['acc'] * 100.)) print(txt) write_logs(txt, results_path) df_gal = splits[domain]['test'] fsem = get_semantic_fname(args.word) path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem) test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories) else: df_gal = splits[domain]['test'] test_loader = torch.utils.data.DataLoader( DataLoader(df_gal, test_transforms, root=args.data_dir, mode=domain), batch_size=args.batch_size * 1, shuffle=False, **kwargs) # instanciate the models output_shape, backbone = get_backbone(args) embed = LinearProjection(output_shape, args.dim_embed) model = ConvNet(backbone, embed) # instanciate the proxies fsem = get_semantic_fname(args.word) path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem) test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories) test_proxynet = ProxyNet(args.n_classes_gal, args.dim_embed, proxies=torch.from_numpy(test_proxies)) # criterion criterion = ProxyLoss(args.temperature) if args.multi_gpu: model = nn.DataParallel(model) # loading checkpoint = torch.load(args.__dict__[key]) model.load_state_dict(checkpoint['state_dict']) txt = ("\n=> loaded checkpoint '{}' (epoch {})".format( args.__dict__[key], checkpoint['epoch'])) print(txt) if args.cuda: backbone.cuda() embed.cuda() model.cuda() test_proxynet.cuda() txt = 'Extracting testing set (%s)...' % (domain) print(txt) x, y, acc = extract_predict(test_loader, model, test_proxynet.proxies.weight, criterion) feats[domain] = x labels[domain] = y np.savez(fpath, features=feats[domain], labels=labels[domain], acc=acc) txt = ('Domain (%s): Acc %.2f' % (domain, acc * 100.)) print(txt) print('\nFew-Shot') fs(feats, labels, test_proxies)
def main(): if args.gan_path is None: both = False else: both = True if args.validation: train_loader = torch.utils.data.DataLoader(AttributeDataset( args.data_dir, args.dataset, features_path=args.gan_path, mode='train', both=both, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=True) val_seen_loader = torch.utils.data.DataLoader( AttributeDataset(args.data_dir, args.dataset, features_path=args.gan_path, mode='val_seen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) val_unseen_loader = torch.utils.data.DataLoader( AttributeDataset(args.data_dir, args.dataset, features_path=args.gan_path, mode='val_unseen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) else: trainval_loader = torch.utils.data.DataLoader( AttributeDataset(args.data_dir, args.dataset, features_path=args.gan_path, mode='trainval', both=both, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=True) test_seen_loader = torch.utils.data.DataLoader(AttributeDataset( args.data_dir, args.dataset, features_path=args.gan_path, mode='test_seen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) test_unseen_loader = torch.utils.data.DataLoader( AttributeDataset(args.data_dir, args.dataset, features_path=args.gan_path, mode='test_unseen', generalized=True, normalize=args.normalize, sentences=args.sentences), batch_size=args.batch_size, shuffle=False) # instanciate the models if args.mlp: mlp = MLP(args.dim_input, [args.nhidden * 2], args.nhidden) else: mlp = LinearProjection(args.dim_input, args.nhidden) embed = LinearProjection(args.nhidden, args.dim_embed) if args.sentences: cam_key = 'sentences' else: cam_key = 'emb' if args.validation: cam = torch.from_numpy(train_loader.dataset.data[cam_key].T) else: cam = torch.from_numpy(trainval_loader.dataset.data[cam_key].T) proxies = ProxyNet(args.n_classes, args.dim_embed, proxies=cam) model = Base(mlp, embed, proxies) criterion = ProxyLoss(temperature=args.temp) if args.cuda: mlp.cuda() embed.cuda() model.cuda() proxies.cuda() parameters_set = [] layers = [] for c in mlp.children(): if isinstance(c, nn.Linear) or isinstance(c, nn.ModuleList): layers.extend(list(c.parameters())) for c in embed.children(): if isinstance(c, nn.Linear): layers.extend(list(c.parameters())) parameters_set.append({'params': layers, 'lr': args.lr}) optimizer = optim.SGD(parameters_set, lr=args.lr, momentum=0.9, nesterov=True, weight_decay=5e-5) n_parameters = sum([p.data.nelement() for p in model.parameters()]) print(' + Number of params: {}'.format(n_parameters)) scheduler = CosineAnnealingLR(optimizer, args.epochs) best_acc = 0 print('Random results:') if args.validation: validate(val_seen_loader, val_unseen_loader, model, criterion) else: validate(test_seen_loader, test_unseen_loader, model, criterion) for epoch in range(args.start_epoch, args.epochs + 1): # update learning rate if args.lr_decay: scheduler.step() # train for one epoch if args.validation: train(train_loader, model, criterion, optimizer, epoch) validate(val_seen_loader, val_unseen_loader, model, criterion) else: train(trainval_loader, model, criterion, optimizer, epoch) validate(test_seen_loader, test_unseen_loader, model, criterion) # saving save_checkpoint({'epoch': epoch, 'state_dict': model.state_dict()}) print('\nFinal evaluation on last epoch model:') validate(test_seen_loader, test_unseen_loader, model, criterion)