FileNotFoundError:TensorSliceReader构造函数不成功:找不到的任何匹配文件/Saved_



我正在尝试构建一个图像分类器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:mynML Prediction ProjectNew folderDetection Potato LiteApimain.py", line 10, in <module>
MODEL = tf.keras.models.load_model("../Saved_Model/1")
File "C:UsersElijah-A-WAppDataLocalProgramsPythonPython310libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersElijah-A-WAppDataLocalProgramsPythonPython310libsite-packagestensorflowpythonsaved_modelload.py", line 915, in load_partial
raise FileNotFoundError(
FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ../Saved_Model/1variablesvariables
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)

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

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

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

然后它起作用了。

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

最新更新