def main(): # --------------------------------------model---------------------------------------- model = sphere20() model = torch.nn.DataParallel(model).cuda() print(model) if not os.path.exists(args.save_path): os.makedirs(args.save_path) model.module.save(args.save_path + 'CosFace_0_checkpoint.pth') # ------------------------------------load image--------------------------------------- train_loader = torch.utils.data.DataLoader( ImageList(root=args.root_path, fileList=args.train_list, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) # range [0.0, 1.0] -> [-1.0,1.0] ])), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True, drop_last=True) print('length of train Dataset: ' + str(len(train_loader.dataset))) print('Number of Classses: ' + str(args.num_class)) # --------------------------------loss function and optimizer----------------------------- MCP = layer.MarginCosineProduct(512, args.num_class).cuda() # MCP = layer.AngleLinear(512, args.num_class).cuda() # MCP = torch.nn.Linear(512, args.num_class, bias=False).cuda() criterion = torch.nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD([{'params': model.parameters()}, {'params': MCP.parameters()}], lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # ----------------------------------------train---------------------------------------- # lfw_eval.eval(args.save_path + 'CosFace_0_checkpoint.pth') for epoch in range(1, args.epochs + 1): # scheduler.step() train(train_loader, model, MCP, criterion, optimizer, epoch) model.module.save(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') lfw_eval.eval(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') print('Finished Training')
def main(): # --------------------------------------model---------------------------------------- if args.network is 'sphere20': model = net.sphere(type=20, is_gray=args.is_gray) model_eval = net.sphere(type=20, is_gray=args.is_gray) elif args.network is 'sphere64': model = net.sphere(type=64, is_gray=args.is_gray) model_eval = net.sphere(type=64, is_gray=args.is_gray) elif args.network is 'LResNet50E_IR': model = net.LResNet50E_IR(is_gray=args.is_gray) model_eval = net.LResNet50E_IR(is_gray=args.is_gray) else: raise ValueError("NOT SUPPORT NETWORK! ") model = torch.nn.DataParallel(model).to(device) model_eval = model_eval.to(device) print(model) if not os.path.exists(args.save_path): os.makedirs(args.save_path) model.module.save(args.save_path + 'CosFace_0_checkpoint.pth') # 512 is dimension of feature classifier = { 'MCP': layer.MarginCosineProduct(512, args.num_class).to(device), 'AL': layer.AngleLinear(512, args.num_class).to(device), 'L': torch.nn.Linear(512, args.num_class, bias=False).to(device) }[args.classifier_type] # ------------------------------------load image--------------------------------------- if args.is_gray: train_transform = transforms.Compose([ transforms.Grayscale(), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize(mean=(0.5, ), std=(0.5, )) ]) # gray else: train_transform = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) # range [0.0, 1.0] -> [-1.0,1.0] ]) train_loader = torch.utils.data.DataLoader(ImageList( root=args.root_path, fileList=args.train_list, transform=train_transform), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True, drop_last=True) print('length of train Database: ' + str(len(train_loader.dataset))) print('Number of Identities: ' + str(args.num_class)) # --------------------------------loss function and optimizer----------------------------- criterion = torch.nn.CrossEntropyLoss().to(device) optimizer = torch.optim.SGD([{ 'params': model.parameters() }, { 'params': classifier.parameters() }], lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # ----------------------------------------train---------------------------------------- # lfw_eval.eval(args.save_path + 'CosFace_0_checkpoint.pth') for epoch in range(1, args.epochs + 1): train(train_loader, model, classifier, criterion, optimizer, epoch) model.module.save(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') lfw_eval.eval( model_eval, args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth', args.is_gray) print('Finished Training')
def main(): # ----------------------------------------load images---------------------------------------- train_loader = torch.utils.data.DataLoader( ImageList( root=root_path, fileList=train_list, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize( mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) # range [0.0, 1.0] -> [-1.0,1.0] ])), batch_size=BatchSize, shuffle=True, num_workers=workers, pin_memory=True, drop_last=True) print('length of train Dataset: ' + str(len(train_loader.dataset))) f.write('length of train Dataset: ' + str(len(train_loader.dataset)) + '\n') print('Number of Classses: ' + str(num_class)) f.write('Number of Classses: ' + str(num_class) + '\n') # ------------------------------------model-------------------------------------------- model_ft = net.sphere20a() # # --------------load model--------------- # model_path = './checkpoints/mnface_30_checkpoints.pth' # state_dict = torch.load(model_path) # model_ft.load_state_dict(state_dict) #------------------------------use gpu-------------------- if use_gpu: # speed up training model_ft = nn.DataParallel(model_ft).cuda() # model_ft = model_ft.cuda() # -----------------------------------loss function and optimizer-------------------------- if multi_sphere: mining_loss = layer.MultiMini(512, num_class) else: mining_loss = layer.miniloss(512, num_class) ce_loss = nn.CrossEntropyLoss() if use_gpu: mining_loss = mining_loss.cuda() ce_loss = ce_loss.cuda() optimizer = optim.SGD([{ 'params': model_ft.parameters() }, { 'params': mining_loss.parameters() }], lr=lr_ori, momentum=0.9, weight_decay=0.0005) # # ------------------------------cosface loss and optimizer------------------------- # MCP = layer.MarginCosineProduct(512, num_class).cuda() # # MCP = layer.AngleLinear(512, args.num_class).cuda() # # MCP = torch.nn.Linear(512, args.num_class, bias=False).cuda() # criterion = torch.nn.CrossEntropyLoss().cuda() # optimizer = torch.optim.SGD([{'params': model_ft.parameters()}, {'params': MCP.parameters()}], # lr=lr_ori, momentum=0.9, weight_decay=0.0005) for epoch in range(1, 30 + 1): # -------------------my loss---------------------------- # x, y = train(train_loader, model_ft, mining_loss, ce_loss, optimizer, epoch) # if multi-sphere train(train_loader, model_ft, mining_loss, ce_loss, optimizer, epoch) model_ft.module.save(save_path + 'mnface_' + str(epoch) + '_checkpoints.pth') acc = lfw_eval.eval(model_path=save_path + 'mnface_' + str(epoch) + '_checkpoints.pth') # if epoch in [1,2,3,10,15,20,30]: # pickle.dump(x, open("/home/baixy/Codes/class-invariant-loss/xarc"+str(epoch)+".pkl", 'wb')) # pickle.dump(y, open("/home/baixy/Codes/class-invariant-loss//yarc" + str(epoch) + ".pkl", 'wb')) # del x # #-------------------cos face-------------------------- # train(train_loader, model_ft, MCP, criterion, optimizer, epoch) # model_ft.module.save(save_path + 'cosface_' + str(epoch) + '_checkpoints.pth') # acc, pred = lfw_eval.eval(save_path + 'cosface_' + str(epoch) + '_checkpoints.pth') writer.add_scalar('Test/LFWAcc', acc, epoch) # fig, (ax0, ax1, ax2) = plt.subplots(nrows=3) # ax0.hist(x1, 100, normed=1, histtype='bar', facecolor='yellowgreen', alpha=0.75) # ax1.hist(x10, 100, normed=1, histtype='bar', facecolor='pink', alpha=0.75) # ax2.hist(x20, 100, normed=1, histtype='bar', facecolor='yellowgreen', alpha=0.75) # plt.show() print('finished training') f.write("finished training" + '\n') f.close()
def main(): # ----------------------------------------load images---------------------------------------- train_loader = torch.utils.data.DataLoader( ImageList( root=root_path, fileList=train_list, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize( mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) # range [0.0, 1.0] -> [-1.0,1.0] ])), batch_size=BatchSize, shuffle=True, num_workers=workers, pin_memory=True, drop_last=True) print('length of train Dataset: ' + str(len(train_loader.dataset))) print('Number of Classses: ' + str(num_class)) # ------------------------------------model-------------------------------------------- model_ft = net.sphere64a() # # --------------load model--------------- # model_path = './checkpoints/mnface_30_checkpoints.pth' # state_dict = torch.load(model_path) # model_ft.load_state_dict(state_dict) #------------------------------use gpu-------------------- if use_gpu: model_ft = nn.DataParallel(model_ft).cuda() # ------------------------------cosface loss and optimizer------------------------- MCP = layer.MarginCosineProduct(512, num_class).cuda() # MCP = layer.AngleLinear(512, args.num_class).cuda() # MCP = torch.nn.Linear(512, args.num_class, bias=False).cuda() criterion = torch.nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD([{ 'params': model_ft.parameters() }, { 'params': MCP.parameters() }], lr=lr_ori, momentum=0.9, weight_decay=0.0005) for epoch in range(1, 38 + 1): # # -------------------my loss---------------------------- # train(train_loader, model_ft, mining_loss, ce_loss, optimizer, epoch) # model_ft.module.save(save_path + 'mnface_' + str(epoch) + '_checkpoints.pth') # acc, pred = lfw_eval.eval(save_path + 'mnface_' + str(epoch) + '_checkpoints.pth') #-------------------cos face-------------------------- train(train_loader, model_ft, MCP, criterion, optimizer, epoch) model_ft.module.save(save_path + 'cosface_' + str(epoch) + '_checkpoints.pth') acc, pred = lfw_eval.eval(save_path + 'cosface_' + str(epoch) + '_checkpoints.pth') writer.add_scalar('Test/LFWAcc', acc, epoch) print('finished training')
def main(): # --------------------------------------model---------------------------------------- # 调用 net.py 文件中的 sphere20() 网络 model = sphere20() # DataParallel 的作用是让数据在多个 gpu 上运行 # 改为 cpu 版本(因 mac 不支持gpu运行) # model = torch.nn.DataParallel(model).cuda() print(model) # 检测保存路径是否已存在,保存 checkpoint if not os.path.exists(args.save_path): os.makedirs(args.save_path) model.module.save(args.save_path + 'CosFace_0_checkpoint.pth') # ------------------------------------load image--------------------------------------- # 加载训练数据集 train_loader = torch.utils.data.DataLoader( ImageList( root=args.root_path, fileList=args.train_list, # 进行图像预处理 transform=transforms.Compose([ # 以 0.5 的概率水平翻转给定的 PIL 图像 transforms.RandomHorizontalFlip(), # 将一个 PIL 图像(H*W*C)在 【0,255】范围内转化为 torch.Tensor(C*H*W) 在 【0.0,1.0】范围内 transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] # 使用 均值 mean 和标准差 standard deviation 来标准化数据 transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5) ) # range [0.0, 1.0] -> [-1.0,1.0] ])), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True, drop_last=True) # 打印 train dataset 的 length print('length of train Dataset: ' + str(len(train_loader.dataset))) # 打印 train dataset 的类别数目 print('Number of Classses: ' + str(args.num_class)) # --------------------------------loss function and optimizer----------------------------- # 实现 cos face 的核心部分,但是使用了 cuda # MCP = layer.MarginCosineProduct(512, args.num_class).cuda() MCP = layer.MarginCosineProduct(512, args.num_class) # MCP = layer.AngleLinear(512, args.num_class).cuda() # MCP = torch.nn.Linear(512, args.num_class, bias=False).cuda() # 修改为不用 cuda # criterion = torch.nn.CrossEntropyLoss().cuda() criterion = torch.nn.CrossEntropyLoss() # 使用(被优化的) SGD 优化器 optimizer = torch.optim.SGD([{ 'params': model.parameters() }, { 'params': MCP.parameters() }], lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay) # ----------------------------------------train---------------------------------------- # lfw_eval.eval(args.save_path + 'CosFace_0_checkpoint.pth') # 开始训练 # 训练 epoch 次,每完整训练一次,存储一次 checkpoint for epoch in range(1, args.epochs + 1): # scheduler.step() train(train_loader, model, MCP, criterion, optimizer, epoch) model.module.save(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') lfw_eval.eval(args.save_path + 'CosFace_' + str(epoch) + '_checkpoint.pth') print('Finished Training')