def train(index = None): # change parameters opt = Options().parse() #opt = Options().parse() if opt is None else opt opt = tranform_options(index, opt) if opt.mode == 'generator': print('Starting generator...') data_generation(opt) elif opt.mode == 'generator_processor': print('Starting generator - processor no save images...') server_processing(opt) else: print('Starting processor...') server_processing(opt)
def visualize(index=2): # change parameters options = Options().parse() #options = Options().parse() if options is None else options options = tranform_options(index, options) # use cuda? options.cuda = torch.cuda.is_available() cudnn.benchmark = True # set True to speedup train_mean = None train_std = None if os.path.exists(os.path.join(options.save, 'mean.npy')): train_mean = np.load(os.path.join(options.save, 'mean.npy')) train_std = np.load(os.path.join(options.save, 'std.npy')) if options.datasetName == 'miniImagenet': dataLoader = miniImagenetDataLoader(type=MiniImagenetPairs, opt=options) elif options.datasetName == 'omniglot': dataLoader = omniglotDataLoader(type=Omniglot_Pairs, opt=options, train_mean=train_mean, train_std=train_std) else: pass # Get the params opt = dataLoader.opt # Use the same seed to split the train - val - test if os.path.exists(os.path.join(options.save, 'dataloader_rnd_seed_arc.npy')): rnd_seed = np.load( os.path.join(options.save, 'dataloader_rnd_seed_arc.npy')) else: rnd_seed = np.random.randint(0, 100000) np.save(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy'), rnd_seed) # Get the DataLoaders from train - val - test train_loader, val_loader, test_loader = dataLoader.get(rnd_seed=rnd_seed) train_mean = dataLoader.train_mean train_std = dataLoader.train_std if not os.path.exists(os.path.join(options.save, 'mean.npy')): np.save(os.path.join(opt.save, 'mean.npy'), train_mean) np.save(os.path.join(opt.save, 'std.npy'), train_std) if opt.cuda: models.use_cuda = True if opt.name is None: # if no name is given, we generate a name from the parameters. # only those parameters are taken, which if changed break torch.load compatibility. #opt.name = "train_{}_{}_{}_{}_{}_wrn".format(str_model_fn, opt.numGlimpses, opt.glimpseSize, opt.numStates, opt.name = "{}_{}_{}_{}_{}_{}_wrn".format( opt.naive_full_type, "fcn" if opt.apply_wrn else "no_fcn", opt.arc_numGlimpses, opt.arc_glimpseSize, opt.arc_numStates, "cuda" if opt.cuda else "cpu") print("[{}]. Will start training {} with parameters:\n{}\n\n".format( multiprocessing.current_process().name, opt.name, opt)) # make directory for storing models. models_path = os.path.join(opt.save, opt.name) if not os.path.isdir(models_path): os.makedirs(models_path) else: shutil.rmtree(models_path) fcn = None convCNN = None if opt.apply_wrn: # Convert the opt params to dict. optDict = dict([(key, value) for key, value in opt._get_kwargs()]) convCNN = ConvCNNFactory.createCNN(opt.wrn_name_type, optDict) if opt.wrn_load: # Load the model in fully convolutional mode fcn, params, stats = convCNN.load(opt.wrn_load, fully_convolutional=True) else: fcn = convCNN.create(fully_convolutional=True) # initialise the model discriminator = ArcBinaryClassifier(num_glimpses=opt.arc_numGlimpses, glimpse_h=opt.arc_glimpseSize, glimpse_w=opt.arc_glimpseSize, channels=opt.arc_nchannels, controller_out=opt.arc_numStates, attn_type=opt.arc_attn_type, attn_unroll=opt.arc_attn_unroll, attn_dense=opt.arc_attn_dense) # load from a previous checkpoint, if specified. if opt.arc_load is not None and os.path.exists(opt.arc_load): if torch.cuda.is_available(): discriminator.load_state_dict(torch.load(opt.arc_load)) else: discriminator.load_state_dict( torch.load(opt.arc_load, map_location=torch.device('cpu'))) if opt.cuda: discriminator.cuda() # Set for the first batch a random seed for AumentationAleju train_loader.dataset.agumentation_seed = int(np.random.rand() * 1000) for batch_idx, (data, label) in enumerate(train_loader): if opt.cuda: data = data.cuda() label = label.cuda() inputs = Variable(data, requires_grad=False) targets = Variable(label) batch_size, npair, nchannels, x_size, y_size = inputs.shape inputs = inputs.view(batch_size * npair, nchannels, x_size, y_size) if fcn: inputs = fcn(inputs) _, nfilters, featx_size, featy_size = inputs.shape inputs = inputs.view(batch_size, npair, nfilters, featx_size, featy_size) #features, updated_states = discriminator(inputs) all_hidden = discriminator.arc._forward(inputs) glimpse_params = torch.tanh(discriminator.arc.glimpser( all_hidden)) # return [num_glimpses*2,batchsize,(x, y, delta)] sample = data[0] _, channels, height, witdth = sample.shape # separate the masks of each image. masks1 = [] masks2 = [] for i in range(glimpse_params.shape[0]): mask = discriminator.arc.glimpse_window.get_attention_mask( glimpse_params[i], mask_h=height, mask_w=witdth) if i % 2 == 1: # the first image outputs the hidden state for the next image masks1.append(mask) else: masks2.append(mask) channels = 3 for glimpse_i, (mask1, mask2) in enumerate(zip(masks1, masks2)): for batch_i in range(data.shape[0]): if len(train_mean.shape) == 1: sample_0 = ((data[batch_i, 0].data.cpu().numpy().transpose( 1, 2, 0) * train_std + train_mean) * 255.0).astype( np.uint8) sample_1 = ((data[batch_i, 1].data.cpu().numpy().transpose( 1, 2, 0) * train_std + train_mean) * 255.0).astype( np.uint8) else: sample_0 = ((data[batch_i, 0].data.cpu().numpy().transpose( 1, 2, 0) * train_std.transpose(1, 2, 0) + train_mean.transpose(1, 2, 0)) * 255.0).astype(np.uint8) sample_1 = ((data[batch_i, 1].data.cpu().numpy().transpose( 1, 2, 0) * train_std.transpose(1, 2, 0) + train_mean.transpose(1, 2, 0)) * 255.0).astype(np.uint8) if sample_0.shape[2] == 1: sample_0 = np.repeat(sample_0, 3, axis=2) sample_1 = np.repeat(sample_1, 3, axis=2) display(opt, sample_0, mask1[batch_i], sample_1, mask2[batch_i], "img_batch_%d_glimpse_%d.png" % (batch_i, glimpse_i))
def train(index=None): # change parameters opt = Options().parse() #opt = Options().parse() if opt is None else opt opt = tranform_options(index, opt) # use cuda? opt.cuda = torch.cuda.is_available() cudnn.benchmark = True # set True to speedup # Load mean/std if exists train_mean = None train_std = None if os.path.exists(os.path.join(opt.save, 'mean.npy')): train_mean = np.load(os.path.join(opt.save, 'mean.npy')) train_std = np.load(os.path.join(opt.save, 'std.npy')) # Load FCN # Convert the opt params to dict. optDict = dict([(key, value) for key, value in opt._get_kwargs()]) fcn = ConvCNNFactory.createCNN(opt.wrn_name_type, optDict) if opt.wrn_load and os.path.exists(opt.wrn_load): if torch.cuda.is_available(): fcn.load_state_dict(torch.load(opt.wrn_load)) else: fcn.load_state_dict( torch.load(opt.wrn_load, map_location=torch.device('cpu'))) if opt.cuda: fcn.cuda() # Load Dataset opt.setType = 'set1' if opt.datasetName == 'miniImagenet': dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=fcn) elif opt.datasetName == 'omniglot': dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=fcn, train_mean=train_mean, train_std=train_std) elif opt.datasetName == 'banknote': dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=fcn, train_mean=train_mean, train_std=train_std) else: pass # Use the same seed to split the train - val - test if os.path.exists(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy')): rnd_seed = np.load( os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy')) else: rnd_seed = np.random.randint(0, 100000) np.save(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy'), rnd_seed) # Get the DataLoaders from train - val - test train_loader, val_loader, test_loader = dataLoader.get(rnd_seed=rnd_seed) train_mean = dataLoader.train_mean train_std = dataLoader.train_std if not os.path.exists(os.path.join(opt.save, 'mean.npy')): np.save(os.path.join(opt.save, 'mean.npy'), train_mean) np.save(os.path.join(opt.save, 'std.npy'), train_std) # Delete memory del dataLoader # Load Set2. opt.setType = 'set2' if opt.datasetName == 'miniImagenet': dataLoader2 = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=fcn) elif opt.datasetName == 'omniglot': dataLoader2 = omniglotDataLoader(type=Omniglot, opt=opt, fcn=fcn, train_mean=train_mean, train_std=train_std) elif opt.datasetName == 'banknote': dataLoader2 = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=fcn, train_mean=train_mean, train_std=train_std) else: pass # Get the DataLoaders from train - val - test #train_loader2, val_loader2, test_loader2 = dataLoader2.get(rnd_seed=rnd_seed) train_loader2, val_loader2, test_loader2 = dataLoader2.get( rnd_seed=rnd_seed, dataPartition=[None, None, 'train+val+test']) del dataLoader2 if opt.cuda: models.use_cuda = True if opt.name is None: # if no name is given, we generate a name from the parameters. # only those parameters are taken, which if changed break torch.load compatibility. #opt.name = "train_{}_{}_{}_{}_{}_wrn".format(str_model_fn, opt.numGlimpses, opt.glimpseSize, opt.numStates, opt.name = "{}_{}_{}_{}_{}_{}_wrn".format( opt.naive_full_type, "fcn" if opt.apply_wrn else "no_fcn", opt.arc_numGlimpses, opt.arc_glimpseSize, opt.arc_numStates, "cuda" if opt.cuda else "cpu") print("[{}]. Will start training {} with parameters:\n{}\n\n".format( multiprocessing.current_process().name, opt.name, opt)) # make directory for storing models. models_path = os.path.join(opt.save, opt.name) if not os.path.isdir(models_path): os.makedirs(models_path) else: shutil.rmtree(models_path) # create logger logger = Logger(models_path) loss_fn = torch.nn.CrossEntropyLoss() if opt.cuda: loss_fn = loss_fn.cuda() optimizer = torch.optim.Adam(params=fcn.parameters(), lr=opt.arc_lr) scheduler = ReduceLROnPlateau(optimizer, mode='min', patience=opt.arc_lr_patience, verbose=True) # load preexisting optimizer values if exists if os.path.exists(opt.arc_optimizer_path): if torch.cuda.is_available(): optimizer.load_state_dict(torch.load(opt.arc_optimizer_path)) else: optimizer.load_state_dict( torch.load(opt.arc_optimizer_path, map_location=torch.device('cpu'))) # Select the epoch functions do_epoch_fn = do_epoch_classification discriminator = None coAttn = None ################################### ## TRAINING ARC/CONVARC ################################### epoch = 0 if opt.arc_resume == True or opt.arc_load is None: try: while epoch < opt.train_num_batches: epoch += 1 train_auc_epoch, train_auc_std_epoch, train_loss_epoch = arc_train.arc_train( epoch, do_epoch_fn, opt, train_loader, discriminator, logger, optimizer=optimizer, loss_fn=loss_fn, fcn=fcn, coAttn=coAttn) # Reduce learning rate when a metric has stopped improving scheduler.step(train_loss_epoch) if epoch % opt.val_freq == 0: val_auc_epoch, val_auc_std_epoch, val_loss_epoch, is_model_saved = arc_val.arc_val( epoch, do_epoch_fn, opt, val_loader, discriminator, logger, optimizer=optimizer, loss_fn=loss_fn, fcn=fcn, coAttn=coAttn) if is_model_saved: print('++++++++++++TESTING FOR SET1++++++++++++++') test_auc_epoch, test_auc_std_epoch = arc_test.arc_test( epoch, do_epoch_fn, opt, test_loader, discriminator, logger) print( '++++++++++++FINISHED TESTING FOR SET1. AUC: %f, AUC_STD: %f ++++++++++++++' % (test_auc_epoch, test_auc_std_epoch)) print('[%s] ... Testing Set2' % multiprocessing.current_process().name) test_auc_epoch, test_auc_std_epoch = arc_test.arc_test( epoch, do_epoch_fn, opt, test_loader2, discriminator, logger) print( '++++++++++++FINISHED TESTING FOR SET2. AUC: %f, AUC_STD: %f ++++++++++++++' % (test_auc_epoch, test_auc_std_epoch)) logger.step() print("[%s] ... training done" % multiprocessing.current_process().name) print( "[%s], best validation auc: %.2f, best validation loss: %.5f" % (multiprocessing.current_process().name, arc_val.best_auc, arc_val.best_validation_loss)) print("[%s] ... exiting training regime " % multiprocessing.current_process().name) except KeyboardInterrupt: pass ################################### #''' UNCOMMENT!!!! TESTING NAIVE - FULLCONTEXT # LOAD AGAIN THE FCN AND ARC models. Freezing the weights. print('[%s] ... Testing Set1' % multiprocessing.current_process().name) test_loader.dataset.mode = 'generator_processor' test_acc_epoch = arc_test.arc_test(epoch, do_epoch_fn, opt, test_loader, discriminator, logger) print('[%s] ... FINISHED! ...' % multiprocessing.current_process().name) #''' # set the mode of the dataset to generator_processor # which generates and processes the images without saving them. opt.mode = 'generator_processor' ## Get the set2 and try print('[%s] ... Loading Set2' % multiprocessing.current_process().name) opt.setType = 'set2' if opt.datasetName == 'miniImagenet': dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=None) elif opt.datasetName == 'omniglot': dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=None, train_mean=None, train_std=None) elif opt.datasetName == 'banknote': dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=None, train_mean=None, train_std=None) else: pass train_loader, val_loader, test_loader = dataLoader.get( rnd_seed=rnd_seed, dataPartition=[None, None, 'train+val+test']) print('[%s] ... Testing Set2' % multiprocessing.current_process().name) test_acc_epoch = arc_test.arc_test(epoch, do_epoch_fn, opt, test_loader, discriminator, logger) print('[%s] ... FINISHED! ...' % multiprocessing.current_process().name)
def train(index=14): # change parameters opt = Options().parse() #opt = Options().parse() if opt is None else opt opt = tranform_options(index, opt) opt.cuda = False # set the mode of the dataset to generator_processor # which generates and processes the images without saving them. opt.mode = 'generator_processor' # Load Dataset opt.setType = 'set1' if opt.datasetName == 'miniImagenet': dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=None) elif opt.datasetName == 'omniglot': dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=None, train_mean=None, train_std=None) elif opt.datasetName == 'banknote': dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=None, train_mean=None, train_std=None) else: pass # Use the same seed to split the train - val - test if os.path.exists(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy')): rnd_seed = np.load( os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy')) else: rnd_seed = np.random.randint(0, 100000) np.save(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy'), rnd_seed) # Get the DataLoaders from train - val - test train_loader, val_loader, test_loader = dataLoader.get( rnd_seed=rnd_seed, dataPartition=['train+val', None, 'test']) if opt.name is None: # if no name is given, we generate a name from the parameters. # only those parameters are taken, which if changed break torch.load compatibility. #opt.name = "train_{}_{}_{}_{}_{}_wrn".format(str_model_fn, opt.numGlimpses, opt.glimpseSize, opt.numStates, opt.name = "{}_{}_{}_{}_{}_{}_wrn".format( opt.naive_full_type, "fcn" if opt.apply_wrn else "no_fcn", opt.arc_numGlimpses, opt.arc_glimpseSize, opt.arc_numStates, "cuda" if opt.cuda else "cpu") print("[{}]. Will start training {} with parameters:\n{}\n\n".format( multiprocessing.current_process().name, opt.name, opt)) # make directory for storing models. models_path = os.path.join(opt.save, opt.name) if not os.path.isdir(models_path): os.makedirs(models_path) else: shutil.rmtree(models_path) # create logger logger = Logger(models_path) # create object features #nameFeatures = 'HoGFeature' #nameFeatures = 'UCIFeature' nameFeatures = opt.wrn_name_type objFeatures = eval(nameFeatures + '()') objClassifier = XGBoostClassifier() train_features = [] train_labels = [] # if the training features exists go to testing if not (os.path.exists(os.path.join(opt.save, 'train_features.npy')) and os.path.exists(os.path.join(opt.save, 'train_labels.npy'))): ## EXTRACT FEATURES TRAIN for i in range(opt.train_num_batches): for batch_idx, (data, labels) in enumerate(tqdm(train_loader)): # transform batch of data and label tensors to numpy data = data.numpy().transpose(0, 2, 3, 1) labels = labels.numpy().tolist() for i in range(len(data)): features = objFeatures.extract(data[i]) train_features.append(features) train_labels.append(labels) # save the features train_features = np.stack(train_features) train_labels = [item for sublist in train_labels for item in sublist] np.save(os.path.join(opt.save, 'train_features.npy'), train_features) np.save(os.path.join(opt.save, 'train_labels.npy'), train_labels) else: train_features = np.load(os.path.join(opt.save, 'train_features.npy')) train_labels = np.load(os.path.join(opt.save, 'train_labels.npy')) ## TRAIN objClassifier.train(train_features, train_labels) objClassifier.save(opt.save) ## EXTRACT FEATURES TEST for j in range(opt.test_num_batches): test_features = [] test_labels = [] for batch_idx, (data, labels) in enumerate(tqdm(test_loader)): # transform batch of data and label tensors to numpy data = data.numpy().transpose(0, 2, 3, 1) labels = labels.numpy().tolist() for i in range(len(data)): features = objFeatures.extract(data[i]) test_features.append(features) test_labels.append(labels) ## PREDICT test_features = np.stack(test_features) test_labels = [item for sublist in test_labels for item in sublist] preds = objClassifier.predict(test_features) test_features_set1 = test_features test_labels_set1 = test_labels preds_set1 = preds show_results(test_labels, preds, 'TEST SET 1. Iter: ' + str(j), show=False) ## Get the set2 and try opt.setType = 'set2' if opt.datasetName == 'miniImagenet': dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=None) elif opt.datasetName == 'omniglot': dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=None, train_mean=None, train_std=None) elif opt.datasetName == 'banknote': dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=None, train_mean=None, train_std=None) else: pass train_loader, val_loader, test_loader = dataLoader.get( rnd_seed=rnd_seed, dataPartition=[None, None, 'train+val+test']) ## EXTRACT FEATURES TEST for j in range(opt.test_num_batches): test_features = [] test_labels = [] for batch_idx, (data, labels) in enumerate(tqdm(test_loader)): # transform batch of data and label tensors to numpy data = data.numpy().transpose(0, 2, 3, 1) labels = labels.numpy().tolist() for i in range(len(data)): features = objFeatures.extract(data[i]) test_features.append(features) test_labels.append(labels) ## PREDICT test_features = np.stack(test_features) test_labels = [item for sublist in test_labels for item in sublist] preds = objClassifier.predict(test_features) test_features_set2 = test_features test_labels_set2 = test_labels preds_set2 = preds show_results(test_labels, preds, 'TEST SET 2. Iter: ' + str(j), show=False) #''' UNCOMMENT!!!! TESTING NAIVE - FULLCONTEXT # LOAD AGAIN THE FCN AND ARC models. Freezing the weights. print('[%s] ... Testing' % multiprocessing.current_process().name) #TODO: TEST!!! print('[%s] ... FINISHED! ...' % multiprocessing.current_process().name)