def train(epoch, model, trainloader): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() optim = torch.optim.Adam(model.parameters()) loss_fc0 = TripletLoss(margin=0.3) loss_fc1 = TripletLoss(margin=0.3) model.fc0.train(True) model.fc1.train(False) output_fc = "fc0" loss_fn = loss_fc0 for batch, (imgs, pids, _) in enumerate(trainloader): imgs, pids = imgs.cuda(), pids.cuda() data_time.update(time.time() - end) clf_outputs = model(imgs) loss = loss_fn(clf_outputs[output_fc], pids) optim.zero_grad() loss.backward() optim.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch + 1) % print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( epoch + 1, batch + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def __init__(self, opt): # Build Models self.grad_clip = opt.grad_clip self.vid_encoding = Video_multilevel_encoding(opt) self.text_encoding = Text_multilevel_encoding(opt) self.vid_mapping = Latent_mapping(opt.visual_mapping_layers, opt.dropout, opt.tag_vocab_size) self.text_mapping = Latent_mapping(opt.text_mapping_layers, opt.dropout, opt.tag_vocab_size) self.init_info() # Loss and Optimizer if opt.loss_fun == 'mrl': self.criterion = TripletLoss(margin=opt.margin, measure=opt.measure, max_violation=opt.max_violation, cost_style=opt.cost_style, direction=opt.direction) if opt.optimizer == 'adam': self.optimizer = torch.optim.Adam(self.params, lr=opt.learning_rate) elif opt.optimizer == 'rmsprop': self.optimizer = torch.optim.RMSprop(self.params, lr=opt.learning_rate) self.Eiters = 0
def __init__(self, opt): # Build Models self.grad_clip = opt.grad_clip self.vid_encoding = Video_multilevel_encoding(opt) self.text_encoding = Text_multilevel_encoding(opt) print(self.vid_encoding) print(self.text_encoding) if torch.cuda.is_available(): self.vid_encoding.cuda() self.text_encoding.cuda() cudnn.benchmark = True # Loss and Optimizer if opt.loss_fun == 'mrl': self.criterion = TripletLoss(margin=opt.margin, measure=opt.measure, max_violation=opt.max_violation, cost_style=opt.cost_style, direction=opt.direction) params = list(self.text_encoding.parameters()) params += list(self.vid_encoding.parameters()) self.params = params if opt.optimizer == 'adam': self.optimizer = torch.optim.Adam(params, lr=opt.learning_rate) elif opt.optimizer == 'rmsprop': self.optimizer = torch.optim.RMSprop(params, lr=opt.learning_rate) self.Eiters = 0
def __init__(self, dataloader, negative_sample_size=4, \ n_epochs=500, loss_mode='cosine', \ starting_lr=1e-3, device='cpu', margin=0.4): self.model = SingleExtractor(conv_channels=128, sample_rate=16000, n_fft=513, n_harmonic=6, semitone_scale=2, learn_bw='only_Q').to(device) self.device = device self.negative_sample_size = negative_sample_size self.n_epochs = n_epochs self.criterion = TripletLoss(mode=loss_mode, device=self.device, margin=margin) self.optimizer = Adam(self.model.parameters(), lr=starting_lr, weight_decay=1e-4) self.current_optimizer = 'adam' self.drop_counter = 0 self.trianing_loss = [] self.best_train_loss = 100 self.model_save_path = 'checkpoints_%s_%f' % (loss_mode, margin) if not os.path.exists(self.model_save_path): os.mkdir(self.model_save_path) self.dataloader = dataloader
def train(epoch, model, optim, trainloader): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() cross_entropy = CrossEntropy(num_classes=num_classes) triplet_loss_fn = TripletLoss(margin=margin) model.fc0.train(True) model.fc1.train(False) output_fc = "fc0" model.base.train(True) for batch, (imgs, pids, _) in enumerate(trainloader): imgs, pids = imgs.cuda(), pids.cuda() data_time.update(time.time() - end) clf_outputs, features = model(imgs) if isinstance(clf_outputs[output_fc], tuple): cross_entropy_loss = DeepSuperVision(cross_entropy, clf_outputs[output_fc], pids) else: cross_entropy_loss = cross_entropy(clf_outputs[output_fc], pids) if isinstance(features, tuple): triplet_loss = DeepSuperVision(triplet_loss_fn, features, pids) else: triplet_loss = triplet_loss_fn(clf_outputs[output_fc], pids) loss = cross_entropy_loss + triplet_loss optim.zero_grad() loss.backward() optim.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch + 1) % print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( epoch + 1, batch + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def train(): ## logging FORMAT = '%(levelname)s %(filename)s:%(lineno)4d: %(message)s' logging.basicConfig(level=logging.INFO, format=FORMAT, stream=sys.stdout) logger = logging.getLogger(__name__) ## model and loss net = EmbedNetwork().cuda() net = nn.DataParallel(net) triplet_loss = TripletLoss( margin=None).cuda() # no margin means soft-margin ## optimizer optim = AdamOptimWrapper(net.parameters(), lr=3e-4, t0=15000, t1=25000) ## dataloader ds = Market1501('datasets/Market-1501-v15.09.15/bounding_box_train', mode='train') sampler = RegularBatchSampler(ds, 18, 4) dl = DataLoader(ds, batch_sampler=sampler, num_workers=4) selector = BatchHardTripletSelector() ## train count = 0 while True: for it, (imgs, lbs) in enumerate(dl): st = time.time() net.train() imgs = imgs.cuda() lbs = lbs.cuda() embds = net(imgs) anchor, positives, negatives = selector(embds, lbs) loss = triplet_loss(anchor, positives, negatives) optim.zero_grad() loss.backward() optim.step() if count % 20 == 0 and it != 0: loss_val = loss.detach().cpu().numpy() time_interval = time.time() - st logger.info('iter:{}, loss:{:4f}, time: {:3f}'.format( count, loss_val, time_interval)) count += 1 if count == 25000: break if count == 25000: break # it seems that there will be errors with dataloader we do not let it finish # its iteration steps for imgs, lbs in dl: pass ## dump model if not os.path.exists('./res'): os.makedirs('./res') logger.info('saving trained model') torch.save(net.module.state_dict(), './res/model.pkl') logger.info('everything finished')
def __init__(self, hparams): super().__init__() self.hparams = hparams self.model = SCAN() self.triplet_loss = TripletLoss() self.log_cues = not self.hparams.cue_log_every == 0 self.grid_maker = GridMaker() if self.hparams.use_focal_loss: self.clf_criterion = FocalLossMultiClass() else: self.clf_criterion = nn.CrossEntropyLoss()
def load_criterion(config, logger): logger.info(f"Using {config.LOSS} loss function") if config.LOSS == "contrastive": criterion = ContrastiveLoss(margin=config.MARGIN) elif config.LOSS == "triplet": criterion = TripletLoss(margin=config.MARGIN) elif config.LOSS == "margin": criterion = MarginLoss(loss_lambda=config.LOSS_LAMBDA) elif config.LOSS == "bce": criterion = nn.BCEWithLogitsLoss() else: raise ValueError(f"Received unknown loss function: {config.LOSS}") return criterion
def __init__(self, args, seg_num_all=50): super(SegmentationNet, self).__init__() self.is_eval = args.eval self.loss_type = args.loss if args.encoder == 'foldnet': self.encoder = FoldNet_Encoder(args) elif args.encoder == 'dgcnn_cls': self.encoder = DGCNN_Cls_Encoder(args) elif args.encoder == 'dgcnn_seg': self.encoder = DGCNN_Seg_Encoder(args) if not self.is_eval: self.segmenter = DGCNN_Seg_Segmenter(args, seg_num_all) if self.loss_type == 'softmax': self.loss = CrossEntropyLoss() elif self.loss_type == 'triplet': self.loss = TripletLoss(margin=args.margin)
def train_unsupervised_triplet(args, Dataset, train_Dataloader, net): margin = args.margin if args.margin == 'soft' else float(args.margin) optimizer = optim.Adam(net.parameters(), lr=args.lr) criterion_triplet = TripletLoss(margin=margin, batch_hard=args.batch_hard) logger = Logger(args.save_model_dir, prefix='train_') for e in range(args.n_epochs): pbar = tqdm(total=len(train_Dataloader), ncols=100, leave=True) pbar.set_description('Epoch %d' % (e)) epoch_loss = 0 iter_count = 0 for i_batch, samples in enumerate(train_Dataloader): iter_count += 1 imgs = samples['img'].squeeze(0) pos_mask = samples['pos_mask'].squeeze(0) neg_mask = samples['neg_mask'].squeeze(0) b_img = Variable(imgs).cuda() pos_mask = Variable(pos_mask).cuda() neg_mask = Variable(neg_mask).cuda() net.zero_grad() #forward pred_feat = net(b_img) b_loss = criterion_triplet(pred_feat, pos_mask=pos_mask, neg_mask=neg_mask, mode='mask') loss = b_loss.mean() epoch_loss += loss.data[0] # backward loss.backward() optimizer.step() logger.append_epoch(e + float(i_batch) / len(train_Dataloader)) logger.append_loss(b_loss.data.cpu().numpy()) logger.append_feat(pred_feat.data.cpu().numpy()) logger.write_log() pbar.update(1) pbar.set_postfix({'loss': '%.2f' % (loss.data[0])}) pbar.close() print('Training total loss = %.3f' % (epoch_loss / iter_count)) if e % args.save_every_n_epoch == 0: torch.save( net.state_dict(), os.path.join(args.save_model_dir, 'model_%d.ckpt' % (e))) logger.plot()
def __init__(self, params, model, dataloader, evaluator): self.params = params self.dataloader = dataloader self.evaluator = evaluator if self.params.optimizer == 'Adam': optimizer = torch.optim.Adam(model.parameters(), lr=self.params.lr, weight_decay=self.params.wd) elif self.params.optimizer == 'Adadelta': optimizer = torch.optim.Adadelta(model.parameters(), lr=self.params.lr, weight_decay=self.params.wd) else: print('Invalid optimizer, quit') exit(0) self.optimizer = optimizer self.loss_function = TripletLoss(margin=params.margin) self.logger = LogCollector( os.path.join(self.params.model_path, 'tr_log'))
def main(): #GENERAL torch.cuda.empty_cache() root = "/home/kuru/Desktop/veri-gms-master/" train_dir = '/home/kuru/Desktop/veri-gms-master/VeRispan/image_train/' source = {'verispan'} target = {'verispan'} workers = 4 height = 320 width = 320 train_sampler = 'RandomSampler' #AUGMENTATION random_erase = True jitter = True aug = True #OPTIMIZATION opt = 'adam' lr = 0.001 weight_decay = 5e-4 momentum = 0.9 sgd_damp = 0.0 nesterov = True warmup_factor = 0.01 warmup_method = 'linear' STEPS = (30, 60) GAMMA = 0.1 WARMUP_FACTOR = 0.01 WARMUP_EPOCHS = 10 WARMUP_METHOD = 'linear' #HYPERPARAMETER max_epoch = 80 start = 0 train_batch_size = 16 test_batch_size = 50 #SCHEDULER lr_scheduler = 'multi_step' stepsize = [30, 60] gamma = 0.1 #LOSS margin = 0.3 num_instances = 4 lambda_tri = 1 #MODEL #arch = 'resnet101' arch = 'resnet50_ibn_a' no_pretrained = False #TEST SETTINGS #load_weights = '/home/kuru/Desktop/veri-gms-master/IBN-Net_pytorch0.4.1/resnet101_ibn_a.pth' #load_weights = '/home/kuru/Desktop/veri-gms-master/IBN-Net_pytorch0.4.1/resnet101_ibn_a.pth' load_weights = '/home/kuru/Desktop/veri-gms-master/IBN-Net_pytorch0.4.1/resnet50_ibn_a.pth' #load_weights = None start_eval = 0 eval_freq = -1 num_classes = 776 feat_dim = 2048 CENTER_LR = 0.5 CENTER_LOSS_WEIGHT = 0.0005 center_criterion = CenterLoss(num_classes=num_classes, feat_dim=feat_dim, use_gpu=True) optimizer_center = torch.optim.SGD(center_criterion.parameters(), lr=CENTER_LR) #MISC use_gpu = True #use_gpu = False print_freq = 10 seed = 1 resume = '' save_dir = '/home/kuru/Desktop/veri-gms-master_noise/spanningtree_veri_pure/' gpu_id = 0, 1 vis_rank = True query_remove = True evaluate = False dataset_kwargs = { 'source_names': source, 'target_names': target, 'root': root, 'height': height, 'width': width, 'train_batch_size': train_batch_size, 'test_batch_size': test_batch_size, 'train_sampler': train_sampler, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } transform_kwargs = { 'height': height, 'width': width, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } optimizer_kwargs = { 'optim': opt, 'lr': lr, 'weight_decay': weight_decay, 'momentum': momentum, 'sgd_dampening': sgd_damp, 'sgd_nesterov': nesterov } lr_scheduler_kwargs = { 'lr_scheduler': lr_scheduler, 'stepsize': stepsize, 'gamma': gamma } use_gpu = torch.cuda.is_available() log_name = 'log_test.txt' if evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(save_dir, log_name)) print('Currently using GPU ', gpu_id) cudnn.benchmark = True print('Initializing image data manager') #dataset = init_imgreid_dataset(root='/home/kuru/Desktop/veri-gms-master/', name='veri') dataset = init_imgreid_dataset(root='/home/kuru/Desktop/veri-gms-master/', name='verispan') train = [] num_train_pids = 0 num_train_cams = 0 print(len(dataset.train)) for img_path, pid, camid, subid, countid in dataset.train: #print(img_path) path = img_path[56:90 + 6] #print(path) folder = path[1:4] #print(folder) #print(img_path, pid, camid,subid,countid) pid += num_train_pids camid += num_train_cams newidd = 0 train.append((path, folder, pid, camid, subid, countid)) #print(train) #break num_train_pids += dataset.num_train_pids num_train_cams += dataset.num_train_cams pid = 0 pidx = {} for img_path, pid, camid, subid, countid in dataset.train: path = img_path[56:90 + 6] folder = path[1:4] pidx[folder] = pid pid += 1 #print(pidx) sub = [] final = 0 xx = dataset.train newids = [] print(train[0:2]) train2 = {} for k in range(0, 770): for img_path, pid, camid, subid, countid in dataset.train: if k == pid: newid = final + subid sub.append(newid) #print(pid,subid,newid) newids.append(newid) train2[img_path] = newid #print(img_path, pid, camid, subid, countid, newid) final = max(sub) #print(final) print(len(newids), final) #train=train2 #print(train2) train3 = [] for img_path, pid, camid, subid, countid in dataset.train: #print(img_path,pid,train2[img_path]) path = img_path[56:90 + 6] #print(path) folder = path[1:4] newid = train2[img_path] #print((path, folder, pid, camid, subid, countid,newid )) train3.append((path, folder, pid, camid, subid, countid, newid)) train = train3 # for (path, folder, pid, camid, subid, countid,newid) in train: # print(path, folder) #path = '/home/kuru/Desktop/adhi/veri-final-draft-master_noise/gmsNoise776/' path = '/home/kuru/Desktop/veri-gms-master/gms/' pkl = {} #pkl[0] = pickle.load('/home/kuru/Desktop/veri-gms-master/gms/620.pkl') entries = os.listdir(path) for name in entries: f = open((path + name), 'rb') ccc = (path + name) #print(ccc) if name == 'featureMatrix.pkl': s = name[0:13] else: s = name[0:3] #print(s) #with open (ccc,"rb") as ff: # pkl[s] = pickle.load(ff) #print(pkl[s]) pkl[s] = pickle.load(f) f.close #print(len(pkl)) print('=> pickle indexing') data_index = search(pkl) print(len(data_index)) transform_t = train_transforms(**transform_kwargs) #print(train[0],train[10]) #data_tfr = vd(pkl_file='index.pkl', dataset = train, root_dir='/home/kuru/Desktop/veri-gms-master/VeRi/image_train/', transform=transform_t) data_tfr = vdspan( pkl_file='index_veryspan.pkl', dataset=train, root_dir='/home/kuru/Desktop/veri-gms-master/VeRispan/image_train/', transform=transform_t) #print(data_tfr) #print(trainloader) #data_tfr2=list(data_tfr) print("lllllllllllllllllllllllllllllllllllllllllllline 433") df2 = [] data_tfr_old = data_tfr for (img, label, index, pid, cid, subid, countid, newid) in data_tfr: #print((img,label,index,pid, cid,subid,countid,newid) ) #print("datframe",(label)) #print(countid) if countid > 4: #print(countid) df2.append((img, label, index, pid, cid, subid, countid, newid)) print("filtered final trainset length", len(df2)) data_tfr = df2 # with open('df2noise_ex.pkl', 'wb') as handle: # b = pickle.dump(df2, handle, protocol=pickle.HIGHEST_PROTOCOL) # with open('df2noise.pkl', 'rb') as handle: # df2 = pickle.load(handle) # data_tfr=df2 # for (img,label,index,pid, cid,subid,countid,newid) in data_tfr : # print("datframe",(label)) #data_tfr = vdspansort( dataset = train, root_dir='/home/kuru/Desktop/veri-gms-master_noise/VeRispan/image_train/', transform=transform_t) #trainloader = DataLoader(df2, sampler=None,batch_size=train_batch_size, shuffle=True, num_workers=workers,pin_memory=True, drop_last=True) trainloader = DataLoader(data_tfr, sampler=None, batch_size=train_batch_size, shuffle=True, num_workers=workers, pin_memory=True, drop_last=True) for batch_idx, (img, label, index, pid, cid, subid, countid, newid) in enumerate(trainloader): #print("trainloader",batch_idx, (label,index,pid, cid,subid,countid,newid)) print("trainloader", batch_idx, (label)) break print('Initializing test data manager') dm = ImageDataManager(use_gpu, **dataset_kwargs) testloader_dict = dm.return_dataloaders() print('Initializing model: {}'.format(arch)) model = models.init_model(name=arch, num_classes=num_train_pids, loss={'xent', 'htri'}, pretrained=not no_pretrained, last_stride=2) print('Model size: {:.3f} M'.format(count_num_param(model))) if load_weights is not None: print("weights loaded") load_pretrained_weights(model, load_weights) #checkpoint = torch.load('/home/kuru/Desktop/veri-gms-master/logg/model.pth.tar-19') #model._load_from_state_dict(checkpoint['state_dict']) #model.load_state_dict(checkpoint['state_dict']) #optimizer.load_state_dict(checkpoint['optimizer']) #print(checkpoint['epoch']) #print(checkpoint['rank1']) os.environ['CUDA_VISIBLE_DEVICES'] = '0' print(torch.cuda.device_count()) model = nn.DataParallel(model).cuda() if use_gpu else model optimizer = init_optimizer(model, **optimizer_kwargs) #optimizer = init_optimizer(model) #optimizer.load_state_dict(checkpoint['optimizer']) scheduler = init_lr_scheduler(optimizer, **lr_scheduler_kwargs) # scheduler = WarmupMultiStepLR(optimizer, STEPS, GAMMA, # WARMUP_FACTOR, # WARMUP_EPOCHS, WARMUP_METHOD) criterion_xent = CrossEntropyLoss(num_classes=num_train_pids, use_gpu=use_gpu, label_smooth=True) criterion_htri = TripletLoss(margin=margin) ranking_loss = nn.MarginRankingLoss(margin=margin) if evaluate: print('Evaluate only') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] _, distmat = test(model, queryloader, galleryloader, train_batch_size, use_gpu, return_distmat=True) if vis_rank: visualize_ranked_results(distmat, dm.return_testdataset_by_name(name), save_dir=osp.join( save_dir, 'ranked_results', name), topk=20) return time_start = time.time() ranklogger = RankLogger(source, target) # # checkpoint = torch.load('/home/kuru/Desktop/market_all/ibna_model/model.pth.tar-79') # # model.load_state_dict(checkpoint['state_dict']) # # optimizer.load_state_dict(checkpoint['optimizer']) # # print(checkpoint['epoch']) # # start_epoch=checkpoint['epoch'] # # start=start_epoch # checkpoint = torch.load('/home/kuru/Desktop/veri-gms-master/spanningtreeveri/model.pth.tar-2') # model.load_state_dict(checkpoint['state_dict']) # optimizer.load_state_dict(checkpoint['optimizer']) # print(checkpoint['epoch']) # start_epoch=checkpoint['epoch'] # start=start_epoch ##start_epoch=resume_from_checkpoint('/home/kuru/Desktop/veri-gms-master/logg/model.pth.tar-20', model, optimizer=None) print('=> Start training') for epoch in range(start, max_epoch): print(epoch, scheduler.get_lr()[0]) #print( torch.cuda.memory_allocated(0)) losses = AverageMeter() #xent_losses = AverageMeter() htri_losses = AverageMeter() accs = AverageMeter() batch_time = AverageMeter() xent_losses = AverageMeter() model.train() for p in model.parameters(): p.requires_grad = True # open all layers end = time.time() for batch_idx, (img, label, index, pid, cid, subid, countid, newid) in enumerate(trainloader): trainX, trainY = torch.zeros( (train_batch_size * 3, 3, height, width), dtype=torch.float32), torch.zeros((train_batch_size * 3), dtype=torch.int64) #pids = torch.zeros((batch_size*3), dtype = torch.int16) #batchcount=0 for i in range(train_batch_size): if (countid[i] > 4): #batchcount=batchcount+1 #print("dfdsfs") labelx = label[i] indexx = index[i] cidx = pid[i] if indexx > len(pkl[labelx]) - 1: indexx = len(pkl[labelx]) - 1 #maxx = np.argmax(pkl[labelx][indexx]) a = pkl[labelx][indexx] minpos = np.argmin(ma.masked_where(a == 0, a)) # print(len(a)) # print(a) # print(ma.masked_where(a==0, a)) # print(labelx,index,pid,cidx,minpos) # print(np.array(data_index).shape) # print(data_index[cidx][1]) pos_dic = data_tfr_old[data_index[cidx][1] + minpos] #print('posdic', pos_dic) neg_label = int(labelx) while True: neg_label = random.choice(range(1, 770)) #print(neg_label) if neg_label is not int(labelx) and os.path.isdir( os.path.join( '/home/kuru/Desktop/veri-gms-master_noise/veriNoise_train_spanning_folder', strint(neg_label))) is True: break negative_label = strint(neg_label) neg_cid = pidx[negative_label] neg_index = random.choice( range(0, len(pkl[negative_label]))) #print(negative_label,neg_cid,neg_index,data_index[neg_cid] ) neg_dic = data_tfr_old[data_index[neg_cid][1] + neg_index] #print('negdic', neg_dic) trainX[i] = img[i] trainX[i + train_batch_size] = pos_dic[0] trainX[i + (train_batch_size * 2)] = neg_dic[0] trainY[i] = cidx trainY[i + train_batch_size] = pos_dic[3] trainY[i + (train_batch_size * 2)] = neg_dic[3] # trainY[i+train_batch_size] = pos_dic[7] # trainY[i+(train_batch_size*2)] = neg_dic[7] #break # else: # print("skiped",countid[i],subid[i],label[i]) #break #print(batchcount) trainX = trainX.cuda() trainY = trainY.cuda() outputs, features = model(trainX) xent_loss = criterion_xent(outputs[0:train_batch_size], trainY[0:train_batch_size]) htri_loss = criterion_htri(features, trainY) centerloss = CENTER_LOSS_WEIGHT * center_criterion( features, trainY) #tri_loss = ranking_loss(features) #ent_loss = xent_loss(outputs[0:batch_size], trainY[0:batch_size], num_train_pids) loss = htri_loss + xent_loss + centerloss loss = htri_loss + xent_loss optimizer.zero_grad() optimizer_center.zero_grad() loss.backward() optimizer.step() # for param in center_criterion.parameters(): # param.grad.data *= (1. /CENTER_LOSS_WEIGHT) # optimizer_center.step() for param_group in optimizer.param_groups: #print(param_group['lr'] ) lrrr = str(param_group['lr']) batch_time.update(time.time() - end) losses.update(loss.item(), trainY.size(0)) htri_losses.update(htri_loss.item(), trainY.size(0)) xent_losses.update(xent_loss.item(), trainY.size(0)) accs.update( accuracy(outputs[0:train_batch_size], trainY[0:train_batch_size])[0]) if (batch_idx) % 50 == 0: print('Train ', end=" ") print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'TriLoss {loss.val:.4f} ({loss.avg:.4f})\t' 'XLoss {xloss.val:.4f} ({xloss.avg:.4f})\t' 'OveralLoss {oloss.val:.4f} ({oloss.avg:.4f})\t' 'Acc {acc.val:.2f} ({acc.avg:.2f})\t' 'lr {lrrr} \t'.format( epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, loss=htri_losses, xloss=xent_losses, oloss=losses, acc=accs, lrrr=lrrr, )) end = time.time() # del loss # del htri_loss # del xent_loss # del htri_losses # del losses # del outputs # del features # del accs # del trainX # del trainY scheduler.step() print('=> Test') save_checkpoint( { 'state_dict': model.state_dict(), #'rank1': rank1, 'epoch': epoch + 1, 'arch': arch, 'optimizer': optimizer.state_dict(), }, save_dir) GPUtil.showUtilization() print(torch.cuda.memory_allocated(), torch.cuda.memory_cached()) for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1, distmat = test(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank1) rank2, distmat2 = test_rerank(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank2) del queryloader del galleryloader del distmat print(torch.cuda.memory_allocated(), torch.cuda.memory_cached()) torch.cuda.empty_cache() if (epoch + 1) == max_epoch: #if (epoch + 1) % 10 == 0: print('=> Test') save_checkpoint( { 'state_dict': model.state_dict(), 'rank1': rank1, 'epoch': epoch + 1, 'arch': arch, 'optimizer': optimizer.state_dict(), }, save_dir) for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1, distmat = test(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank1) # del queryloader # del galleryloader # del distmat if vis_rank: visualize_ranked_results( distmat, dm.return_testdataset_by_name(name), save_dir=osp.join(save_dir, 'ranked_results', name), topk=20)
transforms.CenterCrop(32), transforms.ToTensor(), normalize ]) # 训练集和测试集 train_loader = DataLoader(MyDataset('E:\\DS\\train', transforms), batch_size=4, shuffle=True) test_loader = DataLoader(MyDataset('E:\\DS\\test', transforms), batch_size=4, shuffle=False) # 实例化一个网络 net = Net() # 定义优化和损失函数 optimizer = optim.Adam(net.parameters(), lr=0.001) # Adam 最好的优化函数 crossloss = nn.CrossEntropyLoss() # 交叉熵损失 tripletloss = TripletLoss(10.0) # 三元损失 # trainning CNN # 将所有训练样本训练epoch遍, 测试样本测试epoch遍 for epoch in range(10): running_loss = 0.0 for i, data in enumerate(train_loader): a, p, n, label_a = data a_outputs = net(a) p_outputs = net(p) n_outputs = net(n) optimizer.zero_grad() loss = crossloss(a_outputs, label_a) loss += tripletloss(a_outputs, p_outputs, n_outputs) # inputs, labels = data
def main(): #GENERAL torch.cuda.empty_cache() root = "/home/kuru/Desktop/veri-gms-master_noise/" train_dir = '/home/kuru/Desktop/veri-gms-master_noise/VeRispan/image_train/' source = {'verispan'} target = {'verispan'} workers = 4 height = 280 width = 280 train_size = 32 train_sampler = 'RandomSampler' #AUGMENTATION random_erase = True jitter = True aug = True #OPTIMIZATION opt = 'adam' lr = 0.0003 weight_decay = 5e-4 momentum = 0.9 sgd_damp = 0.0 nesterov = True warmup_factor = 0.01 warmup_method = 'linear' #HYPERPARAMETER max_epoch = 80 start = 0 train_batch_size = 8 test_batch_size = 100 #SCHEDULER lr_scheduler = 'multi_step' stepsize = [30, 60] gamma = 0.1 #LOSS margin = 0.3 num_instances = 4 lambda_tri = 1 #MODEL #arch = 'resnet101' arch='resnet101_ibn_a' no_pretrained = False #TEST SETTINGS load_weights = '/home/kuru/Desktop/veri-gms-master/IBN-Net_pytorch0.4.1/resnet101_ibn_a.pth' #load_weights = None start_eval = 0 eval_freq = -1 #MISC use_gpu = True print_freq = 10 seed = 1 resume = '' save_dir = '/home/kuru/Desktop/veri-gms-master_noise/spanningtree_verinoise_101_stride2/' gpu_id = 0,1 vis_rank = True query_remove = True evaluate = False dataset_kwargs = { 'source_names': source, 'target_names': target, 'root': root, 'height': height, 'width': width, 'train_batch_size': train_batch_size, 'test_batch_size': test_batch_size, 'train_sampler': train_sampler, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } transform_kwargs = { 'height': height, 'width': width, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } optimizer_kwargs = { 'optim': opt, 'lr': lr, 'weight_decay': weight_decay, 'momentum': momentum, 'sgd_dampening': sgd_damp, 'sgd_nesterov': nesterov } lr_scheduler_kwargs = { 'lr_scheduler': lr_scheduler, 'stepsize': stepsize, 'gamma': gamma } use_gpu = torch.cuda.is_available() log_name = 'log_test.txt' if evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(save_dir, log_name)) print('Currently using GPU ', gpu_id) cudnn.benchmark = True print('Initializing image data manager') dataset = init_imgreid_dataset(root='/home/kuru/Desktop/veri-gms-master_noise/', name='verispan') train = [] num_train_pids = 0 num_train_cams = 0 print(len( dataset.train)) for img_path, pid, camid, subid, countid in dataset.train: #print(img_path) path = img_path[56+6:90+6] #print(path) folder = path[1:4] #print(folder) pid += num_train_pids newidd=0 train.append((path, folder, pid, camid,subid,countid)) num_train_pids += dataset.num_train_pids num_train_cams += dataset.num_train_cams pid = 0 pidx = {} for img_path, pid, camid, subid, countid in dataset.train: path = img_path[56+6:90+6] folder = path[1:4] pidx[folder] = pid pid+= 1 sub=[] final=0 xx=dataset.train newids=[] print(train[0:2]) train2={} for k in range(0,770): for img_path, pid, camid, subid, countid in dataset.train: if k==pid: newid=final+subid sub.append(newid) #print(pid,subid,newid) newids.append(newid) train2[img_path]= newid #print(img_path, pid, camid, subid, countid, newid) final=max(sub) #print(final) print(len(newids),final) #train=train2 #print(train2) train3=[] for img_path, pid, camid, subid, countid in dataset.train: #print(img_path,pid,train2[img_path]) path = img_path[56:90+6] #print(path) folder = path[1:4] newid=train2[img_path] #print((path, folder, pid, camid, subid, countid,newid )) train3.append((path, folder, pid, camid, subid, countid,newid )) train = train3 path = '/home/kuru/Desktop/adhi/veri-final-draft-master_noise/gmsNoise776/' pkl = {} #pkl[0] = pickle.load('/home/kuru/Desktop/veri-gms-master/gms/620.pkl') entries = os.listdir(path) for name in entries: f = open((path+name), 'rb') ccc=(path+name) #print(ccc) if name=='featureMatrix.pkl': s = name[0:13] else: s = name[0:3] #print(s) #with open (ccc,"rb") as ff: # pkl[s] = pickle.load(ff) #print(pkl[s]) pkl[s] = pickle.load(f) f.close #print(len(pkl)) with open('cids.pkl', 'rb') as handle: b = pickle.load(handle) #print(b) with open('index.pkl', 'rb') as handle: c = pickle.load(handle) transform_t = train_transforms(**transform_kwargs) data_tfr = vdspan(pkl_file='index_veryspan_noise.pkl', dataset = train, root_dir='/home/kuru/Desktop/veri-gms-master_noise/VeRispan/image_train/', transform=transform_t) print("lllllllllllllllllllllllllllllllllllllllllllline 433") df2=[] data_tfr_old=data_tfr for (img,label,index,pid, cid,subid,countid,newid) in data_tfr : #print((img,label,index,pid, cid,subid,countid,newid) ) #print("datframe",(label)) #print(countid) if countid > 4 : #print(countid) df2.append((img,label,index,pid, cid,subid,countid,newid)) print("filtered final trainset length",len(df2)) data_tfr=df2 trainloader = DataLoader(data_tfr, sampler=None,batch_size=train_batch_size, shuffle=True, num_workers=workers,pin_memory=True, drop_last=True) #data_tfr = vd(pkl_file='index.pkl', dataset = train, root_dir=train_dir,transform=transforms.Compose([Rescale(64),RandomCrop(32),ToTensor()])) #dataloader = DataLoader(data_tfr, batch_size=batch_size, shuffle=False, num_workers=0) for batch_idx, (img,label,index,pid, cid,subid,countid,newid) in enumerate(trainloader): #print("trainloader",batch_idx, (label,index,pid, cid,subid,countid,newid)) print("trainloader",batch_idx, (label)) break print('Initializing test data manager') dm = ImageDataManager(use_gpu, **dataset_kwargs) testloader_dict = dm.return_dataloaders() print('Initializing model: {}'.format(arch)) model = models.init_model(name=arch, num_classes=num_train_pids, loss={'xent', 'htri'}, pretrained=not no_pretrained, last_stride =2 ) print('Model size: {:.3f} M'.format(count_num_param(model))) if load_weights is not None: print("weights loaded") load_pretrained_weights(model, load_weights) print(torch.cuda.device_count()) model = nn.DataParallel(model).cuda() if use_gpu else model optimizer = init_optimizer(model, **optimizer_kwargs) #optimizer = init_optimizer(model) scheduler = init_lr_scheduler(optimizer, **lr_scheduler_kwargs) criterion_xent = CrossEntropyLoss(num_classes=num_train_pids, use_gpu=use_gpu, label_smooth=True) criterion_htri = TripletLoss(margin=margin) ranking_loss = nn.MarginRankingLoss(margin = margin) if evaluate: print('Evaluate only') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] _, distmat = test(model, queryloader, galleryloader, train_batch_size, use_gpu, return_distmat=True) if vis_rank: visualize_ranked_results( distmat, dm.return_testdataset_by_name(name), save_dir=osp.join(save_dir, 'ranked_results', name), topk=20 ) return time_start = time.time() ranklogger = RankLogger(source, target) print('=> Start training') data_index = search(pkl) print(len(data_index)) for epoch in range(start, max_epoch): losses = AverageMeter() #xent_losses = AverageMeter() htri_losses = AverageMeter() accs = AverageMeter() batch_time = AverageMeter() xent_losses=AverageMeter() model.train() for p in model.parameters(): p.requires_grad = True # open all layers end = time.time() for batch_idx, (img,label,index,pid, cid,subid,countid,newid) in enumerate(trainloader): trainX, trainY = torch.zeros((train_batch_size*3,3,height, width), dtype=torch.float32), torch.zeros((train_batch_size*3), dtype = torch.int64) #pids = torch.zeros((batch_size*3), dtype = torch.int16) for i in range(train_batch_size): #print("dfdsfs") labelx = label[i] indexx = index[i] cidx = pid[i] if indexx >len(pkl[labelx])-1: indexx = len(pkl[labelx])-1 #maxx = np.argmax(pkl[labelx][indexx]) a = pkl[labelx][indexx] minpos = np.argmin(ma.masked_where(a==0, a)) #print(minpos) #print(np.array(data_index).shape) #print(data_index[cidx][1]) pos_dic = data_tfr_old[data_index[cidx][1]+minpos] neg_label = int(labelx) while True: neg_label = random.choice(range(1, 770)) #print(neg_label) if neg_label is not int(labelx) and os.path.isdir(os.path.join('/home/kuru/Desktop/adiusb/veri-split/train', strint(neg_label))) is True: break negative_label = strint(neg_label) neg_cid = pidx[negative_label] neg_index = random.choice(range(0, len(pkl[negative_label]))) neg_dic = data_tfr_old[data_index[neg_cid][1]+neg_index] trainX[i] = img[i] trainX[i+train_batch_size] = pos_dic[0] trainX[i+(train_batch_size*2)] = neg_dic[0] trainY[i] = cidx trainY[i+train_batch_size] = pos_dic[3] trainY[i+(train_batch_size*2)] = neg_dic[3] trainX = trainX.cuda() trainY = trainY.cuda() outputs, features = model(trainX) xent_loss = criterion_xent(outputs[0:train_batch_size], trainY[0:train_batch_size]) htri_loss = criterion_htri(features, trainY) #tri_loss = ranking_loss(features) #ent_loss = xent_loss(outputs[0:batch_size], trainY[0:batch_size], num_train_pids) loss = htri_loss+xent_loss optimizer.zero_grad() loss.backward() optimizer.step() batch_time.update(time.time() - end) losses.update(loss.item(), trainY.size(0)) htri_losses.update(htri_loss.item(), trainY.size(0)) xent_losses.update(xent_loss.item(), trainY.size(0)) accs.update(accuracy(outputs[0:train_batch_size], trainY[0:train_batch_size])[0]) if (batch_idx) % 50 == 0: print('Train ', end=" ") print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'TriLoss {loss.val:.4f} ({loss.avg:.4f})\t' 'XLoss {xloss.val:.4f} ({xloss.avg:.4f})\t' 'OveralLoss {oloss.val:.4f} ({oloss.avg:.4f})\t' 'Acc {acc.val:.2f} ({acc.avg:.2f})\t' 'lr {lrrr} \t'.format( epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, loss = htri_losses, xloss = xent_losses, oloss = losses, acc=accs , lrrr=lrrr, )) end = time.time() scheduler.step() print('=> Test') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1, distmat = test(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank1) rank2, distmat2 = test_rerank(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank2) #if (epoch + 1) == max_epoch: if (epoch + 1) % 2 == 0: print('=> Test') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1, distmat = test(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank1) # if vis_rank: # visualize_ranked_results( # distmat, dm.return_testdataset_by_name(name), # save_dir=osp.join(save_dir, 'ranked_results', name), # topk=20) save_checkpoint({ 'state_dict': model.state_dict(), 'rank1': rank1, 'epoch': epoch + 1, 'arch': arch, 'optimizer': optimizer.state_dict(), }, save_dir)
def train_joint( args, #train_vric_dataloader, val_vric_dataloader, train_veri_dataloader, val_veri_dataloader, # train_compcars_dataloader, val_compcars_dataloader, # train_boxcars_dataloader, val_boxcars_dataloader, # train_aic_dataloader, # test_compcars_dataloader, # test_boxcars_dataloader, base_net, veri_id_net): #, color_net, compcars_model_net, boxcars_model_net): optimizer_base = optim.Adam(base_net.parameters(), lr=args.lr) #optimizer_vric = optim.Adam(vric_id_net.parameters(), lr=args.lr) optimizer_veri = optim.Adam(veri_id_net.parameters(), lr=args.lr) # optimizer_color = optim.Adam(color_net.parameters(), lr=args.lr) # optimizer_compcars = optim.Adam(compcars_model_net.parameters(), lr=args.lr) # optimizer_boxcars = optim.Adam(boxcars_model_net.parameters(), lr=args.lr) criterion_triplet = TripletLoss(margin=margin, batch_hard=args.batch_hard) criterion_ce = nn.CrossEntropyLoss() logger = Logger(os.path.join(args.save_model_dir, 'train')) val_logger = Logger(os.path.join(args.save_model_dir, 'val')) test_logger = Logger(os.path.join(args.save_model_dir, 'test')) epoch_size = len( train_veri_dataloader ) #train_veri_dataloader, len(train_compcars_dataloader), len(train_boxcars_dataloader), len(train_aic_dataloader)) print("EPOCH SIZE: ", epoch_size) for e in range(args.n_epochs): pbar = tqdm(total=epoch_size, ncols=100, leave=True) pbar.set_description('Epoch %d' % (e)) for n in range(epoch_size): logger.append_epoch(e + float(n) / epoch_size) # VeRi dataset epoch_loss = 0 #or i, samples in enumerate(train_vric_dataloader): for i, samples in enumerate(train_veri_dataloader): if i == 1: break imgs = samples['img'].view( samples['img'].size(0) * samples['img'].size(1), samples['img'].size(2), samples['img'].size(3), samples['img'].size(4)) classes = samples['class'].view(-1) #colors = samples['color'].view(-1) b_img = Variable(imgs).cuda() classes = Variable(classes).cuda() #colors = Variable(colors).cuda() base_net.zero_grad() #vric_id_net.zero_grad() veri_id_net.zero_grad() #color_net.zero_grad() #forward pred_feat = base_net(b_img) #pred_id = vric_id_net(pred_feat) pred_id = veri_id_net(pred_feat) #pred_color = color_net(pred_feat) b_loss_triplet = criterion_triplet(pred_feat, classes) loss_id = criterion_ce(pred_id, classes) #loss_color = criterion_ce(pred_color, colors) loss = b_loss_triplet.mean() + loss_id epoch_loss += loss.data[0] # backward loss.backward() optimizer_base.step() #optimizer_vric.step() optimizer_veri.step() #optimizer_color.step() # logger.logg({'loss_vric_triplet': b_loss_triplet.data.mean(), # 'loss_vric_triplet_max': b_loss_triplet.data.max(), # 'loss_vric_triplet_min': b_loss_triplet.data.min(), # 'loss_vric_id': loss_id.data[0]}) logger.logg({ 'loss_veri_triplet': b_loss_triplet.data.mean(), 'loss_veri_triplet_max': b_loss_triplet.data.max(), 'loss_veri_triplet_min': b_loss_triplet.data.min(), 'loss_veri_id': loss_id.data[0] }) #'loss_veri_color': loss_color.data[0]}) # # Compcars dataset # for i, samples in enumerate(train_compcars_dataloader): # if i==1: break # img = Variable(samples['img']).cuda() # model = Variable(samples['model']).cuda() # color = Variable(samples['color']).cuda() # base_net.zero_grad() # compcars_model_net.zero_grad() # color_net.zero_grad() # #forward # pred_feat = base_net(img) # pred_model = compcars_model_net(pred_feat) # pred_color = color_net(pred_feat) # loss_model = criterion_ce(pred_model, model) # loss_color = criterion_ce(pred_color, color) # loss = loss_model + loss_color # epoch_loss += loss.data[0] # # backward # loss.backward() # optimizer_base.step() # optimizer_compcars.step() # optimizer_color.step() # logger.logg({'loss_compcars_model': loss_model.data[0], # 'loss_compcars_color': loss_color.data[0]}) # # Boxcars dataset # for i, samples in enumerate(train_boxcars_dataloader): # if i==1: break # img = Variable(samples['img']).cuda() # model = Variable(samples['model']).cuda() # base_net.zero_grad() # boxcars_model_net.zero_grad() # #forward # pred_feat = base_net(img) # pred_model = boxcars_model_net(pred_feat) # loss_model = criterion_ce(pred_model, model) # loss = loss_model # epoch_loss += loss.data[0] # # backward # loss.backward() # optimizer_base.step() # optimizer_boxcars.step() # logger.logg({'loss_boxcars_model': loss_model.data[0]}) # # AIC dataset # for i, samples in enumerate(train_aic_dataloader): # if i==1: break # imgs = samples['img'].squeeze(0) # pos_mask = samples['pos_mask'].squeeze(0) # neg_mask = samples['neg_mask'].squeeze(0) # b_img = Variable(imgs).cuda() # pos_mask = Variable(pos_mask).cuda() # neg_mask = Variable(neg_mask).cuda() # base_net.zero_grad() # #forward # pred_feat = base_net(b_img) # b_loss = criterion_triplet(pred_feat, pos_mask=pos_mask, neg_mask=neg_mask, mode='mask') # loss = b_loss.mean() # epoch_loss += loss.data[0] # # backward # loss.backward() # optimizer_base.step() # logger.logg({'loss_aic_triplet': b_loss.data.mean(), # 'loss_aic_triplet_max': b_loss.data.max(), # 'loss_aic_triplet_min': b_loss.data.min()}) pbar.update(1) pbar.set_postfix({'loss': '%f' % (epoch_loss / (n + 1))}) pbar.close() print('Training total loss = %f' % (epoch_loss / epoch_size)) if e % args.save_every_n_epoch == 0: torch.save( base_net.state_dict(), os.path.join(args.save_model_dir, 'model_%d_base.ckpt' % (e))) torch.save( base_net.state_dict(), os.path.join(args.save_model_dir, 'model_%d_base.pth' % (e))) #torch.save(vric_id_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_vric_id.ckpt'%(e))) #torch.save(vric_id_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_vric_id.pth'%(e))) #torch.save(veri_id_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_veri_id.pth'%(e))) # torch.save(compcars_model_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_compcars_model.ckpt'%(e))) # torch.save(color_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_color.ckpt'%(e))) # torch.save(boxcars_model_net.state_dict(),os.path.join(args.save_model_dir,'model_%d_boxcars_model.ckpt'%(e))) logger.write_log() print('start validation') val_logger.append_epoch(e) base_net.eval() #vric_id_net.eval() veri_id_net.eval() #color_net.eval() #compcars_model_net.eval() #boxcars_model_net.eval() # VRIC # correct = [] # for i,sample in enumerate(val_veri_dataloader): # imgs = sample['img'].view(sample['img'].size(0)*sample['img'].size(1), # sample['img'].size(2), # sample['img'].size(3), # sample['img'].size(4)) # classes = sample['class'].view(sample['class'].size(0)*sample['class'].size(1)) # img = Variable(imgs,volatile=True).cuda() # gt = Variable(classes,volatile=True).cuda() # #pred = vric_id_net(base_net(img)) # pred = veri_id_net(base_net(img)) # print("Validation Pred shape: ", pred.shape) # _, pred_cls = torch.max(pred,dim=1) # print("Validation GT: ", gt) # print("Validation Pred Cls: ", pred_cls) # correct.append(pred_cls.data==gt.data) # print("CORRECT: ", correct) # acc = torch.cat(correct).float().mean() # print('VRIC ID val acc: %.3f' % acc) # val_logger.logg({'vric_id_acc':acc}) correct = [] for i, samples in enumerate(val_veri_dataloader): imgs = samples['img'].view( samples['img'].size(0) * samples['img'].size(1), samples['img'].size(2), samples['img'].size(3), samples['img'].size(4)) classes = samples['class'].view(samples['class'].size(0) * samples['class'].size(1)) b_img = Variable(imgs, volatile=True).cuda() b_class = Variable(classes, volatile=True).cuda() #pred = veri_id_net(base_net(b_img)) pred_feat = base_net(b_img) #pred_cls = torch.max(pred,dim=1) b_loss = criterion_triplet(pred_feat, b_class).data.cpu().numpy().squeeze() print("b_loss: ", b_loss) print("b_loss: ", b_loss.shape) for x in range(b_loss.shape[0]): if b_loss[x] < 1e-3: correct.append(1) else: correct.append(0) print('val acc: %.3f' % (np.mean(correct))) #val_logger.logg({'vric_id_acc':acc}) # correct.append(pred_cls.data==gt.data) # acc = torch.cat(correct).float().mean() # print('VeRi ID val acc: %.3f' % acc) # val_logger.logg({'veri_id_acc':acc}) ''' # VeRi correct = [] for i,sample in enumerate(val_veri_dataloader): imgs = sample['img'].view(sample['img'].size(0)*sample['img'].size(1), sample['img'].size(2), sample['img'].size(3), sample['img'].size(4)) classes = sample['class'].view(sample['class'].size(0)*sample['class'].size(1)) img = Variable(imgs,volatile=True).cuda() gt = Variable(classes,volatile=True).cuda() pred = veri_id_net(base_net(img)) _, pred_cls = torch.max(pred,dim=1) correct.append(pred_cls.data==gt.data) acc = torch.cat(correct).float().mean() print('VeRi ID val acc: %.3f' % acc) val_logger.logg({'veri_id_acc':acc}) ''' # # Compcars # correct_model = [] # correct_color = [] # for i,sample in enumerate(val_compcars_dataloader): # img = Variable(sample['img'],volatile=True).cuda() # gt_model = Variable(sample['model'],volatile=True).cuda() # gt_color = Variable(sample['color'],volatile=True).cuda() # pred_feat = base_net(img) # pred_model = compcars_model_net(pred_feat) # pred_color = color_net(pred_feat) # _, pred_model = torch.max(pred_model,dim=1) # _, pred_color = torch.max(pred_color,dim=1) # correct_model.append(pred_model.data == gt_model.data) # correct_color.append(pred_color.data == gt_color.data) # acc_model = torch.cat(correct_model).float().mean() # acc_color = torch.cat(correct_color).float().mean() # print('CompCars model val acc: %.3f' % acc_model) # print('CompCars color val acc: %.3f' % acc_color) # val_logger.logg({'compcars_model_acc':acc_model}) # val_logger.logg({'compcars_color_acc':acc_color}) # # Boxcars # correct_model = [] # for i,sample in enumerate(val_boxcars_dataloader): # img = Variable(sample['img'],volatile=True).cuda() # gt_model = Variable(sample['model'],volatile=True).cuda() # pred_feat = base_net(img) # pred_model =boxcars_model_net(pred_feat) # _, pred_model = torch.max(pred_model,dim=1) # correct_model.append(pred_model.data == gt_model.data) # acc_model = torch.cat(correct_model).float().mean() # print('BoxCars model val acc: %.3f' % acc_model) # val_logger.logg({'boxcars_model_acc':acc_model}) # val_logger.write_log() # if e%25 == 0: # print('start testing') # test_logger.append_epoch(e) # pbar = tqdm(total=len(test_boxcars_dataloader),ncols=100,leave=True) # pbar.set_description('Test BoxCar') # correct_model = [] # for i,sample in enumerate(test_boxcars_dataloader): # img = Variable(sample['img'],volatile=True).cuda() # gt_model = Variable(sample['model'],volatile=True).cuda() # pred_feat = base_net(img) # pred_model =boxcars_model_net(pred_feat) # _, pred_model = torch.max(pred_model,dim=1) # correct_model.append(pred_model.data == gt_model.data) # pbar.update(1) # pbar.close() # acc_model = torch.cat(correct_model).float().mean() # print('BoxCars model val acc: %.3f' % acc_model) # test_logger.logg({'boxcars_model_acc':acc_model}) # pbar = tqdm(total=len(test_compcars_dataloader),ncols=100,leave=True) # pbar.set_description('Test CompCar_sv') # correct_model = [] # correct_color = [] # for i,sample in enumerate(test_compcars_dataloader): # img = Variable(sample['img'],volatile=True).cuda() # gt_model = Variable(sample['model'],volatile=True).cuda() # gt_color = Variable(sample['color'],volatile=True).cuda() # pred_feat = base_net(img) # pred_model = compcars_model_net(pred_feat) # pred_color = color_net(pred_feat) # _, pred_model = torch.max(pred_model,dim=1) # _, pred_color = torch.max(pred_color,dim=1) # correct_model.append(pred_model.data == gt_model.data) # correct_color.append(pred_color.data == gt_color.data) # pbar.update(1) # pbar.close() # acc_model = torch.cat(correct_model).float().mean() # acc_color = torch.cat(correct_color).float().mean() # print('CompCars model val acc: %.3f' % acc_model) # print('CompCars color val acc: %.3f' % acc_color) # test_logger.logg({'compcars_model_acc':acc_model}) # test_logger.logg({'compcars_color_acc':acc_color}) # test_logger.write_log() base_net.train() #vric_id_net.train() veri_id_net.train()
def main(): dataroot = "2data" datatrainroot = "2data/training_data" datatestroot = "2data/testing_data" datavalidroot = "2data/valid_data" num_triplets_train = 1280 num_triplets_test = 1280 model_architecture = "restnet18" epochs = 3333333 resume_path = "resume" batch_size = 256 num_workers = 1 embedding_dimension = 128 pretrained = True#args.pretrained optimizer = "sgd"#args.optimizer learning_rate = 0.1#args.lr margin = 0.5#args.margin start_epoch = 0 img_size = (224, 224) data_transforms = transforms.Compose([ transforms.Resize(size=img_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize( mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5] ) ]) # Set dataloaders train_dataloader = dataloaderTriplet(datatrainroot, data_transforms, num_triplets =num_triplets_train, batchsize=batch_size, resolution = img_size ) test_dataloader = dataloaderTriplet(datatestroot, data_transforms, num_triplets = num_triplets_train, batchsize = batch_size, resolution= img_size ) # Instantiate model model = Resnet18Triplet(embedding_dimension=embedding_dimension,pretrained=pretrained) if model_architecture == "resnet18": model = Resnet18Triplet( embedding_dimension=embedding_dimension, pretrained=pretrained ) elif model_architecture == "resnet34": model = Resnet34Triplet( embedding_dimension=embedding_dimension, pretrained=pretrained ) elif model_architecture == "resnet50": model = Resnet50Triplet( embedding_dimension=embedding_dimension, pretrained=pretrained ) elif model_architecture == "resnet101": model = Resnet101Triplet( embedding_dimension=embedding_dimension, pretrained=pretrained ) elif model_architecture == "inceptionresnetv2": model = InceptionResnetV2Triplet( embedding_dimension=embedding_dimension, pretrained=pretrained ) print("Using {} model architecture.".format(model_architecture)) # Load model to GPU or multiple GPUs if available flag_train_gpu = torch.cuda.is_available() flag_train_multi_gpu = False """ if flag_train_gpu and torch.cuda.device_count() > 1: model = nn.DataParallel(model) model.cuda() flag_train_multi_gpu = True print('Using multi-gpu training.') elif flag_train_gpu and torch.cuda.device_count() == 1: model.cuda() print('Using single-gpu training.') """ cuda0 = torch.device("cuda:0") model.to(cuda0) # Set optimizers if optimizer == "sgd": optimizer_model = torch.optim.SGD(model.parameters(), lr=learning_rate) elif optimizer == "adagrad": optimizer_model = torch.optim.Adagrad(model.parameters(), lr=learning_rate) elif optimizer == "rmsprop": optimizer_model = torch.optim.RMSprop(model.parameters(), lr=learning_rate) elif optimizer == "adam": optimizer_model = torch.optim.Adam(model.parameters(), lr=learning_rate) # Optionally resume from a checkpoint """ if resume_path: if os.path.isfile(resume_path): print("\nLoading checkpoint {} ...".format(resume_path)) checkpoint = torch.load(resume_path) start_epoch = checkpoint['epoch'] # In order to load state dict for optimizers correctly, model has to be loaded to gpu first if flag_train_multi_gpu: model.module.load_state_dict(checkpoint['model_state_dict']) else: model.load_state_dict(checkpoint['model_state_dict']) optimizer_model.load_state_dict(checkpoint['optimizer_model_state_dict']) print("\nCheckpoint loaded: start epoch from checkpoint = {}\nRunning for {} epochs.\n".format( start_epoch, epochs-start_epoch ) ) else: print("WARNING: No checkpoint found at {}!\nTraining from scratch.".format(resume_path)) """ # Start Training loop print("\nTraining using triplet loss on {} triplets starting for {} epochs:\n".format(num_triplets_train,epochs-start_epoch)) total_time_start = time.time() start_epoch = start_epoch end_epoch = start_epoch + epochs l2_distance = PairwiseDistance(2).to(cuda0) for epoch in range(start_epoch, end_epoch): epoch_time_start = time.time() triplet_loss_sum = 0 num_valid_for_training_triplets = 0 num_total_train_phase_triplets = 0 num_total_test_phase_triplets = 0 num_right_train_phase_triplets = 0 num_right_test_phase_triplets = 0 for phase in ['train', 'test']: if phase == 'train': model.train() dataloader = train_dataloader else : with torch.no_grad(): model.eval() dataloader = test_dataloader for batch_idx, (batch_sample) in enumerate(dataloader): if phase == 'test': with torch.no_grad(): anc_img = batch_sample[0].to(cuda0) pos_img = batch_sample[1].to(cuda0) neg_img = batch_sample[2].to(cuda0) # Forward pass - compute embedding anc_embedding = model(anc_img) pos_embedding = model(pos_img) neg_embedding = model(neg_img) # Forward pass - choose hard negatives only for training pos_dist = l2_distance.forward(anc_embedding, pos_embedding).cpu() neg_dist = l2_distance.forward(anc_embedding, neg_embedding).cpu() all = (neg_dist - pos_dist < margin).numpy().flatten() hard_triplets = np.where(all == 1) num_total_test_phase_triplets += len(all) num_right_test_phase_triplets += len(all)-len(hard_triplets[0]) if phase == 'train': anc_img = batch_sample[0].to(cuda0) pos_img = batch_sample[1].to(cuda0) neg_img = batch_sample[2].to(cuda0) # Forward pass - compute embedding anc_embedding = model(anc_img) pos_embedding = model(pos_img) neg_embedding = model(neg_img) # Forward pass - choose hard negatives only for training pos_dist = l2_distance.forward(anc_embedding, pos_embedding).cpu() neg_dist = l2_distance.forward(anc_embedding, neg_embedding).cpu() all = (neg_dist - pos_dist < margin).numpy().flatten() hard_triplets = np.where(all == 1) num_total_train_phase_triplets += len(all) num_right_train_phase_triplets += len(all)-len(hard_triplets[0]) if len(hard_triplets[0]) == 0: continue anc_hard_embedding = anc_embedding[hard_triplets].to(cuda0) pos_hard_embedding = pos_embedding[hard_triplets].to(cuda0) neg_hard_embedding = neg_embedding[hard_triplets].to(cuda0) # Calculate triplet loss triplet_loss = TripletLoss(margin=margin).forward(anchor=anc_hard_embedding,positive=pos_hard_embedding,negative=neg_hard_embedding).to(cuda0) # Calculating loss triplet_loss_sum += triplet_loss.item() if phase == 'train': num_valid_for_training_triplets += len(anc_hard_embedding) optimizer_model.zero_grad() if phase == 'train': # Backward pass triplet_loss.backward() optimizer_model.step() # Model only trains on hard negative triplets avg_triplet_loss = 0 if (num_valid_for_training_triplets == 0) else triplet_loss_sum / num_valid_for_training_triplets epoch_time_end = time.time() if phase == 'train': accuracy = (1.0 * num_right_train_phase_triplets )/(1.0 *num_total_train_phase_triplets) if phase == 'test': accuracy = (1.0 * num_right_test_phase_triplets) / (1.0 * num_total_test_phase_triplets) # Print training statistics and add to log print('Epoch {} Phase {}:\tAverage Triplet Loss: {:.4f}\tEpoch Time: {:.3f} hours\tNumber of valid training triplets : {}\tAccuracy : {}'.format(epoch+1,phase, avg_triplet_loss,(epoch_time_end - epoch_time_start)/3600,num_valid_for_training_triplets, accuracy)) with open('logs/{}_log_triplet_{}.txt'.format(model_architecture, phase), 'a') as f: val_list = [epoch+1,phase,avg_triplet_loss,num_valid_for_training_triplets, accuracy] log = '\t'.join(str(value) for value in val_list) f.writelines(log + '\n') # Save model checkpoint state = { 'epoch': epoch+1, 'embedding_dimension': embedding_dimension, 'batch_size_training': batch_size, 'model_state_dict': model.state_dict(), 'model_architecture': model_architecture, 'optimizer_model_state_dict': optimizer_model.state_dict() } # For storing data parallel model's state dictionary without 'module' parameter if flag_train_multi_gpu: state['model_state_dict'] = model.module.state_dict() # Save model checkpoint torch.save(state, 'Model_training_checkpoints/model_{}_triplet_epoch_{}.pt'.format(model_architecture, epoch+1)) # Training loop end total_time_end = time.time() total_time_elapsed = total_time_end - total_time_start print("\nTraining finished: total time elapsed: {:.2f} hours.".format(total_time_elapsed/3600))
def main(): init_log_just_created("log/valid.csv") init_log_just_created("log/train.csv") import pandas as pd valid = pd.read_csv('log/valid.csv') max_acc = valid['acc'].max() pretrain = args.pretrain fc_only = args.fc_only except_fc = args.except_fc train_all = args.train_all unfreeze = args.unfreeze.split(',') freeze = args.freeze.split(',') start_epoch = 0 print(f"Transfer learning: {pretrain}") print("Train fc only:", fc_only) print("Train except fc:", except_fc) print("Train all layers:", train_all) print("Unfreeze only:", ', '.join(unfreeze)) print("Freeze only:", ', '.join(freeze)) print(f"Max acc: {max_acc:.4f}") print(f"Learning rate will decayed every {args.step_size}th epoch") model = FaceNetModel(pretrained=pretrain) model.to(device) triplet_loss = TripletLoss(args.margin).to(device) if fc_only: model.unfreeze_only(['fc', 'classifier']) if except_fc: model.freeze_only(['fc', 'classifier']) if train_all: model.unfreeze_all() if len(unfreeze) > 0: model.unfreeze_only(unfreeze) if len(freeze) > 0: model.freeze_only(freeze) optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.learning_rate) scheduler = lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=0.1) if args.load_best or args.load_last: checkpoint = './log/best_state.pth' if args.load_best else './log/last_checkpoint.pth' print('loading', checkpoint) checkpoint = torch.load(checkpoint) modelsaver.current_acc = max_acc start_epoch = checkpoint['epoch'] + 1 model.load_state_dict(checkpoint['state_dict']) print("Stepping scheduler") try: optimizer.load_state_dict(checkpoint['optimizer_state']) except ValueError as e: print("Can't load last optimizer") print(e) if args.continue_step: scheduler.step(checkpoint['epoch']) print(f"Loaded checkpoint epoch: {checkpoint['epoch']}\n" f"Loaded checkpoint accuracy: {checkpoint['accuracy']}\n" f"Loaded checkpoint loss: {checkpoint['loss']}") model = torch.nn.DataParallel(model) for epoch in range(start_epoch, args.num_epochs + start_epoch): print(80 * '=') print('Epoch [{}/{}]'.format(epoch, args.num_epochs + start_epoch - 1)) time0 = time.time() data_loaders, data_size = get_dataloader(args.train_root_dir, args.valid_root_dir, args.train_csv_name, args.valid_csv_name, args.num_train_triplets, args.num_valid_triplets, args.batch_size, args.num_workers) train_valid(model, optimizer, triplet_loss, scheduler, epoch, data_loaders, data_size) print(f' Execution time = {time.time() - time0}') print(80 * '=')
def train_save(model, trainset_loader, testset_loader, opt, epoch=5, loss_cri='Focal', save_interval=4000, log_interval=100, device='cpu', save_ep=False): os.makedirs('./models/', exist_ok=True) os.makedirs('./models/{}/'.format(opt.model_name), exist_ok=True) if opt.optim == 'Adam': optimizer = optim.Adam(model.parameters(), lr=opt.lr, betas=(0.9, 0.999)) elif opt.optim == 'SGD': optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=0.9) if loss_cri == 'Focal': criterion = FocalLoss(alpha=opt.alpha, gamma=opt.gamma) elif loss_cri == 'Triplet': criterion = TripletLoss(margin=opt.margin) elif loss_cri == 'Center': criterion = CenterLoss() # criterion = nn.CrossEntropyLoss() iteration = 0 for ep in range(epoch): model.train() # set training mode for batch_idx, (features, labels) in enumerate(trainset_loader): features, labels = features.to(device), labels.squeeze(1).to( device) optimizer.zero_grad() output = model(features) loss = criterion(output, labels) loss.backward() optimizer.step() if iteration % log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( ep, batch_idx * len(features), len(trainset_loader.dataset), 100. * batch_idx / len(trainset_loader), loss.item())) if iteration % save_interval == 0 and iteration > 0: save_checkpoint( './models/{}/{}_backup.pth'.format(opt.model_name, opt.model_name), model, optimizer) iteration += 1 if opt.save_ep: save_checkpoint( './models/{}/{}_epoch_{}.pth'.format(opt.model_name, opt.model_name, ep), model, optimizer) eval(model, testset_loader, opt, criterion, threshold=opt.threshold, threshold_2=opt.threshold_2, epoch=ep) # save the final model save_checkpoint( './models/{}/{}_final.pth'.format(opt.model_name, opt.model_name), model, optimizer)
val_dataset = MadoriDataset( train=False) if Config.network == 'siamese' else TriMadoriDataset( train=False) # data loaders train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=Config.batch_size) val_dataloader = DataLoader(val_dataset, shuffle=False, batch_size=Config.batch_size) # models net = SiameseNetwork() if Config.network == 'siamese' else TripletNetwork() net = net.to(device) criterion = ContrastiveLoss() if Config.network == 'siamese' else TripletLoss() optimizer = optim.Adam(net.parameters(), lr=0.0005) def train_siamese(): train_loss_history, val_loss_history = [], [] lowest_epoch_train_loss = lowest_epoch_val_loss = float('inf') for epoch in tqdm(range(Config.train_number_epochs)): # training net.train() epoch_train_loss = 0 for batch_no, data in enumerate(train_dataloader): img0, img1, label = data img0, img1, label = img0.to(device), img1.to(device), label.to( device)
def train(parameters: Dict[str, float]) -> nn.Module: global args print("====", args.focus, "=====") torch.manual_seed(args.seed) # args.gpu_devices = "0,1" os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) dataset = data_manager.init_dataset(name=args.dataset, sampling= args.sampling) transform_test = transforms.Compose([ transforms.Resize((args.height, args.width), interpolation=3), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) pin_memory = True if use_gpu else False transform_train = transforms.Compose([ transforms.Resize((args.height, args.width), interpolation=3), transforms.RandomHorizontalFlip(p=0.5), transforms.Pad(10), Random2DTranslation(args.height, args.width), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) batch_size = int(round(parameters.get("batch_size", 32) )) base_learning_rate = 0.00035 # weight_decay = 0.0005 alpha = parameters.get("alpha", 1.2) sigma = parameters.get("sigma", 0.8) l = parameters.get("l", 0.5) beta_ratio = parameters.get("beta_ratio", 0.5) gamma = parameters.get("gamma", 0.1) margin = parameters.get("margin", 0.3) weight_decay = parameters.get("weight_decay", 0.0005) lamb = 0.3 num_instances = 4 pin_memory = True trainloader = DataLoader( VideoDataset(dataset.train, seq_len=args.seq_len, sample='random',transform=transform_train), sampler=RandomIdentitySampler(dataset.train, num_instances=args.num_instances), batch_size=batch_size, num_workers=args.workers, pin_memory=pin_memory, drop_last=True, ) if args.dataset == 'mars_subset' : validation_loader = DataLoader( VideoDataset(dataset.val, seq_len=8, sample='random', transform=transform_test), batch_size=args.test_batch, shuffle=False, num_workers=args.workers, pin_memory=pin_memory, drop_last=False, ) else: queryloader = DataLoader( VideoDataset(dataset.val_query, seq_len=args.seq_len, sample='dense_subset', transform=transform_test), batch_size=args.test_batch, shuffle=False, num_workers=args.workers, pin_memory=pin_memory, drop_last=False, ) galleryloader = DataLoader( VideoDataset(dataset.val_gallery, seq_len=args.seq_len, sample='dense_subset', transform=transform_test), batch_size=args.test_batch, shuffle=False, num_workers=args.workers, pin_memory=pin_memory, drop_last=False, ) criterion_htri = TripletLoss(margin, 'cosine') criterion_xent = CrossEntropyLabelSmooth(dataset.num_train_pids) criterion_center_loss = CenterLoss(use_gpu=1) criterion_osm_caa = OSM_CAA_Loss(alpha=alpha , l=l , osm_sigma=sigma ) args.arch = "ResNet50ta_bt" model = models.init_model(name=args.arch, num_classes=dataset.num_train_pids, loss={'xent', 'htri'}) if use_gpu: model = nn.DataParallel(model).cuda() params = [] for key, value in model.named_parameters(): if not value.requires_grad: continue lr = base_learning_rate weight_decay = weight_decay params += [{"params": [value], "lr": lr, "weight_decay": weight_decay}] optimizer = torch.optim.Adam(params) scheduler = WarmupMultiStepLR(optimizer, milestones=[40, 70], gamma=gamma, warmup_factor=0.01, warmup_iters=10) optimizer_center = torch.optim.SGD(criterion_center_loss.parameters(), lr=0.5) start_epoch = args.start_epoch best_rank1 = -np.inf num_epochs = 121 if 'mars' not in args.dataset : num_epochs = 121 # test_rerank(model, queryloader, galleryloader, args.pool, use_gpu, lamb=lamb , parameters=parameters) for epoch in range (num_epochs): vals = train_model(model, criterion_xent, criterion_htri, optimizer, trainloader, use_gpu , optimizer_center , criterion_center_loss, criterion_osm_caa, beta_ratio) if math.isnan(vals[0]): return 0 scheduler.step() if epoch % 40 ==0 : print("TripletLoss {:.6f} OSM Loss {:.6f} Cross_entropy {:.6f} Total Loss {:.6f} ".format(vals[1] , vals[3] , vals[1] , vals[0])) if args.dataset == 'mars_subset' : result1 = test_validation(model, validation_loader, args.pool, use_gpu, parameters=parameters) del validation_loader else: result1= test_rerank(model, queryloader, galleryloader, args.pool, use_gpu, lamb=lamb , parameters=parameters) del queryloader del galleryloader del trainloader del model del criterion_htri del criterion_xent del criterion_center_loss del criterion_osm_caa del optimizer del optimizer_center del scheduler return result1
def train_on_pretrained_model(options): transform = transforms.Compose([ # [1] transforms.Resize(options.img_size), # [2] transforms.CenterCrop(224), # [3] transforms.ToTensor(), # [4] transforms.Normalize( # [5] mean=[0.485, 0.456, 0.406], # [6] std=[0.229, 0.224, 0.225] # [7] ) ]) with open(options.bert_path, "rb") as fin: bert_tensors = pickle.load(fin) print("loaded bert tensors of size", len(bert_tensors)) train_set = datasets.ImageFolder(options.train_folder_path, transform=transform) print("size of original train_set", len(train_set.imgs)) embed_dim = bert_tensors[0].shape[0] bert_tensors_in_train = torch.tensor( np.array([bert_tensors[int(label)] for label in train_set.classes])) # Making sure that we do not change the BERT values. bert_tensors_in_train.requires_grad = False train_triplet_set = TripletDataSet(image_folder=train_set, bert_tensors=bert_tensors_in_train, num_neg_samples=options.neg_samples, is_train_data=True) train_loader = torch.utils.data.DataLoader(train_triplet_set, batch_size=options.batch_size, shuffle=True) valid_set = datasets.ImageFolder(options.valid_folder_path, transform=transform) print("size of original valid_set", len(valid_set.imgs)) valid_triplet_set = TripletDataSet(image_folder=valid_set, bert_tensors=bert_tensors_in_train, num_neg_samples=options.neg_samples, is_train_data=False) valid_loader = torch.utils.data.DataLoader(valid_triplet_set, batch_size=options.batch_size, shuffle=False) print("number of classes in trainset", len(train_set.classes)) print("saving the BERT tensors") with open(options.model_path + ".configs", "wb") as fout: pickle.dump( (bert_tensors_in_train, train_set.class_to_idx, options.img_size), fout) model = init_net(embed_dim, options) num_epochs = 100 criterion = TripletLoss() # Observe that all parameters are being optimized optimizer = optim.Adam(model.parameters(), lr=options.lr) scheduler = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer) best_loss = float("inf") num_steps, current_steps, running_loss = 0, 0, 0 no_improvement = 0 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Move model to the device specified above print("device is", device) num_gpu = torch.cuda.device_count() if num_gpu > 1: print("Let's use", num_gpu, "GPUs!") model = torch.nn.DataParallel(model) model = model.to(device) for epoch in range(num_epochs): print("training epoch", epoch + 1) for inputs, labels in train_loader: anchor = inputs[0].to(device) positive = inputs[1].to(device) negative = inputs[2].to(device) optimizer.zero_grad() anchor_outputs = model(anchor) loss = criterion(anchor=anchor_outputs, positive=positive, negative=negative) loss.backward() optimizer.step() if num_gpu > 1: loss = loss.mean() running_loss += loss.item() current_steps += 1 num_steps += 1 if current_steps % 100 == 0: print("epoch", epoch, "num_step", num_steps, "running_loss", running_loss / current_steps) current_steps, running_loss = 0, 0 loss_value, total = 0, 0 with torch.no_grad(): model.training = False for inputs, labels in valid_loader: anchor = inputs[0].to(device) positive = inputs[1].to(device) negative = inputs[2].to(device) anchor_outputs = model(anchor) loss = criterion(anchor=anchor_outputs, positive=positive, negative=negative) total += anchor_outputs.size(0) loss_value += loss.item() current_loss = 100.0 * loss_value / total print("current dev loss", current_loss) if current_loss < best_loss: best_loss = current_loss print("saving best dev loss", best_loss) torch.save(model, options.model_path) improved = True else: improved = False no_improvement = 0 if improved else no_improvement + 1 if no_improvement >= 100 and epoch > 3: # no improvement for a long time, and at least 3 epochs print("no improvement over time--> finish") sys.exit(0) model.training = True scheduler.step(-current_loss)
sigma = 0.8769823511927456 alpha = 1.5747007053351507 l = 0.5241677630566622 margin = 0.040520629258433416 beta_ratio = 0.7103921571238655 gamma = 0.368667605025003 weight_decay = 0.014055481861393148 args.arch = "ResNet50ta_bt" model = models.init_model(name=args.arch, num_classes=dataset.num_train_pids, loss={'xent', 'htri'}) print("Model size: {:.5f}M".format( sum(p.numel() for p in model.parameters()) / 1000000.0)) criterion_htri = TripletLoss(margin, 'cosine') criterion_xent = CrossEntropyLabelSmooth(dataset.num_train_pids) criterion_center_loss = CenterLoss(use_gpu=use_gpu) if args.use_OSMCAA: print("USING OSM LOSS") print("config, alpha = %f sigma = %f l=%f" % (alpha, sigma, l)) criterion_osm_caa = OSM_CAA_Loss(alpha=alpha, l=l, osm_sigma=sigma) else: criterion_osm_caa = None if args.cl_centers: print("USING CL CENTERS") print("config, alpha = %f sigma = %f l=%f" % (alpha, sigma, l)) criterion_osm_caa = OSM_CAA_Loss(alpha=alpha, l=l, osm_sigma=sigma)
def train(model, dataset, num_classes, optimizer, scheduler): batch_size = args.batch_size is_best = False best_rank = 0 print("=> Start training") # loss criterion_t = TripletLoss(margin=args.margin).to("cuda") criterion_x = CrossEntropyLossLS(num_classes=num_classes, epsilon=args.epsilon).to("cuda") weight_t = args.weight_t weight_x = 1.0 - args.weight_t _, train_id, _ = map(list, zip(*dataset.train)) train_dataset = ImageDataset(dataset.train, flag="train", process_size=(args.image_height, args.image_width)) # *****training*******# for epoch in range(0, args.max_epoch): # shift to train model.train() indicies = [ x for x in RandomIdentitySampler(train_id, batch_size, args.num_instances) ] for i in range(len(indicies) // batch_size): try: # train_batch[0,1,2] are [imgs, pid, cam_id] imgs, pids, _ = train_dataset.__getbatch__( indicies[i * batch_size:(i + 1) * batch_size]) except: imgs, pids, _ = train_dataset.__getbatch__( indicies[-batch_size:]) imgs = flow.Tensor(np.array(imgs)).to("cuda") pids = flow.Tensor(np.array(pids), dtype=flow.int32).to("cuda") outputs, features = model(imgs) loss_t = compute_loss(criterion_t, features, pids) loss_x = compute_loss(criterion_x, outputs, pids) loss = weight_t * loss_t + weight_x * loss_x loss.backward() optimizer.step() optimizer.zero_grad() scheduler.step() print( "epoch:", epoch + 1, "loss_t:", loss_t.numpy(), "loss_x:", loss_x.numpy(), "loss:", loss.numpy(), "lr:", optimizer.param_groups[0]["lr"], ) # *****testing********# if (epoch + 1) % args.eval_freq == 0 and (epoch + 1) != args.max_epoch: rank1, mAP = evaluate(model, dataset) if (rank1 + mAP) / 2.0 > best_rank: is_best = True else: is_best = False if is_best: flow.save(model.state_dict(), args.flow_weight + "_" + str(epoch)) print("=> End training") print("=> Final test") rank1, _ = evaluate(model, dataset) flow.save(model.state_dict(), args.flow_weight)
sampler=ClassUniformlySampler( dataset_text, class_position=1, k=4, seeds=seeds)) # dataloader_txt = IterLoader(loader_txt) # print('dataloader_txt: ',dataloader_txt.next_one()[1]) ############################################## model = Merge_image_text( num_class=11003, mode='test') #Stage II ,change to 'test',Stage I:'train' model = model.to(device) # criterion = nn.CrossEntropyLoss() criterion = TripletLoss(margin=1).cuda() #no margin means soft-margin #delete module parallel optimizer = optim.SGD( [ { 'params': model.image_feature.backbone.parameters(), 'lr': arg.learning_rate }, # {'params':model.image_feature.fc1.parameters(),'lr':arg.learning_rate}, # {'params':model.image_feature.fc.parameters(),'lr':arg.learning_rate}, { 'params': model.text_feature.parameters(), 'lr': arg.learning_rate / 10 } ],
def save_network(network, epoch_label): save_filename = 'net_%s.pth' % epoch_label save_path = os.path.join('./logs', name, save_filename) torch.save(network.cpu().state_dict(), save_path) if torch.cuda.is_available: network.cuda() if __name__ == '__main__': model = ResNet50(len(class_names), feat_size, metric, margin, scalar, dropout).cuda() xent_criterion = nn.CrossEntropyLoss() tri_criterion = TripletLoss() criterion = [xent_criterion, tri_criterion] # SGD_Step if optim_type == 'SGD_Step': optimizer = optim.SGD(params=model.parameters(), lr=0.01, weight_decay=5e-4, momentum=0.9, nesterov=True) lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=40, gamma=0.1) #lr_scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[20, 60, 70, 80], gamma=0.1) # SGD_Warmup elif optim_type == 'SGD_Warmup': lr_steps = [80, 180] init_lr = 1e-4
def __init__(self, conf, inference=False): logging.info(f'metric learner use {conf}') self.model = torch.nn.DataParallel(BNInception()).cuda() logging.info(f'model generated') if not inference: if conf.use_dataset == 'CUB': self.dataset = MSBaseDataSet(conf, './datasets/CUB_200_2011/cub_train.txt', transform=conf.transform_dict['rand-crop'], mode='RGB') elif conf.use_dataset == 'Cars': self.dataset = MSBaseDataSet(conf, './datasets/CARS196/cars_train.txt', transform=conf.transform_dict['rand-crop'], mode='RGB') elif conf.use_dataset == 'SOP': self.dataset = MSBaseDataSet(conf, './datasets/SOP/sop_train.txt', transform=conf.transform_dict['rand-crop'], mode='RGB') elif conf.use_dataset == 'Inshop': self.dataset = MSBaseDataSet(conf, './datasets/Inshop/inshop_train.txt', transform=conf.transform_dict['rand-crop'], mode='RGB') self.loader = DataLoader( self.dataset, batch_size=conf.batch_size, num_workers=conf.num_workers, shuffle=False, sampler=RandomIdSampler(conf, self.dataset.label_index_dict), drop_last=True, pin_memory=True, ) self.class_num = self.dataset.num_cls self.img_num = self.dataset.num_train myutils.mkdir_p(conf.log_path, delete=True) self.writer = SummaryWriter(str(conf.log_path)) self.step = 0 self.head_npair = NpairLoss().to(conf.device) self.head_semih_triplet = TripletSemihardLoss().to(conf.device) self.head_triplet = TripletLoss(instance=conf.instances).to(conf.device) self.head_multisimiloss = MultiSimilarityLoss().to(conf.device) logging.info('model heads generated') backbone_bn_para, backbone_wo_bn_para = [ [p for k, p in self.model.named_parameters() if ('bn' in k) == is_bn and ('head' in k) == False] for is_bn in [True, False]] head_bn_para, head_wo_bn_para = [ [p for k, p in self.model.module.head.named_parameters() if ('bn' in k) == is_bn] for is_bn in [True, False]] self.optimizer = optim.Adam([ {'params': backbone_bn_para if conf.freeze_bn==False else [], 'lr': conf.lr_p}, {'params': backbone_wo_bn_para, 'weight_decay': conf.weight_decay, 'lr': conf.lr_p}, {'params': head_bn_para, 'lr': conf.lr}, {'params': head_wo_bn_para, 'weight_decay': conf.weight_decay, 'lr': conf.lr}, ]) logging.info(f'{self.optimizer}, optimizers generated') if conf.use_dataset=='CUB' or conf.use_dataset=='Cars': self.board_loss_every = 20 self.evaluate_every = 100 self.save_every = 1000 elif conf.use_dataset=='Inshop': self.board_loss_every = 20 self.evaluate_every = 200 self.save_every = 2000 else: self.board_loss_every = 20 self.evaluate_every = 500 self.save_every = 2000
def train(epoch, model, optim, trainloader): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() cross_entropy = CrossEntropy(num_classes=num_classes) triplet_loss_fn = TripletLoss(margin=margin) model.fc0.train(False) model.fc1.train(True) output_fc = "fc1" model.base.train(True) ################################################3 person_per_batch = 8 imgs_per_person = 4 bmask = [] l_all_pos = [] l_all_neg = [] pos_targets = torch.Tensor() neg_targets = torch.Tensor() C_pos = torch.zeros([train_batch, 256, 2, 4], device=device) C_neg = torch.zeros([train_batch, 256, 2, 4], device=device) ################################### for batch, (imgs, pids, camids) in enumerate(trainloader): #imgs,pids = imgs.cuda(), pids.cuda() pids = torch.Tensor.numpy(pids) camids = torch.Tensor.numpy(camids) uid = list(set(pids)) mask = np.zeros( [2 * person_per_batch, person_per_batch * imgs_per_person]) for i in range(len(uid)): sel = uid[i] # print(sel) pos = -1 neg = -1 k = -1 for j in range(len(pids)): if (pids[j] == sel): k = j break for j in range(len(pids)): if (pids[k] == pids[j] and camids[k] != camids[j]): # Same IDs and diff cam IDs pos = j #Postive break for j in range(len(pids)): if (pids[k] != pids[j]): #Negative # Diff Cam IDs neg = j break mask[2 * i][k] = 1 mask[2 * i][pos] = 1 mask[2 * i + 1][k] = 1 mask[2 * i + 1][neg] = 1 bmask.append(mask) l_batch_pos = [] l_batch_neg = [] kl = mask #bmask[batch] for i in range(len(kl)): l5 = [] for j in range(len(kl[i])): if (kl[i][j] == 1): l5.append(j) if i % 2 < 1: l_batch_pos.append(l5) else: l_batch_neg.append(l5) l_all_pos.append(l_batch_pos) l_all_neg.append(l_batch_neg) data_time.update(time.time() - end) clf_outputs = model(imgs.cuda()) f = activation['fc1.conv2'] f = f.permute(0, 3, 1, 2) m = nn.AdaptiveAvgPool2d((256, 2)) f = m(f) f = f.permute(0, 2, 3, 1) fc1 = clf_outputs[output_fc] for i in range(len(l_batch_pos)): pos_idx0 = l_batch_pos[i][0] pos_idx1 = l_batch_pos[i][1] #print(f[pos_idx0].shape) pos_targets = torch.sub(f[pos_idx1], f[pos_idx0]) C_pos += pos_targets #print(pos_targets.shape) #pos_targets = torch.Tensor(pos_targets) for i in range(len(l_batch_neg)): neg_idx0 = l_batch_neg[i][0] neg_idx1 = l_batch_neg[i][1] neg_targets = torch.sub(f[neg_idx1], f[neg_idx0]) C_neg += neg_targets g = Flatten(C_pos) y = Flatten(C_neg) u = g - y # (bs,2048) v = torch.unsqueeze(u, 2) # (64,2048,1) w = v.permute(0, 2, 1) # (64,1,2048) x_net = torch.matmul(v, w) # (64,2048,2048) y = torch.sum(x_net) y = F.relu(y) alpha = 1e-9 beta = 0 covariance_loss = 1 * (alpha * y - beta) pids = torch.from_numpy(pids) pids = pids.cuda() if isinstance(fc1, tuple): cross_entropy_loss = DeepSuperVision(cross_entropy, fc1, pids) else: cross_entropy_loss = cross_entropy(fc1, pids) """ if isinstance(f,tuple): triplet = DeepSuperVision(triplet_loss_fn,f,pids) else: triplet = triplet_loss_fn(f,pids) """ #print("xent", cross_entropy_loss) #print("covariance", covariance_loss) loss = cross_entropy_loss + covariance_loss #print("xent", cross_entropy_loss) #print("covariance_loss", covariance_loss) optim.zero_grad() loss.backward() optim.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch + 1) % print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( epoch + 1, batch + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def train(epoch, model, optim, trainloader): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() cross_entropy = CrossEntropy(num_classes=num_classes) triplet_loss_fn = TripletLoss(margin=margin) model.fc0.train(False) model.fc1.train(True) output_fc = "fc1" model.base.train(True) ################################################3 person_per_batch = 8 imgs_per_person = 4 bmask = [] l_all_pos = [] l_all_neg = [] pos_targets = torch.Tensor() neg_targets = torch.Tensor() C_pos0 = torch.zeros([train_batch, 256, 2, 4], device=device) C_pos1 = torch.zeros([train_batch, 256, 2, 4], device=device) C_neg0 = torch.zeros([train_batch, 256, 2, 4], device=device) C_neg1 = torch.zeros([train_batch, 256, 2, 4], device=device) ################################### for batch, (imgs, pids, camids) in enumerate(trainloader): #imgs,pids = imgs.cuda(), pids.cuda() pids = torch.Tensor.numpy(pids) camids = torch.Tensor.numpy(camids) uid = list(set(pids)) mask = np.zeros( [2 * person_per_batch, person_per_batch * imgs_per_person]) for i in range(len(uid)): sel = uid[i] # print(sel) pos = -1 neg = -1 k = -1 for j in range(len(pids)): if (pids[j] == sel): k = j break for j in range(len(pids)): if (pids[k] == pids[j] and camids[k] != camids[j]): # Same IDs and diff cam IDs pos = j #Postive break for j in range(len(pids)): if (pids[k] != pids[j]): #Negative # Diff Cam IDs neg = j break mask[2 * i][k] = 1 mask[2 * i][pos] = 1 mask[2 * i + 1][k] = 1 mask[2 * i + 1][neg] = 1 bmask.append(mask) l_batch_pos = [] l_batch_neg = [] kl = mask #bmask[batch] for i in range(len(kl)): l5 = [] for j in range(len(kl[i])): if (kl[i][j] == 1): l5.append(j) if i % 2 < 1: l_batch_pos.append(l5) else: l_batch_neg.append(l5) l_all_pos.append(l_batch_pos) l_all_neg.append(l_batch_neg) data_time.update(time.time() - end) clf_outputs = model(imgs.cuda()) f0 = activation['fc0.conv2'] #bs,2048,8,4 f1 = activation['fc1.conv2'] f0 = f0.permute(0, 3, 1, 2) f1 = f1.permute(0, 3, 1, 2) m = nn.AdaptiveAvgPool2d((256, 2)) f0 = m(f0) f1 = m(f1) f0 = f0.permute(0, 2, 3, 1) f1 = f1.permute(0, 2, 3, 1) fc1 = clf_outputs[output_fc] # Computing postive samples for i in range(len(l_batch_pos)): pos_idx0 = l_batch_pos[i][0] pos_idx1 = l_batch_pos[i][1] pos_targets0 = torch.sub(f0[pos_idx1], f0[pos_idx0]) pos_targets1 = torch.sub(f1[pos_idx1], f1[pos_idx0]) C_pos0 += pos_targets0 C_pos1 += pos_targets1 # Computing negative samples for i in range(len(l_batch_neg)): neg_idx0 = l_batch_neg[i][0] neg_idx1 = l_batch_neg[i][1] neg_targets0 = torch.sub(f0[neg_idx1], f0[neg_idx0]) neg_targets1 = torch.sub(f1[neg_idx1], f1[neg_idx0]) C_neg0 += neg_targets0 C_neg1 += neg_targets1 g0 = Flatten(C_pos0) g1 = Flatten(C_pos1) y0 = Flatten(C_neg0) y1 = Flatten(C_neg1) u0 = g0 - y0 # (bs,2048) u1 = g1 - y1 v0 = torch.unsqueeze(u0, 2) # (64,2048,1) v1 = torch.unsqueeze(u1, 2) w0 = v0.permute(0, 2, 1) # (64,1,2048) w1 = v1.permute(0, 2, 1) x_net0 = torch.matmul(v0, w0) # (64,2048,2048) x_net1 = torch.matmul(v1, w1) r0 = torch.sum(x_net0) r1 = torch.sum(x_net1) r0_hinge = F.relu(r0) r1_hinge = F.relu(r1) alpha = 1e-9 beta = 0 covariance_loss = 1 * (alpha * r0_hinge - beta) domain_g = 1 * (alpha * (r1_hinge - r0_hinge) - beta) pids = torch.from_numpy(pids) pids = pids.cuda() if isinstance(fc1, tuple): cross_entropy_loss = DeepSuperVision(cross_entropy, fc1, pids) else: cross_entropy_loss = cross_entropy(fc1, pids) loss = cross_entropy_loss + covariance_loss + domain_g optim.zero_grad() loss.backward() optim.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch + 1) % print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( epoch + 1, batch + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def main(): #GENERAL root = "/home/kuru/Desktop/veri-gms-master/" train_dir = '/home/kuru/Desktop/veri-gms-master/VeRispan/image_train/' source = {'veri'} target = {'veri'} workers = 2 height = 320 width = 320 train_sampler = 'RandomSampler' #AUGMENTATION random_erase = True jitter = True aug = True #OPTIMIZATION opt = 'adam' lr = 0.0003 weight_decay = 5e-4 momentum = 0.9 sgd_damp = 0.0 nesterov = True warmup_factor = 0.01 warmup_method = 'linear' #HYPERPARAMETER max_epoch = 80 start = 0 train_batch_size = 16 test_batch_size = 50 #SCHEDULER lr_scheduler = 'multi_step' stepsize = [30, 60] gamma = 0.1 #LOSS margin = 0.3 num_instances = 6 lambda_tri = 1 #MODEL arch = 'resnet101_ibn_a' no_pretrained = False #TEST SETTINGS load_weights = '/home/kuru/Desktop/veri-gms-master/IBN-Net_pytorch0.4.1/resnet101_ibn_a.pth' #load_weights = None start_eval = 0 eval_freq = -1 #MISC use_gpu = True use_amp = True print_freq = 50 seed = 1 resume = '' save_dir = '/home/kuru/Desktop/veri-gms-master/logapex/' gpu_id = 0 vis_rank = True query_remove = True evaluate = False dataset_kwargs = { 'source_names': source, 'target_names': target, 'root': root, 'height': height, 'width': width, 'train_batch_size': train_batch_size, 'test_batch_size': test_batch_size, 'train_sampler': train_sampler, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } transform_kwargs = { 'height': height, 'width': width, 'random_erase': random_erase, 'color_jitter': jitter, 'color_aug': aug } optimizer_kwargs = { 'optim': opt, 'lr': lr, 'weight_decay': weight_decay, 'momentum': momentum, 'sgd_dampening': sgd_damp, 'sgd_nesterov': nesterov } lr_scheduler_kwargs = { 'lr_scheduler': lr_scheduler, 'stepsize': stepsize, 'gamma': gamma } use_gpu = torch.cuda.is_available() log_name = 'log_test.txt' if evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(save_dir, log_name)) print('Currently using GPU ', gpu_id) cudnn.benchmark = True print('Initializing image data manager') dataset = init_imgreid_dataset(root='/home/kuru/Desktop/veri-gms-master/', name='veri') train = [] num_train_pids = 0 num_train_cams = 0 for img_path, pid, camid in dataset.train: path = img_path[52:77] #print(path) folder = path[1:4] pid += num_train_pids camid += num_train_cams train.append((path, folder, pid, camid)) num_train_pids += dataset.num_train_pids num_train_cams += dataset.num_train_cams pid = 0 pidx = {} for img_path, pid, camid in dataset.train: path = img_path[52:77] folder = path[1:4] pidx[folder] = pid pid += 1 path = '/home/kuru/Desktop/veri-gms-master/gms/' pkl = {} entries = os.listdir(path) for name in entries: f = open((path + name), 'rb') if name == 'featureMatrix.pkl': s = name[0:13] else: s = name[0:3] pkl[s] = pickle.load(f) f.close transform_t = train_transforms(**transform_kwargs) data_tfr = vd( pkl_file='index.pkl', dataset=train, root_dir='/home/kuru/Desktop/veri-gms-master/VeRi/image_train/', transform=transform_t) trainloader = DataLoader(data_tfr, sampler=None, batch_size=train_batch_size, shuffle=True, num_workers=workers, pin_memory=False, drop_last=True) #data_tfr = vd(pkl_file='index.pkl', dataset = train, root_dir=train_dir,transform=transforms.Compose([Rescale(64),RandomCrop(32),ToTensor()])) #dataloader = DataLoader(data_tfr, batch_size=batch_size, shuffle=False, num_workers=0) print('Initializing test data manager') dm = ImageDataManager(use_gpu, **dataset_kwargs) testloader_dict = dm.return_dataloaders() print('Initializing model: {}'.format(arch)) model = models.init_model(name=arch, num_classes=num_train_pids, loss={'xent', 'htri'}, last_stride=1, pretrained=not no_pretrained, use_gpu=use_gpu) print('Model size: {:.3f} M'.format(count_num_param(model))) if load_weights is not None: print("weights loaded") load_pretrained_weights(model, load_weights) model = (model).cuda() if use_gpu else model #model = nn.DataParallel(model).cuda() if use_gpu else model optimizer = init_optimizer(model, **optimizer_kwargs) #optimizer = init_optimizer(model) model, optimizer = amp.initialize(model, optimizer, opt_level="O2", keep_batchnorm_fp32=True, loss_scale="dynamic") model = nn.DataParallel(model).cuda() if use_gpu else model scheduler = init_lr_scheduler(optimizer, **lr_scheduler_kwargs) criterion_xent = CrossEntropyLoss(num_classes=num_train_pids, use_gpu=use_gpu, label_smooth=True) criterion_htri = TripletLoss(margin=margin) ranking_loss = nn.MarginRankingLoss(margin=margin) if evaluate: print('Evaluate only') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] _, distmat = test(model, queryloader, galleryloader, train_batch_size, use_gpu, return_distmat=True) if vis_rank: visualize_ranked_results(distmat, dm.return_testdataset_by_name(name), save_dir=osp.join( save_dir, 'ranked_results', name), topk=20) return time_start = time.time() ranklogger = RankLogger(source, target) print('=> Start training') data_index = search(pkl) for epoch in range(start, max_epoch): losses = AverageMeter() #xent_losses = AverageMeter() htri_losses = AverageMeter() accs = AverageMeter() batch_time = AverageMeter() model.train() for p in model.parameters(): p.requires_grad = True # open all layers end = time.time() for batch_idx, (img, label, index, pid, cid) in enumerate(trainloader): trainX, trainY = torch.zeros( (train_batch_size * 3, 3, height, width), dtype=torch.float32), torch.zeros((train_batch_size * 3), dtype=torch.int64) #pids = torch.zeros((batch_size*3), dtype = torch.int16) for i in range(train_batch_size): labelx = str(label[i]) indexx = int(index[i]) cidx = int(pid[i]) if indexx > len(pkl[labelx]) - 1: indexx = len(pkl[labelx]) - 1 #maxx = np.argmax(pkl[labelx][indexx]) a = pkl[labelx][indexx] minpos = np.argmax(ma.masked_where(a == 0, a)) pos_dic = data_tfr[data_index[cidx][1] + minpos] neg_label = int(labelx) while True: neg_label = random.choice(range(1, 770)) if neg_label is not int(labelx) and os.path.isdir( os.path.join( '/home/kuru/Desktop/adiusb/veri-split/train', strint(neg_label))) is True: break negative_label = strint(neg_label) neg_cid = pidx[negative_label] neg_index = random.choice(range(0, len(pkl[negative_label]))) neg_dic = data_tfr[data_index[neg_cid][1] + neg_index] trainX[i] = img[i] trainX[i + train_batch_size] = pos_dic[0] trainX[i + (train_batch_size * 2)] = neg_dic[0] trainY[i] = cidx trainY[i + train_batch_size] = pos_dic[3] trainY[i + (train_batch_size * 2)] = neg_dic[3] #print("anc",labelx,'posdic', pos_dic[1],pos_dic[2],'negdic', neg_dic[1],neg_dic[2]) trainX = trainX.cuda() trainY = trainY.cuda() outputs, features = model(trainX) xent_loss = criterion_xent(outputs[0:train_batch_size], trainY[0:train_batch_size]) htri_loss = criterion_htri(features, trainY) #tri_loss = ranking_loss(features) #ent_loss = xent_loss(outputs[0:batch_size], trainY[0:batch_size], num_train_pids) loss = htri_loss + xent_loss optimizer.zero_grad() if use_amp: with amp.scale_loss(loss, optimizer) as scaled_loss: scaled_loss.backward() else: loss.backward() #loss.backward() optimizer.step() batch_time.update(time.time() - end) losses.update(loss.item(), trainY.size(0)) htri_losses.update(htri_loss.item(), trainY.size(0)) accs.update( accuracy(outputs[0:train_batch_size], trainY[0:train_batch_size])[0]) if (batch_idx) % print_freq == 0: print('Train ', end=" ") print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Acc {acc.val:.2f} ({acc.avg:.2f})\t'.format( epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, loss=htri_losses, acc=accs)) end = time.time() scheduler.step() print('=> Test') for name in target: print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1, distmat = test(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank1) rank2, distmat2 = test_rerank(model, queryloader, galleryloader, test_batch_size, use_gpu) ranklogger.write(name, epoch + 1, rank2) del queryloader del galleryloader del distmat #print(torch.cuda.memory_allocated(),torch.cuda.memory_cached()) torch.cuda.empty_cache() if (epoch + 1) == max_epoch: #if (epoch + 1) % 10 == 0: print('=> Test') save_checkpoint( { 'state_dict': model.state_dict(), 'rank1': rank1, 'epoch': epoch + 1, 'arch': arch, 'optimizer': optimizer.state_dict(), }, save_dir) if vis_rank: visualize_ranked_results(distmat, dm.return_testdataset_by_name(name), save_dir=osp.join( save_dir, 'ranked_results', name), topk=20)
def train(): ## setup torch.multiprocessing.set_sharing_strategy('file_system') if not os.path.exists('./res'): os.makedirs('./res') ## model and loss logger.info('setting up backbone model and loss') net = EmbedNetwork().cuda() net = nn.DataParallel(net) triplet_loss = TripletLoss( margin=None).cuda() # no margin means soft-margin ## optimizer logger.info('creating optimizer') optim = AdamOptimWrapper(net.parameters(), lr=3e-4, wd=0, t0=15000, t1=25000) ## dataloader selector = BatchHardTripletSelector() ds = Market1501('datasets/Market-1501-v15.09.15/bounding_box_train', is_train=True) sampler = BatchSampler(ds, 18, 4) dl = DataLoader(ds, batch_sampler=sampler, num_workers=4) diter = iter(dl) ## train logger.info('start training ...') loss_avg = [] count = 0 t_start = time.time() while True: try: imgs, lbs, _ = next(diter) except StopIteration: diter = iter(dl) imgs, lbs, _ = next(diter) net.train() imgs = imgs.cuda() lbs = lbs.cuda() embds = net(imgs) anchor, positives, negatives = selector(embds, lbs) loss = triplet_loss(anchor, positives, negatives) optim.zero_grad() loss.backward() optim.step() loss_avg.append(loss.detach().cpu().numpy()) if count % 20 == 0 and count != 0: loss_avg = sum(loss_avg) / len(loss_avg) t_end = time.time() time_interval = t_end - t_start logger.info('iter: {}, loss: {:4f}, lr: {:4f}, time: {:3f}'.format( count, loss_avg, optim.lr, time_interval)) loss_avg = [] t_start = t_end count += 1 if count == 25000: break ## dump model logger.info('saving trained model') torch.save(net.module.state_dict(), './res/model.pkl') logger.info('everything finished')