def test(args): config_file = args.model lasagne.random.set_rng(np.random.RandomState(0)) print(config_file) config_module = imp.load_source('config', config_file) cfg = config_module.cfg weights_fname = os.path.join(args.log_dir, 'model.ckpt-' + str(args.weights) + '.npz') model = config_module.get_model() print('Compiling theano functions...') tfuncs, tvars, model = make_training_functions(cfg, model, args) metadata = checkpoints.load_weights(weights_fname, model['l_out']) print('Testing...') # Get Test Data x_t = np.load(os.path.join(args.data, 'test.npz'))['features'] y_t = np.load(os.path.join(args.data, 'test.npz'))['targets'] n_rotations = 24 confusion_matrix = np.zeros((40, 40), dtype=np.int) num_test_batches = int(math.ceil(float(len(x_t)) / float(n_rotations))) test_chunk_size = n_rotations * cfg['batches_per_chunk'] num_test_chunks = int(math.ceil(float(len(x_t)) / test_chunk_size)) test_class_error = [] pred_array = [] test_itr = 0 predictions = [] labels = [] pred_array = [] # Evaluate on test set for chunk_index in xrange(num_test_chunks): upper_range = min(len(y_t), (chunk_index + 1) * test_chunk_size) # x_shared = np.asarray(x_t[chunk_index * test_chunk_size:upper_range, :, :, :, :], dtype=np.float32) y_shared = np.asarray(y_t[chunk_index * test_chunk_size:upper_range], dtype=np.float32) num_batches = int(math.ceil(float(len(x_shared)) / n_rotations)) tvars['X_shared'].set_value(4.0 * x_shared - 1.0, borrow=True) tvars['y_shared'].set_value(y_shared, borrow=True) lvs, accs = [], [] for bi in xrange(num_batches): [classifier_loss, test_error_rate, pred, raw_pred, y] = tfuncs['test_function'](bi) test_class_error.append(test_error_rate) pred_array.append(np.array(pred)) labels.append(y[0]) print('Test acc is: ' + str(1 - np.mean(test_class_error))) return pred_array, labels
def main(args): # Load model lasagne.random.set_rng(np.random.RandomState(0)) config_module = imp.load_source('config', args.config_path) cfg = config_module.cfg weights_fname =str(args.config_path)[:-3]+'.npz' model = config_module.get_model() print('Compiling theano functions...') tfuncs, tvars,model = make_training_functions(cfg,model) metadata = checkpoints.load_weights(weights_fname, model['l_out']) best_acc = metadata['best_acc'] if 'best_acc' in metadata else 0 print('best acc = '+str(best_acc)) print('Testing...') # Get Test Data xt = np.asarray(np.load('modelnet40_rot24_test.npz')['features'],dtype=np.float32) yt = np.asarray(np.load('modelnet40_rot24_test.npz')['targets'],dtype=np.float32) n_rotations = 24 confusion_matrix = np.zeros((40,40),dtype=np.int) num_test_batches = int(math.ceil(float(len(xt))/float(n_rotations))) test_chunk_size = n_rotations*cfg['batches_per_chunk'] num_test_chunks=int(math.ceil(float(len(xt))/test_chunk_size)) test_class_error = [] pred_array = [] test_itr=0 # Evaluate on test set for chunk_index in xrange(num_test_chunks): upper_range = min(len(yt),(chunk_index+1)*test_chunk_size) # x_shared = np.asarray(xt[chunk_index*test_chunk_size:upper_range,:,:,:,:],dtype=np.float32) y_shared = np.asarray(yt[chunk_index*test_chunk_size:upper_range],dtype=np.float32) num_batches = int(math.ceil(float(len(x_shared))/n_rotations)) tvars['X_shared'].set_value(6.0 * x_shared-1.0, borrow=True) tvars['y_shared'].set_value(y_shared, borrow=True) lvs, accs = [],[] for bi in xrange(num_batches): test_itr+=1 print(test_itr) [batch_test_class_error,confusion,raw_pred] = tfuncs['test_function'](bi) # Get the test test_class_error.append(batch_test_class_error) pred_array.append(raw_pred) # print(confusion) # confusion_matrix+=confusion # confusion_matrix[confusion,int(yt[cfg['n_rotations']*test_itr])]+=1 # print(confusion_matrix) # Save outputs to csv files. np.savetxt(str(args.config_path)[:-3]+'.csv', np.asarray(pred_array), delimiter=",") t_class_error = 1-float(np.mean(test_class_error)) print('Test error is: ' + str(t_class_error))
def main(): videoFrames, playerBoxes, Width, Height, colors = extractFrame(args.videoPath) print("Video Dimensions: ({},{})".format(Width, Height)) print("Length of videoFrames: {}".format(len(videoFrames))) print("Length of playerBoxes: {}".format(len(playerBoxes))) frames = cropWindows(videoFrames, playerBoxes, seq_length=args.seq_length, vid_stride=args.vid_stride) print("Number of players tracked: {}".format(len(frames))) print("Number of windows: {}".format(len(frames[0]))) print("# Frames per Clip: {}".format(len(frames[0][0]))) print("Frame Shape: {}".format(frames[0][0][0].shape)) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Initialize R(2+1)D Model model = models.video.r2plus1d_18(pretrained=args.pretrained, progress=True) # input of the next hidden layer num_ftrs = model.fc.in_features # New Model is trained with 128x176 images # Calculation: model.fc = nn.Linear(num_ftrs, args.num_classes, bias=True) model = load_weights(model, args) if torch.cuda.is_available(): # Put model into device after updating parameters model = model.to(device) model.eval() predictions = {} for player in range(len(playerBoxes[0])): input_frames = inference_batch(torch.FloatTensor(frames[player])) print(input_frames.shape) input_frames = input_frames.to(device=device) with torch.no_grad(): outputs = model(input_frames) _, preds = torch.max(outputs, 1) print(preds.cpu().numpy().tolist()) predictions[player] = preds.cpu().numpy().tolist() print(predictions) # Writing Video output_path = args.output_path + "lebron_shoots-3.mp4" writeVideo(output_path, videoFrames, playerBoxes, predictions, colors, frame_width=1280, frame_height=720, vid_stride=16)
def test(config): # Load config file config_module = imp.load_source('config', config.model_definition) cfg = config_module.cfg # Get model model = config_module.get_model() # Compile functions log(config.log_file, 'Compiling theano functions...') tfuncs, tvars, model = make_test_function(cfg, model, config) ckptfile = os.path.join( config.log_dir, config.snapshot_prefix + str(config.weights) + '.npz') metadata = checkpoints.load_weights(ckptfile, model['l_out']) x_t = np.load(os.path.join(config.data, 'test.npz'))['features'] y_t = np.load(os.path.join(config.data, 'test.npz'))['targets'] n_rotations = config.num_rotations confusion_matrix = np.zeros((40, 40), dtype=np.int) num_test_batches = int(math.ceil(float(len(x_t)) / float(n_rotations))) test_chunk_size = n_rotations * cfg['batches_per_chunk'] num_test_chunks = int(math.ceil(float(len(x_t)) / test_chunk_size)) evaluate(x_t, y_t, cfg, tfuncs, tvars, config)
gen_layers.append(ll.batch_norm(ll.DenseLayer(gen_layers[-1], num_units=500, nonlinearity=ln.softplus, name='gen-5'), name='gen-6')) gen_layers.append(MLPConcatLayer([gen_layers[-1], gen_in_y], num_classes, name='gen-7')) gen_layers.append(nn.l2normalize(ll.DenseLayer(gen_layers[-1], num_units=28**2, nonlinearity=gen_final_non, name='gen-8'))) # outputs gen_out_x = ll.get_output(gen_layers[-1], {gen_in_y:sym_y_g, gen_in_z:sym_z_rand}, deterministic=False) gen_out_x_shared = ll.get_output(gen_layers[-1], {gen_in_y:sym_y_g, gen_in_z:sym_z_shared}, deterministic=False) gen_out_x_interpolation = ll.get_output(gen_layers[-1], {gen_in_y:sym_y_g, gen_in_z:sym_z_input}, deterministic=False) generate = theano.function(inputs=[sym_y_g], outputs=gen_out_x) generate_shared = theano.function(inputs=[sym_y_g], outputs=gen_out_x_shared) generate_interpolation = theano.function(inputs=[sym_y_g, sym_z_input], outputs=gen_out_x_interpolation) ''' Load pretrained model ''' load_weights(args.oldmodel, gen_layers) # interpolation on latent space (z) class conditionally for i in xrange(10): sample_y = np.int32(np.repeat(np.arange(num_classes), batch_size_g/num_classes)) orignial_z = np.repeat(rng.uniform(size=(num_classes,n_z)), batch_size_g/num_classes, axis=0) target_z = np.repeat(rng.uniform(size=(num_classes,n_z)), batch_size_g/num_classes, axis=0) alpha = np.tile(np.arange(batch_size_g/num_classes) * 1.0 / (batch_size_g/num_classes-1), num_classes) alpha = alpha.reshape(-1,1) z = np.float32((1-alpha)*orignial_z+alpha*target_z) x_gen_batch = generate_interpolation(sample_y, z) x_gen_batch = x_gen_batch.reshape((batch_size_g,-1)) image = paramgraphics.mat_to_img(x_gen_batch.T, dim_input, colorImg=colorImg, tile_shape=(num_classes, 2*num_classes), scale=generation_scale, save_path=os.path.join(outfolder, 'interpolation-'+str(i)+'.png')) # class conditionally generation with shared z and fixed y for i in xrange(10):
givens={sym_x_u: shared_unlabel[slice_x_u_c], sym_x_u_rep: shared_unlabel[slice_x_u_c]}) pretrain_batch_cla = theano.function(inputs=[sym_x_l, sym_y, slice_x_u_c, sym_lr_cla, sym_b_c, sym_unsup_weight], outputs=[pretrain_cost, cla_cost_l, cla_cost_u], updates=pretrain_updates, givens={sym_x_u: shared_unlabel[slice_x_u_c], sym_x_u_rep: shared_unlabel[slice_x_u_c]}) generate = theano.function(inputs=[sym_y_g], outputs=image) inference = theano.function(inputs=[sym_x_u_i], outputs=inf_z) # avg evaluate = theano.function(inputs=[sym_x_eval, sym_y], outputs=[accurracy_eval], givens=cla_avg_givens) ''' Load pretrained model ''' if 'oldmodel' in cfg: from utils.checkpoints import load_weights load_weights(cfg['oldmodel'], dis_layers+[classifier,]+gen_layers) for (p, a) in zip(ll.get_all_params(classifier), avg_params): a.set_value(p.get_value()) ''' train and evaluate ''' init_fn(x_unlabelled[:batch_size]) ''' Pretrain C ''' print 'Start pretraining' for epoch in range(1, 1+pre_num_epoch): # randomly permute data and labels p_l = rng.permutation(x_labelled.shape[0])
train_subset, val_subset = random_split( train_subset, [args.n_total-args.test_n-args.val_n, args.val_n], generator=torch.Generator().manual_seed(1)) train_loader = DataLoader(dataset=train_subset, shuffle=True, batch_size=args.batch_size) val_loader = DataLoader(dataset=val_subset, shuffle=False, batch_size=args.batch_size) test_loader = DataLoader(dataset=test_subset, shuffle=False, batch_size=args.batch_size) dataloaders_dict = {'train': train_loader, 'val': val_loader} # Train optimizer_ft = optim.Adam(params_to_update, lr=args.lr) criterion = nn.CrossEntropyLoss() if args.continue_epoch: model = load_weights(model, args) if torch.cuda.is_available(): # Put model into device after updating parameters model = model.to(device) criterion = criterion.to(device) # Train and evaluate model, train_loss_history, val_loss_history, train_acc_history, val_acc_history, train_f1_score, val_f1_score, plot_epoch = train_model(model, dataloaders_dict, criterion, optimizer_ft, args, start_epoch=args.start_epoch, num_epochs=args.num_epochs)
def train(config): # Set random seed to ensure identical network initializations. # Note that cuDNN's convolutions are nondeterministic, so this # does not guarantee that two networks will behave identically. lasagne.random.set_rng(np.random.RandomState(1234)) # Load config file config_module = imp.load_source('config', config.model_definition) cfg = config_module.cfg # Get model model = config_module.get_model() # Compile functions log(config.log_file, 'Compiling theano functions...') test_function, test_vars, model = make_test_function(cfg, model, config) tfuncs, tvars, model = make_training_functions(cfg, model, config) tfuncs.update(test_function) tvars.update(test_vars) weights = config.weights if weights == -1: start_epoch = 0 else: ld = config.log_dir WEIGHTS = config.weights ckptfile = os.path.join(ld, config.snapshot_prefix + str(WEIGHTS) + '.npz') log(config.log_file, 'Loaded weights.') start_epoch = WEIGHTS + 1 ACC_LOGGER.load( (os.path.join(ld, "{}_acc_train_accuracy.csv".format(config.name)), os.path.join(ld, "{}_acc_eval_accuracy.csv".format(config.name))), epoch=WEIGHTS) LOSS_LOGGER.load( (os.path.join(ld, "{}_loss_train_loss.csv".format(config.name)), os.path.join(ld, '{}_loss_eval_loss.csv'.format(config.name))), epoch=WEIGHTS) metadata = checkpoints.load_weights(ckptfile, model['l_out']) itr = 0 # Load data and shuffle training examples. # Note that this loads the entire dataset into RAM! If you don't # have a lot of RAM, consider only loading chunks of this at a time. log(config.log_file, 'Loading Data') x_test = np.load(os.path.join(config.data, 'test.npz'))['features'] y_test = np.load(os.path.join(config.data, 'test.npz'))['targets'] x = np.load(os.path.join(config.data, 'train.npz'))['features'] # Seed the shuffle np.random.seed(42) # Define shuffle indices index = np.random.permutation(len(x)) # Shuffle inputs x = x[index] # Shuffle targets to match inputs y = np.load(os.path.join(config.data, 'train.npz'))['targets'][index] # Define size of chunk to be loaded into GPU memory chunk_size = cfg['batch_size'] * cfg['batches_per_chunk'] # Determine number of chunks num_chunks = int(math.ceil(len(y) / float(chunk_size))) # Get current learning rate new_lr = np.float32(tvars['learning_rate'].get_value()) # Loop across training epochs! begin = start_epoch end = cfg['max_epochs'] + start_epoch log(config.log_file, 'Starting Training') for epoch in xrange(begin, end + 1): #EVAL evaluate(x_test, y_test, cfg, tfuncs, tvars, config, epoch=epoch) ACC_LOGGER.save(config.log_dir) LOSS_LOGGER.save(config.log_dir) ACC_LOGGER.plot(dest=config.log_dir) LOSS_LOGGER.plot(dest=config.log_dir) # Update Learning Rate if isinstance(cfg['learning_rate'], dict) and epoch > 0: if any(x == epoch for x in cfg['learning_rate'].keys()): lr = np.float32(tvars['learning_rate'].get_value()) new_lr = cfg['learning_rate'][epoch] log(config.log_file, 'Changing learning rate from {} to {}'.format(lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) if cfg['decay_rate'] and epoch > 0: lr = np.float32(tvars['learning_rate'].get_value()) new_lr = lr * (1 - cfg['decay_rate']) log(config.log_file, 'Changing learning rate from {} to {}'.format(lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) # Loop across chunks! #for chunk_index in xrange(1): for chunk_index in xrange(num_chunks): # Define upper index of chunk to load # If you start doing complicated things with data loading, consider # wrapping all of this into its own little function. upper_range = min(len(y), (chunk_index + 1) * chunk_size) # Get current chunk x_shared = np.asarray(x[chunk_index * chunk_size:upper_range, :, :, :, :], dtype=np.float32) y_shared = np.asarray(y[chunk_index * chunk_size:upper_range], dtype=np.float32) # Get repeatable seed to shuffle jittered and unjittered instances within chunk. # Note that this seed varies between chunks, but will be constant across epochs. np.random.seed(chunk_index) # Get shuffled chunk indices for a second round of shuffling indices = np.random.permutation(2 * len(x_shared)) # Get number of batches in this chunk num_batches = 2 * len(x_shared) // cfg['batch_size'] # Combine data with jittered data, then shuffle and change binary range from {0,1} to {-1,3}, then load into GPU memory. tvars['X_shared'].set_value(4.0 * np.append( x_shared, jitter_chunk(x_shared, cfg, chunk_index), axis=0)[indices] - 1.0, borrow=True) tvars['y_shared'].set_value(np.append(y_shared, y_shared, axis=0)[indices], borrow=True) lvs, accs = [], [] # Loop across batches! for bi in xrange(num_batches): [classifier_loss, class_acc] = tfuncs['update_iter'](bi) # Record batch loss and accuracy lvs.append(classifier_loss) accs.append(class_acc) # Update iteration counter itr += 1 if itr % max(config.train_log_frq / config.batch_size, 1) == 0: [closs, c_acc ] = [float(np.mean(lvs)), 1.0 - float(np.mean(accs))] ACC_LOGGER.log(c_acc, epoch, "train_accuracy") LOSS_LOGGER.log(closs, epoch, "train_loss") lvs, accs = [], [] log( config.log_file, 'TRAINING: epoch: {0:^3d}, itr: {1:d}, c_loss: {2:.6f}, class_acc: {3:.5f}' .format(epoch, itr, closs, c_acc)) if not (epoch % cfg['checkpoint_every_nth']) or epoch == end: weights_fname = os.path.join(config.log_dir, config.snapshot_prefix + str(epoch)) checkpoints.save_weights(weights_fname, model['l_out'], { 'itr': itr, 'ts': time.time(), 'learning_rate': new_lr }) log(config.log_file, 'Training done')
sym_index = T.iscalar() bslice = slice(sym_index*pre_batch_size_uc, (sym_index+1)*pre_batch_size_uc) pretrain_batch_cla = theano.function(inputs=[sym_x_l, sym_y, sym_index], outputs=[pretrain_cost], updates=pretrain_updates, givens={sym_x_u: shared_unlabel[bslice]}) generate = theano.function(inputs=[sym_y_g], outputs=image) # avg evaluate = theano.function(inputs=[sym_x_eval, sym_y], outputs=[accurracy_eval], givens=cla_avg_givens) ''' Load pretrained model ''' if 'oldmodel' in cfg: from utils.checkpoints import load_weights load_weights(cfg['oldmodel'], dis_layers+cla_layers+gen_layers) for (p, a) in zip(ll.get_all_params(cla_layers), avg_params): a.set_value(p.get_value()) ''' Pretrain C ''' for epoch in range(1, 1+pre_num_epoch): # randomly permute data and labels p_l = rng.permutation(x_labelled.shape[0]) x_labelled = x_labelled[p_l] y_labelled = y_labelled[p_l] p_u = rng.permutation(x_unlabelled.shape[0]).astype('int32') shared_unlabel.set_value(x_unlabelled[p_u])
outputs=[pretrain_cost], updates=pretrain_updates, givens={sym_x_u_d: shared_unlabel[bslice]}) generate = theano.function(inputs=[sym_y_g], outputs=image) inference = theano.function(inputs=[sym_x_u_i], outputs=inf_z) # avg evaluate = theano.function(inputs=[sym_x_eval, sym_y], outputs=[accurracy_eval], givens=cla_avg_givens) ''' Load pretrained model ''' if 'oldmodel' in cfg: print("Load weights from " + cfg['oldmodel']) from utils.checkpoints import load_weights load_weights(cfg['oldmodel'], cla_layers) for (p, a) in zip(ll.get_all_params(cla_layers), avg_params): a.set_value(p.get_value()) ''' Pretrain C ''' print 'Start pretraining' for epoch in range(1, 1 + pre_num_epoch): # randomly permute data and labels p_l = rng.permutation(x_labelled.shape[0]) x_labelled = x_labelled[p_l] y_labelled = y_labelled[p_l] p_u = rng.permutation(x_unlabelled.shape[0]).astype('int32') shared_unlabel.set_value(x_unlabelled[p_u]) for i in range(num_batches_u):
def main(args): # Load config module config_module = imp.load_source('config', args.config_path) cfg = config_module.cfg # Find weights file weights_fname =str(args.config_path)[:-3]+'.npz' # Get Model model = config_module.get_model() # Compile functions print('Compiling theano functions...') tfuncs, tvars,model = make_testing_functions(cfg,model) # Load weights metadata = checkpoints.load_weights(weights_fname, model['l_out']) # Check if previous best accuracy is in metadata from previous tests best_acc = metadata['best_acc'] if 'best_acc' in metadata else 0 print('best accuracy = '+str(best_acc)) print('Testing...') itr = 0 # Load testing data into memory xt = np.asarray(np.load(args.data_path)['features'],dtype=np.float32) yt = np.asarray(np.load(args.data_path)['targets'],dtype=np.float32) # Get number of rotations to average across. If you want this to be different from # the number of rotations specified in the config file, make sure to change the # indices of the test_batch_slice variable above, as well as which data file # you're reading from. n_rotations = cfg['n_rotations'] # Confusion Matrix: Currently not implemented as it doesn't print prettily, # but easy to add in by uncommenting some of the commented lines in the loop. confusion_matrix = np.zeros((40,40),dtype=np.int) # Determine chunk size test_chunk_size = n_rotations*cfg['batches_per_chunk'] # Determine number of chunks num_test_chunks=int(math.ceil(float(len(xt))/test_chunk_size)) # Total number of test batches. Note that we're treating all the rotations # of a single instance as a single batch. There's definitely a more efficient # way to do this, and you'll want to be more efficient if you implement this in # a validation loop, but testing should be infrequent enough that the extra few minutes # this costs is irrelevant. num_test_batches = int(math.ceil(float(len(xt))/float(n_rotations))) # Prepare test error test_class_error = [] # Initialize test iteration counter test_itr=0 # Loop across chunks! for chunk_index in xrange(num_test_chunks): # Define upper index of chunk upper_range = min(len(yt),(chunk_index+1)*test_chunk_size) # Get chunks x_shared = np.asarray(xt[chunk_index*test_chunk_size:upper_range,:,:,:,:],dtype=np.float32) y_shared = np.asarray(yt[chunk_index*test_chunk_size:upper_range],dtype=np.float32) # Get number of batches for this chunk num_batches = len(x_shared)//n_rotations # Prepare data tvars['X_shared'].set_value(4.0 * x_shared-1.0, borrow=True) tvars['y_shared'].set_value(y_shared, borrow=True) # Loop across batches! for bi in xrange(num_batches): # Increment test iteration counter test_itr+=1 # Test! [batch_test_class_error,pred] = tfuncs['test_function'](bi) # Record test results test_class_error.append(batch_test_class_error) # Optionally, update the confusion matrix # confusion_matrix[pred,int(yt[cfg['n_rotations']*test_itr])]+=1 # Optionally, print confusion matrix # print(confusion_matrix) # Get total accuracy t_class_error = 1-float(np.mean(test_class_error)) print('Test accuracy is: ' + str(t_class_error))
def main(args): # Set random seed to ensure identical network initializations. # Note that cuDNN's convolutions are nondeterministic, so this # does not guarantee that two networks will behave identically. lasagne.random.set_rng(np.random.RandomState(1234)) # Load config file config_module = imp.load_source('config', args.config_path) cfg = config_module.cfg # Get weights and metrics filename weights_fname =str(args.config_path)[:-3]+'.npz' metrics_fname = weights_fname[:-4]+'METRICS.jsonl' # Prepare logs logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s| %(message)s') logging.info('Metrics will be saved to {}'.format(metrics_fname)) mlog = metrics_logging.MetricsLogger(metrics_fname, reinitialize=(not args.resume)) # Get model model = config_module.get_model() # Compile functions logging.info('Compiling theano functions...') tfuncs, tvars,model = make_training_functions(cfg,model) # Resume training if file exists and you turn on the resume tag if os.path.isfile(weights_fname) and args.resume: print('loading weights') metadata = checkpoints.load_weights(weights_fname, model['l_out']) # GPU Memory Info; currently not implemented, but you can potentially # use this information to monitor GPU memory useage. baseGPUmem = sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]/1024./1024/1024 # Training loop logging.info('Training...') itr = 0 # Load data and shuffle training examples. # Note that this loads the entire dataset into RAM! If you don't # have a lot of RAM, consider only loading chunks of this at a time. x = np.load(args.data_path)['features'] # Seed the shuffle np.random.seed(42) # Define shuffle indices index = np.random.permutation(len(x)) # Shuffle inputs x = x[index] # Shuffle targets to match inputs y = np.load(args.data_path)['targets'][index] # Define size of chunk to be loaded into GPU memory chunk_size = cfg['batch_size']*cfg['batches_per_chunk'] # Determine number of chunks num_chunks = int(math.ceil(len(y)/float(chunk_size))) # Get current learning rate new_lr = np.float32(tvars['learning_rate'].get_value()) # Loop across training epochs! for epoch in xrange(cfg['max_epochs']): # Tic epoch_start_time = time.time() # Update Learning Rate if isinstance(cfg['learning_rate'], dict) and epoch > 0: if any(x==epoch for x in cfg['learning_rate'].keys()): lr = np.float32(tvars['learning_rate'].get_value()) new_lr = cfg['learning_rate'][epoch] logging.info('Changing learning rate from {} to {}'.format(lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) if cfg['decay_rate'] and epoch > 0: lr = np.float32(tvars['learning_rate'].get_value()) new_lr = lr*(1-cfg['decay_rate']) logging.info('Changing learning rate from {} to {}'.format(lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) # Loop across chunks! for chunk_index in xrange(num_chunks): # Define upper index of chunk to load # If you start doing complicated things with data loading, consider # wrapping all of this into its own little function. upper_range = min(len(y),(chunk_index+1)*chunk_size) # Get current chunk x_shared = np.asarray(x[chunk_index*chunk_size:upper_range,:,:,:,:],dtype=np.float32) y_shared = np.asarray(y[chunk_index*chunk_size:upper_range],dtype=np.float32) # Get repeatable seed to shuffle jittered and unjittered instances within chunk. # Note that this seed varies between chunks, but will be constant across epochs. np.random.seed(chunk_index) # Get shuffled chunk indices for a second round of shuffling indices = np.random.permutation(2*len(x_shared)) # Get number of batches in this chunk num_batches = 2*len(x_shared)//cfg['batch_size'] # Combine data with jittered data, then shuffle and change binary range from {0,1} to {-1,3}, then load into GPU memory. tvars['X_shared'].set_value(4.0 * np.append(x_shared,jitter_chunk(x_shared, cfg,chunk_index),axis=0)[indices]-1.0, borrow=True) tvars['y_shared'].set_value(np.append(y_shared,y_shared,axis=0)[indices], borrow=True) # Prepare loss values lvs, accs = [],[] # Loop across batches! for bi in xrange(num_batches): # Train! [classifier_loss,class_acc] = tfuncs['update_iter'](bi) # Record batch loss and accuracy lvs.append(classifier_loss) accs.append(class_acc) # Update iteration counter itr += 1 # Average losses and accuracies across chunk [closs,c_acc] = [float(np.mean(lvs)),1.0-float(np.mean(accs))] # Report and log losses and accuracies logging.info('epoch: {0:^3d}, itr: {1:d}, c_loss: {2:.6f}, class_acc: {3:.5f}'.format(epoch, itr, closs, c_acc)) mlog.log(epoch=epoch, itr=itr, closs=closs,c_acc=c_acc) # Every Nth epoch, save weights if not (epoch%cfg['checkpoint_every_nth']): checkpoints.save_weights(weights_fname, model['l_out'], {'itr': itr, 'ts': time.time(), 'learning_rate': new_lr}) logging.info('training done')
def main(args): # Set random seed to ensure identical network initializations. # Note that cuDNN's convolutions are nondeterministic, so this # does not guarantee that two networks will behave identically. lasagne.random.set_rng(np.random.RandomState(1234)) # Load config file config_module = imp.load_source('config', args.config_path) cfg = config_module.cfg # Get weights and metrics filename weights_fname = str(args.config_path)[:-3] + '.npz' metrics_fname = weights_fname[:-4] + 'METRICS.jsonl' # Prepare logs logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s| %(message)s') logging.info('Metrics will be saved to {}'.format(metrics_fname)) mlog = metrics_logging.MetricsLogger(metrics_fname, reinitialize=(not args.resume)) # Get model model = config_module.get_model() # Compile functions logging.info('Compiling theano functions...') tfuncs, tvars, model = make_training_functions(cfg, model) # Resume training if file exists and you turn on the resume tag if os.path.isfile(weights_fname) and args.resume: print('loading weights') metadata = checkpoints.load_weights(weights_fname, model['l_out']) # GPU Memory Info; currently not implemented, but you can potentially # use this information to monitor GPU memory useage. baseGPUmem = sbcuda.cuda_ndarray.cuda_ndarray.mem_info( )[0] / 1024. / 1024 / 1024 # Training loop logging.info('Training...') itr = 0 # Load data and shuffle training examples. # Note that this loads the entire dataset into RAM! If you don't # have a lot of RAM, consider only loading chunks of this at a time. x = np.load(args.data_path)['features'] # Seed the shuffle np.random.seed(42) # Define shuffle indices index = np.random.permutation(len(x)) # Shuffle inputs x = x[index] # Shuffle targets to match inputs y = np.load(args.data_path)['targets'][index] # Define size of chunk to be loaded into GPU memory chunk_size = cfg['batch_size'] * cfg['batches_per_chunk'] # Determine number of chunks num_chunks = int(math.ceil(len(y) / float(chunk_size))) # Get current learning rate new_lr = np.float32(tvars['learning_rate'].get_value()) # Loop across training epochs! for epoch in xrange(cfg['max_epochs']): # Tic epoch_start_time = time.time() # Update Learning Rate if isinstance(cfg['learning_rate'], dict) and epoch > 0: if any(x == epoch for x in cfg['learning_rate'].keys()): lr = np.float32(tvars['learning_rate'].get_value()) new_lr = cfg['learning_rate'][epoch] logging.info('Changing learning rate from {} to {}'.format( lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) if cfg['decay_rate'] and epoch > 0: lr = np.float32(tvars['learning_rate'].get_value()) new_lr = lr * (1 - cfg['decay_rate']) logging.info('Changing learning rate from {} to {}'.format( lr, new_lr)) tvars['learning_rate'].set_value(np.float32(new_lr)) # Loop across chunks! for chunk_index in xrange(num_chunks): # Define upper index of chunk to load # If you start doing complicated things with data loading, consider # wrapping all of this into its own little function. upper_range = min(len(y), (chunk_index + 1) * chunk_size) # Get current chunk x_shared = np.asarray(x[chunk_index * chunk_size:upper_range, :, :, :, :], dtype=np.float32) y_shared = np.asarray(y[chunk_index * chunk_size:upper_range], dtype=np.float32) # Get repeatable seed to shuffle jittered and unjittered instances within chunk. # Note that this seed varies between chunks, but will be constant across epochs. np.random.seed(chunk_index) # Get shuffled chunk indices for a second round of shuffling indices = np.random.permutation(2 * len(x_shared)) # Get number of batches in this chunk num_batches = 2 * len(x_shared) // cfg['batch_size'] # Combine data with jittered data, then shuffle and change binary range from {0,1} to {-1,3}, then load into GPU memory. tvars['X_shared'].set_value(4.0 * np.append( x_shared, jitter_chunk(x_shared, cfg, chunk_index), axis=0)[indices] - 1.0, borrow=True) tvars['y_shared'].set_value(np.append(y_shared, y_shared, axis=0)[indices], borrow=True) # Prepare loss values lvs, accs = [], [] # Loop across batches! for bi in xrange(num_batches): # Train! [classifier_loss, class_acc] = tfuncs['update_iter'](bi) # Record batch loss and accuracy lvs.append(classifier_loss) accs.append(class_acc) # Update iteration counter itr += 1 # Average losses and accuracies across chunk [closs, c_acc] = [float(np.mean(lvs)), 1.0 - float(np.mean(accs))] # Report and log losses and accuracies logging.info( 'epoch: {0:^3d}, itr: {1:d}, c_loss: {2:.6f}, class_acc: {3:.5f}' .format(epoch, itr, closs, c_acc)) mlog.log(epoch=epoch, itr=itr, closs=closs, c_acc=c_acc) # Every Nth epoch, save weights if not (epoch % cfg['checkpoint_every_nth']): checkpoints.save_weights(weights_fname, model['l_out'], { 'itr': itr, 'ts': time.time(), 'learning_rate': new_lr }) logging.info('training done')
def main(args): # Load config file config_module = imp.load_source('config', args.config_path) # Get cfg dict cfg = config_module.cfg # Get model model = config_module.get_model(interp=True) # Compile functions print('Compiling theano functions...') tfuncs, tvars = make_test_functions(cfg, model) # Load model weights print('Loading weights from {}'.format(args.weights_fname)) checkpoints.load_weights(args.weights_fname, model['l_latents']) checkpoints.load_weights(args.weights_fname, model['l_out']) # prepare data loader loader = (data_loader(cfg, args.testing_fname)) # Load test set into local memory and get inferred latent values for each # element of the test set. Consider not doing this if you have limited RAM. print('Evaluating on test set') ygnd = np.empty([1,1,32,32,32],dtype=np.uint8) cc = np.empty([1,10],dtype=np.float32) counter = 0 for x_shared, y_shared in loader: ygnd = np.append(ygnd,x_shared,axis=0) cc = np.append(cc,y_shared,axis=0) # Get rid of first entries. Yeah, you could do this better by starting with a regular ole' python list, # appending inside the for loop and then just calling np.asarray, but I didn't really know any python # when I wrote this. Sue me.* # # *Please don't sue me ygnd = np.delete(ygnd,0,axis=0) cc = np.delete(cc,0,axis=0) print('Test set evaluation complete, render time!') # Total number of models loaded and encoded num_instances = len(ygnd)-1; # Seed the rng for repeatable operation np.random.seed(1) # Index of shuffled data display_ix = np.random.choice(len(ygnd), num_instances,replace=False) # Resolution: Number of blocks per side on each voxel. Setting this to less than 3 # results in some ugly renderings, though it will be faster. Setting it higher makes it # prettier but can slow it down. With this setting, I can run interpolation in real-time on # a laptop with 16GB RAM and a GT730M. A setup with a real graphics card should be able to do # much, much more. v_res = 3 # Dimensionality of the voxel grid dim = 32 # Function to produce the data matrix for rendering. def make_data_matrix(x,intensity): return intensity*np.repeat(np.repeat(np.repeat(x[0][0],v_res,axis=0),v_res,axis=1),v_res,axis=2) # VTK Image Importer dataImporter = vtk.vtkImageImport() # Make the initial data matrix # initial random latent vector z_1 = np.random.randn(1,cfg['num_latents']).astype(np.float32) if cfg['cc']: # If augmenting with class-conditional vector cc_1 = np.zeros((1,10),dtype=np.float32) cc_1[0,np.random.randint(10)] = 1 data_matrix = make_data_matrix(np.asarray(tfuncs['pred'](z_1,cc_1),dtype=np.uint8),128) else: data_matrix = make_data_matrix(np.asarray(tfuncs['pred'](z_1),dtype=np.uint8),128) # VTK bookkeeping stuff to prepare the central model data_string = data_matrix.tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) dataImporter.SetDataScalarTypeToUnsignedChar() dataImporter.SetNumberOfScalarComponents(1) dataImporter.SetDataExtent(0, int(dim*v_res)-1, 0, int(dim*v_res)-1, 0, int(dim*v_res)-1) dataImporter.SetWholeExtent(0, int(dim*v_res)-1, 0, int(dim*v_res)-1, 0, int(dim*v_res)-1) # Prepare the interpolant endpoints # endpoint data importer edi = [0,0,0,0] # Endpoint data matrices dm = [0,0,0,0] # Endpoint Latent values eZ = [0,0,0,0] # Endpoint sigmas eS = [0,0,0,0] # Endpoint class-conditional values eCC = [0,0,0,0] # Endpoint intensity values for colormapping eIs = [64,128,192,255] # VTK Bookkeeping stuff for interpolant endpoints for i in xrange(4): eZ[i] = tfuncs['Zfn'](ygnd[None,display_ix[i]])[0] eS[i] = tfuncs['sigma_fn'](ygnd[None,display_ix[i]])[0] eCC[i] = cc[None,display_ix[i]] # This may need changing to preserve shape? to be a 1x10 matrix instead of a 10x1? dm[i] = make_data_matrix(ygnd[None,display_ix[i]],eIs[i]).tostring() edi[i] = vtk.vtkImageImport() edi[i].CopyImportVoidPointer(dm[i], len(dm[i])) edi[i].SetDataScalarTypeToUnsignedChar() edi[i].SetNumberOfScalarComponents(1) edi[i].SetDataExtent(0, int(dim*v_res)-1, 0, int(dim*v_res)-1, 0, int(dim*v_res)-1) edi[i].SetWholeExtent(0, int(dim*v_res)-1, 0, int(dim*v_res)-1, 0, int(dim*v_res)-1) # Prepare color and transparency values colorFunc = vtk.vtkColorTransferFunction() alphaChannelFunc = vtk.vtkPiecewiseFunction() alphaChannelFunc.AddPoint(0, 0.0) alphaChannelFunc.AddPoint(64,1) alphaChannelFunc.AddPoint(128,1.0) alphaChannelFunc.AddPoint(192,1.0) alphaChannelFunc.AddPoint(255,1.0) colorFunc.AddRGBPoint(0, 0.0, 0.0, 0.0) colorFunc.AddRGBPoint(64, 0.0, 0.4, 0.8) colorFunc.AddRGBPoint(128,0.8,0.0,0.0) colorFunc.AddRGBPoint(192,0.8,0.0,0.7) colorFunc.AddRGBPoint(255,0.0,0.8,0.0) # Prepare volume properties. volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorFunc) volumeProperty.SetScalarOpacity(alphaChannelFunc) volumeProperty.ShadeOn() # Keep this on unless you want everything to look terrible volumeProperty.SetInterpolationTypeToNearest() # Optional settings # volumeProperty.SetSpecular(0.2) # volumeProperty.SetAmbient(0.4) # volumeProperty.SetDiffuse(0.6) # More VTK Bookkeeping stuff. compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() # Specify the data and raycast methods for the rendered volumes. volumeMapper = vtk.vtkVolumeRayCastMapper() volumeMapper.SetVolumeRayCastFunction(compositeFunction) volumeMapper.SetInputConnection(dataImporter.GetOutputPort()) # Endpoint volumeMappers evm = [0,0,0,0] for i in xrange(4): evm[i] = vtk.vtkVolumeRayCastMapper() evm[i].SetVolumeRayCastFunction(compositeFunction) evm[i].SetInputConnection(edi[i].GetOutputPort()) # Prepare the volume for the draggable model. volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) volume.SetPosition([0, 0, 0]) # Endpoint volumes ev = [0,0,0,0] vps = [[0,-150.0,-150.0],[0,150.0,-150.0],[0,-150.0,150.0],[0,150.0,150.0]] for i in xrange(4): ev[i] = vtk.vtkVolume() ev[i].SetMapper(evm[i]) ev[i].SetProperty(volumeProperty) ev[i].SetPosition(vps[i]) ev[i].DragableOff() ev[i].PickableOff() # Simple linear 2D Interpolation function for interpolation between latent values. # Feel free to extend this to use more advanced interpolation methods. def interp2d(x,y,z_): x1 = vps[0][1]+97.5 x2 = vps[1][1] y1 = vps[0][2]+97.5 y2 = vps[2][2] return ( ( (z_[0] * (x2-x) * (y2-y)) + (z_[1] * (x-x1) * (y2-y)) + (z_[2] * (x2-x) * (y-y1)) + (z_[3] * (x-x1) * (y-y1)) ) / ( (x2-x1) * (y2-y1) ) ) ### Interactor Style # This class defines the user interface, and how the system reacts to different user inputs. class MyInteractorStyle(vtk.vtkInteractorStyleSwitch): def __init__(self,parent=None): # Index indicating which models are currently selected for endpoints self.ix = 0 # Togglable flag indicating if the center model is being dragged or not self.drag = 0; # Picker self.picker = vtk.vtkCellPicker() self.picker.SetTolerance(0.001) # Set up observers. These functions watch for specific user actions. self.SetCurrentStyleToTrackballActor() self.GetCurrentStyle().AddObserver("MiddleButtonReleaseEvent",self.middleButtonReleaseEvent) self.GetCurrentStyle().AddObserver("MouseMoveEvent",self.mouseMoveEvent) self.SetCurrentStyleToTrackballCamera() self.GetCurrentStyle().AddObserver("MiddleButtonPressEvent",self.middleButtonPressEvent) self.GetCurrentStyle().AddObserver("KeyPressEvent",self.keyPress) # def mouseMoveEvent(self,obj,event): # Re-render every time the user moves the mouse while clicking. # If you have a slow computer, consider changing this to be thresholded so as to only have a certain resolution # (i.e. to only change if the mouse moves a certain distance, rather than any move) if self.drag and self.picker.GetProp3D(): # Move object. This is a rewrite of the raw move object code; # Normally you would do this with the built-in version of this function and extend # it pythonically in the normal way, but the python bindings for VTK don't expose # this function in that way (it's all hard-coded in C++) so this is a simple # re-implementation that gives more control over how the object is moved. # Specifically, this constrains the draggable object to only move in-plane # and prevents it going out of bounds. center = self.picker.GetProp3D().GetCenter() display_center = [0,0,0] new_point = [0,0,0,0] old_point = [0,0,0,0] motion_vector = [0,0] event_pos = self.GetCurrentStyle().GetInteractor().GetEventPosition() last_event_pos = self.GetCurrentStyle().GetInteractor().GetLastEventPosition() self.ComputeWorldToDisplay(self.GetDefaultRenderer(), center[0], center[1], center[2], display_center) self.ComputeDisplayToWorld(self.GetDefaultRenderer(), event_pos[0], event_pos[1], display_center[2], new_point) self.ComputeDisplayToWorld(self.GetDefaultRenderer(), last_event_pos[0], last_event_pos[1], display_center[2], old_point) # Calculate the position change, making sure to confine the object to within the boundaries. # Consider finding a way to do this so that it depends on position of center instead of mouse new_point[1] = max(min(vps[1][1], new_point[1]), vps[0][1]+97.5) new_point[2] = max(min(vps[2][2], new_point[2]), vps[0][2]+97.5) old_point[1] = max(min(vps[1][1], self.picker.GetProp3D().GetCenter()[1]), vps[0][1]+97.5) old_point[2] = max(min(vps[2][2], self.picker.GetProp3D().GetCenter()[2]), vps[0][2]+97.5) # Increment the position self.picker.GetProp3D().AddPosition(0,new_point[1]-old_point[1],new_point[2]-old_point[2]) # Update Data if cfg['cc']: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)], interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eCC)), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() else: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)]), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() # Update the renderer's pointer to the data so that the GUI has the updated data. dataImporter.CopyImportVoidPointer(data_string, len(data_string)) # Update the window. self.GetCurrentStyle().GetInteractor().Render() return # If the middle button is used to select the center object, change to # dragging mode. Else, use it to pan the view. def middleButtonPressEvent(self,obj,event): clickPos = self.GetCurrentStyle().GetInteractor().GetEventPosition() self.picker.Pick(clickPos[0],clickPos[1],0,self.GetDefaultRenderer()) if self.picker.GetProp3D(): self.SetCurrentStyleToTrackballActor() self.drag=1 # Optional: Add the ability to modify interpolant endpoints. # else: # If we click an interpolant endpoint, change that endpoint somehow. # self.drag = 0 self.GetCurrentStyle().OnMiddleButtonDown() # self.GetCurrentStyle().HighlightProp3D(volume) return # When we release, change style from TrackballActor to TrackballCamera. def middleButtonReleaseEvent(self,obj,event): self.SetCurrentStyleToTrackballCamera() self.drag=0 self.GetCurrentStyle().OnMiddleButtonUp() return # If the user presses an arrow key, swap out interpolant endpoints and re-render. # If the user hits space, sample randomly in the latent space and re-render. # If class-conditional vectors are enabled and the user hits 1-9, render a random # class-conditional object. def keyPress(self,obj,event): key=self.GetCurrentStyle().GetInteractor().GetKeySym() if key == 'Right': # Increment index of which models we're using, re-render all endpoints self.ix+=1 for i in xrange(4): eZ[i] = tfuncs['Zfn'](ygnd[None,display_ix[self.ix+i]])[0] eS[i] = tfuncs['sigma_fn'](ygnd[None,display_ix[self.ix+i]])[0] eCC[i] = cc[None,display_ix[self.ix+i]] dm[i] = make_data_matrix(ygnd[None,display_ix[self.ix+i]],eIs[i]).tostring() edi[i].CopyImportVoidPointer(dm[i], len(dm[i])) if cfg['cc']: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)], interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eCC)), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() else: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)]), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) elif key == 'Left' and (self.ix > 0): self.ix-=1 for i in xrange(4): eZ[i] = tfuncs['Zfn'](ygnd[None,display_ix[self.ix+i]])[0] eS[i] = tfuncs['sigma_fn'](ygnd[None,display_ix[self.ix+i]])[0] eCC[i] = cc[None,display_ix[self.ix+i]] dm[i] = make_data_matrix(ygnd[None,display_ix[self.ix+i]],eIs[i]).tostring() edi[i].CopyImportVoidPointer(dm[i], len(dm[i])) if cfg['cc']: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)], interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eCC)), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() else: data_string = make_data_matrix(np.asarray(tfuncs['pred']([interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)]), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) elif key == 'space': # Random Z, with optional weighting. Z_rand = 0.5*np.random.randn(1,cfg['num_latents']).astype(np.float32) # Optionally, sample using the interpolated sigmas as well. # Z_rand = np.square(np.exp(interp2d(volume.GetCenter()[1],volume.GetCenter()[2],eS))*np.random.randn(1,cfg['num_latents']).astype(np.float32)) if cfg['cc']: # if class-conditional, take the class vector into account cc_rand = np.zeros((1,10),dtype=np.float32) cc_rand[0,np.random.randint(10)] = 1 data_string = make_data_matrix(np.asarray(tfuncs['pred'](interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eZ)+Z_rand,cc_rand), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() else: data_string = make_data_matrix(np.asarray(tfuncs['pred'](Z_rand), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) # Generate random Class-conditional Z elif 0<= int(float(key))<=9 and cfg['cc']: cc_rand = np.zeros((1,10),dtype=np.float32) cc_rand[0,int(float(key))] = 5 Z_rand = np.square(np.exp(interp2d(volume.GetCenter()[1],volume.GetCenter()[2],eS)))*np.random.randn(1,cfg['num_latents']).astype(np.float32)+interp2d(volume.GetCenter()[1],volume.GetCenter()[2],eZ) data_string = make_data_matrix(np.asarray(tfuncs['pred'](Z_rand,cc_rand), dtype=np.uint8), int(interp2d(volume.GetCenter()[1], volume.GetCenter()[2],eIs))).tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) # print(key) # Render and pass event on self.GetCurrentStyle().GetInteractor().Render() self.GetCurrentStyle().OnKeyPress() return # Initialize the render window renderer = vtk.vtkRenderer() renderWin = vtk.vtkRenderWindow() renderWin.AddRenderer(renderer) # Initialize the render interactor renderInteractor = vtk.vtkRenderWindowInteractor() style = MyInteractorStyle() style.SetDefaultRenderer(renderer) renderInteractor.SetInteractorStyle(style)#volume_set=volume,Lvolume_set = Lvolume, Rvolume_set = Rvolume)) renderInteractor.SetRenderWindow(renderWin) # Make boundary plane rgrid = vtk.vtkRectilinearGrid() rgrid.SetDimensions(1,2,2) xCoords = vtk.vtkFloatArray() xCoords.InsertNextValue(30) yCoords = vtk.vtkFloatArray() yCoords.InsertNextValue(vps[0][1]+97.5) yCoords.InsertNextValue(vps[1][1]) rgrid.SetXCoordinates(xCoords) rgrid.SetYCoordinates(yCoords) rgrid.SetZCoordinates(yCoords) plane = vtk.vtkRectilinearGridGeometryFilter() plane.SetInputData(rgrid) rgridMapper = vtk.vtkPolyDataMapper() rgridMapper.SetInputConnection(plane.GetOutputPort()) wireActor = vtk.vtkActor() wireActor.SetMapper(rgridMapper) wireActor.GetProperty().SetRepresentationToWireframe() wireActor.GetProperty().SetColor(0, 0, 0) wireActor.PickableOff() wireActor.DragableOff() # Add model, endpoints, and boundary plane to renderer renderer.AddActor(wireActor) for i in xrange(4): renderer.AddVolume(ev[i]) renderer.AddVolume(volume) # set background to white. Optionally change it to a fun color, like "Lifeblood of the Untenderized." renderer.SetBackground(1.0,1.0,1.0) # Set initial window size. You can drag the window to change size, but keep in mind that # the larger the window, the slower this thing runs. On my laptop, I get little spikes # of lag when I run this on full screen, though a more graphics-cardy setup should do fine. renderWin.SetSize(400, 400) # Exit function def exitCheck(obj, event): if obj.GetEventPending() != 0: obj.SetAbortRender(1) # Add exit function renderWin.AddObserver("AbortCheckEvent", exitCheck) # initialize interactor renderInteractor.Initialize() # Start application! renderer.ResetCamera() renderer.GetActiveCamera().Azimuth(30) renderer.GetActiveCamera().Elevation(20) renderer.GetActiveCamera().Dolly(2.8) renderer.ResetCameraClippingRange() renderWin.Render() renderInteractor.Start()
def main(args): # Load model lasagne.random.set_rng(np.random.RandomState(0)) config_module = imp.load_source('config', args.config_path) cfg = config_module.cfg weights_fname = str(args.config_path)[:-3] + '.npz' model = config_module.get_model() print('Compiling theano functions...') tfuncs, tvars, model = make_training_functions(cfg, model) metadata = checkpoints.load_weights(weights_fname, model['l_out']) best_acc = metadata['best_acc'] if 'best_acc' in metadata else 0 print('best acc = ' + str(best_acc)) print('Testing...') # Get Test Data xt = np.asarray(np.load('modelnet40_rot24_test.npz')['features'], dtype=np.float32) yt = np.asarray(np.load('modelnet40_rot24_test.npz')['targets'], dtype=np.float32) n_rotations = 24 confusion_matrix = np.zeros((40, 40), dtype=np.int) num_test_batches = int(math.ceil(float(len(xt)) / float(n_rotations))) test_chunk_size = n_rotations * cfg['batches_per_chunk'] num_test_chunks = int(math.ceil(float(len(xt)) / test_chunk_size)) test_class_error = [] pred_array = [] test_itr = 0 # Evaluate on test set for chunk_index in xrange(num_test_chunks): upper_range = min(len(yt), (chunk_index + 1) * test_chunk_size) # x_shared = np.asarray(xt[chunk_index * test_chunk_size:upper_range, :, :, :, :], dtype=np.float32) y_shared = np.asarray(yt[chunk_index * test_chunk_size:upper_range], dtype=np.float32) num_batches = int(math.ceil(float(len(x_shared)) / n_rotations)) tvars['X_shared'].set_value(6.0 * x_shared - 1.0, borrow=True) tvars['y_shared'].set_value(y_shared, borrow=True) lvs, accs = [], [] for bi in xrange(num_batches): test_itr += 1 print(test_itr) [batch_test_class_error, confusion, raw_pred] = tfuncs['test_function'](bi) # Get the test test_class_error.append(batch_test_class_error) pred_array.append(raw_pred) # print(confusion) # confusion_matrix+=confusion # confusion_matrix[confusion,int(yt[cfg['n_rotations']*test_itr])]+=1 # print(confusion_matrix) # Save outputs to csv files. np.savetxt(str(args.config_path)[:-3] + '.csv', np.asarray(pred_array), delimiter=",") t_class_error = 1 - float(np.mean(test_class_error)) print('Test error is: ' + str(t_class_error))