查看TensorFlow页面:https://github.com/tensorflow/models/tree/master/inception
他们展示了一张包含其架构的图片,特别是他们的’Inception’模块,这些模块并行包含:
- 1×1的卷积层
- 3×3的卷积层
- 5×5的卷积层
- 平均池化 + 1×1卷积
随后是一个’concat’层。
如何在TensorFlow中创建这个模块?
我想我可以这样做来创建并行操作:
start_layer = input_datafilter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)one_by_one = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')filter = tf.Variable(tf.truncated_normal([3,3,channels,filter_count], stddev=0.1)three_by_three = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')filter = tf.Variable(tf.truncated_normal([5,5,channels,filter_count], stddev=0.1)five_by_five = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)pooling = tf.nn.avg_pool(start_layer, filter, strides=[1,2,2,1], padding='SAME')filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)pooling = tf.nn.conv2d(pooling, filter, strides=[1,1,1,1], padding='SAME')#connect one_by_one, three_by_three, five_by_five, pooling into an concat layer
但是,我如何将这四个操作合并到一个concat层中呢?
回答:
我做了与你非常相似的事情,然后用tf.concat()
完成了。请注意axis=3
,它与我的4D张量匹配,并在第四维度(索引3)上进行拼接。相关文档在这里:这里。
我的最终代码大致如下:
def inception2d(x, in_channels, filter_count): # 偏置维度 = 3*filter_count,然后是平均池化所需的额外in_channels bias = tf.Variable(tf.truncated_normal([3*filter_count + in_channels], mu, sigma)), # 1x1 one_filter = tf.Variable(tf.truncated_normal([1, 1, in_channels, filter_count], mu, sigma)) one_by_one = tf.nn.conv2d(x, one_filter, strides=[1, 1, 1, 1], padding='SAME') # 3x3 three_filter = tf.Variable(tf.truncated_normal([3, 3, in_channels, filter_count], mu, sigma)) three_by_three = tf.nn.conv2d(x, three_filter, strides=[1, 1, 1, 1], padding='SAME') # 5x5 five_filter = tf.Variable(tf.truncated_normal([5, 5, in_channels, filter_count], mu, sigma)) five_by_five = tf.nn.conv2d(x, five_filter, strides=[1, 1, 1, 1], padding='SAME') # 平均池化 pooling = tf.nn.avg_pool(x, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME') x = tf.concat([one_by_one, three_by_three, five_by_five, pooling], axis=3) # 在第四维度上拼接 x = tf.nn.bias_add(x, bias) return tf.nn.relu(x)