0

I'm using TF 2.10.0 with python 3.10.8 and running into TypeError: The added layer must be an instance of class Layer. Received: layer=<class 'keras.layers.pooling.max_pooling2d.MaxPooling2D'> of type <class 'type'>.

I referred to this >https://stackoverflow.com/questions/56089489/how-to-fix-the-added-layer-must-be-an-instance-of-class-layer-while-building-a article to try and fix it but no luck

Here's my code. Please tell me what I'm doing wrong

import tensorflow.keras.layers as layers
from tensorflow.keras.models import Sequential
import numpy as np
import PIL

model = Sequential([
    layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.MaxPool2D(),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPool2D,
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPool2D(),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes, name='output')
    ])

Please help :)

doryfied
  • 13
  • 7
  • Are you sure this is the actual code? Because it does not match with the error message, it clearly says keras.layers.pooling.max_pooling2d.MaxPooling2D, not the tf.keras MaxPool2D. This error happens when mixing keras and tf.keras imports. – Dr. Snoopy Nov 12 '22 at 08:41
  • @Dr.Snoopy Yes, I'm sure this is the actual code. All I'm doing is trying to import the libraries and building the CNN model. I have not imported keras explicitly. – doryfied Nov 12 '22 at 08:44
  • Ah yes, look at this line: layers.MaxPool2D, there is something missing here. – Dr. Snoopy Nov 12 '22 at 08:48
  • @Dr.Snoopy Yes. I got it! I missed a parentheses on the second call to MaxPool2D. THANK YOU! – doryfied Nov 12 '22 at 08:53
  • Parenthesis are missing, compare it with other MaxPool2D calls in your code. – Dr. Snoopy Nov 12 '22 at 08:54

1 Answers1

0

that is correct you did not specify it is a function but interesting looking at the custom class, and favorites working with CONV functions and that is the reason for side padding algorithms.

Sample: Sides padding, working is specific or using tf.pad()

import tensorflow as tf

class MyMaxPoolLayer( tf.keras.layers.MaxPool2D ):
    def __init__( self, units ):
        super(MyMaxPoolLayer, self).__init__( units )
        self.num_units = units

    def build(self, input_shape):
        self.kernel = self.add_weight("kernel",
        shape=[int(input_shape[-1]),
        self.num_units])

    def call(self, inputs):

        max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid')
        
        temp = tf.matmul(inputs, self.kernel)
        temp = tf.reshape(temp, [1, 10, 10, 1])
        temp = max_pool_2d( temp )
        return temp

start = 3
limit = 33
delta = 3
sample = tf.range(start, limit, delta)
sample = tf.cast( sample, dtype=tf.float32 )
sample = tf.constant( sample, shape=( 10, 1, 1, 1 ) )
layer = MyMaxPoolLayer(10)

print( layer(sample) )

Output: We need symmetric they call symmetries.

 ...
 [[ 14.819944  ]
   [ 10.318433  ]
   [ 10.318433  ]
   [  2.3505163 ]
   [-10.546914  ]
   [-11.872433  ]
   [ 13.375727  ]
   [ 13.375727  ]
   [  6.088965  ]]

  [[ 16.466604  ]
   [ 11.464926  ]
   [ 11.464926  ]
   [  2.6116848 ]
   [-11.865278  ]
   [-13.356487  ]
   [ 14.861918  ]
   [ 14.861918  ]
   [  6.7655163 ]]]], shape=(1, 9, 9, 1), dtype=float32)
General Grievance
  • 4,555
  • 31
  • 31
  • 45