RESNET50 problem with broadcasting

Hi all,
I am getting the below error, as per my knowledge I am doing everything right. Can anyone help please?

ValueError                                Traceback (most recent call last)
<ipython-input-60-25bee053480c> in <module>
----> 1 model = ResNet50(input_shape = (64, 64, 3), classes = 6)
      2 print(model.summary())

<ipython-input-59-209814454198> in ResNet50(input_shape, classes)
     37 
     38     ## Stage 3 (≈4 lines)
---> 39     X = convolutional_block(X, f = 3, filters = [128, 128, 512], s = 2)
     40     X = identity_block(X, 3, [128, 128, 512])
     41     X = identity_block(X, 3, [128, 128, 512])

<ipython-input-57-07dd0bca91a8> in convolutional_block(X, f, filters, s, training, initializer)
     52 
     53     # Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
---> 54     X = Add()([X, X_shortcut])
     55     X = Activation('relu')(X)
     56 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    924     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    925       return self._functional_construction_call(inputs, args, kwargs,
--> 926                                                 input_list)
    927 
    928     # Maintains info about the `Layer.call` stack.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1096         # Build layer if applicable (if the `build` method has been
   1097         # overridden).
-> 1098         self._maybe_build(inputs)
   1099         cast_inputs = self._maybe_cast_inputs(inputs, input_list)
   1100 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2641         # operations.
   2642         with tf_utils.maybe_init_scope(self):
-> 2643           self.build(input_shapes)  # pylint:disable=not-callable
   2644       # We must set also ensure that the layer is marked as built, and the build
   2645       # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    321     if input_shape is not None:
    322       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 323     output_shape = fn(instance, input_shape)
    324     # Return shapes from `fn` as TensorShapes.
    325     if output_shape is not None:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    110       else:
    111         shape = input_shape[i][1:]
--> 112       output_shape = self._compute_elemwise_op_output_shape(output_shape, shape)
    113     # If the inputs have different ranks, we have to reshape them
    114     # to make them broadcastable.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
     83           raise ValueError(
     84               'Operands could not be broadcast '
---> 85               'together with shapes ' + str(shape1) + ' ' + str(shape2))
     86         output_shape.append(i)
     87     return tuple(output_shape)

ValueError: Operands could not be broadcast together with shapes (8, 8, 512) (4, 4, 512)

Are you sure that your convolutional_block function passes all its test cases? The arguments you are passing to the convolutional_block look correct. But notice that where the error occurs is that within the convolutional_block function, the shortcut path gives a different shaped output than the main path. They should match. So it seems like that is the place to look for the error: in your convolutional_block logic.

Hi Paul,
The convolutional_block seems pretty simple to me and I have copied and pasted that adjusting the filters. Still I can see the error raised from convolutional_block. any suggestion?

Did you actually run the test cases for convolutional_block and make sure that they all pass?

Hi Paul,
I dont know what do you mean exactly? is there anything else to run prior to model = ResNet50(input_shape = (64, 64, 3), classes = 6)
print(model.summary())?

I am getting the error here.

Yes, there are earlier cells that test both your identity_block function and your convolutional_block function. You need to make sure that all of those tests pass first, before you go on to the full ResNet50 function.

Try doing “Kernel → Restart and Clear Output”, then “Cell → Run All”. Then go back to the beginning of the notebook and scroll through all the code cells and examine the output. Are there any errors thrown before you get to the resnet50 cell?