python - Keras 和 Caffe 中的卷积有什么区别?
问题描述
我正在尝试将大型 Caffe 网络复制到 Keras(基于 tensorflow 后端)。但是即使在单个卷积层上我也遇到了很大的麻烦。
一般的简单卷积:
假设我们有一个形状为 的 4D 输入,我们必须使用内核大小为和步幅的过滤器(1, 500, 500, 3)
对该输入执行一次卷积。96
11
4x4
让我们设置我们的权重和输入变量:
w = np.random.rand(11, 11, 3, 96) # weights 1
b = np.random.rand(96) # weights 2 (bias)
x = np.random.rand(500, 500, 3)
Keras 中的简单卷积:
这就是它在 Keras 中的定义方式:
from keras.layers import Input
from keras.layers import Conv2D
import numpy as np
inp = Input(shape=(500, 500, 3))
conv1 = Conv2D(filters=96, kernel_size=11, strides=(4, 4), activation=keras.activations.relu, padding='valid')(inp)
model = keras.Model(inputs=[inp], outputs=conv1)
model.layers[1].set_weights([w, b]) # set weights for convolutional layer
predicted = model.predict([x.reshape(1, 500, 500, 3)])
print(predicted.reshape(1, 96, 123, 123)) # reshape keras output in the form of Caffe
Caffe 中的简单卷积:
simple.prototxt
:
name: "simple"
input: "inp"
input_shape {
dim: 1
dim: 3
dim: 500
dim: 500
}
layer {
name: "conv1"
type: "Convolution"
bottom: "inp"
top: "conv1"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 96
kernel_size: 11
pad: 0
stride: 4
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
Python 中的 Caffe:
import caffe
net = caffe.Net('simple.prototxt', caffe.TEST)
net.params['conv1'][0].data[...] = w.reshape(96, 3, 11, 11) # set weights 1
net.params['conv1'][1].data[...] = b # set weights 2 (bias)
net.blobs['inp'].reshape(1, 3, 500, 500) # reshape input layer to fit our input array x
print(net.forward(inp=x.reshape(1, 3, 500, 500)).get('conv1'))
问题:
如果我们执行这两个代码片段,我们会注意到输出彼此不同。我知道 Caffe 的对称填充等差异很少,但我什至没有在这里使用填充。然而,Caffe 的输出与 Keras 的输出不同......
为什么会这样?我知道 Theano 后端没有像 Caffe 那样利用相关性,因此它需要内核旋转 180 度,但对于 tensorflow 是否相同?据我所知,Tensorflow 和 Caffe 都使用互相关而不是卷积。
如何在 Keras 和 Caffe 中制作两个使用卷积的相同模型?
任何帮助将不胜感激,谢谢!
解决方案
我找到了问题,但我不知道如何解决它......
这两个卷积层之间的区别在于它们的项目的对齐方式。仅当过滤器的数量等于过滤器的维度时,才会出现此对齐问题。换句话说,只有当我们从卷积中得到一个行数和列数都大于 1的多维数组时,才会出现这样的问题。N
N > 1 && N > S
S
证据:
为了看到这一点,我简化了输入和输出数据,以便我们可以更好地分析两层的机制。
simple.prototxt
:
input: "input"
input_shape {
dim: 1
dim: 1
dim: 2
dim: 2
}
layer {
name: "conv1"
type: "Convolution"
bottom: "input"
top: "conv1"
convolution_param {
num_output: 2
kernel_size: 1
pad: 0
stride: 1
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
simple.py
:
import keras
import caffe
import numpy as np
from keras.layers import Input, Conv2D
from keras.activations import relu
from keras import Model
filters = 2 # greater than 1 and ker_size
ker_size = 1
_input = np.arange(2 * 2).reshape(2, 2)
_weights = [np.reshape([[2 for _ in range(filters)] for _ in range(ker_size*ker_size)], (ker_size, ker_size, 1, filters)), np.reshape([0 for _ in range(filters)], (filters,))] # weights for Keras, main weight is array of 2`s while bias weight is array of 0's
_weights_caffe = [_weights[0].T, _weights[1].T] # just transpose them for Caffe
# Keras Setup
keras_input = Input(shape=(2, 2, 1), dtype='float32')
keras_conv = Conv2D(filters=filters, kernel_size=ker_size, strides=(1, 1), activation=relu, padding='valid')(keras_input)
model = Model(inputs=[keras_input], outputs=keras_conv)
model.layers[1].set_weights([_weights[0], _weights[1]])
# Caffe Setup
net = caffe.Net("simpler.prototxt", caffe.TEST)
net.params['conv1'][0].data[...] = _weights_caffe[0]
net.params['conv1'][1].data[...] = _weights_caffe[1]
net.blobs['input'].data[...] = _input.reshape(1, 1, 2, 2)
# Predictions
print("Input:\n---")
print(_input)
print(_input.shape)
print("\n")
print("Caffe:\n---")
print(net.forward()['conv1'])
print(net.forward()['conv1'].shape)
print("\n")
print("Keras:\n---")
print(model.predict([_input.reshape(1, 2, 2, 1)]))
print(model.predict([_input.reshape(1, 2, 2, 1)]).shape)
print("\n")
输出:
Input:
---
[[0 1]
[2 3]]
(2, 2)
Caffe:
---
[[[[0. 2.]
[4. 6.]]
[[0. 2.]
[4. 6.]]]]
(1, 2, 2, 2)
Keras:
---
[[[[0. 0.]
[2. 2.]]
[[4. 4.]
[6. 6.]]]]
(1, 2, 2, 2)
分析:
如果您查看 Caffe 模型的输出,您会注意到我们的2x2
数组首先被加倍(因此我们有一个包含 2 个数组的2x2
数组),然后使用我们的权重矩阵对这两个数组中的每一个执行矩阵乘法。像这样的东西:
原文:
[[[[0. 2.]
[4. 6.]]
[[0. 2.]
[4. 6.]]]]
转换:
[[[[(0 * 2) (2 * 2)]
[(4 * 2) (6 * 2)]]
[[(0 * 2) (2 * 2)]
[(4 * 2) (6 * 2)]]]]
Tensorflow 做了一些不同的事情,它似乎在做与 Caffe 相同的事情之后首先按升序对齐输出的 2D 向量。这似乎是一种奇怪的行为,我无法理解他们为什么会这样做。
解决方案:
我已经回答了我自己关于问题原因的问题,但我还不知道有什么干净的解决方案。我仍然觉得我的答案不够令人满意,因此我将接受具有实际解决方案的问题。
我知道的唯一解决方案是创建自定义层,这对我来说不是一个很好的解决方案。
推荐阅读
- java - 自定义请求生成器 Apache
- jquery - 将模型的属性作为参数传递给视图控制器
- java - 使用 Quickblox 生成签名 APK 时出错
- c# - 作为初学者程序员,在 Visual Studios 或任何编程应用程序中使用扩展通常是一种不好的做法吗?
- c - 将大量线程固定到单个 CPU 会导致所有内核的利用率飙升
- java - 如何通过 Selenium 和 Java 使用 AShot 库截取整页截图
- javascript - 用 JS 改变时间
- c# - 如何使用 epplus 从数据网格中保存 excel?
- email - 从 cli 中删除 amazon ses 模板
- php - laravel cron 工作不工作