Object Recognition & Orientation Detection


Zhiang Chen, Nov 2016

1. Import Packages


In [1]:
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
import matplotlib.pyplot as plt
import random
import operator
import time
import os

2. Import Data


In [2]:
image_size = 80

file_name = 'cropped_depth_data_numpy'
with open(file_name, 'rb') as f:
    u = pickle._Unpickler(f)
    u.encoding = 'latin1'
    save = u.load()
    image_dict = save['image']
    del save

dataset = list()
str_classes = list()
angles = list()
for name,images in image_dict.items():
    [phase, obj, angle] = name.split('.')[0].split('-')
    for i in range(images.shape[0]):
        dataset.append(images[i,:,:])
        str_classes.append(obj)
        angles.append(int(angle))
        
name2value = {'v8':0,'duck':1,'stapler':2,'pball':3,'tball':4,'sponge':5,'bclip':6,'tape':7,'gstick':8,'cup':9,
              'pen':10,'calc':11,'blade':12,'bottle':13,'cpin':14,'scissors':15,'stape':16,'gball':17,'orwidg':18,
             'glue':19,'spoon':20,'fork':21,'nerf':22,'eraser':23,'empty':24}
name2string = {'v8':'v8 can','duck':'ducky','stapler':'stapler','pball':'ping pang ball','tball':'tennis ball','sponge':'sponge',
               'bclip':'binder clip','tape':'big tape','gstick':'glue stick','cup':'cup','pen':'pen','calc':'calculator',
               'blade':'razor','bottle':'bottle','cpin':'clothespin','scissors':'scissors','stape':'small tape','gball':'golf ball',
               'orwidg':'orange thing','glue':'glue','spoon':'spoon','fork':'fork','nerf':'nerf gun','eraser':'eraser',
               'empty':'empty plate'}
value2name = dict((value,name) for name,value in name2value.items())   

nm_image = len(dataset)
classes = [name2value[name] for name in str_classes]

dataset = np.asarray(dataset).astype(np.float32)
classes = np.asarray(classes)
angles = np.asarray(angles)/18%10


classes = (np.arange(len(name2value)) == classes[:,None]).astype(np.float32)
angles = (np.arange(10) == angles[:,None]).astype(np.float32)
labels = np.concatenate((classes,angles),axis=1)

In [3]:
o_dataset = list()
o_classes = list()
o_angles = list()
o_names = ['duck','stapler','sponge','bclip','pen','calc','blade','cpin','glue','scissors','spoon','fork','nerf','eraser']
for i in range(nm_image):
    if str_classes[i] in o_names:
        o_dataset.append(dataset[i,:,:])
        o_classes.append(classes[i,:])
        o_angles.append(angles[i,:])
        
o_dataset = np.asarray(o_dataset).astype(np.float32)
o_classes = np.asarray(o_classes)
o_angles = np.asarray(o_angles)

3. Data Preprocess


In [4]:
def randomize(dataset, classes, angles):
    permutation = np.random.permutation(classes.shape[0])
    shuffled_dataset = dataset[permutation,:,:]
    shuffled_classes = classes[permutation]
    shuffled_angles = angles[permutation]
    return shuffled_dataset, shuffled_classes, shuffled_angles

dataset, classes, angles = randomize(dataset, classes, angles)
o_dataset, o_classes, o_angles = randomize(o_dataset, o_classes, o_angles)

In [5]:
angle_bias = -15
indices = [random.randint(0,dataset.shape[0]) for x in range(2)]
for index in indices:
    image = dataset[index,:,:]
    print('class: %s' % value2name[np.argmax(classes[index])])
    print('angle: ',np.argmax(angles[index])*18 + angle_bias)
    plt.imshow(image,cmap='Greys_r',vmin=-0.6,vmax=0.4)
    plt.show()


class: calc
angle:  -15
class: bclip
angle:  147

In [6]:
indices = [random.randint(0,o_dataset.shape[0]) for x in range(5)]
for index in indices:
    image = o_dataset[index,:,:]
    print('class: %s' % value2name[np.argmax(o_classes[index])])
    print('angle: ',np.argmax(o_angles[index])*18 + angle_bias)
    plt.imshow(image,cmap='Greys_r',vmin=-0.6,vmax=0.4)
    plt.show()


class: blade
angle:  57
class: glue
angle:  75
class: stapler
angle:  57
class: scissors
angle:  147
class: spoon
angle:  57

In [7]:
num_images = dataset.shape[0]
num_train = int(num_images*0.7)
num_valid = int(num_images*0.15)
num_test = int(num_images*0.15)

train_dataset = dataset[0:num_train,:,:].reshape(-1,image_size,image_size,1)
train_classes = classes[0:num_train,:]
train_angles = angles[0:num_train,:]

valid_dataset = dataset[num_train:(num_train+num_valid),:,:].reshape(-1,image_size,image_size,1)
valid_classes = classes[num_train:(num_train+num_valid),:]
valid_angles = angles[num_train:(num_train+num_valid),:]

test_dataset = dataset[(num_train+num_valid):,:,:].reshape(-1,image_size,image_size,1)
test_classes = classes[(num_train+num_valid):,:]
test_angles = angles[(num_train+num_valid):,:]

print('Training:', train_dataset.shape, train_classes.shape, train_angles.shape)
print('Validation:', valid_dataset.shape, valid_classes.shape, valid_angles.shape)
print('Testing:', test_dataset.shape, test_classes.shape, test_angles.shape)


Training: (7559, 80, 80, 1) (7559, 25) (7559, 10)
Validation: (1620, 80, 80, 1) (1620, 25) (1620, 10)
Testing: (1621, 80, 80, 1) (1621, 25) (1621, 10)

In [8]:
o_num_images = o_dataset.shape[0]
o_num_train = int(o_num_images*0.7)
o_num_valid = int(o_num_images*0.15)
o_num_test = int(o_num_images*0.15)

o_train_dataset = o_dataset[0:o_num_train,:,:].reshape(-1,image_size,image_size,1)
o_train_classes = o_classes[0:o_num_train,:]
o_train_angles = o_angles[0:o_num_train,:]

o_valid_dataset = o_dataset[o_num_train:(o_num_train+o_num_valid),:,:].reshape(-1,image_size,image_size,1)
o_valid_classes = o_classes[o_num_train:(o_num_train+o_num_valid),:]
o_valid_angles = o_angles[o_num_train:(o_num_train+o_num_valid),:]

o_test_dataset = o_dataset[(o_num_train+o_num_valid):,:,:].reshape(-1,image_size,image_size,1)
o_test_classes = o_classes[(o_num_train+o_num_valid):,:]
o_test_angles = o_angles[(o_num_train+o_num_valid):,:]

print('Training:', o_train_dataset.shape, o_train_classes.shape, o_train_angles.shape)
print('Validation:', o_valid_dataset.shape, o_valid_classes.shape, o_valid_angles.shape)
print('Testing:', o_test_dataset.shape, o_test_classes.shape, o_test_angles.shape)


Training: (5250, 80, 80, 1) (5250, 25) (5250, 10)
Validation: (1125, 80, 80, 1) (1125, 25) (1125, 10)
Testing: (1125, 80, 80, 1) (1125, 25) (1125, 10)

4. Define accuracy


