AOSSIE-Org / AOSSIE-Org/PictoPy

Feat: Fix Model Initialization Crash Risk

未关闭
#1,212 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
283
派生
679
平均合并
7 天 2 小时
30 天内合并 PR
3

描述

### Describe the feature

## Model Initialization Error Handling

Both the **YOLO** and **FaceNet** classes initialize the model session without error handling.
If the model file is missing or hardware acceleration (CUDA) fails, the program crashes.

### Files
- `YOLO.py` (Model)
- `FaceNet.py` (Model)

### Issue
`onnxruntime.InferenceSession` is initialized without a `try...except` block.

### Fix
Wrap the initialization in a `try...except` block and:
- provide a fallback to `CPUExecutionProvider`, or
- log a clear error message.

### Add ScreenShots

### Affected Files
- `YOLO.py`
- `FaceNet.py`

### Description
Both model classes initialize an ONNX Runtime session directly using `onnxruntime.InferenceSession(...)` without any exception handling. Since this constructor performs model loading and execution provider initialization, any failure during this process can crash the backend server.

### Code Locations

#### 1. YOLO Model Initialization (`YOLO.py`)
```python
23: self.session = onnxruntime.InferenceSession(
24: self.model_path, providers=ONNX_util_get_execution_providers()
25: )
```

#### 2. FaceNet Model Initialization (`FaceNet.py`)
```python
14: self.session = onnxruntime.InferenceSession(
15: model_path, providers=ONNX_util_get_execution_providers()
16: )
```

### Why This Is a Risk
The `onnxruntime.InferenceSession` constructor is a **blocking operation** that attempts to:

- Load the `.onnx` model file
- Initialize execution providers (e.g., CUDA, CPU)
- Allocate required runtime resources

If anything fails during this step, an exception is raised. Common causes include:

- Missing or corrupted `.onnx` model file
- Incorrect model path
- CUDA or GPU driver incompatibility
- Failure to initialize execution providers
- Permission or filesystem issues

Since these calls are **not wrapped in a `try/except` block**, the exception propagates upward and can **crash the entire backend service during startup**.

### Impact
- Backend server may fail to start
- No graceful error reporting
- No fallback to CPU execution provider
- Reduced system reliability in production

### Record

- [x] I agree to follow this project's Code of Conduct
- [x] I want to work on this issue

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。