In [1]:
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import h5py
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation

%matplotlib inline

In [2]:
IMG_WIDTH = 32 # Side for each transformed Image
IMG_HEIGHT = 32
IMG_DEPTH = 1 # RGB files

In [3]:
NUM_LABELS = 2
h5FileName = 'svhn_' + str(NUM_LABELS) + '.h5'

In [4]:
data = h5py.File(h5FileName)
imgs = np.array(data['images']).astype(float)
labels = np.array(data['digits'])

In [5]:
print (type(imgs))
print (labels.shape)
print (imgs.shape)


<type 'numpy.ndarray'>
(56, 2)
(56, 32, 64, 3)

In [6]:
print (labels[0])
plt.imshow(imgs[0])


[3 8]
Out[6]:
<matplotlib.image.AxesImage at 0x7efeb3054f50>

In [7]:
def dense_to_one_hot(labels_dense, num_classes=10):
    """Convert class labels from scalars to one-hot vectors."""
    num_labels = labels_dense.shape[0]
    index_offset = np.arange(num_labels) * num_classes
    labels_one_hot = np.zeros((num_labels, num_classes))
    index_update = [int(x) for x in index_offset + labels_dense.ravel()]
    labels_one_hot.flat[index_update] = 1
    return labels_one_hot

In [8]:
# Get the dataset
X = imgs.reshape([-1, IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH])
Y = labels

In [9]:
# Generate validation set
ratio = 0.9 # Train/Test set
randIdx = np.random.random(imgs.shape[0]) <= ratio
#print (sum(map(lambda x: int(x), randIdx)))
X_train = X[randIdx]
Y_train = Y[randIdx]
X_test = X[randIdx == False]
Y_test = Y[randIdx == False]
Y_train = [dense_to_one_hot(Y_train[:,idx], num_classes= 10) for idx in range(Y_train.shape[1])] 
Y_test = [dense_to_one_hot(Y_test[:,idx], num_classes= 10) for idx in range(Y_test.shape[1])] 
#del X, Y # release some space


/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/ipykernel/__main__.py:5: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 336 but corresponding boolean dimension is 56
/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/ipykernel/__main__.py:7: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 336 but corresponding boolean dimension is 56

In [10]:
print (X_train.shape)
print (Y_train[0].shape)


(48, 32, 32, 1)
(48, 10)

print (np.mean(Y_train[1], axis = 0))


In [11]:
# Building convolutional network

