def train_model(n_epoch = 1000, batch_size = 50, n_classes = 1011, dataset_path = '/raid/mainak/datasets/nabirds/', image_path = 'images'): #train_set_x, train_set_y_ = get_dataset(dataset_path) #train_set_y_ = train_set_y_.astype(int) #n_train_batches = train_set_x.shape[0] #train_set_y = np.zeros((n_train_batches, n_classes)) #train_set_y[np.arange(n_train_batches), train_set_y_-1] = 1 #print "train_set_y shape: ", train_set_y.shape n_train_batches, n_test_batches = get_data_length(dataset_path) n_train_batches = np.ceil(n_train_batches/batch_size) n_train_batches = n_train_batches.astype(int) x = tf.placeholder(tf.float32, shape=(None,256,256,3)) y_ = tf.placeholder(tf.float32, shape=(None,n_classes)) keep_prob = tf.placeholder("float") cost, y_pred = context_net(x=x, y_=y_, keep_prob=keep_prob, n_classes=n_classes) #cross_entropy = -tf.reduce_sum(y_*tf.log(y_pred)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cost) correct_prediction = tf.equal(tf.argmax(y_pred,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) # Launch the graph in a session. sess = tf.Session() sess.run(tf.initialize_all_variables()) with sess.as_default(): for epoch in range(n_epoch): for minibatch_index in xrange(n_train_batches): batch_x, batch_y = load_image_batch(dataset_path = dataset_path, image_path = image_path, start_idx=(minibatch_index*batch_size), batch_size = batch_size) #batch = [train_set_x[minibatch_index*batch_size: (minibatch_index+1)*batch_size], #train_set_y[minibatch_index*batch_size: (minibatch_index+1)*batch_size]] batch = [batch_x, batch_y] if minibatch_index%100 == 0: train_accuracy, train_cost = sess.run(fetches=[accuracy, cost], feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}) print("step %d, minibatch_index %d, train cost %g, training accuracy %g"%(epoch, minibatch_index, train_cost, train_accuracy)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
def train_net(dataset_path = '/raid/mainak/datasets/nabirds/', image_path = 'images',n_classes=555): global lstm_state_size global baseline global lambda_reward global n_sample global stddev global rnn_dim global saver print_config() lstm_state_size = rnn_dim*2 #im_mean = find_image_mean(image_path) #np.save('im_mean_256.npy', im_mean) #im_mean = np.load('im_mean_256.npy') if context_net_type == "alex": im_mean = np.load('im_mean.npy') elif context_net_type == "3-layer": im_mean = np.load('im_mean_256.npy') n_train_data, n_test_data = get_data_length(dataset_path) n_train_batches = np.ceil(n_train_data/batch_size) n_train_batches = n_train_batches.astype(int) x = tf.placeholder(tf.float32, shape=(None,image_size,image_size,3)) y_ = tf.placeholder(tf.float32, shape=(None,n_classes)) keep_prob = tf.placeholder("float") is_train = tf.placeholder(tf.int32) batch_cnt = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(lr, global_step=batch_cnt, decay_steps=5000, decay_rate=0.7, staircase=True) optimizer = tf.train.RMSPropOptimizer(learning_rate, decay = 0.98) saver = tf.train.Saver() sess = tf.Session() class_cost, locs_cost, reward, accuracy, loc_mean, train_ops, preds = build_net(x, y_, keep_prob, optimizer, learning_rate, batch_cnt, sess) # print len(train_ops) # print train_ops[-1] # Commented Selectively Initializing Variables Part # init_vars = [iv for iv in tf.all_variables() if "context_net" not in iv.name] # sess.run(tf.initialize_variables(init_vars)) # uninitialized_vars = [] # for var in tf.all_variables(): # try: # sess.run(var) # except tf.errors.FailedPreconditionError: # uninitialized_vars.append(var) # print "\n\n\n\n\n\n\n\n\n\n\n" # for var in uninitialized_vars: # print "Uninitialized Variables: ", var.name # print "\n\n\n\n\n\n\n\n\n\n\n" # saver.restore(sess, "models/model_130.ckpt") sess.run(tf.initialize_all_variables()) #tf.assert_variables_initialized(tf.trainable_variables()).eval(session=sess) #with sess.as_default(): with sess.as_default(), tf.device('/gpu:1'): for epoch in range(n_epoch): batch_cost = [] batch_accuracy = [] for minibatch_index in xrange(n_train_batches): #print "LR: ", learning_rate.eval() batch_x, batch_y_ = load_image_batch(im_mean = im_mean, dataset_path = dataset_path, image_path = image_path, start_idx=(minibatch_index*batch_size), batch_size = batch_size, is_train=is_train, im_size=image_size) #print "Batch: ", type(int(batch_y_[0])) batch_y = np.zeros((batch_size, n_classes)) batch_y[np.arange(batch_size), np.int32(batch_y_)-1] = 1 batch = [batch_x, batch_y] feed_dict={x:batch[0], y_:batch[1],is_train:0, keep_prob:0.8} class_cost_v, locs_cost_v, reward_v, accuracy_v, loc_mean_v, train_ops_v, preds_v = sess.run(fetches=[class_cost, locs_cost, reward, accuracy, loc_mean, train_ops[-1], preds], feed_dict=feed_dict) print ("Epoch: %d, minibatch_idx: %d, class cost: %g, locs_cost: %g, accuracy: %g"%(epoch, minibatch_index, class_cost_v, locs_cost_v, accuracy_v)) print "Reward: ", np.array_str(reward_v) print "Locations: ", np.array_str(loc_mean_v[:,1,:]) #print "Predicted Classes: ", preds_v batch_cost.append(class_cost_v) batch_accuracy.append(accuracy_v) print("Epoch: %d, Train Bach Cost: %g, Train Batch Accuracy: %g"%(epoch, np.mean(batch_cost), np.mean(batch_accuracy)))