I been trying to figure out how to create Conv3D layers with a custom kernel in Keras. The solution that I found is to pass the kernel as an initializer (reference) Could any one show an other a solution or help me fix this one below or show an example on how to manually create a custom Conv3D layer with a custom kernel?
I wrote this code:
my_kernel = [[[0.88121283, 0.91759044, 0.5578005 ],
[0.91488576, 0.9526534 , 0.5791151 ],
[0.88121283, 0.91759044, 0.5578005 ]],
[[0.88369477, 0.9201748 , 0.5593715 ],
[0.91746247, 0.9553365 , 0.5807462 ],
[0.88369477, 0.9201748 , 0.5593715 ]],
[[0.88121283, 0.91759044, 0.5578005 ],
[0.91488576, 0.9526534 , 0.5791151 ],
[0.88121283, 0.91759044, 0.5578005 ]]]
def my_init(shape, dtype=None):
kernel = my_kernel
assert kernel.shape == shape
return K.variable(kernel, dtype='float32')
inputs = Input((240, 240, 155, 4))
conv1 = Conv3D(64, (3,3,3), activation='relu', kernel_initializer=my_init((3,3,3)), padding='same')(inputs)
I got this error:
ValueError: Could not interpret initializer identifier: <tf.Variable 'Variable:0' shape=(3, 3, 3) dtype=float32, numpy=
array([[[0.88121283, 0.91759044, 0.5578005 ],
[0.91488576, 0.9526534 , 0.5791151 ],
[0.88121283, 0.91759044, 0.5578005 ]],
[[0.88369477, 0.9201748 , 0.5593715 ],
[0.91746247, 0.9553365 , 0.5807462 ],
[0.88369477, 0.9201748 , 0.5593715 ]],
[[0.88121283, 0.91759044, 0.5578005 ],
[0.91488576, 0.9526534 , 0.5791151 ],
[0.88121283, 0.91759044, 0.5578005 ]]], dtype=float32)>