代码请见:https://github.com/amir-saniyan/ZFNet
# Input: 224x224x3.
with tf.name_scope('input'):
self.X = tf.placeholder(dtype=tf.float32,
shape=[None, self.input_height, self.input_width, self.input_channels], name='X')
# Labels: 1000.
with tf.name_scope('labels'):
self.Y = tf.placeholder(dtype=tf.float32, shape=[None, self.num_classes], name='Y')
# Dropout keep prob.
with tf.name_scope('dropout'):
self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=(), name='dropout_keep_prob')
# Layer 1.
# [Input] ==> 224x224x3
# --> 224x224x3 ==> [Convolution: size=(7x7x3)x96, strides=2, padding=valid] ==> 110x110x96
# --> 110x110x96 ==> [ReLU] ==> 110x110x96
# --> 110x110x96 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 55x55x96
# --> [Output] ==> 55x55x96
# Note: There were some calculation errors in ZFNet architecture:
# floor((224-7)/2) + 1 = 109
# floor((110-3)/2) + 1 = 54
with tf.name_scope('layer1'):
layer1_activations = self.__conv(input=self.X, filter_width=7, filter_height=7, filters_count=96,
stride_x=2, stride_y=2, padding='VALID')
layer1_pool = self.__max_pool(input=layer1_activations, filter_width=3, filter_height=3, stride_x=2,
stride_y=2, padding='VALID')
# Layer 2.
# [Input] ==> 55x55x96
# --> 55x55x96 ==> [Convolution: size=(5x5x96)x256, strides=2, padding=valid] ==> 26x26x256
# --> 26x26x256 ==> [ReLU] ==> 26x26x256
# --> 26x26x256 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 13x13x256
# --> [Output] ==> 13x13x256
# Note: There were some calculation errors in ZFNet architecture:
# floor((26-3)/2) + 1 = 12
with tf.name_scope('layer2'):
layer2_activations = self.__conv(input=layer1_pool, filter_width=5, filter_height=5, filters_count=256,
stride_x=2, stride_y=2, padding='VALID')
layer2_pool = self.__max_pool(input=layer2_activations, filter_width=3, filter_height=3, stride_x=2,
stride_y=2, padding='VALID')
# Layer 3.
# [Input] ==> 13x13x256
# --> 13x13x256 ==> [Convolution: size=(3x3x256)x384, strides=1, padding=same] ==> 13x13x384
# --> 13x13x384 ==> [ReLU] ==> 13x13x384
# --> [Output] ==> 13x13x384
with tf.name_scope('layer3'):
layer3_activations = self.__conv(input=layer2_pool, filter_width=3, filter_height=3, filters_count=384,
stride_x=1, stride_y=1, padding='SAME')
# Layer 4.
# [Input] ==> 13x13x384
# --> 13x13x384 ==> [Convolution: size=(3x3x384)x384, strides=1, padding=same] ==> 13x13x384
# --> 13x13x384 ==> [ReLU] ==> 13x13x384
# --> [Output] ==> 13x13x384
with tf.name_scope('layer4'):
layer4_activations = self.__conv(input=layer3_activations, filter_width=3, filter_height=3,
filters_count=384, stride_x=1, stride_y=1, padding='SAME')
# Layer 5.
# [Input] ==> 13x13x384
# --> 13x13x384 ==> [Convolution: size=(3x3x384)x256, strides=1, padding=same] ==> 13x13x256
# --> 13x13x256 ==> [ReLU] ==> 13x13x256
# --> 13x13x256 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 6x6x256
# --> [Output] ==> 6x6x256
with tf.name_scope('layer5'):
layer5_activations = self.__conv(input=layer4_activations, filter_width=3, filter_height=3,
filters_count=256, stride_x=1, stride_y=1, padding='SAME')
layer5_pool = self.__max_pool(input=layer5_activations, filter_width=3, filter_height=3, stride_x=2,
stride_y=2, padding='VALID')
# Layer 6.
# [Input] ==> 6x6x256=9216
# --> 9216 ==> [Fully Connected: neurons=4096] ==> 4096
# --> 4096 ==> [ReLU] ==> 4096
# --> 4096 ==> [Dropout] ==> 4096
# --> [Output] ==> 4096
with tf.name_scope('layer6'):
pool5_shape = layer5_pool.get_shape().as_list()
flattened_input_size = pool5_shape[1] * pool5_shape[2] * pool5_shape[3]
layer6_fc = self.__fully_connected(input=tf.reshape(layer5_pool, shape=[-1, flattened_input_size]),
inputs_count=flattened_input_size, outputs_count=4096, relu=True)
layer6_dropout = self.__dropout(input=layer6_fc)
# Layer 7.
# [Input] ==> 4096
# --> 4096 ==> [Fully Connected: neurons=4096] ==> 4096
# --> 4096 ==> [ReLU] ==> 4096
# --> 4096 ==> [Dropout] ==> 4096
# --> [Output] ==> 4096
with tf.name_scope('layer7'):
layer7_fc = self.__fully_connected(input=layer6_dropout, inputs_count=4096, outputs_count=4096, relu=True)
layer7_dropout = self.__dropout(input=layer7_fc)
# Layer 8.
# [Input] ==> 4096
# --> 4096 ==> [Logits: neurons=1000] ==> 1000
# --> [Output] ==> 1000
with tf.name_scope('layer8'):
layer8_logits = self.__fully_connected(input=layer7_dropout, inputs_count=4096,
outputs_count=self.num_classes, relu=False, name='logits')