0
$\begingroup$

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
$\endgroup$
1
$\begingroup$

The problem is that your ResNet-18 follows the architecture for ImageNet as outlined in the ResNet paper:

Table 1: ResNet for ImageNet

However, spatial input dimensions of ImageNet are different from CIFAR10 (32x32) so the architecture does not match your input. Instead you can follow the author's description of their CIFAR10 architecture in section 4.2 of the same paper:

The plain/residual architectures follow the form in Fig. 3 (middle/right). The network inputs are 32x32 images, with the per-pixel mean subtracted. The first layer is 3x3 convolutions. Then we use a stack of 6n layers with 3x3 convolutions on the feature maps of sizes 32; 16; 8 respectively, with 2n layers for each feature map size. The numbers of filters are 16; 32; 64 respectively. The subsampling is performed by convolutions with a stride of 2. The network ends with a global average pooling, a 10-way fully-connected layer, and softmax.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.