在多通道图像数据集上训练卷积网络
Training convolutional nets on multi-channel image data sets
我正在尝试从头开始实现卷积神经网络,但我无法弄清楚如何对具有 3 维的 rgb 等多通道图像执行(矢量化)操作。在阅读 this CS231n tutorial 等文章和教程后,很明显为单个输入实现网络,因为输入层将是 3d 矩阵,但数据集中始终有多个数据点。所以,我不知道如何实现这些网络以对整个数据集进行矢量化操作。
我实现了一个将 3d 矩阵作为输入的网络,但现在我意识到它不适用于整个数据集,但我必须在 time.I 上传播一个输入我真的不知道conv 网络是否在整个数据集上矢量化。但如果是,我如何为多通道图像矢量化我的卷积网络?
如果我答对了你的问题,你基本上是在问如何为一个 4-D 张量的小批量做卷积层。
简而言之,您想独立处理批处理中的每个输入并对每个输入应用卷积。在没有使用循环矢量化的情况下进行编码非常简单。
矢量化实现通常基于 im2col technique,它基本上将 4-D 输入张量转换为巨型矩阵并执行矩阵乘法。这是在 python 中使用 numpy.lib.stride_tricks
的前向传递的实现:
import numpy as np
def conv_forward(x, w, b, stride, pad):
N, C, H, W = x.shape
F, _, HH, WW = w.shape
# Check dimensions
assert (W + 2 * pad - WW) % stride == 0, 'width does not work'
assert (H + 2 * pad - HH) % stride == 0, 'height does not work'
# Pad the input
p = pad
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
# Figure out output dimensions
H += 2 * pad
W += 2 * pad
out_h = (H - HH) / stride + 1
out_w = (W - WW) / stride + 1
# Perform an im2col operation by picking clever strides
shape = (C, HH, WW, N, out_h, out_w)
strides = (H * W, W, 1, C * H * W, stride * W, stride)
strides = x.itemsize * np.array(strides)
x_stride = np.lib.stride_tricks.as_strided(x_padded,
shape=shape, strides=strides)
x_cols = np.ascontiguousarray(x_stride)
x_cols.shape = (C * HH * WW, N * out_h * out_w)
# Now all our convolutions are a big matrix multiply
res = w.reshape(F, -1).dot(x_cols) + b.reshape(-1, 1)
# Reshape the output
res.shape = (F, N, out_h, out_w)
out = res.transpose(1, 0, 2, 3)
out = np.ascontiguousarray(out)
return out
请注意,它使用了线性代数库的一些重要功能,这些功能已在 numpy
中实现,但您的库中可能没有。
顺便说一句,您通常不想将整个数据集作为一批推送 - 将其分成几批。
我正在尝试从头开始实现卷积神经网络,但我无法弄清楚如何对具有 3 维的 rgb 等多通道图像执行(矢量化)操作。在阅读 this CS231n tutorial 等文章和教程后,很明显为单个输入实现网络,因为输入层将是 3d 矩阵,但数据集中始终有多个数据点。所以,我不知道如何实现这些网络以对整个数据集进行矢量化操作。
我实现了一个将 3d 矩阵作为输入的网络,但现在我意识到它不适用于整个数据集,但我必须在 time.I 上传播一个输入我真的不知道conv 网络是否在整个数据集上矢量化。但如果是,我如何为多通道图像矢量化我的卷积网络?
如果我答对了你的问题,你基本上是在问如何为一个 4-D 张量的小批量做卷积层。
简而言之,您想独立处理批处理中的每个输入并对每个输入应用卷积。在没有使用循环矢量化的情况下进行编码非常简单。
矢量化实现通常基于 im2col technique,它基本上将 4-D 输入张量转换为巨型矩阵并执行矩阵乘法。这是在 python 中使用 numpy.lib.stride_tricks
的前向传递的实现:
import numpy as np
def conv_forward(x, w, b, stride, pad):
N, C, H, W = x.shape
F, _, HH, WW = w.shape
# Check dimensions
assert (W + 2 * pad - WW) % stride == 0, 'width does not work'
assert (H + 2 * pad - HH) % stride == 0, 'height does not work'
# Pad the input
p = pad
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
# Figure out output dimensions
H += 2 * pad
W += 2 * pad
out_h = (H - HH) / stride + 1
out_w = (W - WW) / stride + 1
# Perform an im2col operation by picking clever strides
shape = (C, HH, WW, N, out_h, out_w)
strides = (H * W, W, 1, C * H * W, stride * W, stride)
strides = x.itemsize * np.array(strides)
x_stride = np.lib.stride_tricks.as_strided(x_padded,
shape=shape, strides=strides)
x_cols = np.ascontiguousarray(x_stride)
x_cols.shape = (C * HH * WW, N * out_h * out_w)
# Now all our convolutions are a big matrix multiply
res = w.reshape(F, -1).dot(x_cols) + b.reshape(-1, 1)
# Reshape the output
res.shape = (F, N, out_h, out_w)
out = res.transpose(1, 0, 2, 3)
out = np.ascontiguousarray(out)
return out
请注意,它使用了线性代数库的一些重要功能,这些功能已在 numpy
中实现,但您的库中可能没有。
顺便说一句,您通常不想将整个数据集作为一批推送 - 将其分成几批。