j_krauss
j_krauss

Reputation: 1

Initializing Keras Convolution Kernel as a numpy array

I would like to initialize the weights for a (5,5) convolutional layer with four channels to be a numpy array. The input to this layer is of shape (128,128,1). In particular, I would like the following:

def custom_weights(shape, dtype=None):
    matrix = np.zeros((1,5,5,4))
    matrix[0,2,2,0,0] = 1
    matrix[0,2,1,0,0] = -1

    matrix[0,2,2,0,1] = 1
    matrix[0,3,2,0,1] = -1

    matrix[0,2,2,0,2] = 2
    matrix[0,2,1,0,2] = -1
    matrix[0,2,3,0,2] = -1

    matrix[0,2,2,0,3] = 2
    matrix[0,1,2,0,3] = -1
    matrix[0,3,2,0,3] = -1
    weights = K.variable(matrix)
    return weights

input_shape = (128, 128, 1)
images = Input(input_shape, name='phi_input')

conv1 = Conv2D(4,[5, 5], use_bias = False, kernel_initializer=custom_weights, padding='valid', name='Conv2D_1', strides=1)(images)

However, when I try to do this, I get an error of

Depth of input (1) is not a multiple of input depth of filter (5) for 'Conv2D_1_19/convolution' (op: 'Conv2D') with input shapes: [?,128,128,1], [1,5,5,4].

Is my error in the shape of the weight matrix?

Upvotes: 0

Views: 749

Answers (1)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11198

There are many inconsistencies (which led to errors) in your code, the error you're getting is not from the given code as it doesn't even index the matrix properly.

matrix = np.zeros((1,5,5,4))
matrix[0,2,2,0,0] = 1

You are initializing a numpy array with 4 dimensions but using 5 indices to change value.

Your dimensions for kernel weights are wrong. Here's the fixed code.

from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
import numpy as np

def custom_weights(shape, dtype=None):
    kernel = np.zeros((5,5,1,4))
    # change value here
    kernel = K.variable(kernel)
    return kernel

input_shape = (128, 128, 1)
images = Input(input_shape, name='phi_input')

conv1 = Conv2D(4,[5, 5], use_bias = False, kernel_initializer=custom_weights, padding='valid', name='Conv2D_1', strides=1)(images)

Upvotes: 1

Related Questions