# Building convolutional network
for numLayers in [1,2,3,4,5]: # Num of Conv layer sets to use
    with tf.Graph().as_default():
        
        # Real-time data preprocessing
        img_prep = ImagePreprocessing()
        img_prep.add_featurewise_zero_center()
        img_prep.add_featurewise_stdnorm()

        # Real-time data augmentation
        img_aug = ImageAugmentation()
        #img_aug.add_random_flip_leftright()
        img_aug.add_random_rotation(max_angle=25.)
        input = input_data(shape=[None, IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH], name='input',
                                                                    data_preprocessing=img_prep,
                                                                    data_augmentation=img_aug)

        # Building convolutional network
        x = tflearn.conv_2d(input, 32, 3, activation='relu', name='conv1_1')
        x = tflearn.conv_2d(x, 32, 3, activation='relu', name='conv1_2')
        x = tflearn.max_pool_2d(x, 2, strides=2, name='maxpool1')
        #x = local_response_normalization(x)

        if numLayers >= 2:
            x = tflearn.conv_2d(x, 64, 3, activation='relu', name='conv2_1')
            x = tflearn.conv_2d(x, 64, 3, activation='relu', name='conv2_2')
            x = tflearn.max_pool_2d(x, 2, strides=2, name='maxpool2')
            #x = local_response_normalization(x)
    
        if numLayers >= 3:
            x = tflearn.conv_2d(x, 256, 3, activation='relu', name='conv3_1')
            x = tflearn.conv_2d(x, 256, 3, activation='relu', name='conv3_2')
            x = tflearn.conv_2d(x, 256, 3, activation='relu', name='conv3_3')
            x = tflearn.max_pool_2d(x, 2, strides=2, name='maxpool3')

        if numLayers >= 4:
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv4_1')
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv4_2')
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv4_3')
            x = tflearn.max_pool_2d(x, 2, strides=2, name='maxpool4')

        if numLayers >= 5:
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_1')
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_2')
            x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_3')
            x = tflearn.max_pool_2d(x, 2, strides=2, name='maxpool5')

        # Training heads
        allHeads = []
        for idx in range(NUM_LABELS):
            fc = fully_connected(x, 1024, activation='tanh')
            #fc = dropout(fc, 0.8)
            #fc = fully_connected(fc, 1024, activation='tanh')
            #fc = dropout(fc, 0.8)
            softmax = fully_connected(fc, 10, activation='softmax')
            networkOut = regression(softmax, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy', name='target' + str(idx))
            allHeads.append(networkOut)

        network = tflearn.merge(allHeads, mode='elemwise_sum')

        model = tflearn.DNN(network, tensorboard_verbose=1)
        feedTrainDict = {'target'+ str(i): Y_train[i] for i in range(NUM_LABELS)}
        #feedTrainDict = {'target0': Y_train[0]}
        feedTestList =  [Y_test[i] for i in range(NUM_LABELS)]
        #feedTestList =  Y_test[0]
        model.fit({'input': X_train}, feedTrainDict, 
                  validation_set= (X_test, feedTestList), n_epoch=1, show_metric=True, 
                  run_id='convnet_svhn_' + str(numLayers))
        #with open('/tmp/tflearn_logs/resultsLayer.txt','a') as f:
        #    strRes = str(numLayers) + ' -> ' + str(model.evaluate([X_test], feedTestList))
        #    f.write(strRes)
        #model.fit({'input': X_train}, feedTrainDict, n_epoch=1, show_metric=True, run_id='convnet_mnist')
        #model.fit({'input': X_train}, {'target0': Y_train[1]}, n_epoch=1, show_metric=True, run_id='convnet_mnist')
        #numImgEachAxis = 8
        #f,ax = plt.subplots(numImgEachAxis, numImgEachAxis, figsize=(10,10))
        #for i in range(numImgEachAxis):
        #    for j in range(numImgEachAxis):
        #        res = np.array([np.argmax(x) for x in model.predict([X_train[i*numImgEachAxis + j]])])
        #        print (str(i) + ',' + str(j) + ' -> ' +str(res))
                #ax[i][j].set_title(str([np.round(x,2) for x in res]))
        #        ax[i][j].imshow(X_train[i*numImgEachAxis + j].reshape((IMG_HEIGHT,IMG_WIDTH)) ,cmap = 'gray')
        #plt.show() # or display.display(plt.gcf()) if you prefer
    #    print (model.evaluate(X_test,feedTestList))


Training Step: 1 
| Adam_0 | epoch: 001 | loss: 0.00000 - acc: 0.0000 | val_loss: 2.84062 - val_acc: 0.3750 -- iter: 48/48
| Adam_1 | epoch: 001 | loss: 0.00000 - acc: 0.0000 | val_loss: 3.39415 - val_acc: 0.0000 -- iter: 48/48
Training Step: 1 
| Adam_0 | epoch: 001 | loss: 0.00000 - acc: 0.0000 | val_loss: 2.84062 - val_acc: 0.3750 -- iter: 48/48
| Adam_1 | epoch: 001 | loss: 0.00000 - acc: 0.0000 | val_loss: 3.39415 - val_acc: 0.0000 -- iter: 48/48
--
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-ff244b547a65> in <module>()
     43 
     44         if numLayers >= 5:
---> 45             x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_1')
     46             x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_2')
     47             x = tflearn.conv_2d(x, 512, 3, activation='relu', name='conv5_3')

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tflearn/layers/conv.pyc in conv_2d(incoming, nb_filter, filter_size, strides, padding, activation, bias, weights_init, bias_init, regularizer, weight_decay, trainable, restore, name)
     89             tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/' + scope, b)
     90 
---> 91         inference = tf.nn.conv2d(incoming, W, strides, padding)
     92         if b: inference = tf.nn.bias_add(inference, b)
     93 

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.pyc in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, data_format, name)
    392                                 strides=strides, padding=padding,
    393                                 use_cudnn_on_gpu=use_cudnn_on_gpu,
--> 394                                 data_format=data_format, name=name)
    395   return result
    396 

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    701           op = g.create_op(op_type_name, inputs, output_types, name=scope,
    702                            input_types=input_types, attrs=attr_protos,
--> 703                            op_def=op_def)
    704           outputs = op.outputs
    705           return _Restructure(ops.convert_n_to_tensor(outputs),

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2317                     original_op=self._default_original_op, op_def=op_def)
   2318     if compute_shapes:
-> 2319       set_shapes_for_outputs(ret)
   2320     self._add_op(ret)
   2321     self._record_op_seen_by_control_dependencies(ret)

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
   1709       raise RuntimeError("No shape function registered for standard op: %s"
   1710                          % op.type)
-> 1711   shapes = shape_func(op)
   1712   if shapes is None:
   1713     raise RuntimeError(

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in conv2d_shape(op)
    244   out_rows, out_cols = get2d_conv_output_size(in_rows, in_cols, filter_rows,
    245                                               filter_cols, stride_r, stride_c,
--> 246                                               padding)
    247 
    248   output_shape = [batch_size, out_rows, out_cols, depth_out]

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in get2d_conv_output_size(input_height, input_width, filter_height, filter_width, row_stride, col_stride, padding_type)
    182   return get_conv_output_size((input_height, input_width),
    183                               (filter_height, filter_width),
--> 184                               (row_stride, col_stride), padding_type)
    185 
    186 

/home/ankdesh/installed/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.pyc in get_conv_output_size(input_size, filter_size, strides, padding_type)
    147          zip(filter_size, input_size)):
    148     raise ValueError("Filter must not be larger than the input: "
--> 149                      "Filter: %r Input: %r" % (filter_size, input_size))
    150 
    151   if padding_type == b"VALID":

ValueError: Filter must not be larger than the input: Filter: (3, 3) Input: (2, 2)

In [ ]: