Skip to content
Snippets Groups Projects
Commit 6e521fb3 authored by tgupta6's avatar tgupta6
Browse files

optimized obj train and eval

parent 92f02b56
No related branches found
No related tags found
No related merge requests found
Showing
with 44 additions and 33 deletions
......@@ -19,6 +19,7 @@ def atr_mini_batch_loader(json_data, image_dir, mean_image, start_index, batch_s
#image = np.zeros(shape=[img_height, img_width, channels])
crop_shape = np.array([image.shape[0], image.shape[1]])/3
grid_config = json_data[i]
counter = 0;
for grid_row in xrange(0,3):
for grid_col in xrange(0,3):
......
No preview for this file type
......@@ -83,20 +83,15 @@ def train(train_params):
train_acc_array_epoch = np.zeros([max_epoch])
for epoch in range(max_epoch):
for i in range(max_iter):
print(i)
# start = time.time()
if i%100==0:
print('Iter: ' + str(i))
train_batch = atr_data_loader.atr_mini_batch_loader(train_json_data, image_dir, mean_image, 1+i*batch_size, batch_size, img_height, img_width)
# end = time.time()
# print(end-start)
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)
# plotter.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'))
# print('Step: {} Val Accuracy: {}'.format(i+1+epoch*max_iter, val_acc_array_iter[i+epoch*max_iter]))
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]
val_acc_array_epoch[epoch] = accuracy.eval(feed_dict_val)
plotter.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'))
_ = atr_saver.save(sess, os.path.join(outdir,'atr_classifier'), global_step=epoch)
_ = obj_atr_saver.save(sess, os.path.join(outdir,'obj_atr_classifier'), global_step=epoch)
......
No preview for this file type
import sys
import json
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
......@@ -20,6 +21,13 @@ def eval(eval_params):
saver.restore(sess, eval_params['model_name'] + '-' + str(eval_params['global_step']))
mean_image = np.load(os.path.join(eval_params['out_dir'], 'mean_image.npy'))
test_json_filename = eval_params['test_json']
with open(test_json_filename, 'r') as json_file:
raw_json_data = json.load(json_file)
test_json_data = dict()
for entry in raw_json_data:
if entry['image_id'] not in test_json_data:
test_json_data[entry['image_id']]=entry['config']
image_dir = eval_params['image_dir']
html_dir = eval_params['html_dir']
if not os.path.exists(html_dir):
......@@ -39,9 +47,7 @@ def eval(eval_params):
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)
#print(test_batch[0].dtype)
# print([np.amax(test_batch[0][0,:, :, :]), np.amax(mean_image)])
test_batch = shape_data_loader.obj_mini_batch_loader(test_json_data, 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
......
No preview for this file type
......@@ -7,10 +7,7 @@ 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)
def obj_mini_batch_loader(json_data, image_dir, mean_image, start_index, batch_size, img_height = 100, img_width = 100, channels = 3):
obj_images = np.empty(shape=[9 * batch_size, img_height / 3, img_width / 3, channels])
obj_labels = np.zeros(shape=[9 * batch_size, 4])
......@@ -19,8 +16,7 @@ def obj_mini_batch_loader(json_filename, image_dir, mean_image, start_index, bat
image_name = os.path.join(image_dir, str(i) + '.jpg')
image = misc.imresize(mpimg.imread(image_name), (img_height, img_width), interp='nearest')
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']
grid_config = json_data[i]
counter = 0
for grid_row in range(0, 3):
......@@ -28,27 +24,29 @@ def obj_mini_batch_loader(json_filename, image_dir, mean_image, start_index, bat
start_row = grid_row * crop_shape[0]
start_col = grid_col * crop_shape[1]
cropped_image = image[start_row:start_row + crop_shape[0], start_col:start_col + crop_shape[1], :]
if np.ndim(mean_image) == 0:
obj_images[9 * (i - start_index) + counter, :, :, :] = cropped_image / 254.0
else:
obj_images[9 * (i - start_index) + counter, :, :, :] = (cropped_image / 254.0) - mean_image
obj_labels[9 * (i - start_index) + counter, grid_config[6 * grid_row + 2 * grid_col]] = 1
counter = counter + 1
return (obj_images, obj_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)
def mean_image_batch(json_data, image_dir, start_index, batch_size, img_height = 100, img_width = 100, channels = 3):
batch = obj_mini_batch_loader(json_data, 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):
def mean_image(json_data, 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 + mean_image_batch(json_data, image_dir, 1 + i * batch_size, batch_size, img_height, img_width, channels)
mean_image = mean_image / max_iter
return mean_image
......
No preview for this file type
import sys
import os
import json
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
......@@ -25,17 +26,25 @@ def train(train_params):
img_width = 75
img_height = 75
train_json_filename = train_params['train_json']
with open(train_json_filename, 'r') as json_file:
raw_json_data = json.load(json_file)
train_json_data = dict()
for entry in raw_json_data:
if entry['image_id'] not in train_json_data:
train_json_data[entry['image_id']]=entry['config']
image_dir = train_params['image_dir']
if train_params['mean_image']=='':
print('Computing mean image')
mean_image = atr_data_loader.mean_image(train_json_filename, image_dir, 1000, 100, img_height, img_width)
mean_image = shape_data_loader.mean_image(train_json_data, image_dir, 1000, 100, img_height, img_width)
else:
print('Loading mean image')
mean_image = np.load(train_params['mean_image'])
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, img_height, img_width)
val_batch = shape_data_loader.obj_mini_batch_loader(train_json_data, 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
......@@ -51,16 +60,17 @@ def train(train_params):
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, img_height, img_width)
if i%100==0:
print('Iter: ' + str(i))
print('Val Acc: ' + str(accuracy.eval(feed_dict_val)))
train_batch = shape_data_loader.obj_mini_batch_loader(train_json_data, 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)
plotter.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.0], savePath=os.path.join(outdir, 'valAcc_vs_iter.pdf'))
print 'Step: {} Val Accuracy: {}'.format(i + 1 + epoch * max_iter, val_acc_array_iter[i + epoch * max_iter])
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]
val_acc_array_epoch[epoch] = accuracy.eval(feed_dict_val)
plotter.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.0], savePath=os.path.join(outdir, 'acc_vs_epoch.pdf'))
save_path = saver.save(sess, os.path.join(outdir, 'obj_classifier'), global_step=epoch)
......
No preview for this file type
import sys
import json
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
......@@ -10,9 +11,9 @@ import attribute_classifiers.train_atr_classifier as atr_trainer
import attribute_classifiers.eval_atr_classifier as atr_evaluator
workflow = {
'train_obj': False,
'eval_obj': False,
'train_atr': False,
'train_obj': True,
'eval_obj': True,
'train_atr': True,
'eval_atr': True,
}
......@@ -21,8 +22,8 @@ obj_classifier_train_params = {
'adam_lr': 0.001,
'train_json': '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/train_anno.json',
'image_dir': '/home/tanmay/Code/GenVQA/GenVQA/shapes_dataset/images',
'mean_image': '/home/tanmay/Code/GenVQA/Exp_Results/Obj_Classifier/mean_image.npy',
# 'mean_image': '',
# 'mean_image': '/home/tanmay/Code/GenVQA/Exp_Results/Obj_Classifier/mean_image.npy',
'mean_image': '',
}
obj_classifier_eval_params = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment