def test(loader): G.eval() F1.eval() test_loss = 0 correct = 0 size = 0 num_class = len(class_list) output_all = np.zeros((0, num_class)) criterion = cb_focal_loss(class_num_list, beta=args.beta, gamma=args.gamma) confusion_matrix = torch.zeros(num_class, num_class) with torch.no_grad(): for batch_idx, data_t in enumerate(loader): im_data_t.data.resize_(data_t[0].size()).copy_(data_t[0]) gt_labels_t.data.resize_(data_t[1].size()).copy_(data_t[1]) feat = G(im_data_t) output1 = F1(feat) output_all = np.r_[output_all, output1.data.cpu().numpy()] size += im_data_t.size(0) pred1 = output1.data.max(1)[1] for t, p in zip(gt_labels_t.view(-1), pred1.view(-1)): confusion_matrix[t.long(), p.long()] += 1 correct += pred1.eq(gt_labels_t.data).cpu().sum() test_loss += criterion(output1, gt_labels_t) / len(loader) print('\nTest set: Average loss: {:.4f}, ' 'Accuracy: {}/{} F1 ({:.4f}%)\n'.format( test_loss, correct, size, 100. * float(correct) / size)) return test_loss.data, 100. * float(correct) / size
def eval_inference_simple(G, F1, class_list, class_num_list, args, alpha=1, gamma=1): target_loader_unl, class_list = return_dataset_test(args) im_data_t = torch.FloatTensor(1) gt_labels_t = torch.LongTensor(1) im_data_t = im_data_t.cuda() gt_labels_t = gt_labels_t.cuda() im_data_t = Variable(im_data_t) gt_labels_t = Variable(gt_labels_t) G.eval() F1.eval() size = 0 test_loss = 0 correct = 0 num_class = len(class_list) output_all = np.zeros((0, num_class)) criterion = cb_focal_loss(class_num_list) confusion_matrix = torch.zeros(num_class, num_class) global_paths=[] global_cosine_sim=[] with torch.no_grad(): for batch_idx, data_t in enumerate(target_loader_unl): im_data_t.data.resize_(data_t[0].size()).copy_(data_t[0]) gt_labels_t.data.resize_(data_t[1].size()).copy_(data_t[1]) paths = list(data_t[2]) feat = G(im_data_t) if "resnet" in args.net: output1 = F1.fc1(feat) output1 = F1.extrafc(output1) output1 = F.normalize(output1, dim=1) output1 = output1.mm(torch.transpose(F.normalize(F1.fc2.weight, dim=1),0,1)) size += im_data_t.size(0) cosine_sim = output1.data.max(1)[0] global_cosine_sim.extend(cosine_sim) global_paths.extend(paths) # computing accuracy pred1 = output1.data.max(1)[1] for t, p in zip(gt_labels_t.view(-1), pred1.view(-1)): confusion_matrix[t.long(), p.long()] += 1 correct += pred1.eq(gt_labels_t.data).cpu().sum() test_loss += criterion(output1, gt_labels_t) / len(target_loader_unl) print('\nTest set: Average loss: {:.4f}, ' 'Accuracy: {}/{} F1 ({:.4f}%)\n'. format(test_loss, correct, size, 100. * float(correct) / size)) paths_to_weights={} for i, path in enumerate(global_paths): paths_to_weights[path] = alpha*(1-global_cosine_sim[i])**gamma + 1.0 return test_loss.data, 100. * float(correct) / size, paths_to_weights
def eval_inference(G, F1, class_list, class_num_list, args): target_loader_unl, class_list = return_dataset_test(args) im_data_t = torch.FloatTensor(1) gt_labels_t = torch.LongTensor(1) im_data_t = im_data_t.cuda() gt_labels_t = gt_labels_t.cuda() im_data_t = Variable(im_data_t) gt_labels_t = Variable(gt_labels_t) G.eval() F1.eval() size = 0 test_loss = 0 correct = 0 num_class = len(class_list) output_all = np.zeros((0, num_class)) criterion = cb_focal_loss(class_num_list) confusion_matrix = torch.zeros(num_class, num_class) global_paths = [] global_pred1 = [] global_cosine_sim = [] with torch.no_grad(): for batch_idx, data_t in enumerate(target_loader_unl): im_data_t.data.resize_(data_t[0].size()).copy_(data_t[0]) gt_labels_t.data.resize_(data_t[1].size()).copy_(data_t[1]) paths = list(data_t[2]) feat = G(im_data_t) if "resnet" in args.net: output1 = F1.fc1(feat) else: output1 = feat output1 = F1.extrafc(output1) output1 = F.normalize(output1, dim=1) output1 = output1.mm( torch.transpose(F.normalize(F1.fc2.weight, dim=1), 0, 1)) size += im_data_t.size(0) cosine_sim = output1.data.max(1)[0] pred1 = output1.data.max(1)[1] global_pred1.extend(pred1) global_cosine_sim.extend(cosine_sim) global_paths.extend(paths) for t, p in zip(gt_labels_t.view(-1), pred1.view(-1)): confusion_matrix[t.long(), p.long()] += 1 correct += pred1.eq(gt_labels_t.data).cpu().sum() test_loss += criterion(output1, gt_labels_t) / len(target_loader_unl) print('\nTest set: Average loss: {:.4f}, ' 'Accuracy: {}/{} F1 ({:.4f}%)\n'.format( test_loss, correct, size, 100. * float(correct) / size)) class_wise_sim_path = {} global_weights = [1 for _ in range(len(global_paths))] for i, pred1 in enumerate(global_pred1): if str(pred1.item()) not in class_wise_sim_path: class_wise_sim_path[str( pred1.item())] = [[global_cosine_sim[i].item()], [global_paths[i]]] else: class_wise_sim_path[str(pred1.item())][0].append( global_cosine_sim[i].item()) #append the cosine similarity class_wise_sim_path[str(pred1.item())][1].append( global_paths[i]) #append the path for pred in class_wise_sim_path.keys(): sorted_paths = [ path for _, path in sorted( zip(class_wise_sim_path[pred][0], class_wise_sim_path[pred] [1])) ] # zip cosine sim and paths and sort them wrt cosine sim top_sorted_paths = sorted_paths[:int( 0.1 * len(sorted_paths))] # take top 10 percentile paths for top_sorted_path in top_sorted_paths: global_weights[global_paths.index(top_sorted_path)] = 1.2 paths_to_weights = {} for i, path in enumerate(global_paths): paths_to_weights[path] = global_weights[i] return test_loss.data, 100. * float(correct) / size, paths_to_weights
def eval_inference(G, F1, class_list, class_num_list, args, step, alpha=2, gamma=1): output_file = "entropy_weight_files/%s_%s_%s_%s_%s.txt" % ( args.method, args.net, args.source, args.target, str(step)) target_loader_unl, class_list = return_dataset_test(args) im_data_t = torch.FloatTensor(1) gt_labels_t = torch.LongTensor(1) im_data_t = im_data_t.cuda() gt_labels_t = gt_labels_t.cuda() im_data_t = Variable(im_data_t) gt_labels_t = Variable(gt_labels_t) G.eval() F1.eval() size = 0 test_loss = 0 correct = 0 num_class = len(class_list) output_all = np.zeros((0, num_class)) criterion = cb_focal_loss(class_num_list) confusion_matrix = torch.zeros(num_class, num_class) global_paths = [] global_cosine_sim = [] with torch.no_grad(): for batch_idx, data_t in enumerate(target_loader_unl): im_data_t.data.resize_(data_t[0].size()).copy_(data_t[0]) gt_labels_t.data.resize_(data_t[1].size()).copy_(data_t[1]) paths = data_t[2] feat = G(im_data_t) if "resnet" in args.net: output1 = F1.fc1(feat) output1 = F1.fc2(output1) output1 = F.normalize(output1, dim=1) output1 = output1.mm( torch.transpose(F.normalize(F1.fc3.weight, dim=1), 0, 1)) else: #print(feat.shape) output1 = F1.fc1(feat) #print(output1.shape) output1 = F.normalize(output1, dim=1) #print(output1.shape) #print(F1.fc2.weight.shape) output1 = output1.mm( torch.transpose(F.normalize(F1.fc2.weight, dim=1), 0, 1)) size += im_data_t.size(0) cosine_sim = output1.data.max(1)[0] global_cosine_sim.extend(cosine_sim) global_paths.extend(paths) # computing accuracy pred1 = output1.data.max(1)[1] for t, p in zip(gt_labels_t.view(-1), pred1.view(-1)): confusion_matrix[t.long(), p.long()] += 1 correct += pred1.eq(gt_labels_t.data).cpu().sum() test_loss += criterion(output1, gt_labels_t) / len(target_loader_unl) print('\nTest set: Average loss: {:.4f}, ' 'Accuracy: {}/{} F1 ({:.4f}%)\n'.format( test_loss, correct, size, 100. * float(correct) / size)) weight_dict = {} with open(output_file, "w") as f: for i, path in enumerate(global_paths): f.write("%f %s\n" % (alpha * (1 - global_cosine_sim[i])**gamma, path)) for i, path in enumerate(global_paths): weight_dict[path] = float(alpha * (1 - global_cosine_sim[i])**gamma) return test_loss.data, 100. * float(correct) / size, weight_dict
def train(): G.train() F1.train() optimizer_g = optim.SGD(params, momentum=0.9, weight_decay=0.0005, nesterov=True) optimizer_f = optim.SGD(list(F1.parameters()), lr=1.0, momentum=0.9, weight_decay=0.0005, nesterov=True) def zero_grad_all(): optimizer_g.zero_grad() optimizer_f.zero_grad() param_lr_g = [] for param_group in optimizer_g.param_groups: param_lr_g.append(param_group["lr"]) param_lr_f = [] for param_group in optimizer_f.param_groups: param_lr_f.append(param_group["lr"]) criterion = cb_focal_loss(class_num_list, beta=args.beta, gamma=args.gamma) all_step = args.steps data_iter_s = iter(source_loader) data_iter_t = iter(target_loader) data_iter_t_unl = iter(target_loader_unl) len_train_source = len(source_loader) len_train_target = len(target_loader) len_train_target_semi = len(target_loader_unl) counter = 0 print("=> loading checkpoint...") #filename = 'freezed_models/%s_%s_%s_%s_%s.ckpt.best.pth.tar' % (args.net,args.method, args.source, args.target,args.num) filename = "freezed_models/ent1p2r.ckpt.best.pth.tar" main_dict = torch.load(filename) best_acc_test = main_dict['best_acc_test'] best_acc = 0 G.load_state_dict(main_dict['G_state_dict']) F1.load_state_dict(main_dict['F1_state_dict']) optimizer_g.load_state_dict(main_dict['optimizer_g']) optimizer_f.load_state_dict(main_dict['optimizer_f']) print("=> loaded checkpoint...") print("=> inferencing from checkpoint...") _, _, paths_to_weights = eval_inference(G, F1, class_list, class_num_list, args, 0) print("=> loaded weight file...") for step in range(main_dict['step'], all_step): optimizer_g = inv_lr_scheduler(param_lr_g, optimizer_g, step, init_lr=args.lr) optimizer_f = inv_lr_scheduler(param_lr_f, optimizer_f, step, init_lr=args.lr) lr = optimizer_f.param_groups[0]['lr'] if step % len_train_target == 0: data_iter_t = iter(target_loader) if step % len_train_target_semi == 0: data_iter_t_unl = iter(target_loader_unl) if step % len_train_source == 0: data_iter_s = iter(source_loader) data_t = next(data_iter_t) data_t_unl = next(data_iter_t_unl) data_s = next(data_iter_s) im_data_s.data.resize_(data_s[0].size()).copy_(data_s[0]) gt_labels_s.data.resize_(data_s[1].size()).copy_(data_s[1]) im_data_t.data.resize_(data_t[0].size()).copy_(data_t[0]) gt_labels_t.data.resize_(data_t[1].size()).copy_(data_t[1]) im_data_tu.data.resize_(data_t_unl[0].size()).copy_(data_t_unl[0]) paths = data_t_unl[2] zero_grad_all() data = torch.cat((im_data_s, im_data_t), 0) target = torch.cat((gt_labels_s, gt_labels_t), 0) output = G(data) out1 = F1(output) if args.net == 'resnet34': reg_loss = regularizer(F1.fc3.weight, att) #reg_loss = 0 else: reg_loss = regularizer(F1.fc2.weight, att) #reg_loss = 0 loss = criterion(out1, target) + 0 * reg_loss loss.backward(retain_graph=True) optimizer_g.step() optimizer_f.step() zero_grad_all() if not args.method == 'S+T': output = G(im_data_tu) if args.method == 'ENT': #loss_t = entropy(F1, output, args.lamda) loss_t = weighted_entropy(F1, output, args.lamda, paths, paths_to_weights) loss_t.backward() optimizer_f.step() optimizer_g.step() elif args.method == 'MME': loss_t = weighted_adentropy(F1, output, args.lamda, paths, paths_to_weights) loss_t.backward() optimizer_f.step() optimizer_g.step() else: raise ValueError('Method cannot be recognized.') log_train = 'S {} T {} Train Ep: {} lr{} \t ' \ 'Loss Classification: {:.6f} Loss T {:.6f} Reg {:.6f} ' \ 'Method {}\n'.format(args.source, args.target, step, lr, loss.data, reg_loss.data, -loss_t.data, args.method) else: log_train = 'S {} T {} Train Ep: {} lr{} \t ' \ 'Loss Classification: {:.6f} Method {}\n'.\ format(args.source, args.target, step, lr, loss.data, args.method) G.zero_grad() F1.zero_grad() zero_grad_all() if step % args.log_interval == 0: print(log_train) if step % args.save_interval == 0 and step > 0: print("Re-weighting for entropy...") loss_test, acc_test, paths_to_weights = eval_inference( G, F1, class_list, class_num_list, args, step) #loss_test, acc_test = test(target_loader_test) loss_val, acc_val = test(target_loader_val) #loss_unseen, acc_unseen = test(target_loader_unseen) G.train() F1.train() if acc_val > best_acc: best_acc = acc_val best_acc_test = acc_test counter = 0 else: counter += 1 if args.early: if counter > args.patience: break print('best acc test %f best acc val %f' % (best_acc_test, best_acc)) print('record %s' % record_file) with open(record_file, 'a') as f: f.write('step %d best %f final %f \n' % (step, best_acc_test, acc_val)) G.train() F1.train() if args.save_check: print('saving model...') #is_best = True #save_mymodel(args, { # 'step': step, # 'arch': args.net, # 'G_state_dict': G.state_dict(), # 'F1_state_dict': F1.state_dict(), # 'best_acc_test': best_acc_test, # 'optimizer_g' : optimizer_g.state_dict(), # 'optimizer_f' : optimizer_f.state_dict(), # }, is_best, None) torch.save( { 'step': step, 'arch': args.net, 'G_state_dict': G.state_dict(), 'F1_state_dict': F1.state_dict(), 'best_acc_test': best_acc_test, 'optimizer_g': optimizer_g.state_dict(), 'optimizer_f': optimizer_f.state_dict(), }, '%s/%s_%s_%s_%s_%s_%s.ckpt.pth.tar' % (args.checkpath, args.net, args.method, args.source, args.target, args.num, str(step)))