def main(): torch.manual_seed(args.seed) os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() args.save_dir = osp.join(args.save_dir, str(args.nExemplars) + '-shot') sys.stdout = Logger(osp.join(args.save_dir, 'log_train.txt')) print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) else: print("Currently using CPU (GPU is highly recommended)") print('Initializing image data manager') dm = DataManager(args, use_gpu) trainloader, testloader = dm.return_dataloaders() model = Model(args) criterion = CrossEntropyLoss() optimizer = init_optimizer(args.optim, model.parameters(), args.lr, args.weight_decay) if use_gpu: model = model.cuda() start_time = time.time() train_time = 0 best_acc = -np.inf best_epoch = 0 print("==> Start training") for epoch in range(args.max_epoch): learning_rate = adjust_learning_rate(optimizer, epoch, args.LUT_lr) start_train_time = time.time() train(epoch, model, criterion, optimizer, trainloader, learning_rate, use_gpu) train_time += round(time.time() - start_train_time) if epoch == 0 or epoch > (args.stepsize[0] - 1) or (epoch + 1) % 10 == 0: acc = test(model, testloader, use_gpu) is_best = acc > best_acc if is_best: best_acc = acc best_epoch = epoch + 1 save_checkpoint( { 'state_dict': model.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_ep' + str(epoch + 1) + '.pth.tar')) print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}". format(best_acc, best_epoch)) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print( "Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.". format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))
def main(): os.system('cp ./train_with_inpaint_read_from_data_fixed.py ' +args.save_dir + 'train_with_inpaint_read_from_data_fixed.py') #exit() loss_fn = losses.GenericLoss('batchsgm', 0.02, 64) torch.manual_seed(args.seed) #os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() #print(use_gpu) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False #torch.cuda.manual_seed(args.seed) #torch.manual_seed(config.SEED) #torch.cuda.manual_seed_all(config.SEED) np.random.seed(args.seed) random.seed(args.seed) sys.stdout = Logger(osp.join(args.save_dir, 'log_train.txt')) print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = False torch.cuda.manual_seed_all(args.seed) torch.cuda.manual_seed(args.seed) #exit(0) else: print("Currently using CPU (GPU is highly recommended)") print('Initializing image data manager') dm = DataManager(args, use_gpu) trainloader, testloader = dm.return_dataloaders() #model_edge = EdgeConnect(config) #model_edge.load() print('\nstart testing...\n') #model_edge.test() #print(args.scale_cls,args.num_classes) #exit(0) #GNN_model=create_models(args,512) #print(args.use_similarity) #exit(0) if args.use_similarity: #GNN_model=create_models(args,512) #model = Model(args,GNN_model,scale_cls=args.scale_cls, num_classes=args.num_classes) print('similarity remove') exit() else: model = Model(scale_cls=args.scale_cls,only_CSEI=only_CSEI, num_classes=args.num_classes) #model_tradclass = Model_tradi(scale_cls=args.scale_cls, num_classes=args.num_classes) #params_tradclass = torch.load('result/%s/CAM/1-shot-seed112_classic_classifier_avg_nouse_CAN/%s' % (args.dataset, 'best_model.pth.tar')) #model_tradclass.load_state_dict(params_tradclass['state_dict']) #params = torch.load('result/%s/CAM/5-shot-seed112_inpaint_batchsgmregular_begin_70epoch/%s' % (args.dataset, 'best_model.pth.tar')) #model.load_state_dict(params['state_dict']) #print('enter model_tradclass') #exit(0) if not only_CSEI: #params = torch.load('result/%s/CAM/5-shot-seed112_inpaint_batchsgmregular_begin_70epoch/%s' % (args.dataset, 'best_model.pth.tar')) #params = torch.load('../fewshot-CAN-master/result/%s/CAM/1-shot-seed112_inpaint_use_CAM_argumenttest_nouse_similarity_read_from_data1/%s' % (args.dataset, 'best_model.pth.tar')) params = torch.load('/home/yfchen/ljj_code/trained_model/mini/only_inpaint/1-shot/%s' % ('best_model.pth.tar')) #params = torch.load("/home/yfchen/ljj_code/spatial_test/result/miniImageNet/CAM/1-shot_only_augment_test_fixed_GPU0_2333/best_model.pth.tar") #params = torch.load('./result/%s/CAM/1-shot-seed112_inpaint_support_fuse_Cam_surport_from_65.3_spatial_tanh_8argument_traingloballabelargurement_contrast_normal/%s' % (args.dataset, 'best_model.pth.tar')) #params = torch.load('result/%s/CAM/1-shot-seed112_inpaint_support_fuse_Cam_surport_from_65.3_debug/%s' % (args.dataset, 'best_model.pth.tar')) #params = torch.load('result/%s/CAM/1-shot-seed112_inpaint_support_fuse_Cam_surport/%s' % (args.dataset, 'best_model.pth.tar')) #params_tradclass = torch.load('result/%s/CAM/1-shot-seed112_classic_classifier_global_avg/%s' % (args.dataset, 'checkpoint_inpaint67.pth.tar')) print(type(params)) model.load_state_dict(params['state_dict'], strict=False) #model_tradclass.load_state_dict(params_tradclass['state_dict']) criterion = CrossEntropyLoss() optimizer = init_optimizer(args.optim, model.parameters(), args.lr, args.weight_decay) if use_gpu: model = model.cuda() #model_tradclass = model_tradclass.cuda() start_time = time.time() train_time = 0 best_acc = -np.inf best_epoch = 0 print("==> Start training") for i in range(1): #print(i+14) acc = test_ori(model, testloader, use_gpu,28) #acc_5 = test_ori_5(model, testloader, use_gpu) #print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}".format( acc, 0)) #exit() #print(args.save_dir) #exit() best_path=args.save_dir+'best_model.pth.tar' if only_CSEI: args.max_epoch=70 for epoch in range(args.max_epoch): if not args.Classic: learning_rate = adjust_learning_rate(optimizer, epoch, args.LUT_lr) else: optimizer_tradclass = init_optimizer(args.optim, model_tradclass.parameters(), args.lr, args.weight_decay) learning_rate = adjust_learning_rate(optimizer_tradclass, epoch, args.LUT_lr) start_train_time = time.time() #model.base.eval() #exit(0) #print(not True) #exit(0) if not only_test: #print(';;;;;;;;;;;') #exit(0) if not args.Classic: print('enter train code') train(epoch, model, criterion,loss_fn, optimizer, trainloader, learning_rate, use_gpu) #print('oooo') else: acc=train(epoch,model_edge, model_tradclass, criterion, optimizer_tradclass, trainloader, learning_rate, use_gpu) train_time += round(time.time() - start_train_time) if epoch == 0 or epoch > (args.stepsize[0]-1) or (epoch + 1) % 10 == 0: print('enter test code') #exit(0) if not args.Classic: #acc = test(model_edge, model, model_tradclass,weight_softmax, testloader, use_gpu) acc = test_ori(model, testloader, use_gpu) #acc_5 = test_ori_5(model, testloader, use_gpu) is_best = acc > best_acc #else: #print(acc) #exit(0) if is_best: best_acc = acc best_epoch = epoch + 1 if not only_test: if not args.Classic: if save_best: save_checkpoint({ 'state_dict': model.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_inpaint' + str(epoch + 1) + '.pth.tar')) else: print('not save') if args.Classic: save_checkpoint({ 'state_dict': model_tradclass.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_classic' + str(epoch + 1) + '.pth.tar')) print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}".format(best_acc, best_epoch)) acc_5 = test_ori_5(model,best_path, testloader, use_gpu) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))
def main(): torch.manual_seed(args.seed) os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() sys.stdout = Logger(osp.join(args.save_dir, 'log_train.txt')) print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) else: print("Currently using CPU (GPU is highly recommended)") print('Initializing image data manager') dm = DataManager(args, use_gpu) trainloader, testloader = dm.return_dataloaders() #print(args.scale_cls,args.num_classes) #exit(0) GNN_model = create_models(args, 512) if args.use_similarity: model = Model(args, GNN_model, scale_cls=args.scale_cls, num_classes=args.num_classes) else: model = Model(scale_cls=args.scale_cls, num_classes=args.num_classes) if only_test: params = torch.load('result/%s/CAM/1-shot-seed112/%s' % (args.dataset, 'best_model.pth.tar')) print(type(params)) #exit(0) #for key in params.keys(): #print(type(key)) #exit(0) model.load_state_dict(params['state_dict']) #exit(0) criterion = CrossEntropyLoss() optimizer = init_optimizer(args.optim, model.parameters(), args.lr, args.weight_decay) if use_gpu: model = model.cuda() start_time = time.time() train_time = 0 best_acc = -np.inf best_epoch = 0 print("==> Start training") for epoch in range(args.max_epoch): learning_rate = adjust_learning_rate(optimizer, epoch, args.LUT_lr) start_train_time = time.time() #exit(0) #print(not True) #exit(0) if not only_test: #print(';;;;;;;;;;;') #exit(0) train(epoch, model, criterion, optimizer, trainloader, learning_rate, use_gpu) train_time += round(time.time() - start_train_time) if epoch == 0 or epoch > (args.stepsize[0] - 1) or (epoch + 1) % 10 == 0: print('enter test code') #exit(0) acc = test(model, testloader, use_gpu) is_best = acc > best_acc #print(acc) #exit(0) if is_best: best_acc = acc best_epoch = epoch + 1 if not only_test: save_checkpoint( { 'state_dict': model.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_ep' + str(epoch + 1) + '.pth.tar')) print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}". format(best_acc, best_epoch)) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print( "Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.". format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))
def main(): torch.manual_seed(args.seed) #os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True #torch.manual_seed(config.SEED) #torch.cuda.manual_seed_all(config.SEED) np.random.seed(args.seed) random.seed(args.seed) sys.stdout = Logger(osp.join(args.save_dir, 'log_train.txt')) print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) else: print("Currently using CPU (GPU is highly recommended)") print('Initializing image data manager') dm = DataManager(args, use_gpu) trainloader, testloader = dm.return_dataloaders() model_edge = EdgeConnect(config) model_edge.load() print('\nstart testing...\n') #model_edge.test() #print(args.scale_cls,args.num_classes) #exit(0) #GNN_model=create_models(args,512) #print(args.use_similarity) #exit(0) if args.use_similarity: GNN_model=create_models(args,512) model = Model(args,GNN_model,scale_cls=args.scale_cls, num_classes=args.num_classes) else: model = Model_mltizhixin(scale_cls=args.scale_cls, num_classes=args.num_classes) model_tradclass = Model_tradi(scale_cls=args.scale_cls, num_classes=args.num_classes) params_tradclass = torch.load('result/%s/CAM/tired_images_ave_class/%s' % (args.dataset, 'best_model.pth.tar')) model_tradclass.load_state_dict(params_tradclass['state_dict']) #params = torch.load('result/%s/CAM/1-shot-seed112_inpaint_use_CAM/%s' % (args.dataset, 'checkpoint_inpaint67.pth.tar')) #model.load_state_dict(params['state_dict']) #print('enter model_tradclass') #exit(0) if False: params = torch.load('result/%s/CAM/1-shot-seed112/%s' % (args.dataset, 'best_model.pth.tar')) params_tradclass = torch.load('result/%s/CAM/1-shot-seed112_classic_classifier_global_avg/%s' % (args.dataset, 'checkpoint_inpaint67.pth.tar')) print(type(params)) #exit(0) #for key in params.keys(): #print(type(key)) #exit(0) #model.load_state_dict(params['state_dict']) model_tradclass.load_state_dict(params_tradclass['state_dict']) #exit(0) #for ind,i in model.state_dict().items(): #print (ind,i.shape) #exit(0) params = list(model_tradclass.parameters()) #fc_params=params[-2] weight_softmax = np.squeeze(params[-2].data.numpy()) #print(weight_softmax.shape,type(params[-2]),params[-2].shape,params[-2].data.shape) #exit(0) criterion = CrossEntropyLoss() optimizer = init_optimizer(args.optim, model.parameters(), args.lr, args.weight_decay) #optimizer_tradclass = init_optimizer(args.optim, model_tradclass.parameters(), args.lr, args.weight_decay) #model_tradclass if use_gpu: model = model.cuda() model_tradclass = model_tradclass.cuda() start_time = time.time() train_time = 0 best_acc = -np.inf best_epoch = 0 print("==> Start training") for epoch in range(args.max_epoch): if not args.Classic: learning_rate = adjust_learning_rate(optimizer, epoch, args.LUT_lr) else: optimizer_tradclass = init_optimizer(args.optim, model_tradclass.parameters(), args.lr, args.weight_decay) learning_rate = adjust_learning_rate(optimizer_tradclass, epoch, args.LUT_lr) #print('enter optimizer_tradclass') #exit(0) start_train_time = time.time() #exit(0) #print(not True) #exit(0) args.Classic=0 if not only_test: #print(';;;;;;;;;;;') #exit(0) if not args.Classic: print('enter train code') train(epoch,model_edge, model, model_tradclass,weight_softmax, criterion, optimizer, trainloader, learning_rate, use_gpu) #print('oooo') else: acc=train(epoch,model_edge, model_tradclass, criterion, optimizer_tradclass, trainloader, learning_rate, use_gpu) train_time += round(time.time() - start_train_time) if epoch == 0 or epoch > (args.stepsize[0]-1) or (epoch + 1) % 10 == 0: print('enter test code') #exit(0) if not args.Classic: #acc = test(model_edge, model, model_tradclass,weight_softmax, testloader, use_gpu) acc = test_ori(model, testloader, use_gpu) is_best = acc > best_acc #else: #print(acc) #exit(0) if is_best: best_acc = acc best_epoch = epoch + 1 if not only_test: if not args.Classic: save_checkpoint({ 'state_dict': model.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_inpaint' + str(epoch + 1) + '.pth.tar')) if args.Classic: save_checkpoint({ 'state_dict': model_tradclass.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_classic' + str(epoch + 1) + '.pth.tar')) print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}".format(best_acc, best_epoch)) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))
def main(): torch.manual_seed(args.seed) os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() model_name = '{}-{}shot-{}kernel-{}group'.format(args.dataset, args.nExemplars, args.kernel, args.groups) if args.suffix is not None: model_name = model_name + '-{}'.format(args.suffix) save_dir = os.path.join(args.save_dir, model_name) os.makedirs(save_dir, exist_ok=True) sys.stdout = Logger(osp.join(save_dir, 'log_train.txt')) print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) else: print("Currently using CPU (GPU is highly recommended)") print('Initializing image data manager') dm = DataManager(args, use_gpu) trainloader, testloader = dm.return_dataloaders() model = Model(num_classes=args.num_classes, groups=args.groups, kernel=args.kernel) #model = nn.DataParallel(model) criterion = CrossEntropyLoss() optimizer = init_optimizer(args.optim, model.parameters(), args.lr, args.weight_decay) if use_gpu: model = model.cuda() start_time = time.time() train_time = 0 best_acc = -np.inf best_epoch = 0 print("==> Start training") warmup_epoch = 5 scheduler = warmup_scheduler(base_lr=args.lr, iter_per_epoch=len(trainloader), max_epoch=args.max_epoch + warmup_epoch, multi_step=[], warmup_epoch=warmup_epoch) for epoch in range(args.max_epoch + warmup_epoch): start_train_time = time.time() train(epoch, model, criterion, optimizer, trainloader, scheduler, use_gpu) train_time += round(time.time() - start_train_time) if epoch == 0 or epoch > (args.stepsize[0] - 1) or (epoch + 1) % 10 == 0: acc = test(model, testloader, use_gpu) is_best = acc > best_acc if is_best: best_acc = acc best_epoch = epoch + 1 if is_best or epoch > args.max_epoch: save_checkpoint( { 'state_dict': model.state_dict(), 'acc': acc, 'epoch': epoch, }, is_best, osp.join(save_dir, 'checkpoint_ep' + str(epoch + 1) + '.pth.tar')) print("==> Test 5-way Best accuracy {:.2%}, achieved at epoch {}". format(best_acc, best_epoch)) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print( "Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.". format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))