【问题标题】:FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ../Saved_Model/1\variables\variablesFileNotFoundError:不成功的 TensorSliceReader 构造函数:未能找到任何匹配的文件 ../Saved_Model/1\\variables\\variables
【发布时间】:2022-09-23 16:50:31
【问题描述】:

我正在尝试构建图像分类器 API。使用 Google Colab 构建模型,因为我没有 GPU。我正在使用 CPU 并将模型下载到 API 应用程序中。

但是当我尝试访问我的模型目录 Saved_Model 时出现此错误。 我知道这与 GPU 和 CUDA 设置有关,但我不知道什么是错误的,或者因为我使用 CPU 是如何排序的。

完全错误:

    Elijah-A-W@DESKTOP-34M2E8U MINGW64 /d/myn/ML Prediction Project/New folder/Detection Potato Lite/Api
$ python main.py
2022-07-29 09:12:32.654485: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 
\'cudart64_110.dll\'; dlerror: cudart64_110.dll not found
2022-07-29 09:12:32.670439: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not 
have a GPU set up on your machine.
2022-07-29 09:13:18.928444: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 
\'nvcuda.dll\'; dlerror: nvcuda.dll not found
2022-07-29 09:13:18.928809: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2022-07-29 09:13:18.934497: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-34M2E8U
2022-07-29 09:13:18.935291: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-34M2E8U
2022-07-29 09:13:19.068867: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File \"D:\\myn\\ML Prediction Project\\New folder\\Detection Potato Lite\\Api\\main.py\", line 10, in <module>
    MODEL = tf.keras.models.load_model(\"../Saved_Model/1\")
  File \"C:\\Users\\Elijah-A-W\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\utils\\traceback_utils.py\", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File \"C:\\Users\\Elijah-A-W\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\tensorflow\\python\\saved_model\\load.py\", line 915, in load_partial
    raise FileNotFoundError(
FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ../Saved_Model/1\\variables\\variables
 You may be trying to load on a different device from the computational device. Consider setting the `experimental_io_device` option in `tf.saved_model.LoadOptions` to the io_device such as \'/job:localhost\'.

完整代码:

from fastapi import FastAPI, File, UploadFile
import uvicorn 
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf

app = FastAPI()

MODEL = tf.keras.models.load_model(\"../Saved_Model/1\")
CLASS_NAMES = [\"Early Blight\", \"Late Blight\", \"Healthy\"]


@app.get(\"/ping\")
async def ping():
    return \"hello, I am alive\"

async def read_file_as_image(data) -> np.ndarray:
    image = np.array(Image.open(BytesIO(data)))     # reading an image as byte & converting into array 
    img_batch = np.expand_dims(image, 0)            # adding extra dimesnion to the loaded img batch 
    prediction = MODEL.predict(img_batch)           # calling the model predict the image batch
    pass

@app.post(\"/predict\")
async def predict(file: UploadFile = File(...)):
    image = read_file_as_image(await file.read())
    return image 

if __name__ == \"__main__\":
 
    uvicorn.run(app, host=\'localhost\', port=5000)

这是项目目录的图像 [![在此处输入图像描述][1]][1]

这是 [1]:https://i.stack.imgur.com/Y4Bg0.png

    标签: python gpu tensorflow2.0 fastapi


    【解决方案1】:

    没能解决这个错误,所以决定在我的本地机器上训练和保存模型然后调用它。它工作正常。

    【讨论】:

      【解决方案2】:

      我有同样的问题并尝试了这个。

      然后它起作用了。

      from keras.models import load_model
      model.save('model.h5')
      model_final = load_model('model.h5')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-04
        • 2022-08-04
        • 2021-06-11
        • 2019-05-16
        • 2018-10-23
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        相关资源
        最近更新 更多