Search code examples
pythondeep-learningconv-neural-network

Absolute value layer in CNN


I'm new to CNN. I'm trying to build a CNN, and I have the following steps:

model.add(Conv2D(8, (5, 5), input_shape=(256, 256, 1), padding='same', use_bias=False)) # use_bias=False
model.add(BatchNormalization())
model.add(Activation(tanh))
model.add(AveragePooling2D (pool_size= (5,5), strides=2))

I'm trying to add absolute value layer after the first step after applying 2DConv.. I'm read the answer in the link Absolute value layer in CNN trying the following code:

rom keras.models import model_from_json
model.add(Conv2D(8, (5, 5), input_shape=(256, 256, 1), padding='same', use_bias=False)) # use_bias=False
    outputlayer1 = model.layers[0].output
    outputlayer = abs(outputlayer1)
    model.layers[0].output = outputlayer
    new_model = model_from_json(model.to_json())
    new_model.summary()

I'm getting the error:

AttributeError: can't set attribute

Please, how I can applying the absolute value on the outputs of 2Dconv layer. With my thanks


Solution

  • One way to access the output of layer number 2, applied to an image would be :

    from keras.models import Model 
    model_part = Model(input = model.input,
                   output = model.layers[1].output)
    vals = model.predict(image)
    print(vals)
    

    Either way, you should check if an absolute value layer is useful in your case, or if it would already be implicit in one of your following layers.