【问题标题】:Implementing a model without a flatten layer in Tensorflow在 TensorFlow 中实现没有展平层的模型
【发布时间】:2018-04-14 17:46:42
【问题描述】:
使用 CNN 的典型方法由卷积部分和全连接层部分组成,它们与展平层相连。该层转换conv的输出。一部分(形状(x, y, z))转换为一维特征向量,该向量传递给由全连接层组成的分类器。
我的问题是在图像中只找到一个像素,它是某个对象的中心,而整个图像的拓扑/纹理导致了这一点。所以我想要一个模型,它没有展平层,而是 FC 部分从 conv 的输出中获取单个向量。部分和 FC 层的输出是到这个感兴趣像素的距离(我认为这些单独的特征向量携带了这些信息)。这个想法很简单,但是如何训练呢?我想知道这是否可以在 tensorflow(或任何其他框架)中以某种方式实现?
另一个选项是带有展平层的典型模型。 FC 部分将从 conv 中获取全部输出。部分(整个特征图),它将预测所需像素的位置。你认为这两种变体是等价的吗?太好了,因为第二个选项很容易在任何框架中实现。
【问题讨论】:
标签:
machine-learning
tensorflow
neural-network
computer-vision
conv-neural-network
【解决方案1】:
这听起来像是一个分割问题,唯一的区别是通常的模型预测边界框(4 个浮动值),而您希望预测单个值(如果这就是您所说的 距离 )。但这在代码方面并不重要,因为它只是将分类头替换为回归头。您描述的两种方法似乎都做同样的事情。
以下是 tensorflow 中的示例代码:
# Assume:
# layer.shape = (?, 16, 16, 64) <- last CNV layer output
# y.shape = (?, 1) <- target distance
# FC layer params: will output (?, 128)
w_fc = tf.Variable(tf.random_normal([16 * 16 * 64, 128]))
b_fc = tf.Variable(tf.random_normal([128]))
# Output layer params: will output (?, 1)
w_out = tf.Variable(tf.random_normal([128, 1]))
b_out = tf.Variable(tf.random_normal([1]))
# Reshape to make applicable to the FC layer
reshaped = tf.reshape(layer, [-1, w_fc.get_shape().as_list()[0]])
fc = tf.add(tf.matmul(reshaped, w_fc), b_fc)
fc = tf.nn.relu(fc)
out = tf.add(tf.matmul(fc, w_out), b_out)
# Standard L2 loss
loss = tf.reduce_mean(tf.nn.l2_loss(out - y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)