def main(): # parse command line input opt = utils.parse_arg() # Set GPU opt.cuda = opt.gpuid >= 0 if opt.cuda: torch.cuda.set_device(opt.gpuid) else: utils.time_str("GPU acceleration is disabled.") # prepare data db = data_prepare.prepare_db(opt) imagenet = data_prepare.ImageNetSmallData(opt, type='centres') # imagenet = None # # # add imagenet dataset db.update({'imagenet': imagenet}) # initialize the model pre_trained_model = model.prepare_model(opt) # prepare M_0(x) model, which is a fixed pre-trained model opt.num_output = 1000 fixed_model = model.prepare_model(opt) # prepare centres if not os.path.exists('../datasets/imagenet/train_centres.txt'): imagenet = data_prepare.ImageNetSmallData(opt, type='all') trainer.prepare_centres(fixed_model, imagenet, opt) # configurate the optimizer optim, sche = optimizer.prepare_optim(pre_trained_model, opt) # train the model trainer.train(pre_trained_model, optim, sche, db, opt, model_0=fixed_model) # trainer.train(pre_trained_model, optim, sche, db, opt, model_0 = None) # save the trained model if opt.save: utils.save_model(pre_trained_model, opt)
def main(): # parse command line input opt = utils.parse_arg() # Set GPU opt.cuda = opt.gpuid >= 0 if opt.cuda: torch.cuda.set_device(opt.gpuid) else: utils.time_str("GPU acceleration is disabled.") # prepare data db = data_prepare.prepare_db(opt) # initializa the model pre_trained_model = model.prepare_model(opt) # configurate the optimizer optim, sche = optimizer.prepare_optim(pre_trained_model, opt) # train the model trainer.train(pre_trained_model, optim, sche, db, opt) # save the trained model utils.save_model(pre_trained_model, opt)
def main(): # logging configuration logging.basicConfig(level=logging.INFO, format="[%(asctime)s]: %(message)s") # parse command line input opt = utils.parse_arg() # Set GPU opt.cuda = opt.gpuid >= 0 if opt.cuda: torch.cuda.set_device(opt.gpuid) else: # please use GPU for training, CPU version is not supported for now. raise NotImplementedError #logging.info("GPU acceleration is disabled.") # prepare training and validation dataset db = data_prepare.prepare_db(opt) # sanity check for FG-NET dataset, not used for now # assertion: the total images in the eval set lists should be 1002 total_eval_imgs = sum([len(db['eval'][i]) for i in range(len(db['eval']))]) print(total_eval_imgs) if db['train'][0].name == 'FGNET': assert total_eval_imgs == 1002, 'The preparation of the evalset is incorrect.' # training if opt.train: best_MAEs = [] last_MAEs = [] # record the current time opt.save_dir += time.asctime(time.localtime(time.time())) # for FG-NET, do training multiple times for leave-one-out validation # for CACD, do training just once for exp_id in range(len(db['train'])): # initialize the model model_train = model.prepare_model(opt) #print("model shape:") # print(db['train'].head) #print( np.array(db['eval']).shape) # configurate the optimizer and learning rate scheduler optim, sche = optimizer.prepare_optim(model_train, opt) # train the model and record mean average error (MAE) model_train, MAE, last_MAE = trainer.train(model_train, optim, sche, db, opt, exp_id) best_MAEs += MAE last_MAEs.append(last_MAE.data.item()) # remove the trained model for leave-one-out validation if exp_id != len(db['train']) - 1: del model_train #np.save('./MAE.npy', np.array(best_MAEs)) #np.save('./Last_MAE.npy', np.array(last_MAEs)) # save the final trained model #utils.save_model(model_train, opt) # testing a pre-trained model elif opt.evaluate: # path to the pre-trained model save_dir = opt.test_model_path #example: save_dir = '../model/CACD_MAE_4.59.pth' model_loaded = torch.load(save_dir) # test the model on the evaluation set # the last subject is the test set (compatible with FG-NET) trainer.evaluate(model_loaded, db['eval'][-1], opt) return
if opt.dataset_name == 'Morph': # Sorry that the MORPH dataset is currently not freely available. # For now I can not release my pre-processed dataset without permission. raise ValueError elif opt.dataset_name == 'CACD': model_path = "../pre-trained/CACD_MAE_4.59.pth" else: raise NotImplementedError # load model model = torch.load(model_path) model.cuda() # prepare dataset db = prepare_db(opt) dataset = db['eval'][0] # compute saliency map # pick a tree within the forest tree_idx = 0 depth = model.forest.trees[0].depth # pick a splitting node index (optional) #node_idx = 0 # 0 - 510 for 511 splitting nodes # get saliency maps for a specified node for different input tensors # vis_utils.get_node_saliency_map(dataset, model, tree_idx, node_idx, name=opt.dataset) # get the computational paths for the input sample, labels, paths, class_pred = vis_utils.get_paths(dataset, model,