0

I am trying to do a custom convolution operation by creating a custom layer in Keras.

However, I am getting a FailedPreconditionError()

This is the code I wrote for the call function is...

def call(self, x):
        kern_x,kern_y = self.kernel.get_shape().as_list()
        img_x,img_y = x.get_shape().as_list()[2:4]
        activation_map = np.random.random((img_x - kern_x + 1, img_y - kern_y + 1))
        img = tf.Session().run(tf.Variable(x,validate_shape=False)) #this is where I am getting the error
        kern = tf.Session().run(tf.constant(self.kernel))

        for i in range(activation_map.shape[0]):
            for j in range(activation_map.shape[1]):
                activation_map[i][j] = fn1(fn2(img[i:i+kern_x,j:j+kern_y],kern))

        return tf.convert_to_tensor(fuzzy_activation_map, np.float32)

x is a <tf.Tensor 'max_pooling2d_1/transpose_1:0' shape=(?, 32, 12, 12) dtype=float32>

The loops are the main part where I am trying to implement a custom filter that I found in a paper. I have separately designed the filter and it works when the input image and the kernel are numpy 2D arrays. I just need the kernel to be trainable.

Rangan Das
  • 323
  • 2
  • 11
  • in https://www.tensorflow.org/api_docs/python/tf/errors/FailedPreconditionError is `op` that gives a hint for the cause of the error. can you catch the error to get the op like in https://stackoverflow.com/questions/33239308/how-to-get-exception-message-in-python-properly ?. for catching all errors cf **General Error Catching** in https://wiki.python.org/moin/HandlingExceptions How do you call the function `call(self,x)` ? – ralf htp Aug 24 '18 at 18:24
  • I am using the Keras documentation on writing custom layers where the main logic of the layer is in the call function. I do not know how that function is being called. However, I can show the layers ` model = Sequential()` `model.add(Conv2D(32, (5, 5), input_shape=(1, 28, 28), activation='relu'))` `model.add(MaxPooling2D(pool_size=(2, 2)))` `model.add(CustomConv2D(5)) #this is the custom layer` ` model.add(Dropout(0.2))` ` model.add(Flatten())` ` model.add(Dense(128, activation='relu'))` ` model.add(Dense(num_classes, activation='softmax'))` – Rangan Das Aug 24 '18 at 18:58

0 Answers0