Creating the CNN model

We are going to construct a complex network. However, it has a straightforward architecture. At the beginning, we use Xavier as the network initializer. Once we initialize the network bias using the Xavier initializer. The input layer is followed by a convolutional layer (convolutional layer 1), which is again followed by a BN layer (that is, BN layer 1). Then there is a pooling layer with strides of two and a kernel size of two. Then another BN layer follows the second convolutional layer. Next, there is the second pooling layer with strides of two and kernel size of two. Well, then the max polling layer is followed by a flattening layer that flattens the input from (None, height, width, channels) to (None, height * width * channels) == (None, 3072).

Once the flattening is completed, the input is fed into the first fully connected layer 1. Then third BN is applied as a normalizer function. Then we will have a dropout layer before we feed the lighter network into the fully connected layer 2 that generates logits of size (None, 62). Too much of a mouthful? Don't worry; we will see it step by step. Let's start the coding by creating the computational graph, creating both features, and labeling placeholders:

graph = tf.Graph()
with graph.as_default():
# Placeholders for inputs and labels.
images_X = tf.placeholder(tf.float32, [None, 32, 32, 3]) # each image's 32x32 size
labels_X = tf.placeholder(tf.int32, [None])

# Initializer: Xavier
biasInit = tf.contrib.layers.xavier_initializer(uniform=True, seed=None, dtype=tf.float32)

# Convolution layer 1: number of neurons 128 and kernel size is 6x6.
conv1 = tf.contrib.layers.conv2d(images_X, num_outputs=128, kernel_size=[6, 6],
biases_initializer=biasInit)

# Batch normalization layer 1: can be applied as a normalizer
# function for conv2d and fully_connected
bn1 = tf.contrib.layers.batch_norm(conv1, center=True, scale=True, is_training=True)

# Max Pooling (down sampling) with strides of 2 and kernel size of 2
pool1 = tf.contrib.layers.max_pool2d(bn1, 2, 2)

# Convolution layer 2: number of neurons 256 and kernel size is 6x6.
conv2 = tf.contrib.layers.conv2d(pool1, num_outputs=256, kernel_size=[4, 4], stride=2,
biases_initializer=biasInit)

# Batch normalization layer 2:
bn2 = tf.contrib.layers.batch_norm(conv2, center=True, scale=True, is_training=True)

# Max Pooling (down-sampling) with strides of 2 and kernel size of 2
pool2 = tf.contrib.layers.max_pool2d(bn2, 2, 2)

# Flatten the input from [None, height, width, channels] to
# [None, height * width * channels] == [None, 3072]
images_flat = tf.contrib.layers.flatten(pool2)

# Fully connected layer 1
fc1 = tf.contrib.layers.fully_connected(images_flat, 512, tf.nn.relu)

# Batch normalization layer 3
bn3 = tf.contrib.layers.batch_norm(fc1, center=True, scale=True, is_training=True)

# apply dropout, if is_training is False, dropout is not applied
fc1 = tf.layers.dropout(bn3, rate=0.25, training=True)

# Fully connected layer 2 that generates logits of size [None, 62].
# Here 62 means number of classes to be predicted.
logits = tf.contrib.layers.fully_connected(fc1, 62, tf.nn.relu)

Up to this point, we have managed to generate the logits of size (None, 62). Then we need to convert the logits to label indexes (int) with the shape (None), which is a 1D vector of length == batch_size:predicted_labels = tf.argmax(logits, axis=1). Then we define cross-entropy as the loss function, which is a good choice for classification:

loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels_X))

Now one of the most important parts is updating the ops and creating an optimizer (Adam in our case):

update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
# Create an optimizer, which acts as the training op.train =
tf.train.AdamOptimizer(learning_rate=0.10).minimize(loss_op)

Finally, we initialize all the ops:

init_op = tf.global_variables_initializer()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset