【发布时间】:2017-08-10 13:08:18
【问题描述】:
谁能通过示例清楚地解释卷积神经网络(在深度学习中)中 1D、2D 和 3D 卷积之间的区别?
【问题讨论】:
标签: machine-learning deep-learning signal-processing conv-neural-network convolution
谁能通过示例清楚地解释卷积神经网络(在深度学习中)中 1D、2D 和 3D 卷积之间的区别?
【问题讨论】:
标签: machine-learning deep-learning signal-processing conv-neural-network convolution
我想用C3D的图片来解释一下。
简而言之,卷积方向和输出形状很重要!
↑↑↑↑↑ 一维卷积 - 基本 ↑↑↑↑↑
import tensorflow as tf
import numpy as np
sess = tf.Session()
ones_1d = np.ones(5)
weight_1d = np.ones(3)
strides_1d = 1
in_1d = tf.constant(ones_1d, dtype=tf.float32)
filter_1d = tf.constant(weight_1d, dtype=tf.float32)
in_width = int(in_1d.shape[0])
filter_width = int(filter_1d.shape[0])
input_1d = tf.reshape(in_1d, [1, in_width, 1])
kernel_1d = tf.reshape(filter_1d, [filter_width, 1, 1])
output_1d = tf.squeeze(tf.nn.conv1d(input_1d, kernel_1d, strides_1d, padding='SAME'))
print sess.run(output_1d)
↑↑↑↑↑ 二维卷积 - 基础 ↑↑↑↑↑
ones_2d = np.ones((5,5))
weight_2d = np.ones((3,3))
strides_2d = [1, 1, 1, 1]
in_2d = tf.constant(ones_2d, dtype=tf.float32)
filter_2d = tf.constant(weight_2d, dtype=tf.float32)
in_width = int(in_2d.shape[0])
in_height = int(in_2d.shape[1])
filter_width = int(filter_2d.shape[0])
filter_height = int(filter_2d.shape[1])
input_2d = tf.reshape(in_2d, [1, in_height, in_width, 1])
kernel_2d = tf.reshape(filter_2d, [filter_height, filter_width, 1, 1])
output_2d = tf.squeeze(tf.nn.conv2d(input_2d, kernel_2d, strides=strides_2d, padding='SAME'))
print sess.run(output_2d)
↑↑↑↑↑ 3D卷积 - 基础 ↑↑↑↑↑
ones_3d = np.ones((5,5,5))
weight_3d = np.ones((3,3,3))
strides_3d = [1, 1, 1, 1, 1]
in_3d = tf.constant(ones_3d, dtype=tf.float32)
filter_3d = tf.constant(weight_3d, dtype=tf.float32)
in_width = int(in_3d.shape[0])
in_height = int(in_3d.shape[1])
in_depth = int(in_3d.shape[2])
filter_width = int(filter_3d.shape[0])
filter_height = int(filter_3d.shape[1])
filter_depth = int(filter_3d.shape[2])
input_3d = tf.reshape(in_3d, [1, in_depth, in_height, in_width, 1])
kernel_3d = tf.reshape(filter_3d, [filter_depth, filter_height, filter_width, 1, 1])
output_3d = tf.squeeze(tf.nn.conv3d(input_3d, kernel_3d, strides=strides_3d, padding='SAME'))
print sess.run(output_3d)
↑↑↑↑↑ 2D 卷积与 3D 输入 - LeNet, VGG, ..., ↑↑↑↑↑
in_channels = 32 # 3 for RGB, 32, 64, 128, ...
ones_3d = np.ones((5,5,in_channels)) # input is 3d, in_channels = 32
# filter must have 3d-shpae with in_channels
weight_3d = np.ones((3,3,in_channels))
strides_2d = [1, 1, 1, 1]
in_3d = tf.constant(ones_3d, dtype=tf.float32)
filter_3d = tf.constant(weight_3d, dtype=tf.float32)
in_width = int(in_3d.shape[0])
in_height = int(in_3d.shape[1])
filter_width = int(filter_3d.shape[0])
filter_height = int(filter_3d.shape[1])
input_3d = tf.reshape(in_3d, [1, in_height, in_width, in_channels])
kernel_3d = tf.reshape(filter_3d, [filter_height, filter_width, in_channels, 1])
output_2d = tf.squeeze(tf.nn.conv2d(input_3d, kernel_3d, strides=strides_2d, padding='SAME'))
print sess.run(output_2d)
in_channels = 32 # 3 for RGB, 32, 64, 128, ...
out_channels = 64 # 128, 256, ...
ones_3d = np.ones((5,5,in_channels)) # input is 3d, in_channels = 32
# filter must have 3d-shpae x number of filters = 4D
weight_4d = np.ones((3,3,in_channels, out_channels))
strides_2d = [1, 1, 1, 1]
in_3d = tf.constant(ones_3d, dtype=tf.float32)
filter_4d = tf.constant(weight_4d, dtype=tf.float32)
in_width = int(in_3d.shape[0])
in_height = int(in_3d.shape[1])
filter_width = int(filter_4d.shape[0])
filter_height = int(filter_4d.shape[1])
input_3d = tf.reshape(in_3d, [1, in_height, in_width, in_channels])
kernel_4d = tf.reshape(filter_4d, [filter_height, filter_width, in_channels, out_channels])
#output stacked shape is 3D = 2D x N matrix
output_3d = tf.nn.conv2d(input_3d, kernel_4d, strides=strides_2d, padding='SAME')
print sess.run(output_3d)
↑↑↑↑↑ CNN 中的奖励 1x1 转换 - GoogLeNet, ..., ↑↑↑↑↑
in_channels = 32 # 3 for RGB, 32, 64, 128, ...
out_channels = 64 # 128, 256, ...
ones_3d = np.ones((1,1,in_channels)) # input is 3d, in_channels = 32
# filter must have 3d-shpae x number of filters = 4D
weight_4d = np.ones((3,3,in_channels, out_channels))
strides_2d = [1, 1, 1, 1]
in_3d = tf.constant(ones_3d, dtype=tf.float32)
filter_4d = tf.constant(weight_4d, dtype=tf.float32)
in_width = int(in_3d.shape[0])
in_height = int(in_3d.shape[1])
filter_width = int(filter_4d.shape[0])
filter_height = int(filter_4d.shape[1])
input_3d = tf.reshape(in_3d, [1, in_height, in_width, in_channels])
kernel_4d = tf.reshape(filter_4d, [filter_height, filter_width, in_channels, out_channels])
#output stacked shape is 3D = 2D x N matrix
output_3d = tf.nn.conv2d(input_3d, kernel_4d, strides=strides_2d, padding='SAME')
print sess.run(output_3d)
in_channels = 32 # 3, 32, 64, 128, ...
out_channels = 64 # 3, 32, 64, 128, ...
ones_4d = np.ones((5,5,5,in_channels))
weight_5d = np.ones((3,3,3,in_channels,out_channels))
strides_3d = [1, 1, 1, 1, 1]
in_4d = tf.constant(ones_4d, dtype=tf.float32)
filter_5d = tf.constant(weight_5d, dtype=tf.float32)
in_width = int(in_4d.shape[0])
in_height = int(in_4d.shape[1])
in_depth = int(in_4d.shape[2])
filter_width = int(filter_5d.shape[0])
filter_height = int(filter_5d.shape[1])
filter_depth = int(filter_5d.shape[2])
input_4d = tf.reshape(in_4d, [1, in_depth, in_height, in_width, in_channels])
kernel_5d = tf.reshape(filter_5d, [filter_depth, filter_height, filter_width, in_channels, out_channels])
output_4d = tf.nn.conv3d(input_4d, kernel_5d, strides=strides_3d, padding='SAME')
print sess.run(output_4d)
sess.close()
【讨论】:
1,然后是 → 用于行 1+stride。卷积本身是移位不变的,那么为什么卷积的方向很重要呢?
根据@runhani 的回答,我将添加更多细节以使解释更加清晰,并将尝试对此进行更多解释(当然还有来自 TF1 和 TF2 的示例)。
我包括的主要附加位之一是,
tf.Variable 的用法
以下是使用 TF 1 和 TF 2 进行一维卷积的方法。
具体来说,我的数据具有以下形状,
[batch size, width, in channels](例如1, 5, 1)[width, in channels, out channels](例如 5, 1, 4)[batch size, width, out_channels](例如 1, 5, 4)import tensorflow as tf
import numpy as np
inp = tf.placeholder(shape=[None, 5, 1], dtype=tf.float32)
kernel = tf.Variable(tf.initializers.glorot_uniform()([5, 1, 4]), dtype=tf.float32)
out = tf.nn.conv1d(inp, kernel, stride=1, padding='SAME')
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(out, feed_dict={inp: np.array([[[0],[1],[2],[3],[4]],[[5],[4],[3],[2],[1]]])}))
import tensorflow as tf
import numpy as np
inp = np.array([[[0],[1],[2],[3],[4]],[[5],[4],[3],[2],[1]]]).astype(np.float32)
kernel = tf.Variable(tf.initializers.glorot_uniform()([5, 1, 4]), dtype=tf.float32)
out = tf.nn.conv1d(inp, kernel, stride=1, padding='SAME')
print(out)
TF2 的工作量要少得多,因为 TF2 不需要 Session 和 variable_initializer 例如。
让我们通过一个信号平滑示例来了解它的作用。左边是原始的,右边是 Convolution 1D 的输出,它有 3 个输出通道。
多通道基本上是输入的多个特征表示。在此示例中,您有由三个不同过滤器获得的三个表示。第一个通道是等权平滑滤波器。第二个是过滤器,它对过滤器中间的权重大于边界。最后一个过滤器的作用与第二个相反。所以你可以看到这些不同的滤镜是如何产生不同的效果的。
一维卷积已成功用于sentence classification 任务。
转为二维卷积。如果你是一个深度学习的人,那么你没有遇到 2D 卷积的机会是……几乎为零。它在 CNN 中用于图像分类、对象检测等以及涉及图像的 NLP 问题(例如图像标题生成)。
让我们尝试一个例子,我在这里得到了一个带有以下过滤器的卷积核,
具体来说,我的数据具有以下形状,
[batch_size, height, width, 1](例如1, 340, 371, 1)[height, width, in channels, out channels](例如3, 3, 1, 3)[batch_size, height, width, out_channels](例如 1, 340, 371, 3)import tensorflow as tf
import numpy as np
from PIL import Image
im = np.array(Image.open(<some image>).convert('L'))#/255.0
kernel_init = np.array(
[
[[[-1, 1.0/9, 0]],[[-1, 1.0/9, -1]],[[-1, 1.0/9, 0]]],
[[[-1, 1.0/9, -1]],[[8, 1.0/9,5]],[[-1, 1.0/9,-1]]],
[[[-1, 1.0/9,0]],[[-1, 1.0/9,-1]],[[-1, 1.0/9, 0]]]
])
inp = tf.placeholder(shape=[None, image_height, image_width, 1], dtype=tf.float32)
kernel = tf.Variable(kernel_init, dtype=tf.float32)
out = tf.nn.conv2d(inp, kernel, strides=[1,1,1,1], padding='SAME')
with tf.Session() as sess:
tf.global_variables_initializer().run()
res = sess.run(out, feed_dict={inp: np.expand_dims(np.expand_dims(im,0),-1)})
import tensorflow as tf
import numpy as np
from PIL import Image
im = np.array(Image.open(<some image>).convert('L'))#/255.0
x = np.expand_dims(np.expand_dims(im,0),-1)
kernel_init = np.array(
[
[[[-1, 1.0/9, 0]],[[-1, 1.0/9, -1]],[[-1, 1.0/9, 0]]],
[[[-1, 1.0/9, -1]],[[8, 1.0/9,5]],[[-1, 1.0/9,-1]]],
[[[-1, 1.0/9,0]],[[-1, 1.0/9,-1]],[[-1, 1.0/9, 0]]]
])
kernel = tf.Variable(kernel_init, dtype=tf.float32)
out = tf.nn.conv2d(x, kernel, strides=[1,1,1,1], padding='SAME')
在这里你可以看到上面代码产生的输出。第一个图像是原始图像,顺时针方向您有第一个过滤器、第二个过滤器和第三个过滤器的输出。
在 2D 卷积的上下文中,更容易理解这些多个通道的含义。假设您正在进行人脸识别。您可以想到(这是一个非常不切实际的简化,但可以理解)每个过滤器代表眼睛、嘴巴、鼻子等。因此每个特征图都是您提供的图像中是否存在该特征的二进制表示.我认为我不需要强调对于人脸识别模型来说,这些都是非常有价值的特征。更多信息请参阅article。
这是我想要表达的说明。
2D 卷积在深度学习领域非常普遍。
CNN(卷积神经网络)对几乎所有计算机视觉任务(例如图像分类、对象检测、视频分类)使用 2D 卷积运算。
现在,随着维度数量的增加,说明发生了什么变得越来越困难。但是,如果很好地理解了 1D 和 2D 卷积的工作原理,那么将这种理解推广到 3D 卷积是非常简单的。就这样吧。
具体来说,我的数据具有以下形状,
[batch size, height, width, depth, in channels](例如1, 200, 200, 200, 1)[height, width, depth, in channels, out channels](例如 5, 5, 5, 1, 3)[batch size, width, height, width, depth, out_channels](例如 1, 200, 200, 2000, 3)import tensorflow as tf
import numpy as np
tf.reset_default_graph()
inp = tf.placeholder(shape=[None, 200, 200, 200, 1], dtype=tf.float32)
kernel = tf.Variable(tf.initializers.glorot_uniform()([5,5,5,1,3]), dtype=tf.float32)
out = tf.nn.conv3d(inp, kernel, strides=[1,1,1,1,1], padding='SAME')
with tf.Session() as sess:
tf.global_variables_initializer().run()
res = sess.run(out, feed_dict={inp: np.random.normal(size=(1,200,200,200,1))})
import tensorflow as tf
import numpy as np
x = np.random.normal(size=(1,200,200,200,1))
kernel = tf.Variable(tf.initializers.glorot_uniform()([5,5,5,1,3]), dtype=tf.float32)
out = tf.nn.conv3d(x, kernel, strides=[1,1,1,1,1], padding='SAME')
在开发涉及 3 维 LIDAR(光检测和测距)数据的机器学习应用程序时,已使用 3D 卷积。
好的,你快到了。所以坚持住。让我们看看 stride 和 padding 是什么。如果您考虑一下它们,它们会非常直观。
如果您跨过走廊,您可以用更少的步数更快地到达那里。但这也意味着您观察到的周围环境比穿过房间时要少。现在让我们用一张漂亮的图片来加强我们的理解!让我们通过 2D 卷积来理解这些。
例如使用tf.nn.conv2d 时,需要将其设置为4 个元素的向量。没有理由对此感到害怕。它只包含按以下顺序的步幅。
二维卷积 - [batch stride, height stride, width stride, channel stride]。在这里,您只需将批处理步幅和通道步幅设置为 1(我已经实施深度学习模型 5 年了,除了 1 之外,从未将它们设置为任何值)。因此,您只需设置 2 步即可。
3D 卷积 - [batch stride, height stride, width stride, depth stride, channel stride]。在这里,您只关心高度/宽度/深度步幅。
现在,您注意到,无论您的步幅有多小(即 1),在卷积期间都会不可避免地发生降维(例如,在卷积 4 个单位宽的图像后,宽度为 3)。这是不可取的,尤其是在构建深度卷积神经网络时。这就是填充来拯救的地方。有两种最常用的填充类型。
SAME 和 VALID
您可以在下面看到不同之处。
最后一句话:如果您非常好奇,您可能会想知道。我们刚刚对全自动降维投下了一颗炸弹,现在谈论的是不同的步幅。但是 stride 的最大优点是您可以控制何时何地以及如何缩小尺寸。
【讨论】:
总而言之,在 1D CNN 中,内核向 1 个方向移动。 1D CNN 的输入和输出数据是二维的。主要用于时间序列数据。
在 2D CNN 中,内核向 2 个方向移动。 2D CNN 的输入和输出数据是 3 维的。主要用于图像数据。
在 3D CNN 中,内核沿 3 个方向移动。 3D CNN 的输入和输出数据是 4 维的。主要用于 3D 图像数据(MRI、CT 扫描)。
您可以在此处找到更多详细信息:https://medium.com/@xzz201920/conv1d-conv2d-and-conv3d-8a59182c4d6
【讨论】:
CNN 1D、2D 或 3D 指的是卷积方向,而不是输入或滤波器维度。
对于 1 通道输入,CNN2D 等于 CNN1D 是内核长度 = 输入长度。 (1 个转化方向)
【讨论】: