繁体   English   中英

FileNotFoundError:不成功的 TensorSliceReader 构造函数:未能找到任何匹配的文件 ../Saved_Model/1\variables\variables

[英]FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ../Saved_Model/1\variables\variables

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

但是当我尝试访问我的 model 目录 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

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

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

然后它起作用了。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM