title: “Concatenate layer” problem when doing GRAD-CAM. How to overcome this in my custom functional model?
body: <p>I am having problems with grad-cam. I would be grateful if anyone could help. my codes are here</p>
  <p><a href="https://www.kaggle.com/mervearmagan/gradcamproblem" rel="nofollow noreferrer">https://www.kaggle.com/mervearmagan/gradcamproblem</a></p>
  <p>Sorry, I couldn't fix the error I got</p>
  <blockquote>
  <p>ValueError: Input 0 is incompatible with layer model_1: expected
  shape=(None, 512, 512, 3), found shape=(512, 512, 3)</p>
  </blockquote>
  <pre><code>img = tf.keras.layers.Input(shape = IMG_SHAPE)
  gender = tf.keras.layers.Input(shape=(1,))
  base_model = tf.keras.applications.InceptionV3(input_shape = IMG_SHAPE, include_top = False, weights = 'imagenet')
  
  cnn_vec=base_model(img)
  cnn_vec = tf.keras.layers.GlobalAveragePooling2D()(cnn_vec)
  cnn_vec = tf.keras.layers.Dropout(0.20)(cnn_vec)
  gender_vec = tf.keras.layers.Dense(32,activation = 'relu')(gender)
  
  features = tf.keras.layers.Concatenate(axis=-1)([cnn_vec,gender_vec])
  
  dense_layer = tf.keras.layers.Dense(256,activation = 'relu')(features)
  dense_layer = tf.keras.layers.Dropout(0.1)(dense_layer)
  dense_layer = tf.keras.layers.Dense(128,activation = 'relu')(dense_layer)
  dense_layer = tf.keras.layers.Dropout(0.1)(dense_layer)
  dense_layer = tf.keras.layers.Dense(64,activation = 'relu')(dense_layer)
  output_layer = tf.keras.layers.Dense(1, activation = 'linear')(dense_layer)
  model = tf.keras.Model(inputs=[img,gender],outputs=output_layer`
  
  def make_gradcam_heatmap(img_array, model, last_conv_layer_name, classifier_layer_names):
          last_conv_layer = model.get_layer(last_conv_layer_name)
          last_conv_layer_model = tf.keras.Model(model.inputs, last_conv_layer.output)
          
          classifier_input = tf.keras.layers.Input(shape=last_conv_layer.output.shape)
          #classifier_input = tf.keras.layers.Input(shape=last_conv_layer.output.shape[1:])
          x = classifier_input
          for layer_name in classifier_layer_names:
              x = model.get_layer(layer_name)(x)
          classifier_model = tf.keras.Model(classifier_input, x)
          
          with tf.GradientTape() as tape:
              last_conv_layer_output =last_conv_layer_model(img_array)
               #last_conv_layer_model(img_array)
              tape.watch(last_conv_layer_output)
              
              preds = classifier_model(last_conv_layer_output)
              top_pred_index = tf.argmax(preds[0])
              top_class_channel = preds[:, top_pred_index]
              
          grads = tape.gradient(top_class_channel, last_conv_layer_output)
          pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
          
          last_conv_layer_output = last_conv_layer_output.numpy()[0]
          pooled_grads = pooled_grads.numpy()
          for i in range(pooled_grads.shape[-1]):
              last_conv_layer_output[:, :, i] *= pooled_grads[i]
          
          heatmap = np.mean(last_conv_layer_output, axis=-1)
          heatmap = np.maximum(heatmap, 0) / np.max(heatmap)
          return heatmap
      
  last_conv_layer_name = 'global_average_pooling2d'
  classifier_layer_names = ['dense_4']
  img = get_input('4360.png' ) 
  inputgender=tf.ones((1,1))
  image=tf.reshape(img,(1,512,512,3))
  heatmap = make_gradcam_heatmap([image,inputgender], model, last_conv_layer_name, classifier_layer_names)
  </code></pre>