使用带有 keras 的预训练模型:AttributeError: 'NoneType' object has no attribute 'shape'

Posted

技术标签:

【中文标题】使用带有 keras 的预训练模型:AttributeError: \'NoneType\' object has no attribute \'shape\'【英文标题】:Using pretrained model with keras: AttributeError: 'NoneType' object has no attribute 'shape'使用带有 keras 的预训练模型:AttributeError: 'NoneType' object has no attribute 'shape' 【发布时间】:2021-01-24 15:43:15 【问题描述】:

我正在运行 Keras 神经网络模型来对图像进行二元分类。 我使用预训练的 VGG16 模型的第一层,并根据教程创建了最后一个全连接层:https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

使用 TensorFlow 后端 2.3.1、Python 3.6、Keras 2.4.3

当我使用 ImageDataGenerator 训练我的模型(使用预先保存的权重)时,会出现以下异常:

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

这是我的代码

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)

完整的错误信息如下:

Traceback (most recent call last):
  File "C:/Users/Luca Mancini/Desktop/Python Project/tutorialPretrained/tutorial.py", line 226, in <module>
    verbose=2)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1829, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
    tmp_logs = train_function(iterator)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
    result = self._call(*args, **kwds)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 697, in _initialize
    *args, **kwds))
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\framework\func_graph.py", 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 <listcomp>
        results = [func(*args, **kwargs) for args in zip(flat_path_list,
    C:\anaconda3\envs\tensor\lib\site-packages\tensorflow\python\util\nest.py:1137 <lambda>
        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 <listcomp>
        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'

谁能解释一下问题出在哪里以及如何解决? 谢谢

【问题讨论】:

代码中的第 226 行是哪一行? model.fit_generator( train_generator, epochs=epochs, validation_data=validation_generator, verbose=2) 就是“verbose=2”那一行 【参考方案1】:

错误在这里:

class_mode=None

来自docs:

class_mode:“分类”、“二进制”、“稀疏”、“输入”或无之一。默认值:“分类”。确定返回的标签数组的类型:-“categorical”将是 2D one-hot 编码标签,-“binary”将是 1D 二进制标签,“sparse”将是 1D 整数标签,-“input”将是相同的图像输入图像(主要用于自动编码器)。 - 如果为None,则不返回任何标签(生成器只会产生批量图像数据,这对于与model.predict() 一起使用很有用)。请注意,在 class_mode None 的情况下,数据仍然需要驻留在 directory 的子目录中才能正常工作。

您没有给模型添加任何标签。您似乎有 2 个班级,所以应该是:

class_mode='binary'

【讨论】:

谢谢,旧版本的“无”仍然存在。 好的。如果我的建议有效,请告诉我,如果有效,请毫不犹豫地对答案进行投票。

以上是关于使用带有 keras 的预训练模型:AttributeError: 'NoneType' object has no attribute 'shape'的主要内容,如果未能解决你的问题,请参考以下文章

keras中的预训练对象检测模型

如何在 Keras 中的预训练 InceptionResNetV2 模型的不同层中找到激活的形状 - Tensorflow 2.0

keras中使用预训练模型进行图片分类

PyTorch 的预训练,是时候学习一下了

如何使用带有灰度图像的预训练神经网络?

Pytorch - 跳过计算每个时期的预训练模型的特征