At avergae pooling after the ConvNet, the error is displayed as the dimensions cannot be negative because the shape the previous output layer is 1,1,512 and on this the maxpooling cannot be done. Is it something that i did wrong in the architecture design?
def identity_block2(X,f,filters):
f1,f2 = filters
X_init = X
X = Conv2D(f1,(3,3),strides=(1,1),padding='same')(X)
X = BatchNormalization()(X)
X = Activation('relu')(X)
X = Conv2D(f1,(3,3),strides=(1,1),padding='same')(X)
X = BatchNormalization()(X)
X = Activation('relu')(X)
X = Add()([X,X_init])
X = Activation('relu')(X)
return X
def conv_block2(X,f,filters,s=2):
f1,f2 = filters
X_init = X
X = Conv2D(f1,(3,3),strides=(s,s),padding='same')(X)
X = BatchNormalization()(X)
X = Activation('relu')(X)
X = Conv2D(f2,(3,3),strides=(1,1),padding='same')(X)
X = BatchNormalization()(X)
X = Activation('relu')(X)
X_init = Conv2D(f1,(3,3),strides=(s,s),padding='same')(X_init)
X_init = BatchNormalization()(X_init)
X = Add()([X,X_init])
X = Activation('relu')(X)]
return X
def resnet18(input_shape,classes):
X_input = Input(shape=input_shape)
X = ZeroPadding2D((3,3))(X_input)
X = Conv2D(64,(7,7),strides=(2,2))(X)
X = MaxPooling2D((3,3),strides=(2,2))(X)
X = conv_block2(X,3,[64,64],s=1)
X = identity_block2(X,3,[64,64])
X = Dropout(0.4)(X)
X = conv_block2(X,3,[128,128],s=2)
X = identity_block2(X,3,[128,128])
X = Dropout(0.4)(X)
X = conv_block2(X,3,[256,256],s=2)
X = identity_block2(X,3,[256,256])
X = Dropout(0.4)(X)
X = conv_block2(X,3,[512,512],s=2)
X = identity_block2(X,3,[512,512])
X = Dropout(0.4)(X)
# It shows error at this point as the dimension of the previous output layer is (1,1,512)
# X = AveragePooling2D((2,2))(X)
X = Flatten()(X)
X = Dense(classes,activation='softmax')(X)
model = Model(inputs=X_input,outputs=X,name='ResNet18')
return model