代码之家  ›  专栏  ›  技术社区  ›  Navaneeth kini K

AttributeError:模块“numpy”没有属性“object”。安装升级版本的numpy和tensorflow后未解决

  •  0
  • Navaneeth kini K  · 技术社区  · 2 年前
    import tensorflow as tf
    from tensorflow.keras.models import load_model
    from fastapi import FastAPI, File, UploadFile
    from io import BytesIO
    from PIL import Image
    import numpy as np
    import uvicorn
    
    app = FastAPI()
    
    MODEL = load_model("../models/1")
    CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]
    
    @app.get("/ping")
    async def ping():
        return "Hello, I am alive"
    
    def read_file_as_image(data) -> np.ndarray:
        image = np.array(Image.open(BytesIO(data)))
        return image
    
    @app.post("/predict")
    async def predict(file: UploadFile = File(...)):
        image = read_file_as_image(await file.read())
        image_batch = np.expand_dims(image, axis=0)
        predictions = MODEL.predict(image_batch)
        predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
        confidence = np.max(predictions[0])
        return {
            'class': predicted_class,
            'confidence': float(confidence)
        }
    
    if __name__ == "__main__":
        uvicorn.run(app, host='localhost', port=8000)
    
    AttributeError: module 'numpy' has no attribute 'object'.
    `np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. 
    The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
        https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
    

    我升级到了的最新版本 numpy tensorflow ,但问题依然存在。

    如何解决此问题?

    0 回复  |  直到 2 年前
        1
  •  0
  •   Musabbir Arrafi    2 年前

    自版本1.24起 numpy , np.object 已弃用,需要替换为 object -发布说明就是这么说的( numpy release notes ). 您需要在代码或正在使用的另一个包中更新此项。 由于您使用的是经过训练的模型,因此一种解决方案就是恢复到 numpy=1.23.4 它将解决以下问题:

    pip install numpy==1.23.4
    

    P.S.输入 numpy 1.23.4 它仍然被弃用,但没有从包中完全删除,所以它仍然会抛出警告,但它会起作用。如果您不想要警告,也可以返回 numpy==1.19 但这可能会产生其他依赖关系的问题,所以我建议使用 numpy==1.23.4 和使用 warnings 库忽略警告消息

    这是我的测试代码:

    import warnings
    import numpy as np
    warnings.filterwarnings("ignore")
    print("Version: ",np.__version__)
    print("np.object: ",np.object)
    

    输出

    Version:  1.23.4
    np.object:  <class 'object'>
    
    推荐文章