def train(epoch, model, criterion_class, criterion_metric, optimizer, trainloader, use_gpu): model.train() losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() xent_losses = AverageMeter() global_losses = AverageMeter() local_losses = AverageMeter() end = time.time() for batch_idx, (imgs, pids, _) in enumerate(trainloader): if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) outputs, features, local_features = model(imgs) if args.htri_only: if isinstance(features, tuple): global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features) else: global_loss, local_loss = criterion_metric(features, pids, local_features) else: if isinstance(outputs, tuple): xent_loss = DeepSupervision(criterion_class, outputs, pids) else: xent_loss = criterion_class(outputs, pids) if isinstance(features, tuple): global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features) else: global_loss, local_loss = criterion_metric(features, pids, local_features) loss = xent_loss + global_loss + local_loss optimizer.zero_grad() loss.backward() optimizer.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) xent_losses.update(xent_loss.item(), pids.size(0)) global_losses.update(global_loss.item(), pids.size(0)) local_losses.update(local_loss.item(), pids.size(0)) if (batch_idx+1) % args.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' 'CLoss {xent_loss.val:.4f} ({xent_loss.avg:.4f})\t' 'GLoss {global_loss.val:.4f} ({global_loss.avg:.4f})\t' 'LLoss {local_loss.val:.4f} ({local_loss.avg:.4f})\t'.format( epoch+1, batch_idx+1, len(trainloader), batch_time=batch_time,data_time=data_time, loss=losses,xent_loss=xent_losses, global_loss=global_losses, local_loss = local_losses))
def train(epoch, model, criterion, optimizer, trainloader, use_gpu): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() for batch_idx, (imgs, pids, _) in enumerate(trainloader): if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) outputs = model(imgs) if isinstance(outputs, tuple): loss = DeepSupervision(criterion, outputs, pids) else: loss = criterion(outputs, pids) optimizer.zero_grad() loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch_idx + 1) % args.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_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def train(epoch, model, criterion_class, criterion_metric, optimizer, trainloader, use_gpu): model.train() losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() xent_losses = AverageMeter() global_losses = AverageMeter() local_losses = AverageMeter() end = time.time() for batch_idx, (imgs, pids) in enumerate(trainloader): # print('pids',pids.shape) if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) # outputs, features, local_features = model(imgs) #embed() if args.aligned: outputs,features,local_features = model(imgs) #embed() elif not args.aligned: # only use global feature to get the classifier results outputs,features= model(imgs) # print('outputs',(outputs.shape)) # htri_only = False(default) if args.htri_only: if isinstance(features, tuple): global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features) else: # print ('pids:', pids) global_loss, local_loss = criterion_metric(features, pids, local_features) else: if isinstance(outputs, tuple): xent_loss = DeepSupervision(criterion_class, outputs, pids) else: if args.use_pcb: xent_loss = 0.0 for logits in outputs: stripe_loss = criterion_class(logits, pids) xent_loss += stripe_loss elif not args.use_pcb: xent_loss = criterion_class(outputs, pids) if isinstance(features, tuple): global_loss, local_loss = DeepSupervision(criterion_metric, features, pids, local_features) else: global_loss, local_loss = criterion_metric(features, pids, local_features) loss = xent_loss + global_loss + local_loss # loss = global_loss optimizer.zero_grad() loss.backward() optimizer.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) xent_losses.update(xent_loss.item(), pids.size(0)) global_losses.update(global_loss.item(), pids.size(0)) local_losses.update(local_loss.item(), pids.size(0)) if (batch_idx+1) % args.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' 'CLoss {xent_loss.val:.4f} ({xent_loss.avg:.4f})\t' 'GLoss {global_loss.val:.4f} ({global_loss.avg:.4f})\t' 'LLoss {local_loss.val:.4f} ({local_loss.avg:.4f})\t'.format( epoch+1, batch_idx+1, len(trainloader), batch_time=batch_time,data_time=data_time, loss=losses,xent_loss=xent_losses, global_loss=global_losses, local_loss = local_losses))
def train_PCB(epoch, model, criterion, optimizer, trainloader, use_gpu): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() end = time.time() for batch_idx, (imgs, pids, _) in enumerate(trainloader): if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) logits_list, globe_x_logits, L2_logits, Y2, Y3 = model( imgs ) # return logits_list ,globe_x_logits,L2_logits,z_total_Y2,z_total_Y3 part = {} for i in range(4): part[i] = logits_list[i] features_globe = globe_x_logits #全局特征 L2_f1 = L2_logits[0] L2_f2 = L2_logits[1] loss_list = {} if isinstance(logits_list[1], tuple): loss = DeepSupervision(criterion, logits_list[1], pids) else: for i in range(4): loss_list[i] = criterion(part[i], pids) loss_globe = criterion(features_globe, pids) loss_L21 = criterion(L2_f1, pids) loss_L22 = criterion(L2_f2, pids) loss_Y2 = criterion(Y2, pids) loss_Y3 = criterion(Y3, pids) #loss = criterion(outputs, pids) if (epoch <= 120): loss = loss_list[0] + loss_list[1] + loss_list[2] + loss_list[ 3] + loss_globe + loss_L21 + loss_L22 optimizer.zero_grad() torch.autograd.backward([ loss_list[0], loss_list[1], loss_list[2], loss_list[3], loss_L21, loss_L22, loss_globe ], [ torch.ones(1).cuda(), torch.ones(1).cuda(), torch.ones(1).cuda(), torch.ones(1).cuda(), torch.ones(1).cuda(), torch.ones(1).cuda(), torch.ones(1).cuda() ]) #loss.backward() optimizer.step() if (epoch > 120): loss = loss_Y2 + loss_Y3 optimizer.zero_grad() torch.autograd.backward( [loss_Y2, loss_Y3], [torch.ones(1).cuda(), torch.ones(1).cuda()]) optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) if (batch_idx + 1) % args.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_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses))
def train(epoch, model, model2, criterion_class, criterion_metric, criterion_ml, optimizer, optimizer2, trainloader, use_gpu): model.train() losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() xent_losses = AverageMeter() htri_losses = AverageMeter() mutual_losses = AverageMeter() end = time.time() for batch_idx, (imgs, pids, _) in enumerate(trainloader): if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) outputs1, features1 = model(imgs) outputs2, features2 = model2(imgs) if args.htri_only: if isinstance(features1, tuple): loss1, dist1 = DeepSupervision(criterion_metric, features1, pids) loss2, dist2 = DeepSupervision(criterion_metric, features2, pids) else: loss1, dist1 = criterion_metric(features1, pids) loss2, dist2 = criterion_metric(features2, pids) else: if isinstance(outputs1, tuple): xent_loss1 = DeepSupervision(criterion_class, outputs1, pids) xent_loss2 = DeepSupervision(criterion_class, outputs2, pids) else: xent_loss1 = criterion_class(outputs1, pids) xent_loss2 = criterion_class(outputs2, pids) if isinstance(features1, tuple): htri_loss1, dist1 = DeepSupervision(criterion_metric, features1, pids) htri_loss2, dist2 = DeepSupervision(criterion_metric, features2, pids) else: htri_loss1, dist1 = criterion_metric(features1, pids) htri_loss2, dist2 = criterion_metric(features2, pids) loss1 = xent_loss1 + htri_loss1 loss2 = xent_loss2 + htri_loss2 #ml_loss = criterion_ml(dist1, dist2, pids) ml_loss = torch.mean(torch.pow(dist1 - dist2, 2)) loss = loss1 + loss2 + ml_loss optimizer.zero_grad() optimizer2.zero_grad() loss.backward() optimizer.step() optimizer2.step() batch_time.update(time.time() - end) end = time.time() losses.update(loss.item(), pids.size(0)) xent_losses.update(loss1.item(), pids.size(0)) htri_losses.update(loss2.item(), pids.size(0)) mutual_losses.update(ml_loss.item(), pids.size(0)) if (batch_idx + 1) % args.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' 'Loss1 {xent_loss.val:.4f} ({xent_loss.avg:.4f})\t' 'Loss2 {htri_loss.val:.4f} ({htri_loss.avg:.4f})\t' 'MLoss {ml_loss.val:.4f} ({ml_loss.avg:.4f})\t'.format( epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses, xent_loss=xent_losses, htri_loss=htri_losses, ml_loss=mutual_losses))
def train(epoch, model, model2, criterion, criterion_ml, optimizer1, optimizer2, trainloader, use_gpu): losses1 = AverageMeter() losses2 = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() model2.train() end = time.time() for batch_idx, (imgs, pids, _) in enumerate(trainloader): if use_gpu: imgs, pids = imgs.cuda(), pids.cuda() # measure data loading time data_time.update(time.time() - end) optimizer2.zero_grad() optimizer1.zero_grad() outputs1 = model(imgs) outputs2 = model2(imgs) # print('11111') # embed() if isinstance(outputs1, tuple): loss1 = DeepSupervision( criterion, outputs1, pids) + 10 * DeepSupervision(criterion_ml, outputs1, outputs2) else: loss1 = criterion(outputs1, pids) + 10 * criterion_ml(outputs1, outputs2) # optimizer1.zero_grad() # loss1.backward() # optimizer1.step() # outputs1 = model(imgs) # outputs2 = model2(imgs) # print('2222') # embed() if isinstance(outputs1, tuple): loss2 = DeepSupervision( criterion, outputs2, pids) + 10 * DeepSupervision(criterion_ml, outputs2, outputs1) else: loss2 = criterion(outputs2, pids) + 10 * criterion_ml(outputs2, outputs1) # optimizer2.zero_grad() # optimizer1.zero_grad() loss1.backward() loss2.backward() optimizer2.step() optimizer1.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() losses1.update(loss1.item(), pids.size(0)) losses2.update(loss2.item(), pids.size(0)) if (batch_idx + 1) % args.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' 'Loss1 {loss1.val:.4f} ({loss1.avg:.4f})\t' 'Loss2 {loss2.val:.4f} ({loss2.avg:.4f})\t'.format( epoch + 1, batch_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss1=losses1, loss2=losses2))