From 2e53b8fbc7b833a48e645fed38d5efb8b3602a17 Mon Sep 17 00:00:00 2001 From: tgupta6 <tgupta6@illinois.edu> Date: Mon, 7 Mar 2016 22:50:24 -0600 Subject: [PATCH] Second model of obj classifier and initial code for attribute classifier --- color_classifiers/__init__.py | 0 color_classifiers/atr_data_io_helper.py | 90 +++++++++ color_classifiers/eval_atr_classifier.py | 69 +++++++ color_classifiers/train_atr_classifier.py | 197 ++++++++++++++++++++ object_classifiers/eval_obj_classifier.py | 51 ++++- object_classifiers/obj_data_io_helper.py | 45 +++-- object_classifiers/obj_data_io_helper.pyc | Bin 3016 -> 4545 bytes object_classifiers/train_obj_classifier.py | 68 +++++-- object_classifiers/train_obj_classifier.pyc | Bin 6532 -> 7697 bytes 9 files changed, 482 insertions(+), 38 deletions(-) create mode 100644 color_classifiers/__init__.py create mode 100644 color_classifiers/atr_data_io_helper.py create mode 100644 color_classifiers/eval_atr_classifier.py create mode 100644 color_classifiers/train_atr_classifier.py diff --git a/color_classifiers/__init__.py b/color_classifiers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/color_classifiers/atr_data_io_helper.py b/color_classifiers/atr_data_io_helper.py new file mode 100644 index 0000000..67337b8 --- /dev/null +++ b/color_classifiers/atr_data_io_helper.py @@ -0,0 +1,90 @@ +import json +import sys +import os +import matplotlib.pyplot as plt +import matplotlib.image as mpimg +import numpy as np +import tensorflow as tf +from scipy import misc + +def atr_mini_batch_loader(json_filename, image_dir, mean_image, start_index, batch_size, img_height=100, img_width=100, channels=3): + + with open(json_filename, 'r') as json_file: + json_data = json.load(json_file) + + atr_images = np.empty(shape=[9*batch_size, img_height/3, img_width/3, channels]) + atr_labels = np.zeros(shape=[9*batch_size, 4]) + + for i in range(start_index, start_index + batch_size): + image_name = os.path.join(image_dir, str(i) + '.jpg') + image = misc.imresize(mpimg.imread(image_name),(img_height, img_width), interp='nearest') +# image.resize((img_height, img_width, 3)) + crop_shape = np.array([image.shape[0], image.shape[1]])/3 + selected_anno = [q for q in json_data if q['image_id']==i] + grid_config = selected_anno[0]['config'] + + counter = 0; + for grid_row in range(0,3): + for grid_col in range(0,3): + start_row = grid_row*crop_shape[0] + start_col = grid_col*crop_shape[1] +# print([start_row, start_col]) + cropped_image = image[start_row:start_row+crop_shape[0], start_col:start_col+crop_shape[1], :] + if np.ndim(mean_image)==0: + atr_images[9*(i-start_index)+counter,:,:,:] = cropped_image/254. + else: + atr_images[9*(i-start_index)+counter,:,:,:] = (cropped_image-mean_image)/254 + atr_labels[9*(i-start_index)+counter, grid_config[6*grid_row+2*grid_col+1]] = 1 + counter = counter + 1 + + # imgplot = plt.imshow(obj_images[0,:,:,:].astype(np.uint8)) + # plt.show() + return (atr_images, atr_labels) + +def mean_image_batch(json_filename, image_dir, start_index, batch_size, img_height=100, img_width=100, channels=3): + batch = obj_mini_batch_loader(json_filename, image_dir, np.empty([]), start_index, batch_size, img_height, img_width, channels) + mean_image = np.mean(batch[0], 0) + return mean_image + +def mean_image(json_filename, image_dir, num_images, batch_size, img_height=100, img_width=100, channels=3): + max_iter = np.floor(num_images/batch_size) + mean_image = np.zeros([img_height/3, img_width/3, channels]) + for i in range(max_iter.astype(np.int16)): + mean_image = mean_image + mean_image_batch(json_filename, image_dir, 1+i*batch_size, batch_size, img_height, img_width, channels) + + mean_image = mean_image/max_iter + tmp_mean_image = mean_image*254 + # imgplot = plt.imshow(tmp_mean_image.astype(np.uint8)) + # plt.show() + return mean_image + + +class html_atr_table_writer(): + def __init__(self, filename): + self.filename = filename + self.html_file = open(self.filename, 'w') + self.html_file.write("""<!DOCTYPE html>\n<html>\n<body>\n<table border="1" style="width:100%"> \n""") + + def add_element(self, col_dict): + self.html_file.write(' <tr>\n') + for key in range(len(col_dict)): + self.html_file.write(""" <td>{}</td>\n""".format(col_dict[key])) + self.html_file.write(' </tr>\n') + + def image_tag(self, image_path, height, width): + return """<img src="{}" alt="IMAGE NOT FOUND!" height={} width={}>""".format(image_path,height,width) + + def close_file(self): + self.html_file.write('</table>\n</body>\n</html>') + self.html_file.close() + + + + +if __name__=="__main__": + + html_writer = html_atr_table_writer('/home/tanmay/Code/GenVQA/Exp_Results/Atr_Classifier_v_1/trial.html') + col_dict={0: 'sam', 1: html_writer.image_tag('something.png',25,25)} + html_writer.add_element(col_dict) + html_writer.close_file() + diff --git a/color_classifiers/eval_atr_classifier.py b/color_classifiers/eval_atr_classifier.py new file mode 100644 index 0000000..2f41202 --- /dev/null +++ b/color_classifiers/eval_atr_classifier.py @@ -0,0 +1,69 @@ +import sys +import os +import matplotlib.pyplot as plt +import matplotlib.image as mpimg +import numpy as np +from scipy import misc +import tensorflow as tf +import obj_data_io_helper as shape_data_loader +from train_obj_classifier import placeholder_inputs, comp_graph_v_1, evaluation + +sess=tf.InteractiveSession() + +x, y, keep_prob = placeholder_inputs() +y_pred = comp_graph_v_1(x, y, keep_prob) + +accuracy = evaluation(y, y_pred) + +saver = tf.train.Saver() + +saver.restore(sess, '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_1/obj_classifier_9.ckpt') + +mean_image = np.load('/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_1/mean_image.npy') + +# Test Data +test_json_filename = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/test_anno.json' +image_dir = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images' + +# Base dir for html visualizer +html_dir = '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_1/html' +if not os.path.exists(html_dir): + os.mkdir(html_dir) + +# HTML file writer +html_writer = shape_data_loader.html_obj_table_writer(os.path.join(html_dir,'index.html')) +col_dict={ + 0: 'Grount Truth', + 1: 'Prediction', + 2: 'Image'} +html_writer.add_element(col_dict) + +shape_dict = { + 0: 'blank', + 1: 'rectangle', + 2: 'triangle', + 3: 'circle'} + +batch_size = 100 +correct = 0 +for i in range(50): + test_batch = shape_data_loader.obj_mini_batch_loader(test_json_filename, image_dir, mean_image, 10000+i*batch_size, batch_size, 75, 75) + feed_dict_test={x: test_batch[0], y: test_batch[1], keep_prob: 1.0} + result = sess.run([accuracy, y_pred], feed_dict=feed_dict_test) + correct = correct + result[0]*batch_size + print(correct) + + for row in range(batch_size*9): + gt_id = np.argmax(test_batch[1][row,:]) + pred_id = np.argmax(result[1][row, :]) + if not gt_id==pred_id: + img_filename = os.path.join(html_dir,'{}_{}.png'.format(i,row)) + misc.imsave(img_filename, test_batch[0][row,:,:,:]) + col_dict = { + 0: shape_dict[gt_id], + 1: shape_dict[pred_id], + 2: html_writer.image_tag('{}_{}.png'.format(i,row), 25, 25)} + html_writer.add_element(col_dict) + +html_writer.close_file() +print('Test Accuracy: {}'.format(correct/5000)) diff --git a/color_classifiers/train_atr_classifier.py b/color_classifiers/train_atr_classifier.py new file mode 100644 index 0000000..6d9c88f --- /dev/null +++ b/color_classifiers/train_atr_classifier.py @@ -0,0 +1,197 @@ +import sys +import os +import matplotlib.pyplot as plt +import matplotlib.image as mpimg +import numpy as np +import tensorflow as tf +import atr_data_io_helper as atr_data_loader + +def plot_accuracy(xdata, ydata, xlim=None, ylim=None, savePath=None): + fig, ax = plt.subplots( nrows=1, ncols=1 ) + ax.plot(xdata, ydata) + plt.xlabel('Iterations') + plt.ylabel('Accuracy') + + if not xlim==None: + plt.xlim(xlim) + + if not ylim==None: + plt.ylim(ylim) + + if not savePath==None: + fig.savefig(savePath) + + + plt.close(fig) + +def plot_accuracies(xdata, ydata_train, ydata_val, xlim=None, ylim=None, savePath=None): + fig, ax = plt.subplots( nrows=1, ncols=1 ) + ax.plot(xdata, ydata_train, xdata, ydata_val) + plt.xlabel('Epochs') + plt.ylabel('Accuracy') + plt.legend(['Train', 'Val'], loc='lower right') + if not xlim==None: + plt.xlim(xlim) + + if not ylim==None: + plt.ylim(ylim) + + if not savePath==None: + fig.savefig(savePath) + + + plt.close(fig) + + +def weight_variable(shape): + initial = tf.truncated_normal(shape, stddev=0.1) + return tf.Variable(initial) + +def bias_variable(shape): + initial = tf.constant(0.1, shape=shape) + return tf.Variable(initial) + +def conv2d(x, W): + return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') + +def max_pool_2x2(x): + return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') + +def max_pool_4x4(x): + return tf.nn.max_pool(x, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + +def placeholder_inputs(): + # Specify placeholder_inputs + x = tf.placeholder(tf.float32, shape=[None, 25, 25, 3]) + y = tf.placeholder(tf.float32, shape=[None, 4]) + keep_prob = tf.placeholder(tf.float32) + return x, y, keep_prob + +def comp_graph_v_1(x, y, keep_prob): + # Specify computation graph + W_conv1 = weight_variable([5, 5, 3, 10]) + b_conv1 = bias_variable([10]) + + h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) + + h_pool1 = max_pool_2x2(h_conv1) + #print(tf.Tensor.get_shape(h_pool1)) + + W_fc1 = weight_variable([13*13*10, 4]) + b_fc1 = bias_variable([4]) + + h_pool1_flat = tf.reshape(h_pool1, [-1, 13*13*10]) + h_pool1_flat_drop = tf.nn.dropout(h_pool1_flat, keep_prob) + + y_pred = tf.nn.softmax(tf.matmul(h_pool1_flat_drop,W_fc1) + b_fc1) + + return y_pred + +def comp_graph_v_2(x, y, keep_prob): + # Specify computation graph + W_conv1 = weight_variable([5, 5, 3, 10]) + b_conv1 = bias_variable([10]) + + h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv1_drop = tf.nn.dropout(h_pool1, keep_prob) + + W_conv2 = weight_variable([5, 5, 10, 20]) + b_conv2 = bias_variable([20]) + + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + h_conv2_drop = tf.nn.dropout(h_pool2, keep_prob) + + W_fc1 = weight_variable([7*7*20, 4]) + b_fc1 = bias_variable([4]) + + h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*20]) + h_pool2_flat_drop = tf.nn.dropout(h_pool2_flat, keep_prob) + + y_pred = tf.nn.softmax(tf.matmul(h_pool2_flat_drop,W_fc1) + b_fc1) + + return y_pred + +def evaluation(y, y_pred): + # Evaluation function + correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_pred,1)) + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) + #tf.scalar_summary("accuracy", accuracy) + + return accuracy + + +def train(): + # Start session + sess = tf.InteractiveSession() + + x, y, keep_prob = placeholder_inputs() + y_pred = comp_graph_v_2(x, y, keep_prob) + + # Specify loss + cross_entropy = -tf.reduce_sum(y*tf.log(y_pred)) + + # Specify training method + train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) + + # Evaluator + accuracy = evaluation(y, y_pred) + + # Merge summaries and write them to ~/Code/Tensorflow_Exp/logDir + merged = tf.merge_all_summaries() + + # Output dir + outdir = '/home/tanmay/Code/GenVQA/Exp_Results/Atr_Classifier_v_1/' + if not os.path.exists(outdir): + os.mkdir(outdir) + + # Training Data + img_width = 75 + img_height = 75 + train_json_filename = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/train_anno.json' + image_dir = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images' + mean_image = atr_data_loader.mean_image(train_json_filename, image_dir, 1000, 100, img_height, img_width) + np.save(os.path.join(outdir, 'mean_image.npy'), mean_image) + + # Val Data + val_batch = atr_data_loader.atr_mini_batch_loader(train_json_filename, image_dir, mean_image, 9501, 499, img_height, img_width) + feed_dict_val={x: val_batch[0], y: val_batch[1], keep_prob: 1.0} + + # Session Saver + saver = tf.train.Saver() + + # Start Training + sess.run(tf.initialize_all_variables()) + batch_size = 100 + max_epoch = 10 + max_iter = 95 + val_acc_array_iter = np.empty([max_iter*max_epoch]) + val_acc_array_epoch = np.zeros([max_epoch]) + train_acc_array_epoch = np.zeros([max_epoch]) + for epoch in range(max_epoch): + for i in range(max_iter): + train_batch = atr_data_loader.atr_mini_batch_loader(train_json_filename, image_dir, mean_image, 1+i*batch_size, batch_size, img_height, img_width) + feed_dict_train={x: train_batch[0], y: train_batch[1], keep_prob: 0.5} + + _, current_train_batch_acc = sess.run([train_step, accuracy], feed_dict=feed_dict_train) + + train_acc_array_epoch[epoch] = train_acc_array_epoch[epoch] + current_train_batch_acc + val_acc_array_iter[i+epoch*max_iter] = accuracy.eval(feed_dict_val) + print('Step: {} Val Accuracy: {}'.format(i+1+epoch*max_iter, val_acc_array_iter[i+epoch*max_iter])) + plot_accuracy(np.arange(0,i+1+epoch*max_iter)+1, val_acc_array_iter[0:i+1+epoch*max_iter], xlim=[1, max_epoch*max_iter], ylim=[0, 1.], savePath=os.path.join(outdir,'valAcc_vs_iter.pdf')) + + + train_acc_array_epoch[epoch] = train_acc_array_epoch[epoch] / max_iter + val_acc_array_epoch[epoch] = val_acc_array_iter[i+epoch*max_iter] + + plot_accuracies(xdata=np.arange(0,epoch+1)+1, ydata_train=train_acc_array_epoch[0:epoch+1], ydata_val=val_acc_array_epoch[0:epoch+1], xlim=[1, max_epoch], ylim=[0, 1.], savePath=os.path.join(outdir,'acc_vs_epoch.pdf')) + + save_path = saver.save(sess, os.path.join(outdir,'obj_classifier_{}.ckpt'.format(epoch))) + + + sess.close() + +if __name__=='__main__': + train() + diff --git a/object_classifiers/eval_obj_classifier.py b/object_classifiers/eval_obj_classifier.py index d9dd4e1..9853116 100644 --- a/object_classifiers/eval_obj_classifier.py +++ b/object_classifiers/eval_obj_classifier.py @@ -3,34 +3,67 @@ import os import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np +from scipy import misc import tensorflow as tf -import obj_data_io_helper as shape_data_loader -from train_obj_classifier import placeholder_inputs, comp_graph, evaluation +import obj_data_io_helper as shape_data_loader +from train_obj_classifier import placeholder_inputs, comp_graph_v_1, comp_graph_v_2, evaluation sess=tf.InteractiveSession() x, y, keep_prob = placeholder_inputs() -y_pred = comp_graph(x, y, keep_prob) +y_pred = comp_graph_v_2(x, y, keep_prob) accuracy = evaluation(y, y_pred) saver = tf.train.Saver() -saver.restore(sess, '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/obj_classifier_5.ckpt') +saver.restore(sess, '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_2/obj_classifier_1.ckpt') -mean_image = np.load('/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/mean_image.npy') +mean_image = np.load('/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_2/mean_image.npy') # Test Data test_json_filename = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/test_anno.json' image_dir = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images' +# Base dir for html visualizer +html_dir = '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_2/html' +if not os.path.exists(html_dir): + os.mkdir(html_dir) + +# HTML file writer +html_writer = shape_data_loader.html_obj_table_writer(os.path.join(html_dir,'index.html')) +col_dict={ + 0: 'Grount Truth', + 1: 'Prediction', + 2: 'Image'} +html_writer.add_element(col_dict) + +shape_dict = { + 0: 'blank', + 1: 'rectangle', + 2: 'triangle', + 3: 'circle'} + batch_size = 100 correct = 0 -for i in range(50): - test_batch = shape_data_loader.obj_mini_batch_loader(test_json_filename, image_dir, mean_image, 10000+i*batch_size, batch_size) - +for i in range(50): + test_batch = shape_data_loader.obj_mini_batch_loader(test_json_filename, image_dir, mean_image, 10000+i*batch_size, batch_size, 75, 75) feed_dict_test={x: test_batch[0], y: test_batch[1], keep_prob: 1.0} - correct = correct + accuracy.eval(feed_dict_test)*batch_size + result = sess.run([accuracy, y_pred], feed_dict=feed_dict_test) + correct = correct + result[0]*batch_size print(correct) + for row in range(batch_size*9): + gt_id = np.argmax(test_batch[1][row,:]) + pred_id = np.argmax(result[1][row, :]) + if not gt_id==pred_id: + img_filename = os.path.join(html_dir,'{}_{}.png'.format(i,row)) + misc.imsave(img_filename, test_batch[0][row,:,:,:]) + col_dict = { + 0: shape_dict[gt_id], + 1: shape_dict[pred_id], + 2: html_writer.image_tag('{}_{}.png'.format(i,row), 25, 25)} + html_writer.add_element(col_dict) + +html_writer.close_file() print('Test Accuracy: {}'.format(correct/5000)) diff --git a/object_classifiers/obj_data_io_helper.py b/object_classifiers/obj_data_io_helper.py index 3f5484a..d0427ab 100644 --- a/object_classifiers/obj_data_io_helper.py +++ b/object_classifiers/obj_data_io_helper.py @@ -5,18 +5,20 @@ import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np import tensorflow as tf +from scipy import misc def obj_mini_batch_loader(json_filename, image_dir, mean_image, start_index, batch_size, img_height=100, img_width=100, channels=3): with open(json_filename, 'r') as json_file: json_data = json.load(json_file) - obj_images = np.empty(shape=[9*batch_size, img_height, img_width, channels]) + obj_images = np.empty(shape=[9*batch_size, img_height/3, img_width/3, channels]) obj_labels = np.zeros(shape=[9*batch_size, 4]) for i in range(start_index, start_index + batch_size): image_name = os.path.join(image_dir, str(i) + '.jpg') - image = mpimg.imread(image_name) + image = misc.imresize(mpimg.imread(image_name),(img_height, img_width), interp='nearest') +# image.resize((img_height, img_width, 3)) crop_shape = np.array([image.shape[0], image.shape[1]])/3 selected_anno = [q for q in json_data if q['image_id']==i] grid_config = selected_anno[0]['config'] @@ -46,26 +48,43 @@ def mean_image_batch(json_filename, image_dir, start_index, batch_size, img_heig def mean_image(json_filename, image_dir, num_images, batch_size, img_height=100, img_width=100, channels=3): max_iter = np.floor(num_images/batch_size) - mean_image = np.zeros([img_height, img_width, channels]) + mean_image = np.zeros([img_height/3, img_width/3, channels]) for i in range(max_iter.astype(np.int16)): mean_image = mean_image + mean_image_batch(json_filename, image_dir, 1+i*batch_size, batch_size, img_height, img_width, channels) mean_image = mean_image/max_iter tmp_mean_image = mean_image*254 - imgplot = plt.imshow(tmp_mean_image.astype(np.uint8)) - plt.show() + # imgplot = plt.imshow(tmp_mean_image.astype(np.uint8)) + # plt.show() return mean_image -if __name__=="__main__": +class html_obj_table_writer(): + def __init__(self, filename): + self.filename = filename + self.html_file = open(self.filename, 'w') + self.html_file.write("""<!DOCTYPE html>\n<html>\n<body>\n<table border="1" style="width:100%"> \n""") + + def add_element(self, col_dict): + self.html_file.write(' <tr>\n') + for key in range(len(col_dict)): + self.html_file.write(""" <td>{}</td>\n""".format(col_dict[key])) + self.html_file.write(' </tr>\n') + + def image_tag(self, image_path, height, width): + return """<img src="{}" alt="IMAGE NOT FOUND!" height={} width={}>""".format(image_path,height,width) + + def close_file(self): + self.html_file.write('</table>\n</body>\n</html>') + self.html_file.close() - json_filename = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/train_anno.json' - batch_size = 1 - start_index = 0 + + - image_dir = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images' +if __name__=="__main__": - batch = obj_mini_batch_loader(json_filename, image_dir, start_index, batch_size) - print(batch[0].shape) - print(batch[1].shape) + html_writer = html_obj_table_writer('/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_1/trial.html') + col_dict={0: 'sam', 1: html_writer.image_tag('something.png',25,25)} + html_writer.add_element(col_dict) + html_writer.close_file() diff --git a/object_classifiers/obj_data_io_helper.pyc b/object_classifiers/obj_data_io_helper.pyc index 73a426be9496860d6a43819bf5c4c1506db53ffc..48169a55a825c93a57adb1cf4cc9cfeb258f4b15 100644 GIT binary patch literal 4545 zcmcInTXQ2v6+S)kMUv&q+VVwV0j;-E5nCy1O^SsIve{+5goMOP#vw~ol}wFiM$#yA zk!N~YJ3I40dEu2;ehzQ^iu{T^@Q|v&cTP(-1bCndOEW#EyU+AF=R23~seH9u|Ld1u ze(cfoH;?B-+<8M0`M4BCMExsTQD4o*E$Uk{x<cbQ>d()*3p8G&es$KZQNKQ;m#E(m z+M;Ng;uY$zkn$_U*Btrt^r}MrRq2|M1sPfUr;$Y&xkWw%psmp>qD!Jr`s);IkYA_I zEy}7YSSNppE}?RhLTC!H%k&^<(s+~n74jiul`bJ<jUM#3h$prw*y1-7cXoJDl?VtY zopqoR{vE&L4xk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_-0<&Ru>bYge1+sy~r zo2Iq7MZVY|1F<E#;)uhu-byr-tbWKInCp6yNA}dbD@69Ad2PN=m6}Kj)Nh(h{u!*a zw3HKZ5auAkx*}DucFO9gM0$iE_`!e`e1E~jU$h*sh*f1O$K%t<WM-?XI1SPR;KuS| z>Z{{m9?9Ex>>WPhK+{vHAi+33nLJUD$3Z0TDuURhexQE|BBcY-<FONr-H{*vFI7zN z*gbaw>CfyAZR~Y2vc_h-GIf~u#ryORQ9s9{>FGpf&ufP}3eL&JEiw>ou|h4})=Huz z-k>2lw&RT5Aab1eUiPYbV=F#!r+(~su5@Ex?#LM*x=}RQ4&zC*Ac}*<dkla3HTa^A zfoeJTSg{gc#CAE08#nXzF8Q)&jjq2z3%kw;h7kqRiv{b7W<QE2GvfY<)EIJXE4^_g zs-$J!kvsMsXThfWqLP~u-Zn!;cB^^z)+Q}0g6}%rG;6JmR=!oOXojF$GqetQ{{d0; B18e{Q diff --git a/object_classifiers/train_obj_classifier.py b/object_classifiers/train_obj_classifier.py index a54e846..19b48a2 100644 --- a/object_classifiers/train_obj_classifier.py +++ b/object_classifiers/train_obj_classifier.py @@ -57,29 +57,58 @@ def conv2d(x, W): def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') +def max_pool_4x4(x): + return tf.nn.max_pool(x, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') + def placeholder_inputs(): # Specify placeholder_inputs - x = tf.placeholder(tf.float32, shape=[None, 100, 100, 3]) + x = tf.placeholder(tf.float32, shape=[None, 25, 25, 3]) y = tf.placeholder(tf.float32, shape=[None, 4]) keep_prob = tf.placeholder(tf.float32) return x, y, keep_prob -def comp_graph(x, y, keep_prob): +def comp_graph_v_1(x, y, keep_prob): # Specify computation graph W_conv1 = weight_variable([5, 5, 3, 10]) b_conv1 = bias_variable([10]) h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) #print(tf.Tensor.get_shape(h_pool1)) - W_fc1 = weight_variable([50*50*10, 4]) + W_fc1 = weight_variable([13*13*10, 4]) b_fc1 = bias_variable([4]) - h_pool1_flat = tf.reshape(h_pool1, [-1, 50*50*10]) + h_pool1_flat = tf.reshape(h_pool1, [-1, 13*13*10]) h_pool1_flat_drop = tf.nn.dropout(h_pool1_flat, keep_prob) - y_pred = tf.nn.softmax(tf.matmul(h_pool1_flat,W_fc1) + b_fc1) + y_pred = tf.nn.softmax(tf.matmul(h_pool1_flat_drop,W_fc1) + b_fc1) + + return y_pred + +def comp_graph_v_2(x, y, keep_prob): + # Specify computation graph + W_conv1 = weight_variable([5, 5, 3, 4]) + b_conv1 = bias_variable([4]) + + h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) + h_pool1 = max_pool_2x2(h_conv1) + h_conv1_drop = tf.nn.dropout(h_pool1, keep_prob) + + W_conv2 = weight_variable([3, 3, 4, 8]) + b_conv2 = bias_variable([8]) + + h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) + h_pool2 = max_pool_2x2(h_conv2) + + W_fc1 = weight_variable([7*7*8, 4]) + b_fc1 = bias_variable([4]) + + h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*8]) + h_pool2_flat_drop = tf.nn.dropout(h_pool2_flat, keep_prob) + + y_pred = tf.nn.softmax(tf.matmul(h_pool2_flat_drop,W_fc1) + b_fc1) return y_pred @@ -97,7 +126,7 @@ def train(): sess = tf.InteractiveSession() x, y, keep_prob = placeholder_inputs() - y_pred = comp_graph(x, y, keep_prob) + y_pred = comp_graph_v_2(x, y, keep_prob) # Specify loss cross_entropy = -tf.reduce_sum(y*tf.log(y_pred)) @@ -112,14 +141,21 @@ def train(): merged = tf.merge_all_summaries() #writer = tf.train.SummaryWriter("/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier", graph_def=tf.GraphDef()) + # Output dir + outdir = '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier_v_2/' + if not os.path.exists(outdir): + os.mkdir(outdir) + # Training Data + img_width = 75 + img_height = 75 train_json_filename = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/train_anno.json' image_dir = '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images' - mean_image = shape_data_loader.mean_image(train_json_filename, image_dir, 1000, 100) - np.save('/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/mean_image.npy', mean_image) + mean_image = shape_data_loader.mean_image(train_json_filename, image_dir, 1000, 100, img_height, img_width) + np.save(os.path.join(outdir, 'mean_image.npy'), mean_image) # Val Data - val_batch = shape_data_loader.obj_mini_batch_loader(train_json_filename, image_dir, mean_image, 9501, 499) + val_batch = shape_data_loader.obj_mini_batch_loader(train_json_filename, image_dir, mean_image, 9501, 499, img_height, img_width) feed_dict_val={x: val_batch[0], y: val_batch[1], keep_prob: 1.0} # Session Saver @@ -127,15 +163,15 @@ def train(): # Start Training sess.run(tf.initialize_all_variables()) - batch_size = 100 - max_epoch = 10 - max_iter = 95 + batch_size = 10 + max_epoch = 2 + max_iter = 950 val_acc_array_iter = np.empty([max_iter*max_epoch]) val_acc_array_epoch = np.zeros([max_epoch]) train_acc_array_epoch = np.zeros([max_epoch]) for epoch in range(max_epoch): for i in range(max_iter): - train_batch = shape_data_loader.obj_mini_batch_loader(train_json_filename, image_dir, mean_image, 1+i*batch_size, batch_size) + train_batch = shape_data_loader.obj_mini_batch_loader(train_json_filename, image_dir, mean_image, 1+i*batch_size, batch_size, img_height, img_width) feed_dict_train={x: train_batch[0], y: train_batch[1], keep_prob: 0.5} _, current_train_batch_acc = sess.run([train_step, accuracy], feed_dict=feed_dict_train) @@ -143,15 +179,15 @@ def train(): train_acc_array_epoch[epoch] = train_acc_array_epoch[epoch] + current_train_batch_acc val_acc_array_iter[i+epoch*max_iter] = accuracy.eval(feed_dict_val) print('Step: {} Val Accuracy: {}'.format(i+1+epoch*max_iter, val_acc_array_iter[i+epoch*max_iter])) - plot_accuracy(np.arange(0,i+1+epoch*max_iter)+1, val_acc_array_iter[0:i+1+epoch*max_iter], xlim=[1, max_epoch*max_iter], ylim=[0, 1.], savePath='/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/valAcc_vs_iter.pdf') + plot_accuracy(np.arange(0,i+1+epoch*max_iter)+1, val_acc_array_iter[0:i+1+epoch*max_iter], xlim=[1, max_epoch*max_iter], ylim=[0, 1.], savePath=os.path.join(outdir,'valAcc_vs_iter.pdf')) train_acc_array_epoch[epoch] = train_acc_array_epoch[epoch] / max_iter val_acc_array_epoch[epoch] = val_acc_array_iter[i+epoch*max_iter] - plot_accuracies(xdata=np.arange(0,epoch+1)+1, ydata_train=train_acc_array_epoch[0:epoch+1], ydata_val=val_acc_array_epoch[0:epoch+1], xlim=[1, max_epoch], ylim=[0, 1.], savePath='/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/acc_vs_epoch.pdf') + plot_accuracies(xdata=np.arange(0,epoch+1)+1, ydata_train=train_acc_array_epoch[0:epoch+1], ydata_val=val_acc_array_epoch[0:epoch+1], xlim=[1, max_epoch], ylim=[0, 1.], savePath=os.path.join(outdir,'acc_vs_epoch.pdf')) - save_path = saver.save(sess, '/home/tanmay/Code/GenVQA/Exp_Results/Shape_Classifier/obj_classifier_{}.ckpt'.format(epoch)) + save_path = saver.save(sess, os.path.join(outdir,'obj_classifier_{}.ckpt'.format(epoch))) sess.close() diff --git a/object_classifiers/train_obj_classifier.pyc b/object_classifiers/train_obj_classifier.pyc index cd5b5cafd0d303334de0fb93f98f0f61611f70e1..550d73d4e5c289d02061f24457f8bc52c638aaee 100644 GIT binary patch delta 2260 zcmZuyO>7%g5T3W|_%HrR?DbEa|FqDwl$4|;EhV%lsc0oiRk@T_Rl|~a9edp-cCy<N z)Lj`VJ#e5x)sqn7ia1mps6ayE0yiYCMS=@RzzHFQ1c)0FeDl^O3DoxcJa692yqS65 zzP)|;?%bgMM>2l!&+lHUQ0EiE|Fam*?{^2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