You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CPU环境加载Colab导出TensorFlow模型遇FileNotFoundError求助

问题描述

在Google Colab(本地无GPU)训练图像分类器并导出模型,下载到本地CPU环境的FastAPI应用后,加载Saved_Model/1目录下的模型时触发FileNotFoundError,怀疑与GPU/CUDA设置有关,但无法准确定位问题。

完整错误信息

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)

项目目录结构

  • Detection Potato Lite/
    • Api/
      • main.py
    • Saved_Model/
      • 1/
        • (包含模型核心文件:variables文件夹、saved_model.pb等)
解决方案

1. 优先解决路径问题

错误核心是找不到模型文件,CUDA警告属于本地无GPU的正常提示,无需处理。

  • 用绝对路径替代相对路径:避免当前工作目录导致的路径偏差,修改模型加载代码:
    import os
    # 获取当前脚本所在目录
    script_dir = os.path.dirname(os.path.abspath(__file__))
    # 拼接模型绝对路径
    model_path = os.path.join(script_dir, "../Saved_Model/1")
    MODEL = tf.keras.models.load_model(model_path)
    
  • 验证路径正确性:加载前打印model_path,确认目录存在且包含variables文件夹和saved_model.pb文件。

2. 处理跨设备加载提示

若路径确认正确仍报错,添加加载选项强制指定CPU设备:

MODEL = tf.keras.models.load_model(
    model_path,
    options=tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')
)

3. 检查模型完整性

确认从Colab下载的Saved_Model/1目录完整,必须包含:

  • saved_model.pb
  • variables/文件夹(内有variables.data-00000-of-00001和variables.index)
    若文件缺失,重新从Colab导出并完整下载。

4. 修复代码逻辑问题

原代码中read_file_as_image函数无返回值,会导致predict接口报错,修改如下:

async def read_file_as_image(data):
    image = np.array(Image.open(BytesIO(data)))
    img_batch = np.expand_dims(image, 0)
    prediction = MODEL.predict(img_batch)
    # 返回预测结果
    predicted_class = CLASS_NAMES[np.argmax(prediction[0])]
    confidence = float(np.max(prediction[0]))
    return {"predicted_class": predicted_class, "confidence": confidence}

内容的提问来源于stack exchange,提问作者andy lacron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:45:53