【问题标题】:How do you create an inception module in tensorflow如何在 tensorflow 中创建初始模块
【发布时间】:2017-03-25 09:44:02
【问题描述】:

看tensorflow页面:https://github.com/tensorflow/models/tree/master/inception

他们展示了一个带有架构的图像,特别是他们的“初始”模块,其中包含并行:

  • 1x1 的转换层
  • 3x3 的转换层
  • 5x5 的转换层
  • 平均池化 + 1x1 转换

后跟一个“concat”层。

如何在 tensorflow 中创建它?

我想我可以按照这样的方式做一些事情来创建并行操作:

start_layer = input_data

filter = 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

但是如何将这 4 个操作组合成一个 concat 层呢?

【问题讨论】:

  • 所有内部模型的结果应该具有相同的维度,然后您只需将其连接到一个张量中。我不确定 tensorflow,但您可以 numpy 安排下一层的输入。
  • 有人从零开始实现了一个初始模块hackathonprojects.wordpress.com/2016/09/25/…

标签: python machine-learning tensorflow


【解决方案1】:

我做了一些与你所做的非常相似的事情,然后用tf.concat() 完成了它。注意 axis=3 匹配我的 4d 张量并连接到第 4 维(索引 3)。 它的文档是here

我的最终代码是这样的:

def inception2d(x, in_channels, filter_count):
    # bias dimension = 3*filter_count and then the extra in_channels for the avg pooling
    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')

    # avg pooling
    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)  # Concat in the 4th dim to stack
    x = tf.nn.bias_add(x, bias)
    return tf.nn.relu(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2016-07-06
    • 2021-11-26
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多