title: Using pretrained model with keras: AttributeError: 'NoneType' object has no attribute 'shape'
body: <p>I'm running a Keras Neural Network model for a binary classification of images.<br/>
  I use the first layer of a pretrained VGG16 model and i created the last fully connected layers from the tutorial:<br/>
  <a href="https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html" rel="nofollow noreferrer">https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html</a></p>
  <p>With Tensorflow backend 2.3.1, Python 3.6, Keras 2.4.3</p>
  <p>While i'm training my model (using presaved weights) with an ImageDataGenerator, this exception occurs:<br/></p>
  <pre><code>AttributeError: 'NoneType' object has no attribute 'shape'
  </code></pre>
  <p>That's my code<br/></p>
  <pre><code>top_model_weight_path = 'feat_extr_model.h5'
  train_data_dir = 'data/train'
  validation_data_dir = 'data/validation'
  nb_train_samples = 54
  nb_validation_samples = 6
  epochs = 10
  batch_size = 16    
  
  base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
  
  top_model = Sequential()
  top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
  top_model.add(Dense(256, activation='relu'))
  top_model.add(Dropout(0.5))
  top_model.add(Dense(1, activation='sigmoid'))
  # note that it is necessary to start with a fully-trained
  # classifier, including the top classifier,
  # in order to successfully do fine-tuning
  top_model.load_weights(top_model_weight_path)
  
  model = Model(inputs=base_model.input, outputs=top_model(base_model.output))
  
  for l in model.layers[:15]:
      l.trainable = False
  model.compile(loss='binary_crossentropy',
                optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
                metrics=['accuracy'])
  
  train_datagen = ImageDataGenerator(rescale=1./255,
                                     shear_range=0.2,
                                     zoom_range=0.2,
                                     horizontal_flip=True)
  test_datagen = ImageDataGenerator(rescale=1./255)
  
  train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                      target_size=(224, 224),
                                                      batch_size=batch_size,
                                                      class_mode=None)
  validation_generator = test_datagen.flow_from_directory(validation_data_dir,
                                                          target_size=(224,224),
                                                          batch_size=batch_size,
                                                          class_mode=None)
  
  model.summary()
  
  # fine-tune the model
  model.fit_generator(
      train_generator,
      epochs=epochs,
      validation_data=validation_generator,
      verbose=2)
  </code></pre>
  <p>The full error message is the following:<br/></p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:/Users/Luca Mancini/Desktop/Python Project/tutorialPretrained/tutorial.py&quot;, line 226, in &lt;module&gt;
      verbose=2)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\deprecation.py&quot;, line 324, in new_func
      return func(*args, **kwargs)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py&quot;, line 1829, in fit_generator
      initial_epoch=initial_epoch)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py&quot;, line 108, in _method_wrapper
      return method(self, *args, **kwargs)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py&quot;, line 1098, in fit
      tmp_logs = train_function(iterator)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 780, in __call__
      result = self._call(*args, **kwds)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 823, in _call
      self._initialize(args, kwds, add_initializers_to=initializers)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 697, in _initialize
      *args, **kwds))
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 2855, in _get_concrete_function_internal_garbage_collected
      graph_function, _, _ = self._maybe_define_function(args, kwargs)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 3213, in _maybe_define_function
      graph_function = self._create_graph_function(args, kwargs)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 3075, in _create_graph_function
      capture_by_value=self._capture_by_value),
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py&quot;, line 986, in func_graph_from_py_func
      func_outputs = python_func(*func_args, **func_kwargs)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 600, in wrapped_fn
      return weak_wrapped_fn().__wrapped__(*args, **kwds)
    File &quot;C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py&quot;, line 973, in wrapper
      raise e.ag_error_metadata.to_exception(e)
  AttributeError: in user code:
  
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function  *
          return step_function(self, iterator)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
          return self._call_for_each_replica(fn, args, kwargs)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
          return fn(*args, **kwargs)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step  **
          outputs = model.train_step(data)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py:759 train_step
          self.compiled_metrics.update_state(y, y_pred, sample_weight)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:388 update_state
          self.build(y_pred, y_true)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:319 build
          self._metrics, y_true, y_pred)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1139 map_structure_up_to
          **kwargs)
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1235 map_structure_with_tuple_paths_up_to
          *flat_value_lists)]
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1234 &lt;listcomp&gt;
          results = [func(*args, **kwargs) for args in zip(flat_path_list,
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1137 &lt;lambda&gt;
          lambda _, *values: func(*values),  # Discards the path arg.
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:419 _get_metric_objects
          return [self._get_metric_object(m, y_t, y_p) for m in metrics]
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:419 &lt;listcomp&gt;
          return [self._get_metric_object(m, y_t, y_p) for m in metrics]
      C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:440 _get_metric_object
          y_t_rank = len(y_t.shape.as_list())
  
      AttributeError: 'NoneType' object has no attribute 'shape'
  </code></pre>
  <p>Can someone explain me where is the problem and how i can fix it?
  Thank you</p>