AttributeError: 'function' object has no attribute 'preprocess_input'

AttributeError: 'function' object has no attribute 'preprocess_input'

我想试试 MobileNetV2 的预测。我总是收到错误消息,但我不知道要更改什么。

import keras
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.applications import imagenet_utils
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications.mobilenetv2 import decode_predictions
from keras.applications.mobilenetv2 import MobileNetV2
from keras.applications.mobilenetv2 import preprocess_input
import numpy as np
from IPython.display import Image
from keras.optimizers import Adam

mobilenet = MobileNetV2()

def process_image(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    pImg = MobileNetV2.preprocess_input(img_array)
    return pImg

process_image(IMG_PATH)

这是错误消息。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-6cdb09c1b552> in <module>()
----> 1 process_image('IMG_PATH')

<ipython-input-28-1b1187d4c25c> in process_image(img_path)
      3     img_array = image.img_to_array(img)
      4     img_array = np.expand_dims(img_array, axis=0)
----> 5     pImg = MobileNetV2.preprocess_input(img_array)
      6     return pImg

AttributeError: 'function' object has no attribute 'preprocess_input'

也许有人可以帮助我。

如果你查看 keras_applications/mobilenet_v2 你会发现 MobileNetV2 没有属性 preprocess_input

您可以从 mobilenet_v2 导入 preprocess_input:

from keras.applications.mobilenetv2 import preprocess_input

那么你可以使用:

pImg = preprocess_input(img_array)