FAILED [100%] examples\RTDETR-ONNXRuntime-Python\main.py:15 ([doctest] main.RTDETR) 037 Methods: 038 draw_detections: Draw bounding boxes and labels on the input image. 039 preprocess: Preprocess the input image for model inference. 040 bbox_cxcywh_to_xyxy: Convert bounding boxes from center format to corner format. 041 postprocess: Postprocess model output to extract and visualize detections. 042 main: Execute the complete object detection pipeline. 043 044 Examples: 045 Initialize RT-DETR detector and run inference 046 >>> detector = RTDETR("./rtdetr-l.onnx", "image.jpg", conf_thres=0.5) UNEXPECTED EXCEPTION: NoSuchFile("[ONNXRuntimeError] : 3 : NO_SUCHFILE : Load model from ./rtdetr-l.onnx failed:Load model ./rtdetr-l.onnx failed. File doesn't exist") Traceback (most recent call last): File "F:\softWare\Anaconda\envs\pytorch_env\Lib\doctest.py", line 1368, in __run exec(compile(example.source, filename, "single", File "<doctest main.RTDETR[0]>", line 1, in <module> File "F:\yolov11\ultralytics-v8.3.147\examples\RTDETR-ONNXRuntime-Python\main.py", line 67, in __init__ self.session = ort.InferenceSession(model_path, providers=["CUDAExecutionProvider", "CPUExecutionProvider"]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\86159\AppData\Roaming\Python\Python312\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 472, in __init__ self._create_inference_session(providers, provider_options, disabled_optimizers) File "C:\Users\86159\AppData\Roaming\Python\Python312\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 550, in _create_inference_session sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ onnxruntime.capi.onnxruntime_pybind11_state.NoSuchFile: [ONNXRuntimeError] : 3 : NO_SUCHFILE : Load model from ./rtdetr-l.onnx failed:Load model ./rtdetr-l.onnx failed. File doesn't exist F:\yolov11\ultralytics-v8.3.147\examples\RTDETR-ONNXRuntime-Python\main.py:46: UnexpectedException这个报错怎么解决
时间: 2025-06-02 13:13:05 浏览: 6
### ONNXRuntime加载模型文件失败的解决方案
当ONNXRuntime在加载`rtdetr-l.onnx`模型时出现`NoSuchFile`错误,这通常表明系统未能找到指定路径下的模型文件。以下是一些可能的原因及解决方法:
#### 1. 检查模型文件路径是否正确
确保提供的路径是正确的,并且模型文件确实存在于该路径下。如果使用的是相对路径,请确认脚本运行的工作目录是否与预期一致[^1]。
```python
import os
# 检查模型文件是否存在
model_path = "path/to/rtdetr-l.onnx"
if not os.path.exists(model_path):
print(f"模型文件不存在: {model_path}")
```
#### 2. 确认文件权限
即使路径正确,文件可能由于权限问题无法被访问。请检查当前用户是否有读取该文件的权限[^2]。
```bash
ls -l path/to/rtdetr-l.onnx
```
如果权限不足,可以尝试修改文件权限:
```bash
chmod 644 path/to/rtdetr-l.onnx
```
#### 3. 验证模型文件完整性
有时模型文件可能在下载或传输过程中损坏,导致无法加载。可以通过比较文件大小或校验和来验证文件完整性[^3]。
```python
import hashlib
def compute_sha256(file_path):
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
# 计算并打印SHA-256校验值
print(compute_sha256(model_path))
```
将计算出的哈希值与官方提供的哈希值进行对比以确认文件未损坏。
#### 4. 检查ONNXRuntime版本兼容性
某些模型可能需要特定版本的ONNXRuntime才能正常加载。请确保使用的ONNXRuntime版本与模型兼容[^4]。
```python
import onnxruntime as ort
print("ONNXRuntime version:", ort.__version__)
```
如果版本不匹配,可以尝试安装兼容版本:
```bash
pip install onnxruntime==1.14.1 # 根据需求选择合适的版本
```
#### 5. 使用绝对路径加载模型
为避免路径解析问题,建议使用绝对路径加载模型文件[^5]。
```python
session = ort.InferenceSession(os.path.abspath(model_path))
```
#### 6. 捕获并记录详细错误信息
通过捕获异常并打印详细错误信息,可以帮助进一步定位问题所在[^6]。
```python
try:
session = ort.InferenceSession(model_path)
except Exception as e:
print(f"加载模型失败: {e}")
```
---
阅读全文
相关推荐











