def main(): cuda = torch.cuda.is_available() anchor_transform = transforms.Compose([ transforms.RandomAffine(degrees=90, translate=(0.25, 0.25)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.CenterCrop(128), transforms.Resize(IMG_SIZE), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) train_transforms = transforms.Compose([ transforms.Resize(IMG_SIZE), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Let's use 12 while developing as it reduces the start time. dset_train = GeoTileDataset(TILE_FILE, transform=train_transforms, center_transform=anchor_transform) pd_files = dset_train.get_file_df() weights = pd_files.frequency train_sampler = WeightedRandomSampler(weights, len(dset_train)) # Should numworkers be 1? kwargs = {'num_workers': 8, 'pin_memory': True} if cuda else {} online_train_loader = DataLoader(dset_train, batch_size=BATCH_SIZE, sampler=train_sampler, **kwargs) model = Loc2Vec() if cuda: model.cuda() loss_fn = OnlineTripletLoss(MARGIN, HardestNegativeTripletSelector(MARGIN), SemihardNegativeTripletSelector(MARGIN), RandomNegativeTripletSelector(MARGIN)) optimizer = optim.Adam(model.parameters(), lr=1e-3) scheduler = lr_scheduler.StepLR(optimizer, 16, gamma=0.1, last_epoch=-1) # Mixed precision training model, optimizer = amp.initialize(model, optimizer, opt_level="O1") # if torch.cuda.device_count() > 1: # print("Let's use", torch.cuda.device_count(), "GPUs!") # model = nn.DataParallel(model) fit(online_train_loader, online_train_loader, model, loss_fn, optimizer, scheduler, N_EPOCHS, cuda, LOG_INTERVAL)
def __init__(self, config, w_embedding, c_embedding=None): self.config = config self.model = Hi_Attention(self.config, w_embedding, c_embedding) self.classification_net = ClassificationNet(self.config) self.triplet_selector = HardestNegativeTripletSelector(self.config.margin) self.online_triplet_loss = OnlineTripletLoss(self.config.margin, self.triplet_selector) self.optim = optim.Adam(self.model.parameters(), lr = config.lr, weight_decay = config.weight_decay) self.criterion = nn.CrossEntropyLoss() if config.use_gpu: self.model = self.model.cuda() self.classification_net = self.classification_net.cuda()
def __init__(self, config, w_embedding, c_embedding=None, l_embedding=None): self.config = config self.l_embedding = l_embedding self.model = Hi_Attention(self.config, w_embedding = w_embedding, c_embedding = c_embedding) self.classification_net = ClassificationNet(self.config) self.triplet_selector = HardestNegativeTripletSelector(self.config.margin) self.online_triplet_loss = OnlineTripletLoss(self.config.margin, self.triplet_selector) ignored_params = list(map(id, self.model.elmo._scalar_mixes[0].parameters())) base_params = filter(lambda p: id(p) not in ignored_params, self.model.parameters()) self.optim = optim.Adam( [{'params': base_params}, {'params': self.model.elmo._scalar_mixes[0].parameters(), "lr": 1e-2}], lr = self.config.lr, weight_decay = config.weight_decay) # self.optim = optim.Adam(self.model.parameters(), lr = config.lr, weight_decay = config.weight_decay) self.criterion = nn.CrossEntropyLoss() if config.use_gpu: self.model = self.model.cuda() self.classification_net = self.classification_net.cuda()
def train(conf): """Total training procedure. """ data_loader = DataLoader(ImageDataset_SST(conf.data_root, conf.train_file), conf.batch_size, True, num_workers=4) conf.device = torch.device('cuda:0') #criterion = OnlineContrastiveLoss(margin=2.5, pair_selector=HardNegativePairSelector(cpu=False)).cuda(torch.device('cuda:0')) triplet_selector = FunctionNegativeTripletSelector( margin=2.5, negative_selection_fn=random_hard_negative, cpu=False) criterion = OnlineTripletLoss(margin=2.5, triplet_selector=triplet_selector).cuda( conf.device) backbone_factory = BackboneFactory(conf.backbone_type, conf.backbone_conf_file) model = backbone_factory.get_backbone() if conf.resume: model.load_state_dict(torch.load(args.pretrain_model)) model = torch.nn.DataParallel(model).cuda() parameters = [p for p in model.parameters() if p.requires_grad] optimizer = optim.SGD(parameters, lr=conf.lr, momentum=conf.momentum, weight_decay=1e-4) lr_schedule = optim.lr_scheduler.MultiStepLR(optimizer, milestones=conf.milestones, gamma=0.1) loss_meter = AverageMeter() model.train() for epoch in range(conf.epoches): train_one_epoch(data_loader, model, optimizer, criterion, epoch, loss_meter, conf) lr_schedule.step() if conf.evaluate: conf.evaluator.evaluate(model)
query_set, batch_sampler=test_batch_sampler, **kwargs) print('dataloaders built') # Build model and load checkpoint # model = build_retriever(cfg.model) model = Vgg16L2(num_dim=128) model.cuda() print('model built') # Set up the network and training parameters from losses import OnlineTripletLoss from utils import AllTripletSelector, HardestNegativeTripletSelector, RandomNegativeTripletSelector, SemihardNegativeTripletSelector # Strategies for selecting triplets within a minibatch from metrics import AverageNonzeroTripletsMetric margin = 1. loss_fn = OnlineTripletLoss(margin, SemihardNegativeTripletSelector(margin)) lr = 1e-4 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = 100 log_interval = 50 print('start training') torch.autograd.set_detect_anomaly(True) fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs,
cudnn.benchmark = True if args.resume: # Load checkpoint. print('==> Resuming from checkpoint..') assert os.path.isdir( 'checkpoint'), 'Error: no checkpoint directory found!' #if os.path.isfile('./checkpoint/triplet_ckpt.t7'): if True: checkpoint = torch.load('./checkpoint/triplet_ckpt.t7') best_acc = checkpoint['acc'] else: checkpoint = torch.load('./checkpoint/classification_ckpt.t7') best_acc = 0 net.load_state_dict(checkpoint['net']) start_epoch = checkpoint['epoch'] print('the current best acc is %.3f on epoch %d' % (best_acc, start_epoch)) criterion = OnlineTripletLoss(margin=1, triplet_selector=AllTripletSelector()) optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) for epoch in range(start_epoch, start_epoch + args.epochs): train(epoch) test(epoch)
train_dataset, batch_sampler=train_batch_sampler, **kwargs) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) # 使用的网络和损失函数,以及数据选择的类 from networks import HardNet from losses import OnlineTripletLoss from utils import AllTripletSelector, HardestNegativeTripletSelector, RandomNegativeTripletSelector # 多种样本选择方式 # 参数配置 margin = 1. embedding_net = HardNet() model = embedding_net if cuda: model.cuda() loss_fn = OnlineTripletLoss(margin, HardestNegativeTripletSelector(margin)) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = 100 log_interval = 10 # 训练 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda,
if args.network == 'resnext': model = EmbeddingResNeXt(args.tuning) if args.network == 'googlenet': model = EmbeddingGoogleNet(args.tuning) if args.classify: model.classification_setup( args.train_max - 1) # dataset object samples train_max - 1 pids if cuda: model.cuda() if args.classify: loss_fn = torch.nn.CrossEntropyLoss() else: margin = args.margin if args.triplets == 'batch-hard': loss_fn = OnlineTripletLoss(margin, HardestNegativeTripletSelector(margin)) if args.triplets == 'semi-hard': loss_fn = OnlineTripletLoss( margin, SemihardNegativeTripletSelector(margin)) if args.triplets == 'random-negative': loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.decay) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = args.epochs log_interval = args.log if args.checkpoint: checkpoint = torch.load('checkpoint.pth.tar')
def main(): # print('fsafsdaf:', args.training_dataset, args.arch) print(">> Creating directory if it does not exist:\n>> '{}'".format( args.directory)) if not os.path.exists(args.directory): os.makedirs(args.directory) log_dir = os.path.join(args.directory, 'log') if not os.path.exists(log_dir): os.mkdir(log_dir) params = {'architecture': args.arch, 'pooling': args.pool} n_classes = args.n_classes n_samples = args.n_samples cuda = args.cuda input_size = args.image_size transform, transform_te, transform_label = init_transform(input_size) kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {} online_train_loader, online_test_loader = init_data_loader( args.root, n_classes, n_samples, transform, transform_te, transform_label, kwargs) # Set up the network and training parameters model = init_network(params) parameters = [] # add feature parameters parameters.append({'params': model.features.parameters()}) if cuda: # print('model cuda:', cuda) model.cuda() pos_margin = 1.0 neg_margin = 0.3 # define optimizer if args.optimizer == 'sgd': optimizer = torch.optim.SGD(parameters, args.lr, momentum=args.momentum, weight_decay=args.weight_decay) elif args.optimizer == 'adam': optimizer = torch.optim.Adam(parameters, args.lr, weight_decay=args.weight_decay) metrics = [AverageNonzeroTripletsMetric()] if args.loss.startswith('OnlineContrastiveEucLoss'): loss_fn = OnlineContrastiveEucLoss(pos_margin, neg_margin, HardNegativePairSelector()) elif args.loss.startswith('OnlineContrastiveCosLoss'): loss_fn = OnlineContrastiveCosLoss(args.loss_margin) elif args.loss.startswith('OnlineTriplet'): loss_fn = OnlineTripletLoss( args.loss_margin, HardestNegativeTripletSelector(args.loss_margin)) exp_decay = math.exp(-0.01) scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=exp_decay) writer = SummaryWriter(log_dir=log_dir) writer.add_graph(model.features, torch.ones([1, 3, 224, 224]).cuda()) fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, writer, metrics=metrics, args=args)
def main(args): if os.path.exists('models') is False: os.makedirs('models') # img_list, base_path, item_dict = read_data(args.dataset, args.bbox) img_list, base_path, item_dict = read_data("DeepFashion2", bbox_gt=False) model_save_path = args.model_path # 'models/siames_triplet_df2.pth' # writer = SummaryWriter('runs/fashion_mnist_experiment_1') model = ResNetbasedNet() if os.path.exists(model_save_path): model.load_state_dict(torch.load(model_save_path)) cuda = torch.cuda.is_available() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if cuda: if torch.cuda.device_count() > 1: model = nn.DataParallel(model) model.to(device) kwargs = {'num_workers': 8, 'pin_memory': True} if cuda else {} if not args.phase: train_dataset = DeepFashionDataset(img_list['train'], root=base_path) train_batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.source, n_classes=32, n_samples=4) online_train_loader = torch.utils.data.DataLoader( train_dataset, batch_sampler=train_batch_sampler, **kwargs) test_dataset = DeepFashionDataset(img_list['validation'], root=base_path) test_batch_sampler = BalancedBatchSampler(test_dataset.labels, test_dataset.source, n_classes=32, n_samples=4) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) margin = 1. loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 5, gamma=0.1, last_epoch=-1) n_epochs = 20 log_interval = 200 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, model_save_path, metrics=[AverageNonzeroTripletsMetric()]) else: model.eval() gallery_dataset = DeepFashionDataset(img_list['validation'], root=base_path) acc, query_cnt = get_topK_acc(gallery_dataset, item_dict['validation'], model, cuda, open('retrieval_result.txt', 'a'), 100) np.savetxt('TopK_accuracy.txt', np.concatenate((acc, np.asarray([query_cnt]))), fmt='%1.5f', delimiter=',')
if args.resume: # Load checkpoint. print('==> Resuming from checkpoint..') assert os.path.isdir( 'checkpoint'), 'Error: no checkpoint directory found!' #if os.path.isfile('./checkpoint/triplet_HardestNegativeTripletSelector_ckpt.t7'): if True: checkpoint = torch.load( './checkpoint/triplet_HardestNegativeTripletSelector_ckpt.t7') best_acc = checkpoint['acc'] else: checkpoint = torch.load('./checkpoint/classification_ckpt.t7') best_acc = 0 net.load_state_dict(checkpoint['net']) start_epoch = checkpoint['epoch'] current_best_epoch = start_epoch print('the current best acc is %.3f on epoch %d' % (best_acc, start_epoch)) criterion = OnlineTripletLoss( margin=1, triplet_selector=HardestNegativeTripletSelector(margin=1)) optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) for epoch in range(start_epoch, start_epoch + args.epochs): train(epoch) test(epoch)
def hard_triplet_baseline_exp(device='3', lr=1e-3, n_epochs=300, n_classes=10, n_samples=12, margin=0.3, log_interval=50): """ :param device: :param lr: :param n_epochs: :param n_classes: :param n_samples: :return: """ os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # get the mean and std of dataset train/a standarizer = TaskbStandarizer(data_manager=Dcase18TaskbData()) mu, sigma = standarizer.load_mu_sigma(mode='train', device='a') # get the normalized train dataset train_dataset = DevSet(mode='train', device='a', transform=Compose( [Normalize(mean=mu, std=sigma), ToTensor()])) test_dataset = DevSet(mode='test', device='a', transform=Compose( [Normalize(mean=mu, std=sigma), ToTensor()])) train_batch_sampler = BalanceBatchSampler(dataset=train_dataset, n_classes=n_classes, n_samples=n_samples) train_batch_loader = DataLoader(dataset=train_dataset, batch_sampler=train_batch_sampler, num_workers=1) test_batch_sampler = BalanceBatchSampler(dataset=test_dataset, n_classes=n_classes, n_samples=n_samples) test_batch_loader = DataLoader(dataset=test_dataset, batch_sampler=test_batch_sampler, num_workers=1) model = networks.embedding_net_shallow() model = model.cuda() loss_fn = OnlineTripletLoss( margin=margin, triplet_selector=utils.RandomNegativeTripletSelector(margin=margin)) optimizer = optim.Adam(model.parameters(), lr=lr) scheduler = lr_scheduler.StepLR(optimizer=optimizer, step_size=30, gamma=0.5) fit(train_loader=train_batch_loader, val_loader=test_batch_loader, model=model, loss_fn=loss_fn, optimizer=optimizer, scheduler=scheduler, n_epochs=n_epochs, log_interval=log_interval, metrics=[AverageNoneZeroTripletsMetric()]) train_embedding_tl, train_labels_tl = utils.extract_embeddings( train_batch_loader, model) utils.plot_embeddings(train_embedding_tl, train_labels_tl) test_embedding_tl, test_labels_tl = utils.extract_embeddings( test_batch_loader, model) utils.plot_embeddings(test_embedding_tl, test_labels_tl)
param.requires_grad = True model_trained_mobilenet = model print("All trainable parameters of model are") for name, param in model_trained_mobilenet.named_parameters(): if param.requires_grad: print(name, param.shape) contact_model = multi_task_model_classification(model_trained_mobilenet) contact_less_model = multi_task_model_classification(model_trained_mobilenet) # In[22]: if cuda: contact_less_model.cuda() contact_model.cuda() AllTripletSelector1 = AllTripletSelector() loss_fn = OnlineTripletLoss(margin, AllTripletSelector1) lr = 1e-3 contact_optimizer = optim.Adam(contact_model.parameters(), lr=lr, weight_decay=1e-4) contact_less_optimizer = optim.Adam(contact_less_model.parameters(), lr=lr, weight_decay=1e-4) contact_scheduler = lr_scheduler.StepLR(contact_optimizer, 8, gamma=0.1, last_epoch=-1) contact_less_scheduler = lr_scheduler.StepLR(contact_less_optimizer, 8, gamma=0.1, last_epoch=-1)
if opt.sch: scheduler_cosine = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, opt.epoch) scheduler_warmup = GradualWarmupScheduler(optimizer, multiplier=8, total_epoch=10,after_scheduler=scheduler_cosine) else: scheduler_warmup = None margin=opt.margin if opt.loss=='cxe': criterion_cl = nn.CrossEntropyLoss() elif opt.loss == 'smooth': criterion_cl = LabelSmoothingLoss(smoothing=opt.alpha) if opt.trip_loss == 'ranking': criterion = OnlineTripletLoss(margin, HardestNegativeTripletSelector(margin)).to(device) elif opt.trip_loss == 'impr': criterion = OnlineTripletLoss1(margin, HardestNegativeTripletSelector(margin)).to(device) if opt.vlad: vlad = NetVLAD().to(device) def train(model,model_cl,triplet_train_loader,optimizer,criterion,criterion_cl,epoch,beta,scheduler=None,vlad=None): model.train() model_cl.train() for batch_idx, (data, target) in enumerate(triplet_train_loader): #anchor, pos, neg = data #anchor = anchor.to(device) #pos = pos.to(device) #neg = neg.to(device) data = data.to(device) target = target.to(device)
if args.resume: # Load checkpoint. print('==> Resuming from checkpoint..') assert os.path.isdir( 'checkpoint'), 'Error: no checkpoint directory found!' #if os.path.isfile('./checkpoint/triplet_RandomNegativeTripletSelector_ckpt.t7'): if True: checkpoint = torch.load( './checkpoint/triplet_RandomNegativeTripletSelector_ckpt.t7') best_acc = checkpoint['acc'] else: checkpoint = torch.load('./checkpoint/classification_ckpt.t7') best_acc = 0 net.load_state_dict(checkpoint['net']) start_epoch = checkpoint['epoch'] current_best_epoch = start_epoch print('the current best acc is %.3f on epoch %d' % (best_acc, start_epoch)) criterion = OnlineTripletLoss( margin=1, triplet_selector=RandomNegativeTripletSelector(margin=1)) optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) for epoch in range(start_epoch, start_epoch + args.epochs): train(epoch) test(epoch)
if args.resume: # Load checkpoint. print('==> Resuming from checkpoint..') assert os.path.isdir( 'checkpoint'), 'Error: no checkpoint directory found!' #if os.path.isfile('./checkpoint/triplet_SemihardNegativeTripletSelector_ckpt.t7'): if True: checkpoint = torch.load( './checkpoint/triplet_SemihardNegativeTripletSelector_ckpt.t7') best_acc = checkpoint['acc'] else: checkpoint = torch.load('./checkpoint/classification_ckpt.t7') best_acc = 0 net.load_state_dict(checkpoint['net']) start_epoch = checkpoint['epoch'] print('the current best acc is %.3f on epoch %d' % (best_acc, start_epoch)) criterion = OnlineTripletLoss( margin=1, triplet_selector=SemihardNegativeTripletSelector(margin=1)) optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4) for epoch in range(start_epoch, start_epoch + args.epochs): train(epoch) test(epoch)
def train_net(self, num_epochs, lr, triplet_selector, gmp_lambda, margin=1., scheduler_step=config.SCHEDULER_STEP, lr_decay=config.LR_DECAY, weight_decay=config.WEIGHT_DECAY, log_interval=50): # TODO: different LR for different layers # parameter_options = [ # {'params': encoder.model.layer1.parameters(), 'lr': lr / 1000}, # {'params': encoder.model.layer2.parameters(), 'lr': lr / 100}, # {'params': encoder.model.layer3.parameters(), 'lr': lr / 10}, # {'params': encoder.model.layer4.parameters(), 'lr': lr / 10}, # {'params': encoder.model.avgpool.parameters(), 'lr': lr}, # {'params': encoder.model.fc.parameters(), 'lr': lr} # ] parameters = [{ 'params': self.encoder.feature_extractor.parameters() }, { 'params': self.encoder.pool.parameters(), 'lr': config.POOL_LR_MULTIPLIER * lr, 'weight_decay': 0 }] if self.optimizer == 'adam' or self.optimizer == 'Adam': optimizer = optim.Adam(parameters, lr, weight_decay=weight_decay, amsgrad=True) elif self.optimizer in ('sgd', 'SGD'): optimizer = optim.SGD(parameters, lr, momentum=0.9, weight_decay=weight_decay, nesterov=True) scheduler = None plateau_scheduler = None if self.scheduler == 'step-lr': scheduler = lr_scheduler.StepLR(optimizer, scheduler_step, lr_decay) plateau_scheduler = None elif self.scheduler == 'plateau': scheduler = None plateau_scheduler = lr_scheduler.ReduceLROnPlateau( optimizer, 'max', 0.1, patience=scheduler_step, verbose=True) train_sampler = PerClassBatchSampler(self.data.train_data, num_classes=self.num_classes, num_samples=self.num_samples) val_sampler = PerClassBatchSampler(self.data.val_data, num_classes=self.num_classes, num_samples=self.num_samples) train_data_loader = self.data.get_train_data_loader( batch_sampler=train_sampler) val_loader = self.data.get_val_data_loader(batch_sampler=val_sampler) cuda = torch.cuda.is_available() if cuda: self.encoder.cuda() loss_fn = OnlineTripletLoss(margin, triplet_selector) triplet_net = TripletNetwork(self.encoder, triplet_loss=loss_fn) if config.USE_PATCHES: trans = torchvision.transforms.Compose([ PatchCrop(400, resize_to=256), # torchvision.transforms.Resize(256), torchvision.transforms.Lambda(lambda crops: [ torchvision.transforms.ToTensor()(crop) for crop in crops ]), ]) seq_loader = self.data.get_val_data_loader2(transform=trans) test_loader = self.data.get_test_data_loader(transform=trans) else: seq_loader = self.data.get_sequential_data_loader(batch_size=15) test_loader = self.data.get_competition_data_loader() triplet_net.train(train_data_loader, val_loader, num_epochs, optimizer, scheduler, cuda, log_interval, gmp_lambda, lr, seq_loader, margin, test_loader=test_loader, plateau_scheduler=plateau_scheduler, metrics=[ AverageNonzeroTripletsMetric(), ])
def hard_triplet_with_knn_exp(device='3', ckpt_prefix='Run01', lr=1e-3, embedding_epochs=10, classify_epochs=100, n_classes=10, n_samples=12, margin=0.3, log_interval=50, log_level="INFO", k=3, embed_dims=64, embed_net='vgg'): """ knn as classifier. :param device: :param lr: :param n_epochs: :param n_classes: :param n_samples: :param k: kNN parameter :return: """ SEED = 0 torch.manual_seed(SEED) torch.cuda.manual_seed(SEED) np.random.seed(SEED) kwargs = locals() log_file = '{}/ckpt/hard_triplet_with_knn_exp/{}.log'.format(ROOT_DIR, ckpt_prefix) if not os.path.exists(os.path.dirname(log_file)): os.makedirs(os.path.dirname(log_file)) logging.basicConfig(filename=log_file, level=getattr(logging, log_level.upper(), None)) logging.info(str(kwargs)) os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # get the mean and std of dataset train/a standarizer = TaskbStandarizer(data_manager=Dcase18TaskbData()) mu, sigma = standarizer.load_mu_sigma(mode='train', device='a') # get the normalized train dataset train_dataset = DevSet(mode='train', device='a', transform=Compose([ Normalize(mean=mu, std=sigma), ToTensor() ])) test_dataset = DevSet(mode='test', device='a', transform=Compose([ Normalize(mean=mu, std=sigma), ToTensor() ])) train_batch_sampler = BalanceBatchSampler(dataset=train_dataset, n_classes=n_classes, n_samples=n_samples) train_batch_loader = DataLoader(dataset=train_dataset, batch_sampler=train_batch_sampler, num_workers=1) test_batch_sampler = BalanceBatchSampler(dataset=test_dataset, n_classes=n_classes, n_samples=n_samples) test_batch_loader = DataLoader(dataset=test_dataset, batch_sampler=test_batch_sampler, num_workers=1) if embed_net == 'vgg': model = networks.vggish_bn() elif embed_net == 'shallow': model = networks.embedding_net_shallow() else: print("{} network doesn't exist.".format(embed_net)) return model = model.cuda() loss_fn = OnlineTripletLoss(margin=margin, triplet_selector=RandomNegativeTripletSelector(margin=margin)) optimizer = optim.Adam(model.parameters(), lr=lr) scheduler = lr_scheduler.StepLR(optimizer=optimizer, step_size=30, gamma=0.5) # fit(train_loader=train_batch_loader, val_loader=test_batch_loader, model=model, loss_fn=loss_fn, # optimizer=optimizer, scheduler=scheduler, n_epochs=embedding_epochs, log_interval=log_interval, # metrics=[AverageNoneZeroTripletsMetric()]) train_hist = History(name='train/a') val_hist = History(name='test/a') ckpter = CheckPoint(model=model, optimizer=optimizer, path='{}/ckpt/hard_triplet_with_knn_exp'.format(ROOT_DIR), prefix=ckpt_prefix, interval=1, save_num=1) for epoch in range(1, embedding_epochs + 1): scheduler.step() train_loss, metrics = train_epoch(train_loader=train_batch_loader, model=model, loss_fn=loss_fn, optimizer=optimizer, log_interval=log_interval, metrics=[AverageNoneZeroTripletsMetric()]) train_logs = dict() train_logs['loss'] = train_loss for metric in metrics: train_logs[metric.name()] = metric.value() train_hist.add(logs=train_logs, epoch=epoch) test_acc = kNN(model=model, train_loader=train_batch_loader, test_loader=test_batch_loader, k=k, embed_dims=embed_dims) test_logs = {'acc': test_acc} val_hist.add(logs=test_logs, epoch=epoch) train_hist.clear() train_hist.plot() val_hist.plot() logging.info('Epoch{:04d}, {:15}, {}'.format(epoch, train_hist.name, str(train_hist.recent))) logging.info('Epoch{:04d}, {:15}, {}'.format(epoch, val_hist.name, str(val_hist.recent))) ckpter.check_on(epoch=epoch, monitor='acc', loss_acc=val_hist.recent) # train classifier using learned embeddings. classify_model = networks.classifier() classify_model = classify_model.cuda() classify_loss_fn = nn.CrossEntropyLoss() classify_optimizer = optim.Adam(classify_model.parameters(), lr=lr) classify_scheduler = lr_scheduler.StepLR(optimizer=classify_optimizer, step_size=30, gamma=0.5) classify_train_hist = History(name='classify_train/a') classify_val_hist = History(name='classify_val/a') classify_ckpter = CheckPoint(model=classify_model, optimizer=classify_optimizer, path='{}/ckpt/hard_triplet_with_knn_exp'.format(ROOT_DIR), prefix=ckpt_prefix, interval=1, save_num=1) # reload best embedding model best_model_filename = Reporter(ckpt_root=os.path.join(ROOT_DIR, 'ckpt'), exp='hard_triplet_with_knn_exp').select_best(run=ckpt_prefix).selected_ckpt model.load_state_dict(torch.load(best_model_filename)['model_state_dict']) train_embedding, train_labels = extract_embeddings(train_batch_loader, model, embed_dims) test_embedding, test_labels = extract_embeddings(test_batch_loader, model, embed_dims) classify_train_dataset = DatasetWrapper(data=train_embedding, labels=train_labels, transform=ToTensor()) classify_test_dataset = DatasetWrapper(data=test_embedding, labels=test_labels, transform=ToTensor()) classify_train_loader = DataLoader(dataset=classify_train_dataset, batch_size=128, shuffle=True, num_workers=1) classify_test_loader = DataLoader(dataset=classify_test_dataset, batch_size=128, shuffle=False, num_workers=1) fit(train_loader=classify_train_loader, val_loader=classify_test_loader, model=classify_model, loss_fn=classify_loss_fn, optimizer=classify_optimizer, scheduler=classify_scheduler, n_epochs=classify_epochs, log_interval=log_interval, metrics=[AccumulatedAccuracyMetric()], train_hist=classify_train_hist, val_hist=classify_val_hist, ckpter=classify_ckpter, logging=logging)
train_dataset, batch_sampler=train_batch_sampler, **kwargs) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) # Set up the network and training parameters from networks import EmbeddingNet from losses import OnlineTripletLoss from utils import AllTripletSelector, HardestNegativeTripletSelector, RandomNegativeTripletSelector, SemihardNegativeTripletSelector # Strategies for selecting triplets within a minibatch from metrics import AverageNonzeroTripletsMetric margin = 1. embedding_net = EmbeddingNet() model = embedding_net if cuda: model.cuda() loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = 20 log_interval = 50 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval,
def main(args): model_path = args.model_path save_dir = args.save_dir vec_dim = 128 data_type = ['validation' ] if args.phase == 'test' else ['train', 'validation'] img_list, base_path, item_dict = read_data("DeepFashion2", bbox_gt=True, type_list=data_type) # model = ResNetbasedNet(vec_dim=vec_dim, max_pool=True, load_path=model_path, clf2_num=2, adv_eta=1e-4) model = ResNetbasedNet(vec_dim=vec_dim, max_pool=True, load_path=model_path, clf2_num=2) domain_adap = args.domain_adap adv_train = args.adv_train is_cud = torch.cuda.is_available() device = torch.device("cuda" if is_cud else "cpu") if is_cud: if torch.cuda.device_count() > 1: model = nn.DataParallel(model) model.to(device) kwargs = {'num_workers': 8, 'pin_memory': True} if is_cud else {} if args.phase == 'train': train_dataset = DeepFashionDataset(img_list['train'], root=base_path, augment=True) train_batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.source, n_classes=64, n_samples=4) online_train_loader = torch.utils.data.DataLoader( train_dataset, batch_sampler=train_batch_sampler, **kwargs) test_dataset = DeepFashionDataset(img_list['validation'], root=base_path) test_batch_sampler = BalancedBatchSampler(test_dataset.labels, test_dataset.source, n_classes=64, n_samples=4) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) margin = 0.2 loss_fn = OnlineTripletLoss(margin, HardestNegativeTripletSelector(margin), domain_adap) # loss_fn = AllTripletLoss(margin) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=1e-5, weight_decay=5e-4) # scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", patience=4, threshold=0.001, cooldown=2, min_lr=1e-4 / (10 * 2),) scheduler = lr_scheduler.ReduceLROnPlateau( optimizer, mode="max", patience=4, threshold=1, cooldown=2, min_lr=1e-5 / (10 * 2), ) n_epochs = 300 log_interval = 200 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, is_cud, log_interval, save_dir, metrics=[AverageNonzeroTripletsMetric()], start_epoch=200, criterion=criterion, domain_adap=domain_adap, adv_train=adv_train) # fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, is_cud, log_interval, # save_dir, metrics=[AverageNonzeroTripletsMetric()], start_epoch=0, criterion=criterion, # adv_train=True, adv_epsilon=0.01, adv_alph=0.007, adv_iter=1) else: with torch.no_grad(): model.eval() test_dataset = DeepFashionDataset(img_list['validation'], root=base_path) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=256, shuffle=False, num_workers=4) embedding_mtx = torch.zeros((len(test_dataset), vec_dim)) labels = np.zeros(len(test_dataset)) top_k = 500 idx_ = 0 start_time = time.time() cf_mtx = np.zeros( 4, dtype=float ) # predict_user_real_user / predict_user_real_shop / predict_shop_real_user / predict_shop_real_shop for idx, (data, target, _, source) in enumerate(test_loader): emb_vecs = model(data.cuda()) embedding_mtx[idx_:idx_ + len(data)] = emb_vecs[0] predict = torch.argmax(emb_vecs[1], dim=1).cpu().numpy() real = source.cpu().numpy() cf_mtx[0] += np.sum((predict == 0) & (real == 0)) cf_mtx[1] += np.sum((predict == 0) & (real == 1)) cf_mtx[2] += np.sum((predict == 1) & (real == 0)) cf_mtx[3] += np.sum((predict == 1) & (real == 1)) labels[idx_:idx_ + len(data)] = np.asarray(target) idx_ += len(data) if idx % 20 == 0: print('processing {}/{}... elapsed time {}s'.format( idx + 1, len(test_loader), time.time() - start_time)) print('Total: {}, Domain Classification Acc: {:.5f}'.format( np.sum(cf_mtx), (cf_mtx[0] + cf_mtx[3]) / np.sum(cf_mtx))) print('Recall User Photo: {:.5f}'.format(cf_mtx[0] / (cf_mtx[0] + cf_mtx[2]))) print('Recall Shop Photo: {:.5f}'.format(cf_mtx[3] / (cf_mtx[1] + cf_mtx[3]))) np.save(os.path.join(save_dir, 'emb_mtx.npy'), embedding_mtx) with open(os.path.join(save_dir, 'file_info.txt'), 'w') as f: for i in range(len(test_dataset)): f.write('{},{},{},{}\n'.format(img_list['validation'][i][0], test_dataset[i][1], test_dataset[i][2], test_dataset[i][3])) print('save files!') distance_mtx = pdist(embedding_mtx) sorted_idx = torch.argsort(distance_mtx, dim=1).cpu().numpy() result_arr = np.zeros((sorted_idx.shape[0], top_k)) for idx in range(sorted_idx.shape[0]): result_arr[idx] = sorted_idx[idx][sorted_idx[idx] != idx][:top_k] result_arr[idx] = labels[result_arr[idx].astype( np.int)] == labels[idx] if idx % 1000 == 0: print(idx) for k in [1, 5, 10, 20, 100, 200, 500]: topk_accuracy = np.sum( np.sum(result_arr[:, :k], axis=1) > 0) / result_arr.shape[0] print('Top-{} Accuracy: {:.5f}'.format(k, topk_accuracy))