kiurtis commited on
Commit
c23d8cd
·
1 Parent(s): 8d8b751

testing dynamic onnx

Browse files
Files changed (3) hide show
  1. app.py +4 -1
  2. requirements.txt +6 -1
  3. tasks/image.py +24 -11
app.py CHANGED
@@ -4,7 +4,7 @@ from tasks import text, image, audio
4
 
5
  # Load environment variables
6
  load_dotenv()
7
-
8
  app = FastAPI(
9
  title="Frugal AI Challenge API",
10
  description="API for the Frugal AI Challenge evaluation endpoints"
@@ -12,8 +12,11 @@ app = FastAPI(
12
 
13
  # Include all routers
14
  app.include_router(text.router)
 
15
  app.include_router(image.router)
 
16
  app.include_router(audio.router)
 
17
 
18
  @app.get("/")
19
  async def root():
 
4
 
5
  # Load environment variables
6
  load_dotenv()
7
+ print(1)
8
  app = FastAPI(
9
  title="Frugal AI Challenge API",
10
  description="API for the Frugal AI Challenge evaluation endpoints"
 
12
 
13
  # Include all routers
14
  app.include_router(text.router)
15
+ print(2)
16
  app.include_router(image.router)
17
+ print(3)
18
  app.include_router(audio.router)
19
+ print(4)
20
 
21
  @app.get("/")
22
  async def root():
requirements.txt CHANGED
@@ -14,4 +14,9 @@ ultralytics-thop==2.0.14
14
  #opencv-python==4.11.0.86
15
  python-dotenv==1.0.0
16
  onnxruntime==1.19.2
17
- matplotlib==3.8.1
 
 
 
 
 
 
14
  #opencv-python==4.11.0.86
15
  python-dotenv==1.0.0
16
  onnxruntime==1.19.2
17
+ matplotlib==3.8.1
18
+ onnx==1.17.0
19
+ tensorrt==10.8.0.43
20
+ tensorrt_cu12==10.8.0.43
21
+ tensorrt_cu12_bindings==10.8.0.43
22
+ tensorrt_cu12_libs==10.8.0.43
tasks/image.py CHANGED
@@ -1,25 +1,38 @@
1
  from fastapi import APIRouter
 
2
  from datetime import datetime
 
3
  from datasets import load_dataset
 
4
  import numpy as np
 
5
  from sklearn.metrics import accuracy_score, precision_score, recall_score
 
6
  import random
 
7
  import os
 
8
  from ultralytics import YOLO
 
9
 
10
  from .utils.evaluation import ImageEvaluationRequest
 
11
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
 
12
 
13
  from dotenv import load_dotenv
 
14
  load_dotenv()
 
15
 
16
  router = APIRouter()
 
17
 
18
  #MODEL_TYPE = "YOLOv11n"
19
  DESCRIPTION = f"YOLOv8n model with batch 128 inference on CPU"
20
-
21
  ROUTE = "/image"
22
-
23
  def parse_boxes(annotation_string):
24
  """Parse multiple boxes from a single annotation string.
25
  Each box has 5 values: class_id, x_center, y_center, width, height"""
@@ -32,7 +45,7 @@ def parse_boxes(annotation_string):
32
  box = values[i+1:i+5]
33
  boxes.append(box)
34
  return boxes
35
-
36
  def compute_iou(box1, box2):
37
  """Compute Intersection over Union (IoU) between two YOLO format boxes."""
38
  # Convert YOLO format (x_center, y_center, width, height) to corners
@@ -61,7 +74,7 @@ def compute_iou(box1, box2):
61
  union = box1_area + box2_area - intersection
62
 
63
  return intersection / (union + 1e-6)
64
-
65
  def compute_max_iou(true_boxes, pred_box):
66
  """Compute maximum IoU between a predicted box and all true boxes"""
67
  max_iou = 0
@@ -69,17 +82,17 @@ def compute_max_iou(true_boxes, pred_box):
69
  iou = compute_iou(true_box, pred_box)
70
  max_iou = max(max_iou, iou)
71
  return max_iou
72
-
73
  def load_model(path_to_model, model_type="YOLO"):
74
  if model_type == "YOLO":
75
  model = YOLO(path_to_model)
76
  else:
77
  raise NotImplementedError
78
  return model
79
-
80
  def get_boxes_list(predictions):
81
  return [box.tolist() for box in predictions.boxes.xywhn]
82
-
83
  @router.post(ROUTE, tags=["Image Task"],
84
  description=DESCRIPTION)
85
  async def evaluate_image(request: ImageEvaluationRequest):
@@ -120,11 +133,11 @@ async def evaluate_image(request: ImageEvaluationRequest):
120
  #PATH_TO_MODEL = 'models/best_YOLOv11n_1280.onnx'
121
  #PATH_TO_MODEL = 'models/best_yolov6n_1280.pt'
122
  #PATH_TO_MODEL = 'models/best_YOLOv11n_1280_real_half.onnx'
123
- PATH_TO_MODEL = 'models/best_YOLOv8n.onnx'
124
  INFERENCE_ENGINE_TYPE = 'pt'
125
  INPUT_SIZE = 640
126
  N_TEST_BATCHES = 2
127
- BATCH_SIZE = 128 # Can be adjusted as needed
128
 
129
  def preprocessor(frame):
130
  #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Only when read from file
@@ -136,8 +149,8 @@ async def evaluate_image(request: ImageEvaluationRequest):
136
 
137
  class Inference:
138
  def __init__(self, model, image):
139
- self.session = onnxruntime.InferenceSession(model,#, providers=["CPUExecutionProvider"]
140
- providers=["CUDAExecutionProvider"]
141
  )
142
  model_inputs = self.session.get_inputs()
143
  input_shape = model_inputs[0].shape
 
1
  from fastapi import APIRouter
2
+ print(1)
3
  from datetime import datetime
4
+ print(2)
5
  from datasets import load_dataset
6
+ print(3)
7
  import numpy as np
8
+ print(4)
9
  from sklearn.metrics import accuracy_score, precision_score, recall_score
10
+ print(5)
11
  import random
12
+ print(6)
13
  import os
14
+ print(7)
15
  from ultralytics import YOLO
16
+ print(8)
17
 
18
  from .utils.evaluation import ImageEvaluationRequest
19
+ print(9)
20
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
21
+ print(10)
22
 
23
  from dotenv import load_dotenv
24
+ print(11)
25
  load_dotenv()
26
+ print(12)
27
 
28
  router = APIRouter()
29
+ print(13)
30
 
31
  #MODEL_TYPE = "YOLOv11n"
32
  DESCRIPTION = f"YOLOv8n model with batch 128 inference on CPU"
33
+ print(14)
34
  ROUTE = "/image"
35
+ print(15)
36
  def parse_boxes(annotation_string):
37
  """Parse multiple boxes from a single annotation string.
38
  Each box has 5 values: class_id, x_center, y_center, width, height"""
 
45
  box = values[i+1:i+5]
46
  boxes.append(box)
47
  return boxes
48
+ print(16)
49
  def compute_iou(box1, box2):
50
  """Compute Intersection over Union (IoU) between two YOLO format boxes."""
51
  # Convert YOLO format (x_center, y_center, width, height) to corners
 
74
  union = box1_area + box2_area - intersection
75
 
76
  return intersection / (union + 1e-6)
77
+ print(17)
78
  def compute_max_iou(true_boxes, pred_box):
79
  """Compute maximum IoU between a predicted box and all true boxes"""
80
  max_iou = 0
 
82
  iou = compute_iou(true_box, pred_box)
83
  max_iou = max(max_iou, iou)
84
  return max_iou
85
+ print(18)
86
  def load_model(path_to_model, model_type="YOLO"):
87
  if model_type == "YOLO":
88
  model = YOLO(path_to_model)
89
  else:
90
  raise NotImplementedError
91
  return model
92
+ print(19)
93
  def get_boxes_list(predictions):
94
  return [box.tolist() for box in predictions.boxes.xywhn]
95
+ print(20)
96
  @router.post(ROUTE, tags=["Image Task"],
97
  description=DESCRIPTION)
98
  async def evaluate_image(request: ImageEvaluationRequest):
 
133
  #PATH_TO_MODEL = 'models/best_YOLOv11n_1280.onnx'
134
  #PATH_TO_MODEL = 'models/best_yolov6n_1280.pt'
135
  #PATH_TO_MODEL = 'models/best_YOLOv11n_1280_real_half.onnx'
136
+ PATH_TO_MODEL = 'models/best_YOLOv8n_dynamic.onnx'
137
  INFERENCE_ENGINE_TYPE = 'pt'
138
  INPUT_SIZE = 640
139
  N_TEST_BATCHES = 2
140
+ BATCH_SIZE = 64 # Can be adjusted as needed
141
 
142
  def preprocessor(frame):
143
  #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Only when read from file
 
149
 
150
  class Inference:
151
  def __init__(self, model, image):
152
+ self.session = onnxruntime.InferenceSession(model, providers=["CPUExecutionProvider"]
153
+ #providers=["CUDAExecutionProvider"]
154
  )
155
  model_inputs = self.session.get_inputs()
156
  input_shape = model_inputs[0].shape