def main(): board = cn.LockGPU() print 'Using board', board if len(sys.argv) < 4: Usage() sys.exit(1) pbtxt_file = sys.argv[1] params_file = sys.argv[2] means_file = sys.argv[3] model = cn.ConvNet(pbtxt_file) model.Load(params_file) model.SetNormalizer(means_file, 224, 224) print model.GetLayerNames() # Random inputs. data = np.random.randn(128, 224 * 224 * 3) model.Fprop(data) output = model.GetState('output') print output # Load image. image_data = LoadImage('../examples/imagenet/test_images/0.jpg') model.Fprop(image_data) last_hidden_layer = model.GetState('hidden7') output = model.GetState('output') print output.argmax() cn.FreeGPU(board)
def init_model_lib(self): num_peers = WORLD.Get_size() # Adjust our learning rate based on the number of other workers - more workers -> larger # batch size -> higher learning rate. print 'Resetting model parameters for %d peers.' % num_peers rate_adjustment = num_peers / 32. for l in self.layers: pass #if 'epsW' in l: l['epsW'] = [rate_adjustment * num_peers for w in l['epsW']] #if 'epsB' in l: l['epsB'] = rate_adjustment * num_peers #if 'momW' in l: l['momW'] = [w / num_peers for w in l['momW']] #if 'momB' in l: l['momB'] = w / num_peers self.model = convnet.ConvNet(self.layers, self.minibatch_size, self.device_ids[0]) self.model.start()
def build_net(key, log_hash=None, global_step=None): fit = Fit() & key data = (Region() & key).load_data() cnn = convnet.ConvNet(data, log_dir='region%d' % key['region_num'], log_hash=log_hash, global_step=global_step) layers = (Net.ConvLayer() & key).fetch(order_by='layer_num') conv_smooth, conv_sparse, readout_sparse = ( Net.RegPath() & key).fetch1('conv_smooth_weight', 'conv_sparse_weight', 'readout_sparse_weight') cnn.build(filter_sizes=layers['filter_size'], out_channels=layers['out_channels'], strides=layers['stride'], paddings=layers['padding'], smooth_weights=layers['rel_smooth_weight'] * conv_smooth, sparse_weights=layers['rel_sparse_weight'] * conv_sparse, readout_sparse_weight=readout_sparse) return cnn
def __init__(self, net_type="wideresnet", depth=28, width=2, norm=None, dropout_rate=0.0, n_classes=10, in_channels=3): super(F, self).__init__() if net_type == "wideresnet": self.f = wideresnet.Wide_ResNet(depth, width, norm=norm, dropout_rate=dropout_rate, inpu_channels=in_channels) elif net_type == "convnet": self.f = convnet.ConvNet(depth=depth, widen_factor=width, norm=norm, input_channels=in_channels) self.energy_output = nn.Linear(self.f.last_dim, 1) self.class_output = nn.Linear(self.f.last_dim, n_classes)
'target': np.asarray([labels[image_categories[i]] for i in test_idx], dtype=np.int32) } # construct DataFeeder train_feeder = masalachai.DataFeeder(train_data, batchsize=16) test_feeder = masalachai.DataFeeder(test_data, batchsize=16) # hook preprocess train_feeder.hook_preprocess(preprocess) test_feeder.hook_preprocess(preprocess) ######################## # Model setup # ######################## network = convnet.ConvNet() chainer.serializers.load_npz('caltech_chain.npz', network) model = masalachai.models.ClassifierModel(network) if gpu_id >= 0: mdoel.to_gpu() # Optimizer setup optimizer = chainer.optimizers.Adam() optimizer.setup(model) chainer.serializers.load_npz('caltech_optimizer.npz', optimizer) ######################## # Trainer setup # ######################## train_nitr = 1000 trainer = masalachai.trainers.SupervisedTrainer(optimizer, logger,
# models directory modeldir = os.path.join(workingdir, 'models/example') my_model = os.path.join(modeldir, 'model.cpkt') # load data datadir = os.path.join(os.getcwd(), './data/mnist') data_provider = read_data_sets(datadir) x_test = data_provider.test.images x_test = np.pad(x_test, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant') # pad input y_test = np.argmax(data_provider.test.labels, 1) # dense labels # network definition net = convnet.ConvNet(channels=1, n_class=10, is_training=False, cost_name='baseline') # classification performance n_test = data_provider.test.images.shape[0] batch_size = 512 predictions = np.zeros_like(y_test) for count, kk in enumerate(range(0, n_test, batch_size)): if count == int(n_test / batch_size): start = kk end = x_test.shape[0] else: start = kk end = kk + batch_size n_samples = end - start
def train(): """ Performs training and evaluation of your model. First define your graph using vgg.py with your fully connected layer. Then define necessary operations such as trainer (train_step in this case), savers and summarizers. Finally, initialize your model within a tf.Session and do the training. --------------------------------- How often to evaluate your model: --------------------------------- - on training set every PRINT_FREQ iterations - on test set every EVAL_FREQ iterations --------------------------- How to evaluate your model: --------------------------- Evaluation on test set should be conducted over full batch, i.e. 10k images, while it is alright to do it over minibatch for train set. """ # Set the random seeds for reproducibility. DO NOT CHANGE. tf.set_random_seed(42) np.random.seed(42) ######################## # PUT YOUR CODE HERE # ######################## std = 0.001 reg_strength = 0.0001 x = tf.placeholder(tf.float32, [None, None, None, 3]) y = tf.placeholder(tf.float32, [None, 10]) model = convnet.ConvNet() pool5, assign_ops = vgg.load_pretrained_VGG16_pool5(x) #pool5 = tf.stop_gradient(pool5) with tf.variable_scope('fc') as var_scope: with tf.name_scope('flatten') as scope: flat = tf.reshape(pool5, [-1, pool5.get_shape()[3].value], name='flatten') with tf.name_scope('fc1') as scope: W = tf.get_variable( 'w1', initializer=tf.random_normal_initializer(stddev=std), shape=[flat.get_shape()[1].value, 384], regularizer=tf.contrib.layers.l2_regularizer(reg_strength)) b = tf.Variable(tf.zeros([384])) h = tf.nn.relu(tf.matmul(flat, W) + b, name='h1') with tf.name_scope('fc2') as scope: W = tf.get_variable( 'w2', initializer=tf.random_normal_initializer(stddev=std), shape=[384, 192], regularizer=tf.contrib.layers.l2_regularizer(reg_strength)) b = tf.Variable(tf.zeros([192])) h2 = tf.nn.relu(tf.matmul(h, W) + b, name='h2') with tf.name_scope('fc3') as scope: W = tf.get_variable( 'final_w', initializer=tf.random_normal_initializer(stddev=std), shape=[192, 10], regularizer=tf.contrib.layers.l2_regularizer(reg_strength)) b = tf.Variable(tf.zeros([10])) logits = tf.matmul(h2, W) + b loss = model.loss(logits, y) accuracy = model.accuracy(logits, y) step = train_step(loss) init = tf.initialize_all_variables() saver = tf.train.Saver() sess = tf.Session() sess.run(init) #for op in assign_ops: sess.run(assign_ops) merged = tf.merge_all_summaries() train_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/VGGtrain', sess.graph) test_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/VGGtest') cifar10 = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py') x_test, y_test = cifar10.test.images, cifar10.test.labels for i in range(FLAGS.max_steps): batch_xs, batch_ys = cifar10.train.next_batch(FLAGS.batch_size) summary, _ = sess.run([merged, step], feed_dict={ x: batch_xs, y: batch_ys }) if i % FLAGS.print_freq == 0: train_writer.add_summary(summary, i) if i % FLAGS.eval_freq == 0: summary, acc, l = sess.run([merged, accuracy, loss], feed_dict={ x: x_test[0:1000], y: y_test[0:1000] }) print('iteration: ' + str(i) + 'Accuracy: ' + str(acc) + 'Loss: ' + str(l)) test_writer.add_summary(summary, i) test_writer.close() train_writer.close() save_path = saver.save(sess, "checkpoints/vgg")
def classify_images(datasets, options={}): print ('-'*100) print ('Classifier: {}'.format(options['classifier'])) shortest_path_length = 1000000 if options['classifier'] == 'BASELINE': inliers_distribution = np.load('data/baseline-classifiers/inliers_distribution.npy') outliers_distribution = np.load('data/baseline-classifiers/outliers_distribution.npy') trained_classifier = [inliers_distribution, outliers_distribution] elif options['classifier'] == 'CONVNET': # load convnet checkpoint here print ('Loading ConvNet checkpoint: {}'.format(options['classifier_file'])) checkpoint = torch.load(options['classifier_file']) kwargs = {} options['model'] = 'resnet18' # options['convnet_lr'] = 0.01 options['convnet_use_images'] = False options['convnet_use_warped_images'] = False options['convnet_use_feature_match_map'] = False options['convnet_use_track_map'] = False options['convnet_use_non_rmatches_map'] = True options['convnet_use_rmatches_map'] = True options['convnet_use_matches_map'] = False options['convnet_use_photometric_error_maps'] = True options['convnet_use_rmatches_secondary_motion_map'] = False options['range_min'] = 0 options['range_max'] = 5000 options['loss'] = 'cross-entropy' options['features'] = 'RM' options['mlp-layer-size'] = 256 options['use_small_weights'] = False options['num_workers'] = 10 options['batch_size'] = 64 options['shuffle'] = False options['convnet_input_size'] = 224 options['triplet-sampling-strategy'] = 'normal' options['log_interval'] = 1 options['experiment'] = 'pe_unfiltered+use-BF-data' trained_classifier = convnet.ConvNet(options, **kwargs) trained_classifier.cuda() trained_classifier.load_state_dict(checkpoint['state_dict']) print ('-'*100) print ('-'*100) for i,t in enumerate(datasets): print ('#'*100) print ('\tRunning classifier for dataset: {}'.format(os.path.basename(t))) data = dataset.DataSet(t) results = {} dsets_te_ = [] fns_te_ = [] num_rmatches_te_ = [] # print data.all_feature_maps() # import sys; sys.exit(1) for im1 in sorted(data.all_feature_maps()): im1_all_matches, im1_valid_rmatches, im1_all_robust_matches = data.load_all_matches(im1) for im2 in im1_all_robust_matches: rmatches = im1_all_robust_matches[im2] # if im1 == 'DSC_1746.JPG' and im2 == 'DSC_1754.JPG': # import pdb; pdb.set_trace() if len(rmatches) == 0: if im1 not in results: results[im1] = {} if im2 not in results: results[im2] = {} results[im1][im2] = {'im1': im1, 'im2': im2, 'score': 0.0, 'num_rmatches': 0.0, 'shortest_path_length': 0} results[im2][im1] = {'im1': im2, 'im2': im1, 'score': 0.0, 'num_rmatches': 0.0, 'shortest_path_length': 0} continue fns_te_.append([im1,im2]) dsets_te_.append(t) num_rmatches_te_.append(len(im1_all_robust_matches[im2])) dsets_te = np.array(dsets_te_) fns_te = np.array(fns_te_) R11s_te = np.ones((len(dsets_te),)) R12s_te = np.ones((len(dsets_te),)) R13s_te = np.ones((len(dsets_te),)) R21s_te = np.ones((len(dsets_te),)) R22s_te = np.ones((len(dsets_te),)) R23s_te = np.ones((len(dsets_te),)) R31s_te = np.ones((len(dsets_te),)) R32s_te = np.ones((len(dsets_te),)) R33s_te = np.ones((len(dsets_te),)) num_rmatches_te = np.array(num_rmatches_te_) num_matches_te = np.ones((len(dsets_te),)) spatial_entropy_1_8x8_te = np.ones((len(dsets_te),)) spatial_entropy_2_8x8_te = np.ones((len(dsets_te),)) spatial_entropy_1_16x16_te = np.ones((len(dsets_te),)) spatial_entropy_2_16x16_te = np.ones((len(dsets_te),)) pe_histogram_te = np.ones((len(dsets_te),)) pe_polygon_area_percentage_te = np.ones((len(dsets_te),)) nbvs_im1_te = np.ones((len(dsets_te),)) nbvs_im2_te = np.ones((len(dsets_te),)) te_histogram_te = np.ones((len(dsets_te),)) ch_im1_te = np.ones((len(dsets_te),)) ch_im2_te = np.ones((len(dsets_te),)) vt_rank_percentage_im1_im2_te = np.ones((len(dsets_te),)) vt_rank_percentage_im2_im1_te = np.ones((len(dsets_te),)) sq_rank_scores_mean_te = np.ones((len(dsets_te),)) sq_rank_scores_min_te = np.ones((len(dsets_te),)) sq_rank_scores_max_te = np.ones((len(dsets_te),)) sq_distance_scores_te = np.ones((len(dsets_te),)) lcc_im1_15_te = np.ones((len(dsets_te),)) lcc_im2_15_te = np.ones((len(dsets_te),)) min_lcc_15_te = np.ones((len(dsets_te),)) max_lcc_15_te = np.ones((len(dsets_te),)) lcc_im1_20_te = np.ones((len(dsets_te),)) lcc_im2_20_te = np.ones((len(dsets_te),)) min_lcc_20_te = np.ones((len(dsets_te),)) max_lcc_20_te = np.ones((len(dsets_te),)) lcc_im1_25_te = np.ones((len(dsets_te),)) lcc_im2_25_te = np.ones((len(dsets_te),)) min_lcc_25_te = np.ones((len(dsets_te),)) max_lcc_25_te = np.ones((len(dsets_te),)) lcc_im1_30_te = np.ones((len(dsets_te),)) lcc_im2_30_te = np.ones((len(dsets_te),)) min_lcc_30_te = np.ones((len(dsets_te),)) max_lcc_30_te = np.ones((len(dsets_te),)) lcc_im1_35_te = np.ones((len(dsets_te),)) lcc_im2_35_te = np.ones((len(dsets_te),)) min_lcc_35_te = np.ones((len(dsets_te),)) max_lcc_35_te = np.ones((len(dsets_te),)) lcc_im1_40_te = np.ones((len(dsets_te),)) lcc_im2_40_te = np.ones((len(dsets_te),)) min_lcc_40_te = np.ones((len(dsets_te),)) max_lcc_40_te = np.ones((len(dsets_te),)) shortest_path_length_te = np.ones((len(dsets_te),)) #np.array(shortest_paths_te_) mds_rank_percentage_im1_im2_te = np.ones((len(dsets_te),)) mds_rank_percentage_im2_im1_te = np.ones((len(dsets_te),)) distance_rank_percentage_im1_im2_gt_te = np.ones((len(dsets_te),)) distance_rank_percentage_im2_im1_gt_te = np.ones((len(dsets_te),)) labels_te = np.ones((len(dsets_te),)) arg = [ \ dsets_te, fns_te, R11s_te, R12s_te, R13s_te, R21s_te, R22s_te, R23s_te, R31s_te, R32s_te, R33s_te, num_rmatches_te, num_matches_te, spatial_entropy_1_8x8_te, \ spatial_entropy_2_8x8_te, spatial_entropy_1_16x16_te, spatial_entropy_2_16x16_te, pe_histogram_te, pe_polygon_area_percentage_te, \ nbvs_im1_te, nbvs_im2_te, te_histogram_te, ch_im1_te, ch_im2_te, vt_rank_percentage_im1_im2_te, vt_rank_percentage_im2_im1_te, \ sq_rank_scores_mean_te, sq_rank_scores_min_te, sq_rank_scores_max_te, sq_distance_scores_te, \ lcc_im1_15_te, lcc_im2_15_te, min_lcc_15_te, max_lcc_15_te, \ lcc_im1_20_te, lcc_im2_20_te, min_lcc_20_te, max_lcc_20_te, \ lcc_im1_25_te, lcc_im2_25_te, min_lcc_25_te, max_lcc_25_te, \ lcc_im1_30_te, lcc_im2_30_te, min_lcc_30_te, max_lcc_30_te, \ lcc_im1_35_te, lcc_im2_35_te, min_lcc_35_te, max_lcc_35_te, \ lcc_im1_40_te, lcc_im2_40_te, min_lcc_40_te, max_lcc_40_te, \ shortest_path_length_te, \ mds_rank_percentage_im1_im2_te, mds_rank_percentage_im2_im1_te, \ distance_rank_percentage_im1_im2_gt_te, distance_rank_percentage_im2_im1_gt_te, \ labels_te, np.ones((len(labels_te))), \ False, trained_classifier, options ] if options['classifier'] == 'BASELINE': results_fns, results_rmatches, _, scores, spl, _ = baseline_histogram_classifier(arg) elif options['classifier'] == 'CONVNET': results_fns, results_rmatches, _, scores, spl, _ = convnet.classify_convnet_image_match_inference(arg) print ("\tFinished classifying data for {} using {}".format(t.split('/')[-1], options['classifier'])) for i,(im1,im2) in enumerate(results_fns): if im1 not in results: results[im1] = {} if im2 not in results: results[im2] = {} score = round(scores[i], 3) results[im1][im2] = {'im1': im1, 'im2': im2, 'score': score, 'num_rmatches': results_rmatches[i], 'shortest_path_length': spl[i]} results[im2][im1] = {'im1': im2, 'im2': im1, 'score': score, 'num_rmatches': results_rmatches[i], 'shortest_path_length': spl[i]} data.save_image_matching_results(results, robust_matches_threshold=options['robust_matches_threshold'], classifier=options['classifier'])
'target': np.asarray([labels[image_categories[i]] for i in test_idx], dtype=np.int32) } # construct DataFeeder train_feeder = masalachai.DataFeeder(train_data, batchsize=16) test_feeder = masalachai.DataFeeder(test_data, batchsize=16) # hook preprocess train_feeder.hook_preprocess(preprocess) test_feeder.hook_preprocess(preprocess) ######################## # Model setup # ######################## model = masalachai.models.ClassifierModel(convnet.ConvNet()) if gpu_id >= 0: mdoel.to_gpu() # Optimizer setup optimizer = chainer.optimizers.Adam() optimizer.setup(model) ######################## # Trainer setup # ######################## train_nitr = 1000 trainer = masalachai.trainers.SupervisedTrainer(optimizer, logger, (train_feeder, ), test_feeder, gpu_id) trainer.train(train_nitr, log_interval=1, test_interval=100, test_nitr=1)
import convnet as cn import numpy as np import sys pbtxt_file = "/home/fs/ylu/Code/convnet/examples/imagenet/CLS_net_20140801232522.pbtxt" params_file = "/home/fs/ylu/Code/convnet/examples/imagenet/CLS_net_20140801232522.h5" means_file = "/home/fs/ylu/Code/convnet/examples/imagenet/pixel_mean.h5" model = cn.ConvNet(pbtxt_file) # Load the model architecture. model.Load(params_file) # Set the weights and biases. model.SetNormalizer(means_file, 224) # Set the mean and std for input normalization. data = np.random.randn(128, 224* 224* 3) # 128 images of size 224x224 as a numpy array. model.Fprop(data) # Fprop through the model. # Returns the state of the requested layer as a numpy array. last_hidden_layer = model.GetState('hidden7') output = model.GetState('output') print output.shape, last_hidden_layer.shape # (128, 1000) (128, 4096).
def train(): """ Performs training and evaluation of ConvNet model. First define your graph using class ConvNet and its methods. Then define necessary operations such as trainer (train_step in this case), savers and summarizers. Finally, initialize your model within a tf.Session and do the training. --------------------------- How to evaluate your model: --------------------------- Evaluation on test set should be conducted over full batch, i.e. 10k images, while it is alright to do it over minibatch for train set. --------------------------------- How often to evaluate your model: --------------------------------- - on training set every print_freq iterations - on test set every eval_freq iterations ------------------------ Additional requirements: ------------------------ Also you are supposed to take snapshots of your model state (i.e. graph, weights and etc.) every checkpoint_freq iterations. For this, you should study TensorFlow's tf.train.Saver class. For more information, please checkout: [https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html] """ # Set the random seeds for reproducibility. DO NOT CHANGE. tf.set_random_seed(42) np.random.seed(42) ######################## # PUT YOUR CODE HERE # ######################## Convnn = convnet.ConvNet() Convnn.summary = SUMMARY_DEFAULT with tf.name_scope('x'): x = tf.placeholder("float", [None, 32, 32, 3], name="X_train") with tf.name_scope('y'): y = tf.placeholder("float", [None, Convnn.n_classes], name="Y_train") # initialize graph, accuracy and loss logits = Convnn.inference(x) loss = Convnn.loss(logits, y) accuracy = Convnn.accuracy(logits, y) optimizer = train_step(loss) init = tf.initialize_all_variables() if SUMMARY_DEFAULT: merge = tf.merge_all_summaries() if SAVER_DEFAULT: saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) cifar10 = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py') x_test, y_test = cifar10.test.images, cifar10.test.labels if SUMMARY_DEFAULT: train_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/train", sess.graph) test_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/test") for i in range(1, FLAGS.max_steps + 1): x_train, y_train = cifar10.train.next_batch(FLAGS.batch_size) _, l_train, acc_train = sess.run([optimizer, loss, accuracy], feed_dict={ x: x_train, y: y_train }) if SUMMARY_DEFAULT: _, l_train, acc_train, summary = sess.run( [optimizer, loss, accuracy, merge], feed_dict={ x: x_train, y: y_train }) train_writer.add_summary(summary, i) else: _, l_train, acc_train = sess.run([optimizer, loss, accuracy], feed_dict={ x: x_train, y: y_train }) if i % EVAL_FREQ_DEFAULT == 0 or i == 1: print( "Iteration {0:d}/{1:d}. Train Loss = {2:.3f}, Train Accuracy = {3:.3f}" .format(i, FLAGS.max_steps, l_train, acc_train)) if SUMMARY_DEFAULT: l_val, acc_val, summary = sess.run([loss, accuracy, merge], feed_dict={ x: x_test, y: y_test }) test_writer.add_summary(summary, i) else: l_val, acc_val = sess.run([loss, accuracy], feed_dict={ x: x_test, y: y_test }) print( "Iteration {0:d}/{1:d}. Validation Loss = {2:.3f}, Validation Accuracy = {3:.3f}" .format(i, FLAGS.max_steps, l_val, acc_val)) if SAVER_DEFAULT: saver.save(sess, FLAGS.checkpoint_dir + '/convnet.ckpt')
def feature_extraction(): """ This method restores a TensorFlow checkpoint file (.ckpt) and rebuilds inference model with restored parameters. From then on you can basically use that model in any way you want, for instance, feature extraction, finetuning or as a submodule of a larger architecture. However, this method should extract features from a specified layer and store them in data files such as '.h5', '.npy'/'.npz' depending on your preference. You will use those files later in the assignment. Args: [optional] Returns: None """ ######################## # PUT YOUR CODE HERE # ######################## # Set the random seeds for reproducibility. DO NOT CHANGE. tf.set_random_seed(42) np.random.seed(42) ######################## # PUT YOUR CODE HERE # ######################## print("Creating model") Convnn = convnet.ConvNet() Convnn.summary = SUMMARY_DEFAULT with tf.name_scope('x'): x = tf.placeholder("float", [None, 32, 32, 3], name="X_train") with tf.name_scope('y'): y = tf.placeholder("float", [None, Convnn.n_classes], name="Y_train") # initialize graph, accuracy and loss logits = Convnn.inference(x) loss = Convnn.loss(logits, y) accuracy = Convnn.accuracy(logits, y) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver = tf.train.Saver() print("loading previous session") saver.restore(sess, FLAGS.checkpoint_dir + "/convnet.ckpt") #saver.restore(sess, FLAGS.checkpoint_dir + "/my_model.cpkt") print("Evaluating model") cifar10 = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py') x_test, y_test = cifar10.test.images, cifar10.test.labels print(x_test.shape) print(y_test.shape) l, acc, flatten, fcl1, fcl2, logits = sess.run( [ loss, accuracy, Convnn.flatten, Convnn.fcl1, Convnn.fcl2, Convnn.logits ], feed_dict={ x: x_test[0:1000, :, :, :], y: y_test[0:1000] }) print("Calculating TSNE") tnse = TSNE(n_components=2, init='pca', random_state=0) pca = tnse.fit_transform(fcl2) prediction = np.argmax(logits, axis=1) fig = plt.figure() classes = [ 'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] #for i in range(Convnn.n_classes): # class_points = pca[prediction == i] # plot = plt.scatter(class_points[:,0], class_points[:,1], color=plt.cm.Set1(i*25), alpha=0.5) # plots.append(plot) plt.scatter(pca[:, 0], pca[:, 1], c=prediction, alpha=0.4) plt.legend(tuple(classes)) plt.savefig('images/tsne_plot.png')
strategy = 'baseline' # ['baseline', 'reorder', 'subsets', 'weights']' curriculum_type = 'prior_knowledge' # ['uncertainty', 'uncertainty'] modeldir = os.path.join('./models/', str(perc), strategy) my_model = os.path.join(modeldir, 'model' + '.cpkt') # load data datadir = os.path.join(os.getcwd(), './data/mnist') data_provider = read_data_sets(datadir) x_test = data_provider.test.images # set of images to evaluate x_test = np.pad(x_test, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant') # pad input y_test = np.argmax(data_provider.test.labels, 1) # dense labels # network definition net = convnet.ConvNet(channels=1, n_class=10, is_training=False) # classification performance n_test = data_provider.test.images.shape[0] batch_size = 512 predictions = np.zeros_like(y_test) for count, kk in enumerate(range(0, n_test, batch_size)): if count == int(n_test / batch_size): start = kk end = x_test.shape[0] else: start = kk end = kk + batch_size n_samples = end - start
def main(): best_prec = 0 start_epoch = 0 root_path = os.getcwd() model_path = os.path.join(root_path, 'best_model.pth') checkpoint_path = os.path.join(root_path, 'resume', 'checkpoint.pth.tar') global args args = parser.parse_args() if args.tensorboard: configure("runs") print('tensorboard is used, cmd: tensorboard --logdir runs') merge_spec = np.load('merge_spec.npy') merge_labels = np.load('merge_labels.npy') print('speech percentage:{}/{}'.format(merge_labels.sum(), merge_labels.shape[0])) train_percent = 0.95 train_num = int(merge_spec.shape[0] * train_percent) ext_frames = 40 train_dataset = VADDataset(merge_spec[0:train_num, :], merge_labels[0:train_num], ext_frames) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.jobs, pin_memory=True) eval_dataset = VADDataset(merge_spec[train_num:, :], merge_labels[train_num:], ext_frames) eval_loader = torch.utils.data.DataLoader(eval_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.jobs, pin_memory=True) model = convnet.ConvNet() print(model) model = torch.nn.DataParallel(model).cuda() criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=1e-4) cudnn.benchmark = True if args.evaluate: if os.path.isfile(model_path): model.load_state_dict(torch.load(model_path)) validate(eval_loader, model, criterion) else: print("No model found, EXIT") return if args.resume: if os.path.isfile(checkpoint_path): print("==> Loading checkpoint...") checkpoint = torch.load(checkpoint_path) start_epoch = checkpoint['epoch'] best_prec = checkpoint['best_prec'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print( "==> Load checkpoint: success! Epoch: {}".format(start_epoch)) else: print("==> no checkpoint found") for epoch in range(start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch) train(train_loader, model, criterion, optimizer, epoch) prec = validate(eval_loader, model, criterion) if (prec > best_prec): best_prec = prec torch.save(model.state_dict(), model_path) torch.save( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_prec': best_prec, 'optimizer': optimizer.state_dict() }, checkpoint_path)
def train(): """ Performs training and evaluation of ConvNet model. First define your graph using class ConvNet and its methods. Then define necessary operations such as trainer (train_step in this case), savers and summarizers. Finally, initialize your model within a tf.Session and do the training. --------------------------- How to evaluate your model: --------------------------- Evaluation on test set should be conducted over full batch, i.e. 10k images, while it is alright to do it over minibatch for train set. --------------------------------- How often to evaluate your model: --------------------------------- - on training set every print_freq iterations - on test set every eval_freq iterations ------------------------ Additional requirements: ------------------------ Also you are supposed to take snapshots of your model state (i.e. graph, weights and etc.) every checkpoint_freq iterations. For this, you should study TensorFlow's tf.train.Saver class. For more information, please checkout: [https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html] """ # Set the random seeds for reproducibility. DO NOT CHANGE. tf.set_random_seed(42) np.random.seed(42) ######################## # PUT YOUR CODE HERE # ######################## cifar10 = cifar10_utils.get_cifar10('cifar10/cifar-10-batches-py') x_test, y_test = cifar10.test.images, cifar10.test.labels model = convnet.ConvNet(n_classes=10) x = tf.placeholder(tf.float32, [None, 32, 32, 3]) y = tf.placeholder(tf.float32, [None, 10]) logits = model.inference(x) loss = model.loss(logits, y) accuracy = model.accuracy(logits, y) step = train_step(loss) init = tf.initialize_all_variables() saver = tf.train.Saver() sess = tf.Session() sess.run(init) merged = tf.merge_all_summaries() train_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/train', sess.graph) test_writer = tf.train.SummaryWriter(FLAGS.log_dir + '/test') for i in range(FLAGS.max_steps): batch_xs, batch_ys = cifar10.train.next_batch(FLAGS.batch_size) summary, _ = sess.run([merged, step], feed_dict={x: batch_xs, y: batch_ys}) if i % FLAGS.print_freq == 0: train_writer.add_summary(summary, i) if i % FLAGS.eval_freq == 0: summary, acc, l = sess.run([merged, accuracy, loss], feed_dict={x: x_test, y: y_test}) print('iteration: ' + str(i) + 'Accuracy: ' + str(acc) + 'Loss: ' + str(l)) test_writer.add_summary(summary, i) test_writer.close() train_writer.close() save_path = saver.save(sess, "checkpoints/convnet")
def main(): # if args.tensorboard: # if not args.name: # raise RuntimeError('Please provide a name for tensorboard to store') # configure("runs/{}".format(args.name)) # print('tensorboard is used, log to runs/{}'.format(args.name)) best_prec = 0 ext_frame_num = 24 train_percent = 0.9 # Data loading feats = np.load(args.feats) labels = np.load(args.labels) train_frame_num = int(train_percent * len(labels)) train_feats = feats[:,:,:train_frame_num] train_labels = labels[:train_frame_num] test_feats = feats[:,:,train_frame_num:] test_labels = labels[train_frame_num:] train_dataset = HADDataset(train_feats, train_labels, ext_frame_num) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=12, pin_memory=True, sampler=None) eval_dataset = HADDataset(test_feats, test_labels, ext_frame_num) eval_loader = torch.utils.data.DataLoader(eval_dataset, batch_size=args.batch_size, shuffle=False, num_workers=12, pin_memory=True) # create model input_dim = [feats.shape[0], feats.shape[1], ext_frame_num*2+1] model = convnet.ConvNet(input_dim) print(model) model = torch.nn.DataParallel(model).to(device) # define loss function (criterion) and optimizer criterion = nn.BCELoss() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) cudnn.benchmark = True # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_prec = checkpoint['best_prec'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) if args.evaluate: validate(eval_loader, model, criterion) return for epoch in range(args.start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch) # train for one epoch train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set prec1 = validate(eval_loader, model, criterion, epoch) # remember best eer and save checkpoint is_best = prec1 > best_prec best_prec = max(prec1, best_prec) save_checkpoint({ 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_prec': best_prec, 'optimizer' : optimizer.state_dict(), }, is_best)
transforms.ToPILImage(), transforms.Scale(size=[32, 32]), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) testset = imagefolder.ImageFolderWithPath(root='./images', transform=transform) testloader = torch.utils.data.DataLoader(testset) classes = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') gallery_path = './final_gallery/' weights_file = './trained_model_weights' # Model instance net = convnet.ConvNet() if os.path.isfile(weights_file): net.load_state_dict(torch.load(weights_file)) else: print('No model weight file found...') exit() # Get prediction, copy file according that for data in testloader: (image, label), (path, _) = data outputs = net(Variable(image)) _, predicted = torch.max(outputs.data, 1) c = (predicted == label).squeeze() prediction = classes[predicted[0]]
# load data perc = 30 # percentage of training data datadir = os.path.join(os.getcwd(), './data/mnist') # data directory data_provider = read_data_sets(datadir, percentage_train=perc / 100.0) n_train = data_provider.train.num_examples print('Number of training images {:d}'.format(n_train)) # more training parameters iters_per_epoch = np.ceil(1.0 * n_train / batch_size).astype(np.int32) decay_steps = decay_after_epoch * iters_per_epoch opt_kwargs = dict(learning_rate=lr, decay_steps=decay_steps, decay_rate=decay_rate) # definition of the network net = convnet.ConvNet(channels=1, n_class=10, is_training=True, cost_name=cost) # definition of the trainer trainer = convnet.Trainer(net, optimizer=name_opt, batch_size=batch_size, opt_kwargs=opt_kwargs) # start training path = trainer.train(data_provider, modeldir, training_iters=iters_per_epoch, epochs=nepochs, dropout=dropout) print('Optimization Finished!')