In [9]:
def accuracy_classes(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

def accuracy_angles(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

5. Build and Train Net

Classification Net

In [12]:
nm_classes = 25
nm_angles = 10
num_channels = 1 

batch_size = 30
patch_size = 5
kernel_size = 2
depth1 = 6
depth2 = 16 
depth3 = 10 
F7_classes = 120
F8_classes = 84
F9_classes = nm_classes
F7_angles = 120
F8_angles = 84
F9_angles = nm_angles

keep_prob1 = 0.8
keep_prob2 = 0.8

graph = tf.Graph()

with graph.as_default():
    # Input data
    tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
    # convolution's input is a tensor of shape [batch,in_height,in_width,in_channels]
    tf_train_classes = tf.placeholder(tf.float32, shape=(batch_size, nm_classes))
    tf_train_angles = tf.placeholder(tf.float32, shape=(batch_size, nm_angles))
    
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)
    
    # Variables(weights and biases)
    C1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth1], stddev=0.1))
    # convolution's weights are called filter in tensorflow
    # it is a tensor of shape [kernel_hight,kernel_width,in_channels,out_channels]
    C1_biases = tf.Variable(tf.zeros([depth1]))
                            
    # S1_weights # Sub-sampling doesn't need weights and biases
    # S1_biases
    
    C3_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth1, depth2], stddev=0.1))
    C3_biases = tf.Variable(tf.constant(1.0, shape=[depth2]))
                            
    # S4_weights
    # S4_biases
     
    C5_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth2, depth3], stddev=0.1))
    C5_biases = tf.Variable(tf.constant(1.0, shape=[depth3]))
    
    # S6_weights
    # S6_biases
    
    F7_classes_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_classes], stddev=0.1))
    F7_classes_biases = tf.Variable(tf.constant(1.0, shape=[F7_classes]))
    F7_angles_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_angles], stddev=0.1))
    F7_angles_biases = tf.Variable(tf.constant(1.0, shape=[F7_angles]))
    
    F8_classes_weights = tf.Variable(tf.truncated_normal([F7_classes,F8_classes], stddev=0.1))
    F8_classes_biases = tf.Variable(tf.constant(1.0, shape=[F8_classes]))
    F8_angles_weights = tf.Variable(tf.truncated_normal([F7_angles,F8_angles], stddev=0.1))
    F8_angles_biases = tf.Variable(tf.constant(1.0, shape=[F8_angles]))
    
    F9_classes_weights = tf.Variable(tf.truncated_normal([F8_classes,F9_classes], stddev=0.1))
    F9_classes_biases = tf.Variable(tf.constant(1.0, shape=[F9_classes]))
    F9_angles_weights = tf.Variable(tf.truncated_normal([F8_angles,F9_angles], stddev=0.1))
    F9_angles_biases = tf.Variable(tf.constant(1.0, shape=[F9_angles]))
    
    saver = tf.train.Saver()
    # Model
    def train_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        
        hidden_input = tf.nn.dropout(reshape,keep_prob1) # dropout on the input layer
        
        hidden_classes = tf.nn.relu(tf.matmul(hidden_input, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(hidden_input, F7_angles_weights) + F7_angles_biases)
        
        hidden_classes = tf.nn.dropout(hidden_classes, keep_prob2) # dropout on the first FC layer
        hidden_angles = tf.nn.dropout(hidden_angles, keep_prob2)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    def test_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        hidden_classes = tf.nn.relu(tf.matmul(reshape, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(reshape, F7_angles_weights) + F7_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    # Training computation.
    logits_classes, logits_angles = train_model(tf_train_dataset)
    loss_classes = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_classes, tf_train_classes))
    loss_angles = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_angles, tf_train_angles))
    loss =  loss_classes #+ loss_angles 
    
    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
  
    # Predictions for the training, validation, and test data.
    train_classes_logits, train_angles_logits = test_model(tf_train_dataset)
    train_classes_prediction = tf.nn.softmax(logits_classes)
    train_angles_prediction = tf.nn.softmax(logits_angles)
    
    valid_classes_logits, valid_angles_logits = test_model(tf_valid_dataset)
    valid_classes_prediction = tf.nn.softmax(valid_classes_logits)
    valid_angles_prediction = tf.nn.softmax(valid_angles_logits)
    
    test_classes_logits, test_angles_logits = test_model(tf_test_dataset)
    test_classes_prediction = tf.nn.softmax(test_classes_logits)
    test_angles_prediction = tf.nn.softmax(test_angles_logits)

Classification Training


In [13]:
## training
start_time = time.time()

num_steps = 100000
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
config.log_device_placement = True
with tf.Session(graph=graph, config = config) as session:
  tf.initialize_all_variables().run()
  print('Initialized')
  for step in range(num_steps):
    offset = (step * batch_size) % (train_classes.shape[0] - batch_size)
    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
    batch_classes = train_classes[offset:(offset + batch_size), :]
    batch_angles = train_angles[offset:(offset + batch_size), :]
    feed_dict = {tf_train_dataset : batch_data, tf_train_classes : batch_classes, tf_train_angles : batch_angles}
    _, l, classes_predictions, angles_predictions = session.run(
      [optimizer, loss, train_classes_prediction, train_angles_prediction], feed_dict=feed_dict)
    if (step % 1000 == 0):
      print('Minibatch loss at step %d: %f' % (step, l))
      print('Minibatch classes accuracy: %.1f%%' % accuracy_classes(classes_predictions, batch_classes))
      #print('Minibatch angles accuracy: %.1f%%' % accuracy_angles(angles_predictions, batch_angles))
      print('Validation classes accuracy: %.1f%%' % accuracy_classes(valid_classes_prediction.eval(), valid_classes))
      #print('Validation angles accuracy: %.1f%%' % accuracy_angles(valid_angles_prediction.eval(), valid_angles))
      print('--------------------------------------')
  print('Test classes accuracy: %.1f%%' % accuracy_classes(test_classes_prediction.eval(), test_classes))
  #print('Test angles accuracy: %.1f%%' % accuracy_angles(test_angles_prediction.eval(), test_angles))
  end_time = time.time()
  duration = (end_time - start_time)/60
  print("Excution time: %0.2fmin" % duration)
  save_path = saver.save(session, "model.ckpt")
  print("Model saved in file: %s" % save_path)
  '''
  i_test = 0
  while(i_test!=''):
        i_test = input("Input an index of test image (or Enter to quit): ")
        label = test_labels[int(i_test),:].tolist()
        #print("Correct label: "+value2name[label.index(1)])
        image = test_dataset[int(i_test),:,:,:].reshape((-1,image_size,image_size,num_channels)).astype(np.float32)
        prediction = tf.nn.softmax(model(image))
        pre_dict = dict(zip(list(range(num_labels)),prediction.eval()[0]))
        sorted_pre_dict = sorted(pre_dict.items(), key=operator.itemgetter(1))
        name1 = value2name[sorted_pre_dict[-1][0]]
        name1 = name2string[name1]
        value1 = str(sorted_pre_dict[-1][1])
        name2 = value2name[sorted_pre_dict[-2][0]]
        name2 = name2string[name2]
        value2 = str(sorted_pre_dict[-2][1])
        tile = name1+': '+value1+'\n'+name2+': '+value2
        image = image.reshape((image_size,image_size)).astype(np.float32)
        plt.imshow(image,cmap='Greys_r')
        plt.suptitle(tile, fontsize=12)
        plt.xlabel(value2name[label.index(1)], fontsize=12)
        plt.show()
  '''


