model.fit() 导致 ‘TypeError: ‘module’ object is not callable’

每当我尝试使用TensorFlow训练我的模型时,我都会遇到一个TypeError。目前为止:

history = model.fit(train_batches,                    steps_per_epoch=train_steps,                    class_weight=class_weights,                    validation_data=validation_batches,                    validation_steps=val_steps,                    epochs=30,                    verbose=1,                    callbacks=callbacks_list                    )

会给我以下错误:

Traceback (most recent call last):  File "/home/brian/Desktop/381-deep-learning/main.py", line 359, in <module>    callbacks=callbacks_list  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper    return method(self, *args, **kwargs)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 815, in fit    model=self)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__    model=model)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 772, in __init__    peek, x = self._peek_and_restore(x)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 830, in _peek_and_restore    peek = next(x)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 104, in __next__    return self.next(*args, **kwargs)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 116, in next    return self._get_batches_of_transformed_samples(index_array)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 239, in _get_batches_of_transformed_samples    x = self.image_data_generator.standardize(x)  File "/home/brian/Desktop/381-deep-learning/venv/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py", line 704, in standardize    x = self.preprocessing_function(x)TypeError: 'module' object is not callable

这是我的模型定义:

resnet = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()predictions_layer = tf.keras.layers.Dense(7, activation='softmax')(x)# Create an Image Data Generator to input later into our model.data_generation = ImageDataGenerator(    # Use Inception ResNet v2    preprocessing_function=tf.keras.applications.inception_resnet_v2)model = tf.keras.Model(inputs=resnet.input, outputs=predictions_layer)

到目前为止,一切都正常运行,我还仔细检查了所有的导入语句(通常会导致此类错误),一切看起来都正常:

import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'import pandas as pdimport numpy as npimport shutilimport tensorflow as tffrom tensorflow.keras.layers import Dense, Dropoutfrom tensorflow.keras.optimizers import SGDfrom tensorflow.keras.metrics import categorical_crossentropyfrom tensorflow.keras.preprocessing.image import ImageDataGeneratorfrom tensorflow.keras.models import Modelfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpointfrom sklearn.metrics import confusion_matrixfrom tensorflow.keras.preprocessing.image import ImageDataGeneratorfrom sklearn.model_selection import train_test_splitfrom PIL import ImageFileImageFile.LOAD_TRUNCATED_IMAGES = True

到目前为止,我尝试过的一些方法包括降级TensorFlow版本(2.2.0 -> 1.15.2)和Keras版本(2.3.1 -> 1.2.2);这样做的唯一区别是我的模型可以进入第一个epoch,但随后以同样的错误失败。

另外,如果这有帮助的话,这里是我用来编译模型的方法:

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0007), loss='categorical_crossentropy',              metrics=[tf.keras.metrics.categorical_accuracy])

以及有问题的callbacks_list(然而,在使用Pycharm调试器后,这个列表的值如预期):

checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='categorical_accuracy', verbose=1,                             save_best_only=True, mode='max')reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='categorical_accuracy', factor=0.5, patience=2,                                   verbose=1, mode='max', min_lr=0.00001)callbacks_list = [checkpoint, reduce_lr]

我还提供了一个整个文件的pastebin,以防我遗漏了任何可能导致问题的重要信息: https://pastebin.com/raw/XCWj0b78


回答:

我调用了一个模块而不是模块中包含的类。

# Create an Image Data Generator to input later into our model.data_generation = ImageDataGenerator(    # Use Inception ResNet v2    preprocessing_function=tf.keras.applications.inception_resnet_v2.preprocess_input)

阅读文档后,我在生成器中使用了.preprocess_input()函数。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注