How To Manipulate (constrain) The Weights Of The Filter Kernel In Conv2d In Keras?
I understand that there are several options for kernel_constraint in Conv2D in Keras: max_norm, non_neg or unit_norm.. But what I needed is to set the anchor (center) position in t
Solution 1:
You need a custom Conv2D layer for that, where you change its call method to apply the zero at the center.
classZeroCenterConv2D(Conv2D):
def__init__(self, filters, kernel_size, **kwargs):
super(ZeroCenterConv2D, self).__init__(filters, kernel_size, **kwargs)
defcall(self, inputs):
assert self.kernel_size[0] % 2 == 1, "Error: the kernel size is an even number"assert self.kernel_size[1] % 2 == 1, "Error: the kernel size is an even number"
centerX = (self.kernel_size[0] - 1) // 2
centerY = (self.kernel_size[1] - 1) // 2
kernel_mask = np.ones(self.kernel_size + (1, 1))
kernel_mask[centerX, centerY] = 0
kernel_mask = K.variable(kernel_mask)
customKernel = self.kernel * kernel_mask
outputs = K.conv2d(
inputs,
customKernel,
strides=self.strides,
padding=self.padding,
data_format=self.data_format,
dilation_rate=self.dilation_rate)
if self.use_bias:
outputs = K.bias_add(
outputs,
self.bias,
data_format=self.data_format)
if self.activation isnotNone:
return self.activation(outputs)
return outputs
This will not replace the actual weights, though, but the center ones will never be used.
When you use layer.get_weights()
of model.get_weights()
, you will see the center weights as they were initialized (not as zeros).
Post a Comment for "How To Manipulate (constrain) The Weights Of The Filter Kernel In Conv2d In Keras?"