def classifier(x, dropout):
"""
AlexNet fully connected layers definition
Args:
x: tensor of shape [batch_size, width, height, channels]
dropout: probability of non dropping out units
Returns:
fc3: 1000 linear tensor taken just before applying the softmax operation
it is needed to feed it to tf.softmax_cross_entropy_with_logits()
softmax: 1000 linear tensor representing the output probabilities of the image to classify
"""
pool5 = cnn(x)
dim = pool5.get_shape().as_list()
flat_dim = dim[1] * dim[2] * dim[3] # 6 * 6 * 256
flat = tf.reshape(pool5, [-1, flat_dim])
with tf.name_scope('alexnet_classifier') as scope:
with tf.name_scope('alexnet_classifier_fc1') as inner_scope:
wfc1 = tu.weight([flat_dim, 4096], name='wfc1')
bfc1 = tu.bias(0.0, [4096], name='bfc1')
fc1 = tf.add(tf.matmul(flat, wfc1), bfc1)
#fc1 = tu.batch_norm(fc1)
fc1 = tu.relu(fc1)
fc1 = tf.nn.dropout(fc1, dropout)
with tf.name_scope('alexnet_classifier_fc2') as inner_scope:
wfc2 = tu.weight([4096, 4096], name='wfc2')
bfc2 = tu.bias(0.0, [4096], name='bfc2')
fc2 = tf.add(tf.matmul(fc1, wfc2), bfc2)
#fc2 = tu.batch_norm(fc2)
fc2 = tu.relu(fc2)
fc2 = tf.nn.dropout(fc2, dropout)
with tf.name_scope('alexnet_classifier_output') as inner_scope:
wfc3 = tu.weight([4096, 1000], name='wfc3')
bfc3 = tu.bias(0.0, [1000], name='bfc3')
fc3 = tf.add(tf.matmul(fc2, wfc3), bfc3)
softmax = tf.nn.softmax(fc3)
return fc3, softmax
评论列表
文章目录