Initialized
Minibatch loss at step 0: 5.278069
Minibatch classes accuracy: 3.3%
Validation classes accuracy: 2.9%
--------------------------------------
Minibatch loss at step 1000: 2.499802
Minibatch classes accuracy: 13.3%
Validation classes accuracy: 28.2%
--------------------------------------
Minibatch loss at step 2000: 2.074587
Minibatch classes accuracy: 23.3%
Validation classes accuracy: 25.4%
--------------------------------------
Minibatch loss at step 3000: 1.716553
Minibatch classes accuracy: 36.7%
Validation classes accuracy: 49.0%
--------------------------------------
Minibatch loss at step 4000: 1.264886
Minibatch classes accuracy: 40.0%
Validation classes accuracy: 48.1%
--------------------------------------
Minibatch loss at step 5000: 1.420281
Minibatch classes accuracy: 50.0%
Validation classes accuracy: 60.7%
--------------------------------------
Minibatch loss at step 6000: 1.000854
Minibatch classes accuracy: 50.0%
Validation classes accuracy: 74.8%
--------------------------------------
Minibatch loss at step 7000: 1.039420
Minibatch classes accuracy: 63.3%
Validation classes accuracy: 73.2%
--------------------------------------
Minibatch loss at step 8000: 0.993793
Minibatch classes accuracy: 60.0%
Validation classes accuracy: 78.3%
--------------------------------------
Minibatch loss at step 9000: 0.692982
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 84.4%
--------------------------------------
Minibatch loss at step 10000: 0.803101
Minibatch classes accuracy: 66.7%
Validation classes accuracy: 79.6%
--------------------------------------
Minibatch loss at step 11000: 0.618140
Minibatch classes accuracy: 73.3%
Validation classes accuracy: 88.0%
--------------------------------------
Minibatch loss at step 12000: 0.364354
Minibatch classes accuracy: 80.0%
Validation classes accuracy: 85.9%
--------------------------------------
Minibatch loss at step 13000: 0.839202
Minibatch classes accuracy: 63.3%
Validation classes accuracy: 80.3%
--------------------------------------
Minibatch loss at step 14000: 0.916968
Minibatch classes accuracy: 70.0%
Validation classes accuracy: 89.3%
--------------------------------------
Minibatch loss at step 15000: 0.636159
Minibatch classes accuracy: 76.7%
Validation classes accuracy: 89.2%
--------------------------------------
Minibatch loss at step 16000: 0.422737
Minibatch classes accuracy: 86.7%
Validation classes accuracy: 89.3%
--------------------------------------
Minibatch loss at step 17000: 0.502701
Minibatch classes accuracy: 86.7%
Validation classes accuracy: 89.1%
--------------------------------------
Minibatch loss at step 18000: 0.444250
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 92.0%
--------------------------------------
Minibatch loss at step 19000: 0.319701
Minibatch classes accuracy: 86.7%
Validation classes accuracy: 92.3%
--------------------------------------
Minibatch loss at step 20000: 0.133573
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 91.5%
--------------------------------------
Minibatch loss at step 21000: 0.184367
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 94.3%
--------------------------------------
Minibatch loss at step 22000: 0.364807
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 93.7%
--------------------------------------
Minibatch loss at step 23000: 0.468114
Minibatch classes accuracy: 80.0%
Validation classes accuracy: 82.3%
--------------------------------------
Minibatch loss at step 24000: 0.139240
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 95.1%
--------------------------------------
Minibatch loss at step 25000: 0.145877
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 94.1%
--------------------------------------
Minibatch loss at step 26000: 0.308767
Minibatch classes accuracy: 90.0%
Validation classes accuracy: 93.7%
--------------------------------------
Minibatch loss at step 27000: 0.212211
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 95.1%
--------------------------------------
Minibatch loss at step 28000: 0.194873
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 94.8%
--------------------------------------
Minibatch loss at step 29000: 0.546270
Minibatch classes accuracy: 80.0%
Validation classes accuracy: 87.4%
--------------------------------------
Minibatch loss at step 30000: 0.341678
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 93.3%
--------------------------------------
Minibatch loss at step 31000: 0.356959
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 94.7%
--------------------------------------
Minibatch loss at step 32000: 0.433823
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 91.2%
--------------------------------------
Minibatch loss at step 33000: 0.351649
Minibatch classes accuracy: 83.3%
Validation classes accuracy: 94.4%
--------------------------------------
Minibatch loss at step 34000: 0.104025
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 94.5%
--------------------------------------
Minibatch loss at step 35000: 0.066922
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 95.0%
--------------------------------------
Minibatch loss at step 36000: 0.464043
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 95.7%
--------------------------------------
Minibatch loss at step 37000: 0.156272
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 95.5%
--------------------------------------
Minibatch loss at step 38000: 0.141843
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 95.6%
--------------------------------------
Minibatch loss at step 39000: 0.292136
Minibatch classes accuracy: 90.0%
Validation classes accuracy: 96.1%
--------------------------------------
Minibatch loss at step 40000: 0.213317
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 95.7%
--------------------------------------
Minibatch loss at step 41000: 0.204817
Minibatch classes accuracy: 90.0%
Validation classes accuracy: 96.2%
--------------------------------------
Minibatch loss at step 42000: 0.500784
Minibatch classes accuracy: 80.0%
Validation classes accuracy: 95.7%
--------------------------------------
Minibatch loss at step 43000: 0.102489
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 95.6%
--------------------------------------
Minibatch loss at step 44000: 0.042750
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 96.9%
--------------------------------------
Minibatch loss at step 45000: 0.024540
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.1%
--------------------------------------
Minibatch loss at step 46000: 0.031915
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 96.8%
--------------------------------------
Minibatch loss at step 47000: 0.125071
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.0%
--------------------------------------
Minibatch loss at step 48000: 0.168428
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 95.8%
--------------------------------------
Minibatch loss at step 49000: 0.526142
Minibatch classes accuracy: 86.7%
Validation classes accuracy: 95.9%
--------------------------------------
Minibatch loss at step 50000: 0.050478
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 96.4%
--------------------------------------
Minibatch loss at step 51000: 0.047938
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.5%
--------------------------------------
Minibatch loss at step 52000: 0.036825
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 96.8%
--------------------------------------
Minibatch loss at step 53000: 0.277455
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 96.7%
--------------------------------------
Minibatch loss at step 54000: 0.108056
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 96.5%
--------------------------------------
Minibatch loss at step 55000: 0.203545
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 96.9%
--------------------------------------
Minibatch loss at step 56000: 0.035445
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.1%
--------------------------------------
Minibatch loss at step 57000: 0.017905
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.1%
--------------------------------------
Minibatch loss at step 58000: 0.006771
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.3%
--------------------------------------
Minibatch loss at step 59000: 0.291525
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.5%
--------------------------------------
Minibatch loss at step 60000: 0.056823
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.8%
--------------------------------------
Minibatch loss at step 61000: 0.115988
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 96.6%
--------------------------------------
Minibatch loss at step 62000: 0.083699
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.4%
--------------------------------------
Minibatch loss at step 63000: 0.036018
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.2%
--------------------------------------
Minibatch loss at step 64000: 0.101768
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 65000: 0.026908
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.5%
--------------------------------------
Minibatch loss at step 66000: 0.070501
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 67000: 0.039330
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.8%
--------------------------------------
Minibatch loss at step 68000: 0.104845
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.2%
--------------------------------------
Minibatch loss at step 69000: 0.032782
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.8%
--------------------------------------
Minibatch loss at step 70000: 0.127962
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 71000: 0.079311
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.7%
--------------------------------------
Minibatch loss at step 72000: 0.059389
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 73000: 0.028471
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.8%
--------------------------------------
Minibatch loss at step 74000: 0.131517
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.7%
--------------------------------------
Minibatch loss at step 75000: 0.013847
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 76000: 0.086757
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.3%
--------------------------------------
Minibatch loss at step 77000: 0.189925
Minibatch classes accuracy: 90.0%
Validation classes accuracy: 96.6%
--------------------------------------
Minibatch loss at step 78000: 0.022187
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 79000: 0.046150
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.5%
--------------------------------------
Minibatch loss at step 80000: 0.002929
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 96.9%
--------------------------------------
Minibatch loss at step 81000: 0.006596
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 82000: 0.044712
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.5%
--------------------------------------
Minibatch loss at step 83000: 0.030936
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 84000: 0.271206
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 96.5%
--------------------------------------
Minibatch loss at step 85000: 0.081385
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 86000: 0.015044
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 87000: 0.031906
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.4%
--------------------------------------
Minibatch loss at step 88000: 0.052502
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.5%
--------------------------------------
Minibatch loss at step 89000: 0.094608
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.7%
--------------------------------------
Minibatch loss at step 90000: 0.002354
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 97.8%
--------------------------------------
Minibatch loss at step 91000: 0.037515
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.5%
--------------------------------------
Minibatch loss at step 92000: 0.153691
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 97.9%
--------------------------------------
Minibatch loss at step 93000: 0.028288
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.5%
--------------------------------------
Minibatch loss at step 94000: 0.047656
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 95000: 0.118123
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.0%
--------------------------------------
Minibatch loss at step 96000: 0.030221
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.5%
--------------------------------------
Minibatch loss at step 97000: 0.072033
Minibatch classes accuracy: 96.7%
Validation classes accuracy: 98.1%
--------------------------------------
Minibatch loss at step 98000: 0.081720
Minibatch classes accuracy: 93.3%
Validation classes accuracy: 98.3%
--------------------------------------
Minibatch loss at step 99000: 0.010602
Minibatch classes accuracy: 100.0%
Validation classes accuracy: 98.7%
--------------------------------------
Test classes accuracy: 98.1%
Excution time: 33.60min
Model saved in file: model.ckpt

Joint Training


In [21]:
nm_classes = 25
nm_angles = 10
num_channels = 1 

batch_size = 30
patch_size = 5
kernel_size = 2
depth1 = 6
depth2 = 16 
depth3 = 10 
F7_classes = 120
F8_classes = 84
F9_classes = nm_classes
F7_angles = 120
F8_angles = 84
F9_angles = nm_angles

keep_prob1 = 0.5
keep_prob2_classes = 0.5
keep_prob2_angles = 0.8

graph = tf.Graph()

with graph.as_default():
    # Input data
    tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
    # convolution's input is a tensor of shape [batch,in_height,in_width,in_channels]
    tf_train_classes = tf.placeholder(tf.float32, shape=(batch_size, nm_classes))
    tf_train_angles = tf.placeholder(tf.float32, shape=(batch_size, nm_angles))
    
    tf_valid_dataset = tf.constant(o_valid_dataset)
    tf_test_dataset = tf.constant(o_test_dataset)
    
    # Variables(weights and biases)
    C1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth1], stddev=0.1))
    # convolution's weights are called filter in tensorflow
    # it is a tensor of shape [kernel_hight,kernel_width,in_channels,out_channels]
    C1_biases = tf.Variable(tf.zeros([depth1]))
                            
    # S1_weights # Sub-sampling doesn't need weights and biases
    # S1_biases
    
    C3_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth1, depth2], stddev=0.1))
    C3_biases = tf.Variable(tf.constant(1.0, shape=[depth2]))
                            
    # S4_weights
    # S4_biases
     
    C5_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth2, depth3], stddev=0.1))
    C5_biases = tf.Variable(tf.constant(1.0, shape=[depth3]))
    
    # S6_weights
    # S6_biases
    
    F7_classes_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_classes], stddev=0.1))
    F7_classes_biases = tf.Variable(tf.constant(1.0, shape=[F7_classes]))
    F7_angles_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_angles], stddev=0.1))
    F7_angles_biases = tf.Variable(tf.constant(1.0, shape=[F7_angles]))
    
    F8_classes_weights = tf.Variable(tf.truncated_normal([F7_classes,F8_classes], stddev=0.1))
    F8_classes_biases = tf.Variable(tf.constant(1.0, shape=[F8_classes]))
    F8_angles_weights = tf.Variable(tf.truncated_normal([F7_angles,F8_angles], stddev=0.1))
    F8_angles_biases = tf.Variable(tf.constant(1.0, shape=[F8_angles]))
    
    F9_classes_weights = tf.Variable(tf.truncated_normal([F8_classes,F9_classes], stddev=0.1))
    F9_classes_biases = tf.Variable(tf.constant(1.0, shape=[F9_classes]))
    F9_angles_weights = tf.Variable(tf.truncated_normal([F8_angles,F9_angles], stddev=0.1))
    F9_angles_biases = tf.Variable(tf.constant(1.0, shape=[F9_angles]))
    
    saver = tf.train.Saver()
    # Model
    def train_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        
        hidden_input = tf.nn.dropout(reshape,keep_prob1) # dropout on the input layer
        
        hidden_classes = tf.nn.relu(tf.matmul(hidden_input, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(hidden_input, F7_angles_weights) + F7_angles_biases)
        
        hidden_classes = tf.nn.dropout(hidden_classes, keep_prob2_classes) # dropout on the first FC layer
        hidden_angles = tf.nn.dropout(hidden_angles, keep_prob2_angles)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    def test_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        hidden_classes = tf.nn.relu(tf.matmul(reshape, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(reshape, F7_angles_weights) + F7_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    # Training computation.
    logits_classes, logits_angles = train_model(tf_train_dataset)
    loss_classes = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_classes, tf_train_classes))
    loss_angles = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_angles, tf_train_angles))
    loss =   loss_angles + loss_classes 
    
    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.002).minimize(loss)
  
    # Predictions for the training, validation, and test data.
    train_classes_logits, train_angles_logits = test_model(tf_train_dataset)
    train_classes_prediction = tf.nn.softmax(logits_classes)
    train_angles_prediction = tf.nn.softmax(logits_angles)
    
    valid_classes_logits, valid_angles_logits = test_model(tf_valid_dataset)
    valid_classes_prediction = tf.nn.softmax(valid_classes_logits)
    valid_angles_prediction = tf.nn.softmax(valid_angles_logits)
    
    test_classes_logits, test_angles_logits = test_model(tf_test_dataset)
    test_classes_prediction = tf.nn.softmax(test_classes_logits)
    test_angles_prediction = tf.nn.softmax(test_angles_logits)
    
num_steps = 80000
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
config.log_device_placement = True  
start_time = time.time()
with tf.Session(graph=graph, config = config) as session:
  saver.restore(session, "model.ckpt")
  print("Model restored.")
  for step in range(num_steps):
    offset = (step * batch_size) % (o_train_classes.shape[0] - batch_size)
    batch_data = o_train_dataset[offset:(offset + batch_size), :, :, :]
    batch_classes = o_train_classes[offset:(offset + batch_size), :]
    batch_angles = o_train_angles[offset:(offset + batch_size), :]
    feed_dict = {tf_train_dataset : batch_data, tf_train_classes : batch_classes, tf_train_angles : batch_angles}
    _, l, classes_predictions, angles_predictions = session.run(
      [optimizer, loss, train_classes_prediction, train_angles_prediction], feed_dict=feed_dict)
    if (step % 1000 == 0):
      print('Minibatch loss at step %d: %f' % (step, l))
      print('Minibatch classes accuracy: %.1f%%' % accuracy_classes(classes_predictions, batch_classes))
      print('Minibatch angles accuracy: %.1f%%' % accuracy_angles(angles_predictions, batch_angles))
      print('Validation classes accuracy: %.1f%%' % accuracy_classes(valid_classes_prediction.eval(), o_valid_classes))
      print('Validation angles accuracy: %.1f%%' % accuracy_angles(valid_angles_prediction.eval(), o_valid_angles))
      print('--------------------------------------')
  print('Test classes accuracy: %.1f%%' % accuracy_classes(test_classes_prediction.eval(), o_test_classes))
  print('Test angles accuracy: %.1f%%' % accuracy_angles(test_angles_prediction.eval(), o_test_angles))
  end_time = time.time()
  duration = (end_time - start_time)/60
  print("Excution time: %0.2fmin" % duration)
  save_path = saver.save(session, "model1.ckpt")
  print("Model saved in file: %s" % save_path)


Model restored.
Minibatch loss at step 0: 5.963478
Minibatch classes accuracy: 66.7%
Minibatch angles accuracy: 10.0%
Validation classes accuracy: 98.3%
Validation angles accuracy: 12.4%
--------------------------------------
Minibatch loss at step 1000: 2.889675
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 10.0%
Validation classes accuracy: 97.2%
Validation angles accuracy: 28.4%
--------------------------------------
Minibatch loss at step 2000: 2.659708
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 30.0%
Validation classes accuracy: 96.2%
Validation angles accuracy: 37.1%
--------------------------------------
Minibatch loss at step 3000: 2.274526
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 30.0%
Validation classes accuracy: 97.0%
Validation angles accuracy: 41.8%
--------------------------------------
Minibatch loss at step 4000: 2.800834
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 13.3%
Validation classes accuracy: 96.6%
Validation angles accuracy: 44.4%
--------------------------------------
Minibatch loss at step 5000: 2.100690
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 30.0%
Validation classes accuracy: 97.3%
Validation angles accuracy: 45.0%
--------------------------------------
Minibatch loss at step 6000: 2.342364
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 43.3%
Validation classes accuracy: 96.4%
Validation angles accuracy: 47.6%
--------------------------------------
Minibatch loss at step 7000: 1.808241
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 43.3%
Validation classes accuracy: 96.1%
Validation angles accuracy: 50.3%
--------------------------------------
Minibatch loss at step 8000: 2.077463
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 33.3%
Validation classes accuracy: 96.2%
Validation angles accuracy: 51.6%
--------------------------------------
Minibatch loss at step 9000: 2.028700
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 36.7%
Validation classes accuracy: 96.4%
Validation angles accuracy: 54.6%
--------------------------------------
Minibatch loss at step 10000: 2.305450
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 33.3%
Validation classes accuracy: 95.3%
Validation angles accuracy: 53.9%
--------------------------------------
Minibatch loss at step 11000: 2.130649
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 36.7%
Validation classes accuracy: 95.8%
Validation angles accuracy: 57.5%
--------------------------------------
Minibatch loss at step 12000: 2.265357
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 36.7%
Validation classes accuracy: 96.4%
Validation angles accuracy: 58.7%
--------------------------------------
Minibatch loss at step 13000: 1.833302
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 50.0%
Validation classes accuracy: 97.3%
Validation angles accuracy: 60.3%
--------------------------------------
Minibatch loss at step 14000: 2.172452
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 50.0%
Validation classes accuracy: 96.2%
Validation angles accuracy: 61.9%
--------------------------------------
Minibatch loss at step 15000: 1.629906
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 97.3%
Validation angles accuracy: 62.7%
--------------------------------------
Minibatch loss at step 16000: 2.022757
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 95.8%
Validation angles accuracy: 62.9%
--------------------------------------
Minibatch loss at step 17000: 2.270527
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 36.7%
Validation classes accuracy: 95.4%
Validation angles accuracy: 65.4%
--------------------------------------
Minibatch loss at step 18000: 1.501388
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 63.3%
Validation classes accuracy: 96.4%
Validation angles accuracy: 66.9%
--------------------------------------
Minibatch loss at step 19000: 1.985760
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 50.0%
Validation classes accuracy: 96.7%
Validation angles accuracy: 67.3%
--------------------------------------
Minibatch loss at step 20000: 1.509893
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 97.0%
Validation angles accuracy: 68.1%
--------------------------------------
Minibatch loss at step 21000: 1.612223
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 46.7%
Validation classes accuracy: 96.2%
Validation angles accuracy: 70.0%
--------------------------------------
Minibatch loss at step 22000: 1.707777
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 33.3%
Validation classes accuracy: 96.4%
Validation angles accuracy: 70.8%
--------------------------------------
Minibatch loss at step 23000: 1.564596
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 97.5%
Validation angles accuracy: 72.3%
--------------------------------------
Minibatch loss at step 24000: 2.104582
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 46.7%
Validation classes accuracy: 97.6%
Validation angles accuracy: 73.1%
--------------------------------------
Minibatch loss at step 25000: 1.652917
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 97.2%
Validation angles accuracy: 71.8%
--------------------------------------
Minibatch loss at step 26000: 1.171622
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 95.6%
Validation angles accuracy: 73.2%
--------------------------------------
Minibatch loss at step 27000: 1.350838
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 43.3%
Validation classes accuracy: 96.9%
Validation angles accuracy: 76.2%
--------------------------------------
Minibatch loss at step 28000: 1.861501
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 93.9%
Validation angles accuracy: 73.5%
--------------------------------------
Minibatch loss at step 29000: 1.906242
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 46.7%
Validation classes accuracy: 96.4%
Validation angles accuracy: 74.8%
--------------------------------------
Minibatch loss at step 30000: 1.292266
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.4%
Validation angles accuracy: 75.8%
--------------------------------------
Minibatch loss at step 31000: 1.636071
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 96.7%
Validation angles accuracy: 77.4%
--------------------------------------
Minibatch loss at step 32000: 1.113985
Minibatch classes accuracy: 93.3%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 96.7%
Validation angles accuracy: 77.1%
--------------------------------------
Minibatch loss at step 33000: 1.375087
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 97.0%
Validation angles accuracy: 77.0%
--------------------------------------
Minibatch loss at step 34000: 1.506069
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 94.7%
Validation angles accuracy: 76.9%
--------------------------------------
Minibatch loss at step 35000: 1.699621
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 97.3%
Validation angles accuracy: 78.4%
--------------------------------------
Minibatch loss at step 36000: 1.778811
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 43.3%
Validation classes accuracy: 95.6%
Validation angles accuracy: 79.1%
--------------------------------------
Minibatch loss at step 37000: 1.471088
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 96.6%
Validation angles accuracy: 81.2%
--------------------------------------
Minibatch loss at step 38000: 1.846522
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 96.2%
Validation angles accuracy: 78.5%
--------------------------------------
Minibatch loss at step 39000: 1.492639
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 96.0%
Validation angles accuracy: 80.3%
--------------------------------------
Minibatch loss at step 40000: 1.640616
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 94.8%
Validation angles accuracy: 80.5%
--------------------------------------
Minibatch loss at step 41000: 1.267297
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 97.0%
Validation angles accuracy: 82.0%
--------------------------------------
Minibatch loss at step 42000: 1.578387
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 43.3%
Validation classes accuracy: 96.2%
Validation angles accuracy: 80.4%
--------------------------------------
Minibatch loss at step 43000: 1.522158
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 96.8%
Validation angles accuracy: 80.9%
--------------------------------------
Minibatch loss at step 44000: 1.582261
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 50.0%
Validation classes accuracy: 97.2%
Validation angles accuracy: 82.6%
--------------------------------------
Minibatch loss at step 45000: 1.126304
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 96.6%
Validation angles accuracy: 83.2%
--------------------------------------
Minibatch loss at step 46000: 1.464939
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.1%
Validation angles accuracy: 81.3%
--------------------------------------
Minibatch loss at step 47000: 1.113444
Minibatch classes accuracy: 96.7%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 96.8%
Validation angles accuracy: 83.1%
--------------------------------------
Minibatch loss at step 48000: 1.044989
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.6%
Validation angles accuracy: 82.8%
--------------------------------------
Minibatch loss at step 49000: 1.209431
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 96.4%
Validation angles accuracy: 83.1%
--------------------------------------
Minibatch loss at step 50000: 1.441586
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 96.7%
Validation angles accuracy: 84.2%
--------------------------------------
Minibatch loss at step 51000: 1.272110
Minibatch classes accuracy: 96.7%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 96.6%
Validation angles accuracy: 85.2%
--------------------------------------
Minibatch loss at step 52000: 1.261072
Minibatch classes accuracy: 96.7%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 96.0%
Validation angles accuracy: 84.9%
--------------------------------------
Minibatch loss at step 53000: 1.167302
Minibatch classes accuracy: 93.3%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 95.7%
Validation angles accuracy: 83.6%
--------------------------------------
Minibatch loss at step 54000: 1.402131
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 93.4%
Validation angles accuracy: 83.8%
--------------------------------------
Minibatch loss at step 55000: 1.378647
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 92.4%
Validation angles accuracy: 82.3%
--------------------------------------
Minibatch loss at step 56000: 1.190618
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 96.0%
Validation angles accuracy: 85.7%
--------------------------------------
Minibatch loss at step 57000: 0.940521
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 83.3%
Validation classes accuracy: 96.3%
Validation angles accuracy: 85.2%
--------------------------------------
Minibatch loss at step 58000: 0.959072
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 96.9%
Validation angles accuracy: 85.9%
--------------------------------------
Minibatch loss at step 59000: 1.404709
Minibatch classes accuracy: 93.3%
Minibatch angles accuracy: 53.3%
Validation classes accuracy: 97.1%
Validation angles accuracy: 85.5%
--------------------------------------
Minibatch loss at step 60000: 1.079811
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.7%
Validation angles accuracy: 87.5%
--------------------------------------
Minibatch loss at step 61000: 1.129885
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 95.5%
Validation angles accuracy: 85.7%
--------------------------------------
Minibatch loss at step 62000: 1.126426
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 96.4%
Validation angles accuracy: 86.2%
--------------------------------------
Minibatch loss at step 63000: 1.246542
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 97.2%
Validation angles accuracy: 86.8%
--------------------------------------
Minibatch loss at step 64000: 1.452992
Minibatch classes accuracy: 76.7%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 95.7%
Validation angles accuracy: 86.8%
--------------------------------------
Minibatch loss at step 65000: 1.723482
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 50.0%
Validation classes accuracy: 96.3%
Validation angles accuracy: 87.9%
--------------------------------------
Minibatch loss at step 66000: 1.167270
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 95.9%
Validation angles accuracy: 86.4%
--------------------------------------
Minibatch loss at step 67000: 1.178674
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 83.3%
Validation classes accuracy: 97.2%
Validation angles accuracy: 86.8%
--------------------------------------
Minibatch loss at step 68000: 1.185628
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 56.7%
Validation classes accuracy: 97.2%
Validation angles accuracy: 88.6%
--------------------------------------
Minibatch loss at step 69000: 0.846219
Minibatch classes accuracy: 93.3%
Minibatch angles accuracy: 80.0%
Validation classes accuracy: 94.8%
Validation angles accuracy: 88.5%
--------------------------------------
Minibatch loss at step 70000: 0.996331
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 66.7%
Validation classes accuracy: 95.6%
Validation angles accuracy: 88.7%
--------------------------------------
Minibatch loss at step 71000: 0.919983
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 96.6%
Validation angles accuracy: 88.5%
--------------------------------------
Minibatch loss at step 72000: 1.177838
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.7%
Validation angles accuracy: 88.0%
--------------------------------------
Minibatch loss at step 73000: 1.231144
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 96.2%
Validation angles accuracy: 88.1%
--------------------------------------
Minibatch loss at step 74000: 1.211080
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 60.0%
Validation classes accuracy: 95.6%
Validation angles accuracy: 88.5%
--------------------------------------
Minibatch loss at step 75000: 1.231600
Minibatch classes accuracy: 86.7%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 96.8%
Validation angles accuracy: 89.6%
--------------------------------------
Minibatch loss at step 76000: 1.229326
Minibatch classes accuracy: 90.0%
Minibatch angles accuracy: 73.3%
Validation classes accuracy: 96.3%
Validation angles accuracy: 89.9%
--------------------------------------
Minibatch loss at step 77000: 1.543080
Minibatch classes accuracy: 80.0%
Minibatch angles accuracy: 70.0%
Validation classes accuracy: 95.8%
Validation angles accuracy: 88.9%
--------------------------------------
Minibatch loss at step 78000: 0.985912
Minibatch classes accuracy: 73.3%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 93.1%
Validation angles accuracy: 86.3%
--------------------------------------
Minibatch loss at step 79000: 1.056902
Minibatch classes accuracy: 83.3%
Minibatch angles accuracy: 76.7%
Validation classes accuracy: 96.1%
Validation angles accuracy: 89.6%
--------------------------------------
Test classes accuracy: 95.2%
Test angles accuracy: 91.2%
Excution time: 27.28min
Model saved in file: model1.ckpt

Fine Training


In [43]:
angles_list = np.asarray([i*18 + angle_bias for i in range(10)]).astype(np.float32)

def RMSE_angles(predictions, labels):
  '''Root Mean Squared Error'''
  prd_angles = np.sum(np.multiply(predictions, angles_list),axis=1)/np.sum(predictions,axis=1)
  true_angles = np.sum(np.multiply(labels, angles_list),axis=1)
  return np.sqrt(np.mean((prd_angles-true_angles)**2))

In [67]:
nm_classes = 25
nm_angles = 10
num_channels = 1 

batch_size = 30
patch_size = 5
kernel_size = 2
depth1 = 6
depth2 = 16 
depth3 = 10 
F7_classes = 120
F8_classes = 84
F9_classes = nm_classes
F7_angles = 120
F8_angles = 84
F9_angles = nm_angles

keep_prob1 = 0.5
keep_prob2_classes = 0.8
keep_prob2_angles = 0.3

graph = tf.Graph()

with graph.as_default():
    # Input data
    tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
    # convolution's input is a tensor of shape [batch,in_height,in_width,in_channels]
    tf_train_classes = tf.placeholder(tf.float32, shape=(batch_size, nm_classes))
    tf_train_angles = tf.placeholder(tf.float32, shape=(batch_size, nm_angles))
    
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)
    
    # Variables(weights and biases)
    C1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth1], stddev=0.1))
    # convolution's weights are called filter in tensorflow
    # it is a tensor of shape [kernel_hight,kernel_width,in_channels,out_channels]
    C1_biases = tf.Variable(tf.zeros([depth1]))
                            
    # S1_weights # Sub-sampling doesn't need weights and biases
    # S1_biases
    
    C3_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth1, depth2], stddev=0.1))
    C3_biases = tf.Variable(tf.constant(1.0, shape=[depth2]))
                            
    # S4_weights
    # S4_biases
     
    C5_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth2, depth3], stddev=0.1))
    C5_biases = tf.Variable(tf.constant(1.0, shape=[depth3]))
    
    # S6_weights
    # S6_biases
    
    F7_classes_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_classes], stddev=0.1))
    F7_classes_biases = tf.Variable(tf.constant(1.0, shape=[F7_classes]))
    F7_angles_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_angles], stddev=0.1))
    F7_angles_biases = tf.Variable(tf.constant(1.0, shape=[F7_angles]))
    
    F8_classes_weights = tf.Variable(tf.truncated_normal([F7_classes,F8_classes], stddev=0.1))
    F8_classes_biases = tf.Variable(tf.constant(1.0, shape=[F8_classes]))
    F8_angles_weights = tf.Variable(tf.truncated_normal([F7_angles,F8_angles], stddev=0.1))
    F8_angles_biases = tf.Variable(tf.constant(1.0, shape=[F8_angles]))
    
    F9_classes_weights = tf.Variable(tf.truncated_normal([F8_classes,F9_classes], stddev=0.1))
    F9_classes_biases = tf.Variable(tf.constant(1.0, shape=[F9_classes]))
    F9_angles_weights = tf.Variable(tf.truncated_normal([F8_angles,F9_angles], stddev=0.1))
    F9_angles_biases = tf.Variable(tf.constant(1.0, shape=[F9_angles]))
    
    saver = tf.train.Saver()
    # Model
    def train_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        
        hidden_input = tf.nn.dropout(reshape,keep_prob1) # dropout on the input layer
        
        hidden_classes = tf.nn.relu(tf.matmul(hidden_input, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(hidden_input, F7_angles_weights) + F7_angles_biases)
        
        hidden_classes = tf.nn.dropout(hidden_classes, keep_prob2_classes) # dropout on the first FC layer
        hidden_angles = tf.nn.dropout(hidden_angles, keep_prob2_angles)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    def test_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        hidden_classes = tf.nn.relu(tf.matmul(reshape, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(reshape, F7_angles_weights) + F7_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    # Training computation.
    logits_classes, logits_angles = train_model(tf_train_dataset)
    loss_classes = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_classes, tf_train_classes))
    loss_angles = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_angles, tf_train_angles))
    loss =   loss_classes + 0.2*loss_angles
    
    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
  
    # Predictions for the training, validation, and test data.
    train_classes_logits, train_angles_logits = test_model(tf_train_dataset)
    train_classes_prediction = tf.nn.softmax(logits_classes)
    train_angles_prediction = tf.nn.softmax(logits_angles)
    
    valid_classes_logits, valid_angles_logits = test_model(tf_valid_dataset)
    valid_classes_prediction = tf.nn.softmax(valid_classes_logits)
    valid_angles_prediction = tf.nn.softmax(valid_angles_logits)
    
    test_classes_logits, test_angles_logits = test_model(tf_test_dataset)
    test_classes_prediction = tf.nn.softmax(test_classes_logits)
    test_angles_prediction = tf.nn.softmax(test_angles_logits)
    
num_steps = 2000
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
config.log_device_placement = True  
start_time = time.time()
with tf.Session(graph=graph, config = config) as session:
  saver.restore(session, "model1.ckpt")
  print("Model restored.")
  for step in range(num_steps):
    offset = (step * batch_size) % (train_classes.shape[0] - batch_size)
    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
    batch_classes = train_classes[offset:(offset + batch_size), :]
    batch_angles = train_angles[offset:(offset + batch_size), :]
    feed_dict = {tf_train_dataset : batch_data, tf_train_classes : batch_classes, tf_train_angles : batch_angles}
    _, l, classes_predictions, angles_predictions = session.run(
      [optimizer, loss, train_classes_prediction, train_angles_prediction], feed_dict=feed_dict)
    if (step % 1000 == 0):
      print('Minibatch loss at step %d: %f' % (step, l))
      print('Minibatch classes accuracy: %.1f%%' % accuracy_classes(classes_predictions, batch_classes))
      print('Minibatch RMSE of angles: %.1f' % RMSE_angles(angles_predictions, batch_angles))
      print('Validation classes accuracy: %.1f%%' % accuracy_classes(valid_classes_prediction.eval(), valid_classes))
      print('Validation RMSE of angles: %.1f' % RMSE_angles(valid_angles_prediction.eval(), valid_angles))
      print('--------------------------------------')
  print('Test classes accuracy: %.1f%%' % accuracy_classes(test_classes_prediction.eval(), test_classes))
  print('Test RMSE of accuracy: %.1f' % RMSE_angles(test_angles_prediction.eval(), test_angles))
  end_time = time.time()
  duration = (end_time - start_time)/60
  print("Excution time: %0.2fmin" % duration)
  save_path = saver.save(session, "model2.ckpt")
  print("Model saved in file: %s" % save_path)


Model restored.
Minibatch loss at step 0: 4.923201
Minibatch classes accuracy: 50.0%
Minibatch RMSE of angles: 42.9
Validation classes accuracy: 66.1%
Validation RMSE of angles: 34.9
--------------------------------------
Minibatch loss at step 1000: 0.786114
Minibatch classes accuracy: 90.0%
Minibatch RMSE of angles: 43.1
Validation classes accuracy: 97.2%
Validation RMSE of angles: 34.6
--------------------------------------
Test classes accuracy: 97.7%
Test RMSE of accuracy: 32.7
Excution time: 0.70min
Model saved in file: model2.ckpt

6. Test Visualization


In [68]:
nm_classes = 25
nm_angles = 10
num_channels = 1 

batch_size = 30
patch_size = 5
kernel_size = 2
depth1 = 6
depth2 = 16 
depth3 = 10 
F7_classes = 120
F8_classes = 84
F9_classes = nm_classes
F7_angles = 120
F8_angles = 84
F9_angles = nm_angles

keep_prob1 = 0.5
keep_prob2_classes = 0.8
keep_prob2_angles = 0.5

graph = tf.Graph()

