如何在Keras中使用自定义2D卷积内核进行实验? [英] How to experiment with custom 2d-convolution kernels in Keras?
问题描述
在具有kernel_size=3
的默认Conv2D
层中,其中一个过滤器的切片的权重可以这样命名:
In a default Conv2D
layer with kernel_size=3
the weights of a slice of one of the filters could be named like this:
A B C
D E F
G H I
使用kernel_size=5
这样:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
现在,我想基于带有conv层的conv层构建(和训练/测试)一个模型,
Now I'd like to build (and train/test) a model based on conv layers with kernels like that:
A A B C C
A A B C C
D D E F F
G G H I I
G G H I I
如何实现这样的自定义层喜欢吗?
How could the implementation of such a custom layer look like?
推荐答案
也许像这样吗?
class CustomConv2D(Layer):
def __init__(self, filters, **kwargs):
self.filters = filters
self.kernel_size = (3, 3)
super(CustomConv2D, self).__init__(**kwargs)
def build(self, input_shape):
# only have a 3x3 kernel
shape = self.kernel_size + (input_shape[-1], self.filters)
self.kernel = self.add_weight(name='kernel', shape=shape,
initializer='glorot_uniform')
super(CustomConv2D, self).build(input_shape)
def call(self, x):
# duplicate rows 0 and 2
dup_rows = K.stack([self.kernel[0]]*2 + [self.kernel[1]] + [self.kernel[2]]*2, axis=0)
# duplicate cols 0 and 2
dup_cols = K.stack([dup_rows[:,0]]*2 + [dup_rows[:,1]] + [dup_rows[:,2]]*2, axis=1)
# having a 5x5 kernel now
return K.conv2d(x, dup_cols)
def compute_output_shape(self, input_shape):
return input_shape[:-1] + (self.filters,)
诀窍是在3x3内核中(硬编码,您可能要泛化它)仅将每个过滤器仅存储9个权重,并复制第一行和最后一行和最后一列,以使其成为所需的5x5内核.然后,将此内核传递给K.conv2d()
,就像在原始的Conv2d实现.
The trick is to simply store only 9 weights per filter in a 3x3 kernel (hard coded, you may want to generalize it) and to duplicate the first and last rows and columns to make it a 5x5 kernel the way you want it. Then this kernel is passed to K.conv2d()
just like in the original Conv2d implementation.
我对其进行了测试,它似乎可以正常工作.您可能需要添加其他参数,例如padding,bias等.
I tested it and it seems to be working. You may want to add other parameters like padding, bias, etc.
这篇关于如何在Keras中使用自定义2D卷积内核进行实验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!