Skip to content
Advertisement

Using pretrained model with keras: AttributeError: ‘NoneType’ object has no attribute ‘shape’

I’m running a Keras Neural Network model for a binary classification of images.
I use the first layer of a pretrained VGG16 model and i created the last fully connected layers from the tutorial:
https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

With Tensorflow backend 2.3.1, Python 3.6, Keras 2.4.3

While i’m training my model (using presaved weights) with an ImageDataGenerator, this exception occurs:

AttributeError: 'NoneType' object has no attribute 'shape'

That’s my 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)

The full error message is the following:

Traceback (most recent call last):
  File "C:/Users/Luca Mancini/Desktop/Python Project/tutorialPretrained/tutorial.py", line 226, in <module>
    verbose=2)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonutildeprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py", line 1829, in fit_generator
    initial_epoch=initial_epoch)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py", line 1098, in fit
    tmp_logs = train_function(iterator)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerdef_function.py", line 780, in __call__
    result = self._call(*args, **kwds)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerdef_function.py", line 823, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerdef_function.py", line 697, in _initialize
    *args, **kwds))
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerfunction.py", line 2855, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerfunction.py", line 3213, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerfunction.py", line 3075, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonframeworkfunc_graph.py", line 986, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythoneagerdef_function.py", line 600, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:anaconda3envstensorlibsite-packagestensorflowpythonframeworkfunc_graph.py", line 973, in wrapper
    raise e.ag_error_metadata.to_exception(e)
AttributeError: in user code:

    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py:806 train_function  *
        return step_function(self, iterator)
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:anaconda3envstensorlibsite-packagestensorflowpythondistributedistribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:anaconda3envstensorlibsite-packagestensorflowpythondistributedistribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:anaconda3envstensorlibsite-packagestensorflowpythondistributedistribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py:789 run_step  **
        outputs = model.train_step(data)
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginetraining.py:759 train_step
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginecompile_utils.py:388 update_state
        self.build(y_pred, y_true)
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginecompile_utils.py:319 build
        self._metrics, y_true, y_pred)
    C:anaconda3envstensorlibsite-packagestensorflowpythonutilnest.py:1139 map_structure_up_to
        **kwargs)
    C:anaconda3envstensorlibsite-packagestensorflowpythonutilnest.py:1235 map_structure_with_tuple_paths_up_to
        *flat_value_lists)]
    C:anaconda3envstensorlibsite-packagestensorflowpythonutilnest.py:1234 <listcomp>
        results = [func(*args, **kwargs) for args in zip(flat_path_list,
    C:anaconda3envstensorlibsite-packagestensorflowpythonutilnest.py:1137 <lambda>
        lambda _, *values: func(*values),  # Discards the path arg.
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginecompile_utils.py:419 _get_metric_objects
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginecompile_utils.py:419 <listcomp>
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    C:anaconda3envstensorlibsite-packagestensorflowpythonkerasenginecompile_utils.py:440 _get_metric_object
        y_t_rank = len(y_t.shape.as_list())

    AttributeError: 'NoneType' object has no attribute 'shape'

Can someone explain me where is the problem and how i can fix it? Thank you

Advertisement

Answer

The error is here:

class_mode=None

From the docs:

class_mode: One of “categorical”, “binary”, “sparse”, “input”, or None. Default: “categorical”. Determines the type of label arrays that are returned: – “categorical” will be 2D one-hot encoded labels, – “binary” will be 1D binary labels, “sparse” will be 1D integer labels, – “input” will be images identical to input images (mainly used to work with autoencoders). – If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.

You’re not giving any labels to your model. You seem to have 2 classes so it should be:

class_mode='binary'
Advertisement