def full_training(args): if not os.path.isdir(args.expdir): os.makedirs(args.expdir) elif os.path.exists(args.expdir + '/results.npy'): return if 'ae' in args.task: os.mkdir(args.expdir + '/figs/') train_batch_size = args.train_batch_size // 4 if args.task == 'rot' else args.train_batch_size test_batch_size = args.test_batch_size // 4 if args.task == 'rot' else args.test_batch_size yield_indices = (args.task == 'inst_disc') datadir = args.datadir + args.dataset trainloader, valloader, num_classes = general_dataset_loader.prepare_data_loaders( datadir, image_dim=args.image_dim, yield_indices=yield_indices, train_batch_size=train_batch_size, test_batch_size=test_batch_size, train_on_10_percent=args.train_on_10, train_on_half_classes=args.train_on_half) _, testloader, _ = general_dataset_loader.prepare_data_loaders( datadir, image_dim=args.image_dim, yield_indices=yield_indices, train_batch_size=train_batch_size, test_batch_size=test_batch_size, ) args.num_classes = num_classes if args.task == 'rot': num_classes = 4 elif args.task == 'inst_disc': num_classes = args.low_dim if args.task == 'ae': net = models.AE([args.code_dim], image_dim=args.image_dim) elif args.task == 'jigsaw': net = JigsawModel(num_perms=args.num_perms, code_dim=args.code_dim, gray_prob=args.gray_prob, image_dim=args.image_dim) else: net = models.resnet26(num_classes, mlp_depth=args.mlp_depth, normalize=(args.task == 'inst_disc')) if args.task == 'inst_disc': train_lemniscate = LinearAverage(args.low_dim, trainloader.dataset.__len__(), args.nce_t, args.nce_m) train_lemniscate.cuda() args.train_lemniscate = train_lemniscate test_lemniscate = LinearAverage(args.low_dim, valloader.dataset.__len__(), args.nce_t, args.nce_m) test_lemniscate.cuda() args.test_lemniscate = test_lemniscate if args.source: try: old_net = torch.load(args.source) except: print("Falling back encoding") from functools import partial import pickle pickle.load = partial(pickle.load, encoding="latin1") pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1") old_net = torch.load(args.source, map_location=lambda storage, loc: storage, pickle_module=pickle) # net.load_state_dict(old_net['net'].state_dict()) old_net = old_net['net'] if hasattr(old_net, "module"): old_net = old_net.module old_state_dict = old_net.state_dict() new_state_dict = net.state_dict() for key, weight in old_state_dict.items(): if 'linear' not in key: new_state_dict[key] = weight elif key == 'linears.0.weight' and weight.shape[0] == num_classes: new_state_dict['linears.0.0.weight'] = weight elif key == 'linears.0.bias' and weight.shape[0] == num_classes: new_state_dict['linears.0.0.bias'] = weight net.load_state_dict(new_state_dict) del old_net net = torch.nn.DataParallel(net).cuda() start_epoch = 0 if args.task in ['ae', 'inst_disc']: best_acc = np.inf else: best_acc = -1 results = np.zeros((4, start_epoch + args.nb_epochs)) net.cuda() cudnn.benchmark = True if args.task in ['ae']: args.criterion = nn.MSELoss() else: args.criterion = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd) print("Start training") train_func = eval('utils_pytorch.train_' + args.task) test_func = eval('utils_pytorch.test_' + args.task) if args.test_first: with torch.no_grad(): test_func(0, valloader, net, best_acc, args, optimizer) for epoch in range(start_epoch, start_epoch + args.nb_epochs): utils_pytorch.adjust_learning_rate(optimizer, epoch, args) st_time = time.time() # Training and validation train_acc, train_loss = train_func(epoch, trainloader, net, args, optimizer) test_acc, test_loss, best_acc = test_func(epoch, valloader, net, best_acc, args, optimizer) # Record statistics results[0:2, epoch] = [train_loss, train_acc] results[2:4, epoch] = [test_loss, test_acc] np.save(args.expdir + '/results.npy', results) print('Epoch lasted {0}'.format(time.time() - st_time)) sys.stdout.flush() if (args.task == 'rot') and (train_acc >= 98) and args.early_stopping: break if args.task == 'inst_disc': args.train_lemniscate = None args.test_lemniscate = None else: best_net = torch.load(args.expdir + 'checkpoint.t7')['net'] if args.task in ['ae', 'inst_disc']: best_acc = np.inf else: best_acc = -1 final_acc, final_loss, _ = test_func(0, testloader, best_net, best_acc, args, None)
os.mkdir(args.ckpdir) if not os.path.isdir(args.svdir): os.mkdir(args.svdir) config_task.isdropout1 = (args.dropout[0] == '1') config_task.isdropout2 = (args.dropout[1] == '1') ##################################### # Prepare data loaders train_loaders, val_loaders, num_classes = imdbfolder.prepare_data_loaders(args.dataset,args.datadir,args.imdbdir,True) args.num_classes = num_classes # Create the network net = models.resnet26(num_classes) start_epoch = 0 best_acc = 0 # best test accuracy results = np.zeros((4,start_epoch+args.nb_epochs,len(args.num_classes))) all_tasks = range(len(args.dataset)) np.random.seed(1993) if args.use_cuda: net.cuda() cudnn.benchmark = True args.criterion = nn.CrossEntropyLoss() optimizer = sgd.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd)
os.mkdir(args.ckpdir) if not os.path.isdir(args.svdir): os.mkdir(args.svdir) config_task.isdropout1 = (args.dropout[0] == '1') config_task.isdropout2 = (args.dropout[1] == '1') ##################################### # Prepare data loaders train_loaders, val_loaders, num_classes = imdbfolder.prepare_data_loaders(args.dataset,args.datadir,args.imdbdir,True) args.num_classes = num_classes # Create the network net = models.resnet26(num_classes) start_epoch = 0 best_acc = 0 # best test accuracy results = np.zeros((4,start_epoch+args.nb_epochs,len(args.num_classes))) all_tasks = range(len(args.dataset)) np.random.seed(1993) if args.use_cuda: net.cuda() cudnn.benchmark = True args.criterion = nn.CrossEntropyLoss() optimizer = sgd.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd)
from PIL import Image from pandas import Series, DataFrame from torch.utils.data import Dataset from torchvision import transforms, utils import os import glob normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) preprocess = transforms.Compose([ transforms.ToTensor(), ]) path = [ '/home/students/student3_15/00_astar/00_baseline/00_drkaggle/prepBG_train/44214_left.jpeg' ] img_pil = Image.open(path[0]) img_pil = img_pil.resize((224, 224)) img_tensor = preprocess(img_pil).resize(1, 3, 224, 224) model = resnet26() file_path = "model_linear.pkl" checkpoint = torch.load(file_path) model.load_state_dict(checkpoint) print(model(img_tensor)) model1 = resnet26() file_path = "model_notrain.pkl" checkpoint1 = torch.load(file_path) model1.load_state_dict(checkpoint1) print(model1(img_tensor))
def extract_feature(self): # model = resnet26() # file_path = "resnet26_pretrained.t7" # checkpoint = torch.load(file_path) # model = checkpoint['net'] # for name, module in model._modules.items(): # self.recursion_change_bn(model) net = resnet26() checkpoint = torch.load("resnet26_pretrained.t7") net_old = checkpoint['net'] store_data = [] t = 0 for name, m in net_old.named_modules(): if isinstance(m, nn.Conv2d): store_data.append(m.weight.data) t += 1 element = 0 for name, m in net.named_modules(): if isinstance(m, nn.Conv2d) and 'parallel_blocks' not in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) element += 1 element = 1 for name, m in net.named_modules(): if isinstance(m, nn.Conv2d) and 'parallel_blocks' in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) element += 1 store_data = [] store_data_bias = [] store_data_rm = [] store_data_rv = [] for name, m in net_old.named_modules(): if isinstance(m, nn.BatchNorm2d): store_data.append(m.weight.data) store_data_bias.append(m.bias.data) store_data_rm.append(m.running_mean) store_data_rv.append(m.running_var) element = 0 for name, m in net.named_modules(): if isinstance(m, nn.BatchNorm2d) and 'parallel_block' not in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) m.bias.data = torch.nn.Parameter( store_data_bias[element].clone()) m.running_var = store_data_rv[element].clone() m.running_mean = store_data_rm[element].clone() element += 1 element = 1 for name, m in net.named_modules(): if isinstance(m, nn.BatchNorm2d) and 'parallel_block' in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) m.bias.data = torch.nn.Parameter( store_data_bias[element].clone()) m.running_var = store_data_rv[element].clone() m.running_mean = store_data_rm[element].clone() element += 1 model = net print(model) model.cuda() model.eval() dict1 = {} for name, param in model.named_parameters(): dict1[name] = param print("###############dict1$#############") print(dict1) data = { 'train': CustomDataset(split="train", seed=42), 'test': CustomDataset(split="test", seed=42) } dataloaders = { 'train': DataLoader(data['train'], batch_size=20, shuffle=True), 'test': DataLoader(data['test'], batch_size=20, shuffle=False) } device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") feature_list = [] with torch.no_grad(): for split in ['train', 'test']: for i, (inputs, labels) in enumerate(dataloaders[split]): inputs = inputs.to(device) labels = labels.to(device) # x_feature_batchs= model(inputs) x_feature_batchs = model(inputs) if i: features = np.concatenate([features, x_feature_batchs], axis=0) else: features = x_feature_batchs feature_list.append(features) np.save("feature1.npy", feature_list[0]) np.save("feature2.npy", feature_list[1]) return feature_list[0], feature_list[1]
def SiameseNet(code_dim=256): return resnet26(num_classes=[code_dim])
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True): model_ft = None input_size = 0 net = resnet26(num_classes=num_classes) if model_name == "resnet26": checkpoint = torch.load("resnet26_pretrained.t7") net_old = checkpoint['net'] store_data = [] t = 0 for name, m in net_old.named_modules(): if isinstance(m, nn.Conv2d): store_data.append(m.weight.data) t += 1 element = 0 for name, m in net.named_modules(): if isinstance(m, nn.Conv2d) and 'parallel_blocks' not in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) element += 1 element = 1 for name, m in net.named_modules(): if isinstance(m, nn.Conv2d) and 'parallel_blocks' in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) element += 1 store_data = [] store_data_bias = [] store_data_rm = [] store_data_rv = [] for name, m in net_old.named_modules(): if isinstance(m, nn.BatchNorm2d): store_data.append(m.weight.data) store_data_bias.append(m.bias.data) store_data_rm.append(m.running_mean) store_data_rv.append(m.running_var) element = 0 for name, m in net.named_modules(): if isinstance(m, nn.BatchNorm2d) and 'parallel_block' not in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) m.bias.data = torch.nn.Parameter( store_data_bias[element].clone()) m.running_var = store_data_rv[element].clone() m.running_mean = store_data_rm[element].clone() element += 1 element = 1 for name, m in net.named_modules(): if isinstance(m, nn.BatchNorm2d) and 'parallel_block' in name: m.weight.data = torch.nn.Parameter(store_data[element].clone()) m.bias.data = torch.nn.Parameter( store_data_bias[element].clone()) m.running_var = store_data_rv[element].clone() m.running_mean = store_data_rm[element].clone() element += 1 set_parameter_requires_grad(net, feature_extract) return net