Spaces:
Running
Running
Upload 2 files
Browse files- app.py +39 -2
- requirements.txt +5 -2
app.py
CHANGED
@@ -1,11 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
import torch
|
5 |
import onnxruntime as ort
|
|
|
6 |
from ultralytics import YOLO
|
7 |
import os
|
8 |
from typing import Tuple, List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Configuration - UPDATE THESE VALUES
|
11 |
MODEL_PT_PATH = "model.pt" # Your trained PyTorch model
|
@@ -14,9 +42,14 @@ INPUT_SIZE = 640 # Must match training size
|
|
14 |
CLASS_NAMES = ["class0", "class1"] # Your actual class names
|
15 |
CONF_THRESHOLD = 0.5 # Confidence threshold
|
16 |
IOU_THRESHOLD = 0.45 # NMS IoU threshold
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def convert_pt_to_onnx():
|
19 |
"""Convert PyTorch model to ONNX format if not exists"""
|
|
|
20 |
if not os.path.exists(MODEL_ONNX_PATH):
|
21 |
print("Converting PyTorch model to ONNX...")
|
22 |
try:
|
@@ -43,15 +76,19 @@ def convert_pt_to_onnx():
|
|
43 |
|
44 |
def load_onnx_model() -> ort.InferenceSession:
|
45 |
"""Initialize ONNX runtime session"""
|
46 |
-
|
|
|
47 |
try:
|
48 |
-
return ort.InferenceSession(MODEL_ONNX_PATH, providers=providers)
|
|
|
49 |
except Exception as e:
|
50 |
raise RuntimeError(f"Failed to load ONNX model: {str(e)}")
|
51 |
|
52 |
# Initialize model
|
53 |
convert_pt_to_onnx()
|
54 |
ort_session = load_onnx_model()
|
|
|
|
|
55 |
|
56 |
def letterbox_image(image: np.ndarray) -> Tuple[np.ndarray, float, Tuple[int, int]]:
|
57 |
"""
|
|
|
1 |
+
# for Zero GPU Spaces compatibility
|
2 |
+
import spaces
|
3 |
+
@spaces.GPU
|
4 |
+
def dummy_gpu():
|
5 |
+
pass
|
6 |
+
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
9 |
import cv2
|
10 |
import torch
|
11 |
import onnxruntime as ort
|
12 |
+
from optimum.onnxruntime import ORTModel
|
13 |
from ultralytics import YOLO
|
14 |
import os
|
15 |
from typing import Tuple, List
|
16 |
+
import subprocess
|
17 |
+
|
18 |
+
def install_cuda_toolkit():
|
19 |
+
print("Installing CUDA Toolkit.")
|
20 |
+
#CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run"
|
21 |
+
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run"
|
22 |
+
CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL)
|
23 |
+
subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE])
|
24 |
+
subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE])
|
25 |
+
subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"])
|
26 |
+
|
27 |
+
os.environ["CUDA_HOME"] = "/usr/local/cuda"
|
28 |
+
os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"])
|
29 |
+
os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % (
|
30 |
+
os.environ["CUDA_HOME"],
|
31 |
+
"" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"],
|
32 |
+
)
|
33 |
+
# Fix: arch_list[-1] += '+PTX'; IndexError: list index out of range
|
34 |
+
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
35 |
+
|
36 |
+
install_cuda_toolkit()
|
37 |
|
38 |
# Configuration - UPDATE THESE VALUES
|
39 |
MODEL_PT_PATH = "model.pt" # Your trained PyTorch model
|
|
|
42 |
CLASS_NAMES = ["class0", "class1"] # Your actual class names
|
43 |
CONF_THRESHOLD = 0.5 # Confidence threshold
|
44 |
IOU_THRESHOLD = 0.45 # NMS IoU threshold
|
45 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
46 |
+
|
47 |
+
session_options = ort.SessionOptions()
|
48 |
+
session_options.log_severity_level = 0
|
49 |
|
50 |
def convert_pt_to_onnx():
|
51 |
"""Convert PyTorch model to ONNX format if not exists"""
|
52 |
+
print(f'Converting model on {"cuda" if torch.cuda.is_available() else "cpu"}')
|
53 |
if not os.path.exists(MODEL_ONNX_PATH):
|
54 |
print("Converting PyTorch model to ONNX...")
|
55 |
try:
|
|
|
76 |
|
77 |
def load_onnx_model() -> ort.InferenceSession:
|
78 |
"""Initialize ONNX runtime session"""
|
79 |
+
print(f'Loading model on {"cuda" if torch.cuda.is_available() else "cpu"}')
|
80 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if DEVICE != "cpu" else ['CPUExecutionProvider']
|
81 |
try:
|
82 |
+
#return ort.InferenceSession(MODEL_ONNX_PATH, providers=providers, session_options=session_options, export=True)
|
83 |
+
return ORTModel.load_model(MODEL_ONNX_PATH, provider='CUDAExecutionProvider' if DEVICE != "cpu" else 'CPUExecutionProvider', session_options=session_options)
|
84 |
except Exception as e:
|
85 |
raise RuntimeError(f"Failed to load ONNX model: {str(e)}")
|
86 |
|
87 |
# Initialize model
|
88 |
convert_pt_to_onnx()
|
89 |
ort_session = load_onnx_model()
|
90 |
+
print("Available Providers: ", ort_session._providers)
|
91 |
+
#assert "CUDAExecutionProvider" in ort_session._providers
|
92 |
|
93 |
def letterbox_image(image: np.ndarray) -> Tuple[np.ndarray, float, Tuple[int, int]]:
|
94 |
"""
|
requirements.txt
CHANGED
@@ -1,7 +1,10 @@
|
|
|
|
|
|
1 |
gradio
|
2 |
numpy
|
3 |
-
|
|
|
|
|
4 |
opencv-python
|
5 |
Pillow
|
6 |
-
torch
|
7 |
ultralytics
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cu124
|
2 |
+
torch
|
3 |
gradio
|
4 |
numpy
|
5 |
+
onnxslim
|
6 |
+
#onnxruntime-gpu==1.19.2
|
7 |
+
optimum[onnxruntime-gpu]
|
8 |
opencv-python
|
9 |
Pillow
|
|
|
10 |
ultralytics
|