with graph.as_default():
    # Input data
    tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
    # convolution's input is a tensor of shape [batch,in_height,in_width,in_channels]
    tf_train_classes = tf.placeholder(tf.float32, shape=(batch_size, nm_classes))
    tf_train_angles = tf.placeholder(tf.float32, shape=(batch_size, nm_angles))
    
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)
    
    # Variables(weights and biases)
    C1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth1], stddev=0.1))
    # convolution's weights are called filter in tensorflow
    # it is a tensor of shape [kernel_hight,kernel_width,in_channels,out_channels]
    C1_biases = tf.Variable(tf.zeros([depth1]))
                            
    # S1_weights # Sub-sampling doesn't need weights and biases
    # S1_biases
    
    C3_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth1, depth2], stddev=0.1))
    C3_biases = tf.Variable(tf.constant(1.0, shape=[depth2]))
                            
    # S4_weights
    # S4_biases
     
    C5_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth2, depth3], stddev=0.1))
    C5_biases = tf.Variable(tf.constant(1.0, shape=[depth3]))
    
    # S6_weights
    # S6_biases
    
    F7_classes_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_classes], stddev=0.1))
    F7_classes_biases = tf.Variable(tf.constant(1.0, shape=[F7_classes]))
    F7_angles_weights = tf.Variable(tf.truncated_normal([6 * 6 * depth3, F7_angles], stddev=0.1))
    F7_angles_biases = tf.Variable(tf.constant(1.0, shape=[F7_angles]))
    
    F8_classes_weights = tf.Variable(tf.truncated_normal([F7_classes,F8_classes], stddev=0.1))
    F8_classes_biases = tf.Variable(tf.constant(1.0, shape=[F8_classes]))
    F8_angles_weights = tf.Variable(tf.truncated_normal([F7_angles,F8_angles], stddev=0.1))
    F8_angles_biases = tf.Variable(tf.constant(1.0, shape=[F8_angles]))
    
    F9_classes_weights = tf.Variable(tf.truncated_normal([F8_classes,F9_classes], stddev=0.1))
    F9_classes_biases = tf.Variable(tf.constant(1.0, shape=[F9_classes]))
    F9_angles_weights = tf.Variable(tf.truncated_normal([F8_angles,F9_angles], stddev=0.1))
    F9_angles_biases = tf.Variable(tf.constant(1.0, shape=[F9_angles]))
    
    saver = tf.train.Saver()
    # Model
    def train_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        
        hidden_input = tf.nn.dropout(reshape,keep_prob1) # dropout on the input layer
        
        hidden_classes = tf.nn.relu(tf.matmul(hidden_input, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(hidden_input, F7_angles_weights) + F7_angles_biases)
        
        hidden_classes = tf.nn.dropout(hidden_classes, keep_prob2_classes) # dropout on the first FC layer
        hidden_angles = tf.nn.dropout(hidden_angles, keep_prob2_angles)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    def test_model(data):
        conv = tf.nn.conv2d(data, C1_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C1_biases)
        
        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
                                
        conv = tf.nn.conv2d(hidden, C3_weights, [1, 1, 1, 1], padding='VALID')
        hidden = tf.nn.relu(conv + C3_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)
        
        conv = tf.nn.conv2d(hidden,C5_weights, [1,1,1,1], padding = 'VALID')
        hidden = tf.nn.relu(conv + C5_biases)

        max_pool = tf.nn.max_pool(hidden,[1,kernel_size,kernel_size,1],[1,2,2,1],'VALID')
        hidden = tf.nn.relu(max_pool)

        shape = hidden.get_shape().as_list()
        reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
        hidden_classes = tf.nn.relu(tf.matmul(reshape, F7_classes_weights) + F7_classes_biases)
        hidden_angles = tf.nn.relu(tf.matmul(reshape, F7_angles_weights) + F7_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F8_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F8_angles_weights)
        hidden_classes = tf.nn.relu(fc_classes + F8_classes_biases)
        hidden_angles = tf.nn.relu(fc_angles + F8_angles_biases)
        
        fc_classes = tf.matmul(hidden_classes,F9_classes_weights)
        fc_angles = tf.matmul(hidden_angles,F9_angles_weights)
        output_classes = fc_classes + F9_classes_biases
        output_angles = fc_angles + F9_angles_biases
    
        return output_classes, output_angles
    
    # Training computation.
    logits_classes, logits_angles = train_model(tf_train_dataset)
    loss_classes = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_classes, tf_train_classes))
    loss_angles = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits_angles, tf_train_angles))
    loss =   loss_angles + loss_classes 
    
    # Optimizer.
    optimizer = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
  
    # Predictions for the training, validation, and test data.
    train_classes_logits, train_angles_logits = test_model(tf_train_dataset)
    train_classes_prediction = tf.nn.softmax(logits_classes)
    train_angles_prediction = tf.nn.softmax(logits_angles)
    
    valid_classes_logits, valid_angles_logits = test_model(tf_valid_dataset)
    valid_classes_prediction = tf.nn.softmax(valid_classes_logits)
    valid_angles_prediction = tf.nn.softmax(valid_angles_logits)
    
    test_classes_logits, test_angles_logits = test_model(tf_test_dataset)
    test_classes_prediction = tf.nn.softmax(test_classes_logits)
    test_angles_prediction = tf.nn.softmax(test_angles_logits)
    
num_labels = 25
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
config.log_device_placement = True  
with tf.Session(graph=graph, config = config) as session:
  saver.restore(session, "model2.ckpt")
  print("Model restored.")
  print('Test classes accuracy: %.1f%%' % accuracy_classes(test_classes_prediction.eval(), test_classes))
  print('Test RMSE of accuracy: %.1f' % RMSE_angles(test_angles_prediction.eval(), test_angles))
  i_test = 0
  while(i_test!=''):
        i_test = input("Input an index of test image (or Enter to quit): ")
        classes = test_classes[int(i_test),:].tolist()
        #print("Correct label: "+value2name[label.index(1)])
        image = test_dataset[int(i_test),:,:,:].reshape((-1,image_size,image_size,num_channels)).astype(np.float32)
        class_logits,angle_logits = test_model(image)
        class_prediction = tf.nn.softmax(class_logits)
        angle_prediction = tf.nn.softmax(angle_logits).eval()
        angle = np.sum(np.multiply(angle_prediction, angles_list))/np.sum(angle_prediction)
        true_angle = np.sum(np.multiply(test_angles[int(i_test),:], angles_list))
        pre_dict = dict(zip(list(range(num_labels)),class_prediction.eval()[0]))
        sorted_pre_dict = sorted(pre_dict.items(), key=operator.itemgetter(1))
        name1 = value2name[sorted_pre_dict[-1][0]]
        value1 = str(sorted_pre_dict[-1][1])
        name2 = value2name[sorted_pre_dict[-2][0]]
        value2 = str(sorted_pre_dict[-2][1])
        tile = name1+': '+value1+'  '+name2+': '+value2+' angle:'+str(angle)
        image = image.reshape((image_size,image_size)).astype(np.float32)
        plt.imshow(image,cmap='Greys_r')
        plt.suptitle(tile, fontsize=12)
        plt.xlabel(value2name[classes.index(1)]+' '+str(true_angle), fontsize=12)
        plt.show()


Model restored.
Test classes accuracy: 97.7%
Test RMSE of accuracy: 32.7
Input an index of test image (or Enter to quit): 1
Input an index of test image (or Enter to quit): 2
Input an index of test image (or Enter to quit): 3
Input an index of test image (or Enter to quit): 4
Input an index of test image (or Enter to quit): 5
Input an index of test image (or Enter to quit): 6
Input an index of test image (or Enter to quit): 7
Input an index of test image (or Enter to quit): 8
Input an index of test image (or Enter to quit): 9
Input an index of test image (or Enter to quit): 11
Input an index of test image (or Enter to quit): 14
Input an index of test image (or Enter to quit): 15
Input an index of test image (or Enter to quit): 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-68-21e1bc3f89c3> in <module>()
    182   while(i_test!=''):
    183         i_test = input("Input an index of test image (or Enter to quit): ")
--> 184         classes = test_classes[int(i_test),:].tolist()
    185         #print("Correct label: "+value2name[label.index(1)])
    186         image = test_dataset[int(i_test),:,:,:].reshape((-1,image_size,image_size,num_channels)).astype(np.float32)

ValueError: invalid literal for int() with base 10: ''