Upload 6 files
Browse files- README.md +58 -0
- convert_coreml_macos.py +84 -0
- model.onnx +3 -0
README.md
CHANGED
|
@@ -109,6 +109,64 @@ with torch.no_grad():
|
|
| 109 |
| 4 | 96.83% | 0.9600 |
|
| 110 |
| **5** | **97.35%** | **0.9666** |
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
## License
|
| 113 |
|
| 114 |
This model is for research purposes only.
|
|
|
|
| 109 |
| 4 | 96.83% | 0.9600 |
|
| 110 |
| **5** | **97.35%** | **0.9666** |
|
| 111 |
|
| 112 |
+
## Files
|
| 113 |
+
|
| 114 |
+
| File | Size | Description |
|
| 115 |
+
|------|:----:|-------------|
|
| 116 |
+
| `pytorch_model.bin` | 121 MB | PyTorch weights (FP32) |
|
| 117 |
+
| `model.onnx` | 164 MB | ONNX model for mobile deployment |
|
| 118 |
+
| `config.json` | 1.2 KB | Model configuration |
|
| 119 |
+
| `model.py` | 6.9 KB | Model architecture code |
|
| 120 |
+
| `convert_coreml_macos.py` | 2.2 KB | CoreML conversion script (macOS) |
|
| 121 |
+
|
| 122 |
+
## Platform-specific Usage
|
| 123 |
+
|
| 124 |
+
### PyTorch (Server/Desktop)
|
| 125 |
+
|
| 126 |
+
```python
|
| 127 |
+
import torch
|
| 128 |
+
from model import DriverBehaviorModel
|
| 129 |
+
|
| 130 |
+
model = DriverBehaviorModel(num_classes=5, pretrained=False)
|
| 131 |
+
checkpoint = torch.load("pytorch_model.bin", map_location="cpu")
|
| 132 |
+
model.load_state_dict(checkpoint["model"])
|
| 133 |
+
model.eval()
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### iOS (CoreML)
|
| 137 |
+
|
| 138 |
+
1. Copy `model.onnx` to macOS
|
| 139 |
+
2. Run conversion script:
|
| 140 |
+
```bash
|
| 141 |
+
python convert_coreml_macos.py
|
| 142 |
+
```
|
| 143 |
+
3. Add generated `DriverBehavior.mlpackage` to Xcode project
|
| 144 |
+
|
| 145 |
+
### Android (ONNX Runtime)
|
| 146 |
+
|
| 147 |
+
```kotlin
|
| 148 |
+
// build.gradle
|
| 149 |
+
implementation 'com.microsoft.onnxruntime:onnxruntime-android:1.16.0'
|
| 150 |
+
|
| 151 |
+
// Kotlin
|
| 152 |
+
val session = OrtEnvironment.getEnvironment()
|
| 153 |
+
.createSession(assetManager.open("model.onnx").readBytes())
|
| 154 |
+
|
| 155 |
+
val output = session.run(mapOf("video_input" to inputTensor))
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
## Preprocessing (All Platforms)
|
| 159 |
+
|
| 160 |
+
```
|
| 161 |
+
Input Shape: [1, 3, 30, 224, 224] (batch, channels, frames, height, width)
|
| 162 |
+
Channel Order: RGB
|
| 163 |
+
Normalization: (pixel / 255.0 - mean) / std
|
| 164 |
+
- mean = [0.485, 0.456, 0.406]
|
| 165 |
+
- std = [0.229, 0.224, 0.225]
|
| 166 |
+
Resize: 224x224 (BILINEAR)
|
| 167 |
+
Frames: 30 frames uniformly sampled
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
## License
|
| 171 |
|
| 172 |
This model is for research purposes only.
|
convert_coreml_macos.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
macOS에서 실행: ONNX → CoreML FP16 변환
|
| 4 |
+
|
| 5 |
+
사용법:
|
| 6 |
+
python convert_coreml_macos.py
|
| 7 |
+
|
| 8 |
+
필요 패키지:
|
| 9 |
+
pip install coremltools onnx
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import coremltools as ct
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
# 입력 파일
|
| 16 |
+
ONNX_PATH = "model.onnx" # 같은 폴더에 있어야 함
|
| 17 |
+
OUTPUT_PATH = "DriverBehavior.mlpackage"
|
| 18 |
+
|
| 19 |
+
print("=" * 60)
|
| 20 |
+
print("ONNX → CoreML FP16 변환 (macOS)")
|
| 21 |
+
print("=" * 60)
|
| 22 |
+
|
| 23 |
+
# 변환
|
| 24 |
+
print("\n[1] CoreML 변환 중...")
|
| 25 |
+
mlmodel = ct.convert(
|
| 26 |
+
ONNX_PATH,
|
| 27 |
+
minimum_deployment_target=ct.target.iOS15,
|
| 28 |
+
convert_to="mlprogram",
|
| 29 |
+
compute_precision=ct.precision.FLOAT16, # FP16 (Metal 최적화)
|
| 30 |
+
compute_units=ct.ComputeUnit.ALL, # CPU + GPU + Neural Engine
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# 메타데이터
|
| 34 |
+
mlmodel.author = "C-Team"
|
| 35 |
+
mlmodel.short_description = "Driver Behavior Detection - Video Swin Transformer"
|
| 36 |
+
mlmodel.version = "1.0"
|
| 37 |
+
|
| 38 |
+
# 저장
|
| 39 |
+
mlmodel.save(OUTPUT_PATH)
|
| 40 |
+
print(f"✓ 저장 완료: {OUTPUT_PATH}")
|
| 41 |
+
|
| 42 |
+
# 검증
|
| 43 |
+
print("\n[2] 모델 검증 중...")
|
| 44 |
+
import onnxruntime as ort
|
| 45 |
+
|
| 46 |
+
# 더미 입력
|
| 47 |
+
dummy_input = np.random.randn(1, 3, 30, 224, 224).astype(np.float32)
|
| 48 |
+
|
| 49 |
+
# ONNX 출력
|
| 50 |
+
sess = ort.InferenceSession(ONNX_PATH)
|
| 51 |
+
onnx_out = sess.run(None, {'video_input': dummy_input})[0]
|
| 52 |
+
|
| 53 |
+
# CoreML 출력
|
| 54 |
+
coreml_out = mlmodel.predict({'video_input': dummy_input})['class_logits']
|
| 55 |
+
|
| 56 |
+
# 비교
|
| 57 |
+
max_diff = np.abs(onnx_out - coreml_out).max()
|
| 58 |
+
print(f" ONNX vs CoreML 최대 차이: {max_diff:.6f}")
|
| 59 |
+
|
| 60 |
+
onnx_pred = onnx_out.argmax()
|
| 61 |
+
coreml_pred = coreml_out.argmax()
|
| 62 |
+
|
| 63 |
+
if onnx_pred == coreml_pred:
|
| 64 |
+
print(" ✓ 예측 클래스 일치!")
|
| 65 |
+
else:
|
| 66 |
+
print(f" ⚠ 예측 다름: ONNX={onnx_pred}, CoreML={coreml_pred}")
|
| 67 |
+
|
| 68 |
+
print("\n" + "=" * 60)
|
| 69 |
+
print("변환 완료!")
|
| 70 |
+
print("=" * 60)
|
| 71 |
+
print(f"""
|
| 72 |
+
iOS 사용법:
|
| 73 |
+
1. {OUTPUT_PATH}를 Xcode 프로젝트에 드래그앤드롭
|
| 74 |
+
2. 자동으로 Metal FP16 가속 적용됨
|
| 75 |
+
|
| 76 |
+
입력 형식:
|
| 77 |
+
- Shape: [1, 3, 30, 224, 224]
|
| 78 |
+
- 정규화: (pixel/255 - [0.485,0.456,0.406]) / [0.229,0.224,0.225]
|
| 79 |
+
- 채널: RGB
|
| 80 |
+
|
| 81 |
+
출력:
|
| 82 |
+
- [1, 5] 로짓 → argmax로 클래스 예측
|
| 83 |
+
- 0: 정상, 1: 졸음운전, 2: 물건찾기, 3: 휴대폰사용, 4: 운전자폭행
|
| 84 |
+
""")
|
model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:036ac05c4b782d2ea1e24eb61366f197e0692ba24abaddca6798d56c1a337cec
|
| 3 |
+
size 171169182
|