def load_data(self, test): self.vocab = helper.Vocab() self.tag_vocab = helper.Vocab() self.vocab.load_vocab_from_file(self.config.vocab_path, sep='\t') self.vocab.limit_vocab_length(self.config.vocab_size) self.tag_vocab.load_vocab_from_file(self.config.id2tag_path) self.tag_vocab.limit_vocab_length(1000) self.config.class_num = len(self.tag_vocab) if test == False: self.train_data = helper.loadData(self.config.train_data, self.vocab, self.tag_vocab) self.dev_data = helper.loadData(self.config.val_data, self.vocab, self.tag_vocab) step_p_epoch = len(self.train_data) // self.config.batch_size else: self.test_data = helper.loadData(self.config.test_data, self.vocab, self.tag_vocab) step_p_epoch = 0 self.step_p_epoch = step_p_epoch
def read_data(): # Output -> # source: Torch tensor on CPU [Nx3] # template: Torch tensor on CPU [Nx3] # rotation_ab: Torch tensor on CPU [Nx3] template = helper.loadData('train_data') template = template[0, 0:1024, :].reshape(1, -1, 3) poses = np.array( [[0, 0, 0, 30 * (np.pi / 180), 40 * (np.pi / 180), 0 * (np.pi / 180)]]) source = helper.apply_transformation(template, poses) return torch.from_numpy(source), torch.from_numpy(template)
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable(0) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:'+str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. bn_decay = get_bn_decay(batch) # Calculate BN decay. learning_rate = get_learning_rate(batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model(source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=bn_decay,PN=POINTNET,POOL=FLAGS.pn_pool,out_features=out_features) # Find the predicted transformation. predicted_transformation = MODEL.get_pose(source_global_feature,template_global_feature,is_training_pl, bn_decay=bn_decay,lim_rot=FLAGS.lim_rot) # Find the loss using source and transformed template point cloud. # loss = MODEL.get_loss(predicted_transformation, BATCH_SIZE, template_pointclouds_pl, source_pointclouds_pl) loss = 0 with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) saver.restore(sess, FLAGS.model_path) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = {'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'step': batch} templates = helper.loadData(FLAGS.data_dict) # pairs = helper.read_pairs(FLAGS.data_dict, FLAGS.pairs_file) eval_poses = helper.read_poses(FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_network(sess, ops, templates, eval_poses)
def train(): if not torch.cuda.is_available(): args.device = 'cpu' args.device = torch.device(args.device) action = Action() model = action.create_model() model.to(args.device) model.cuda() min_loss = float('inf') learnable_params = filter(lambda p: p.requires_grad, model.parameters()) if args.optimizer == 'adam': optimizer = torch.optim.Adam(learnable_params) else: optimizer = torch.optim.SGD(learnable_params, lr=0.1) templates = helper.loadData(args.data_dict) poses = helper.read_poses(args.data_dict, args.train_poses) eval_poses = helper.read_poses(args.data_dict, args.eval_poses) print(templates.shape, poses.shape, eval_poses.shape) for epoch in range(args.max_epoch): log_string("############## EPOCH: %0.4d ##############" % epoch) train_loss = action.train_one_epoch(model, poses, templates, optimizer) eval_loss = action.eval_one_epoch(model, eval_poses, templates) log_string("Training Loss: {} and Evaluation Loss: {}".format( train_loss, eval_loss)) is_best = eval_loss < min_loss snap = { 'epoch': epoch + 1, 'model': model.state_dict(), 'min_loss': min_loss, 'optimizer': optimizer.state_dict() } if is_best: save_checkpoint(snap, os.path.join(LOG_DIR, 'model'), 'snap_best') save_checkpoint(model.state_dict(), os.path.join(LOG_DIR, 'model'), 'model_best') log_string("Best Evaluation Loss: {}".format(eval_loss)) save_checkpoint(snap, os.path.join(LOG_DIR, 'model'), 'snap_last') save_checkpoint(model.state_dict(), os.path.join(LOG_DIR, 'model'), 'model_last')
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable(0) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:'+str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate(batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl, transformation_pl, gt_transformation_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_VOXEL) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model(source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=None) # Find the predicted transformation. predicted_transformation = MODEL.get_pose(source_global_feature,template_global_feature,is_training_pl, bn_decay=None) # Find the loss using source and transformed template point cloud. loss = MODEL.get_loss_v1(predicted_transformation, transformation_pl, gt_transformation_pl, BATCH_SIZE) # Add the loss in tensorboard. # Get training optimization algorithm. if OPTIMIZER == 'momentum': optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=MOMENTUM) elif OPTIMIZER == 'adam': optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.minimize(loss, global_step=batch) with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() tf.summary.scalar('loss', loss) tf.summary.scalar('learning_rate', learning_rate) # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Add summary writers merged = tf.summary.merge_all() if FLAGS.mode == 'train': # Create summary writers only for train mode. train_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'train'), sess.graph) eval_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'eval')) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) # Just to initialize weights with pretrained model. if FLAGS.use_pretrained_model: saver.restore(sess,os.path.join('log_512pts_1024feat_6itr_180deg_random_poses','model250.ckpt')) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = {'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'transformation_pl': transformation_pl, 'gt_transformation_pl': gt_transformation_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch} templates = helper.loadData(FLAGS.data_dict) poses = helper.read_poses(FLAGS.data_dict, TRAIN_POSES) # Read all the poses data for training. eval_poses = helper.read_poses(FLAGS.data_dict, EVAL_POSES) # Read all the poses data for evaluation. if FLAGS.mode == 'train': # For actual training. for epoch in range(MAX_EPOCH): log_string('**** EPOCH %03d ****' % (epoch)) sys.stdout.flush() # Train for all triaining poses. train_one_epoch(sess, ops, train_writer, templates, poses) save_path = saver.save(sess, os.path.join(LOG_DIR, FLAGS.results+".ckpt")) if epoch % 10 == 0: # Evaluate the trained network after 50 epochs. eval_one_epoch(sess, ops, eval_writer, templates, eval_poses) # Save the variables to disk. if epoch % 50 == 0: # Store the Trained weights in log directory. save_path = saver.save(sess, os.path.join(LOG_DIR, "models", "model"+str(epoch)+".ckpt")) log_string("Model saved in file: %s" % save_path)
def run(): NUM_POINT = FLAGS.num_point if not use_noise_data: templates = helper.loadData(FLAGS.data_dict) pairs = helper.read_pairs(FLAGS.data_dict, FLAGS.pairs_file) else: templates, sources = helper.read_noise_data(FLAGS.data_dict) # templates = helper.loadData(FLAGS.data_dict) eval_poses = helper.read_poses( FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_poses = eval_poses[0:1, :] num_batches = eval_poses.shape[0] TIME, ITR, Trans_Err, Rot_Err = [], [], [], [] idxs_5_5, idxs_10_1, idxs_20_2 = [], [], [] counter = 0 for fn, gt_pose in enumerate(eval_poses): if fn > 0: break if not use_noise_data: # template_idx = pairs[fn,1] template_idx = 0 M_given = templates[template_idx, :, :] S_given = helper.apply_transformation(M_given.reshape((1, -1, 3)), gt_pose.reshape((1, 6)))[0] else: M_given = templates[fn, :, :] S_given = sources[fn, :, :] # M_given = np.loadtxt('template_car_itr.txt') # S_given = np.loadtxt('source_car_itr.txt') # helper.display_clouds_data(M_given) # helper.display_clouds_data(S_given) # To generate point cloud for Xueqian: For CAD model figures # gt_pose = np.array([[0.5,0.2,0.4,40*(np.pi/180),20*(np.pi/180),30*(np.pi/180)]]) # templates = helper.loadData('unseen_data') # gt_pose = np.array([[-0.3,-0.7,0.4,-34*(np.pi/180),31*(np.pi/180),-27*(np.pi/180)]]) # gt_pose = np.array([[0.5929,-0.0643,-0.961,0.4638,-0.3767,-0.6253]]) # M_given = templates[48,:,:] # S_given = helper.apply_transformation(M_given.reshape(1,-1,3),gt_pose) # S_given = helper.add_noise(S_given) # S_given = S_given[0] M_given = M_given[0:NUM_POINT, :] # template data S_given = S_given[0:NUM_POINT, :] # source data tree_M = KDTree(M_given) tree_M_sampled = KDTree(M_given[0:100, :]) final_pose, model_data, sensor_data, predicted_data, _, time_elapsed, itr = icp.icp_test( S_given[0:100, :], M_given, tree_M, M_given[0:100, :], tree_M_sampled, S_given, gt_pose.reshape((1, 6)), 100, FLAGS.threshold) translation_error, rotational_error = find_errors( gt_pose[0], final_pose[0]) print(translation_error, rotational_error) TIME.append(time_elapsed) ITR.append(itr) Trans_Err.append(translation_error) Rot_Err.append(rotational_error) if rotational_error < 20 and translation_error < 0.2: if rotational_error < 10 and translation_error < 0.1: if rotational_error < 5 and translation_error < 0.05: idxs_5_5.append(fn) idxs_10_1.append(fn) idxs_20_2.append(fn) print('Batch: {}, Iterations: {}, Time: {}'.format( counter, itr, time_elapsed)) # counter += 1 # helper.display_three_clouds(M_given, S_given, predicted_data, "") # np.savetxt('template_piano.txt',M_given) # np.savetxt('source_piano.txt',S_given) # np.savetxt('predicted_piano.txt',predicted_data) log = { 'TIME': TIME, 'ITR': ITR, 'Trans_Err': Trans_Err, 'Rot_Err': Rot_Err, 'idxs_5_5': idxs_5_5, 'idxs_10_1': idxs_10_1, 'idxs_20_2': idxs_20_2, 'num_batches': num_batches } helper.log_test_results(FLAGS.log_dir, FLAGS.filename, log)
added = 0 #Wait for the other processes to dump a pickle file while not isfile(trainNP+"XtrainImages.h5"): print "sleeping because Train folder empty \r", time.sleep(1.) print "" #Load the training data print "Loading np training arrays" loadedUp = False while not loadedUp: try: trainImages = helper.loadData(trainNP+"XtrainImages") trainVecs = helper.loadData(trainNP+"XtrainVecs") trainTargets = helper.loadData(trainNP+"ytrain") loadedUp = True except Exception as e: err = e print err, " \r", time.sleep(2) print "" subprocess.call("rm "+trainNP+"XtrainImages.h5",shell=True) subprocess.call("rm "+trainNP+"XtrainVecs.h5",shell=True) subprocess.call("rm "+trainNP+"ytrain.h5",shell=True) #train the model on it
import open3d as o3d import numpy as np import torch import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import helper templates = helper.loadData('train_data') # Display given Point Cloud Data in blue color (default). def display_clouds_data(data, color='C0'): # Arguments: # data: array of point clouds (num_points x 3) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') try: data = data.tolist() except: pass X, Y, Z = [], [], [] for row in data: X.append(row[0]) Y.append(row[1]) Z.append(row[2]) ax.scatter(X, Y, Z, color=color) plt.show() # PyTorch based Code
def train(): graph = tf.Graph() with graph.as_default(): with tf.device('/cpu:0'): batch = tf.Variable( 0 ) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:' + str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate( batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl = MODEL.placeholder_inputs( BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model( source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=None, PN=POINTNET, POOL=FLAGS.pn_pool, out_features=out_features) # Find the predicted transformation. predicted_transformation = MODEL.get_pose( source_global_feature, template_global_feature, is_training_pl, bn_decay=None, lim_rot=FLAGS.lim_rot) # Find the loss using source and transformed template point cloud. # loss = MODEL.get_loss(predicted_transformation, BATCH_SIZE, template_pointclouds_pl, source_pointclouds_pl) # Add the loss in tensorboard. # loss - ours: batch_size = BATCH_SIZE predicted_position = tf.slice(predicted_transformation, [0, 0], [batch_size, 3]) predicted_quat = tf.slice(predicted_transformation, [0, 3], [batch_size, 4]) norm_predicted_quat = tf.reduce_sum(tf.square(predicted_quat), 1) norm_predicted_quat = tf.sqrt(norm_predicted_quat) norm_predicted_quat = tf.reshape(norm_predicted_quat, (batch_size, 1)) const = tf.constant(0.0000001, shape=(batch_size, 1), dtype=tf.float32) norm_predicted_quat = tf.add(norm_predicted_quat, const) predicted_norm_quat = tf.divide(predicted_quat, norm_predicted_quat) transformed_predicted_point_cloud = helper.transformation_quat_tensor( source_pointclouds_pl, predicted_norm_quat, predicted_position) is_training_pl_1 = tf.placeholder(tf.bool, shape=()) # flag for batch norm # load our model as loss function: saver31 = tf.train.import_meta_graph( os.path.join(LOG_DIR, 'model.ckpt.meta'), import_scope='g1', input_map={ 'input1': transformed_predicted_point_cloud, 'input2': template_pointclouds_pl, 'Placeholder': is_training_pl_1 }, clear_devices=True) labels12 = graph.get_tensor_by_name('g1/labels12:0') if ADD_NOISE_MODEL: add_noise = graph.get_tensor_by_name('g1/add_noise:0') # set optimizer: if OPTIMIZER == 'momentum': optimizer3 = tf.train.MomentumOptimizer(learning_rate, momentum=MOMENTUM) elif OPTIMIZER == 'adam': optimizer3 = tf.train.AdamOptimizer(learning_rate, name='Adam2') # optimizer only on g2 variables. # get loss: # pred_AB3 = (graph.get_tensor_by_name('g1/pc_compare/output1:0') + 1) / 2.0 # pred_BA3 = (graph.get_tensor_by_name('g1/pc_compare/output2:0') + 1) / 2.0 pred_AB3 = (graph.get_tensor_by_name('g1/pc_compare/output1:0')) pred_BA3 = (graph.get_tensor_by_name('g1/pc_compare/output2:0')) loss = (tf.reduce_mean(pred_AB3[:, :, :, 0]) + tf.reduce_mean(pred_BA3[:, :, :, 0])) / 2.0 loss_c = chmafer_dist(transformed_predicted_point_cloud, template_pointclouds_pl) train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Network') grads = optimizer3.compute_gradients(loss, train_vars) train_op = optimizer3.apply_gradients(grads, global_step=batch) # train_op = optimizer.minimize(loss, global_step=batch) with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() sum_lr = tf.summary.scalar('learning_rate', learning_rate) sum_loss = tf.summary.scalar('loss', loss) chamfer_loss = tf.summary.scalar('loss_chamfer', loss_c) # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Add summary writers # merged = tf.summary.merge_all() merged = tf.summary.merge([sum_loss, sum_lr, chamfer_loss]) if FLAGS.mode == 'train': # Create summary writers only for train mode. train_writer = tf.summary.FileWriter( os.path.join(LOG_DIR, 'train'), sess.graph) eval_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'eval')) # Init variables init = tf.global_variables_initializer() # sess.run(init, {is_training_pl: True}) MODEL_PATH1 = os.path.join(LOG_DIR, 'model.ckpt') saver31.restore(sess, MODEL_PATH1) initialize_uninitialized_vars(sess) print('models restored') # Just to initialize weights with pretrained model. if FLAGS.use_pretrained_model: saver.restore( sess, os.path.join('log_512pts_1024feat_6itr_180deg_random_poses', 'model250.ckpt')) if ADD_NOISE_MODEL: # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = { 'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch, 'labels12': labels12, 'is_training_pl_1': is_training_pl_1, 'add_noise': add_noise } else: # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = { 'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch, 'labels12': labels12, 'is_training_pl_1': is_training_pl_1 } templates = helper.loadData(FLAGS.data_dict) poses = helper.read_poses( FLAGS.data_dict, TRAIN_POSES) # Read all the poses data for training. eval_poses = helper.read_poses( FLAGS.data_dict, EVAL_POSES) # Read all the poses data for evaluation. if FLAGS.mode == 'train': print_('Training Started!', color='r', style='bold') # For actual training. for epoch in range(MAX_EPOCH): log_string('**** EPOCH %03d ****' % (epoch)) sys.stdout.flush() # Train for all triaining poses. train_one_epoch(sess, ops, train_writer, templates, poses) save_path = saver.save( sess, os.path.join(LOG_DIR, FLAGS.results + ".ckpt")) if epoch % 10 == 0: # Evaluate the trained network after 50 epochs. eval_one_epoch(sess, ops, eval_writer, templates, eval_poses) # Save the variables to disk. if epoch % 50 == 0: # Store the Trained weights in log directory. save_path = saver.save( sess, os.path.join(LOG_DIR, "models", "model" + str(epoch) + ".ckpt")) log_string("Model saved in file: %s" % save_path) print_('Training Successful!!', color='r', style='bold')
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable( 0 ) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:' + str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate( batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network_L') as _: # Object of network class. network_L = MODEL.Network() # Get the placeholders. source_pointclouds_pl_L, template_pointclouds_pl_L = network_L.placeholder_inputs( BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature_L, template_global_feature_L = network_L.get_model( source_pointclouds_pl_L, template_pointclouds_pl_L, FLAGS.feature_size, is_training_pl, bn_decay=None) # Find the predicted transformation. predicted_transformation_L = network_L.get_pose( source_global_feature_L, template_global_feature_L, is_training_pl, bn_decay=None) loss = 0 with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) # Just to initialize weights with pretrained model. saver.restore(sess, FLAGS.model_path) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network_L. ops_L = { 'source_pointclouds_pl': source_pointclouds_pl_L, 'template_pointclouds_pl': template_pointclouds_pl_L, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation_L, 'loss': loss, 'step': batch } #templates = helper.process_templates(FLAGS.data_dict) # Read all the templates. templates = helper.loadData(FLAGS.data_dict) eval_poses = helper.read_poses( FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_poses = eval_poses[0:10, :] eval_network(sess, ops_L, templates, eval_poses)
from helper import loadData, splitData from GaussianNaiveBayes import GaussianNaiveBayes splitRatio = 0.67 dataset = loadData('dataset/pima-indians-diabetes.csv') trainset, testset = splitData(dataset, splitRatio) print('Split {0} rows into train={1} and test={2} rows').format( len(dataset), len(trainset), len(testset)) # Driver clf = GaussianNaiveBayes() clf.fit(trainset) clf.predict(testset) print "Accuracy: {0}".format(clf.score(testset))
parser.add_argument('--epochs', dest="epochs", action="store", default=5) # Use GPU for training: python train.py data_dir --gpu parser.add_argument('--gpu', dest="gpu", action="store", default="gpu") parser = parser.parse_args() data_dir = parser.data_directory save_dir = parser.save_dir arch = parser.arch learning_rate = parser.learning_rate hidden_units = parser.hidden_units epochs = parser.epochs gpu = parser.gpu # 1) Load Data image_datasets, trainloader, testloader, validloader = helper.loadData( data_dir) # 2) Build Model model = helper.build_model(arch, hidden_units) # 3) Train Model model, optimizer, criterion = helper.train_model(model, trainloader, validloader, learning_rate, epochs, gpu) # 4) Save the checkpoint model.to('cpu') model.class_to_idx = image_datasets['train_data'].class_to_idx checkpoint = { 'model': model, 'hidden_units': hidden_units, 'optimizer_state_dict': optimizer.state_dict, 'criterion': criterion, 'epochs': epochs,
################################################################################## # EMD in PyTorch and Tensorflow # This code is written by Vinit Sarode. ################################################################################## import torch import tensorflow as tf import helper from helper import print_ import time from emd import EMDLoss from emd_tf import tf_util_loss ######################################### Data Loading ######################################### templates = helper.loadData('test_data') start_idx = 0 end_idx = 32 num_point = 1024 batch_size = end_idx - start_idx ######################################### Pytorch Testing ######################################### p1 = torch.from_numpy(templates[start_idx:end_idx, 0:num_point, :]).cuda() p2 = torch.from_numpy(templates[start_idx:end_idx, 0:num_point, :]).cuda() dist = EMDLoss() p1.requires_grad = True p2.requires_grad = True print_('PyTorch Results: ', color='r', style='bold')