satvs commited on
Commit
11d5013
·
1 Parent(s): d26ff53

[feat] add yolo nano inference baseline

Browse files
requirements.txt CHANGED
@@ -1,3 +1,5 @@
 
 
1
  fastapi>=0.68.0
2
  uvicorn>=0.15.0
3
  codecarbon>=2.3.1
@@ -7,4 +9,7 @@ pydantic>=1.10.0
7
  python-dotenv>=1.0.0
8
  gradio>=4.0.0
9
  requests>=2.31.0
10
- librosa==0.10.2.post1
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu124
2
+
3
  fastapi>=0.68.0
4
  uvicorn>=0.15.0
5
  codecarbon>=2.3.1
 
9
  python-dotenv>=1.0.0
10
  gradio>=4.0.0
11
  requests>=2.31.0
12
+ librosa==0.10.2.post1
13
+ ultralytics==8.3.59
14
+ torch==2.5.1
15
+ torchvision==0.20.1
tasks/image.py CHANGED
@@ -14,7 +14,7 @@ load_dotenv()
14
 
15
  router = APIRouter()
16
 
17
- DESCRIPTION = "Random Baseline"
18
  ROUTE = "/image"
19
 
20
  def parse_boxes(annotation_string):
@@ -73,9 +73,7 @@ async def evaluate_image(request: ImageEvaluationRequest):
73
  """
74
  Evaluate image classification and object detection for forest fire smoke.
75
 
76
- Current Model: Random Baseline
77
- - Makes random predictions for both classification and bounding boxes
78
- - Used as a baseline for comparison
79
 
80
  Metrics:
81
  - Classification accuracy: Whether an image contains smoke or not
@@ -98,8 +96,17 @@ async def evaluate_image(request: ImageEvaluationRequest):
98
  #--------------------------------------------------------------------------------------------
99
  # YOUR MODEL INFERENCE CODE HERE
100
  # Update the code below to replace the random baseline with your model inference
101
- #--------------------------------------------------------------------------------------------
102
-
 
 
 
 
 
 
 
 
 
103
  predictions = []
104
  true_labels = []
105
  pred_boxes = []
@@ -110,26 +117,28 @@ async def evaluate_image(request: ImageEvaluationRequest):
110
  annotation = example.get("annotations", "").strip()
111
  has_smoke = len(annotation) > 0
112
  true_labels.append(int(has_smoke))
113
-
114
- # Make random classification prediction
115
- pred_has_smoke = random.random() > 0.5
 
 
 
 
 
 
116
  predictions.append(int(pred_has_smoke))
117
 
118
- # If there's a true box, parse it and make random box prediction
119
  if has_smoke:
120
  # Parse all true boxes from the annotation
121
  image_true_boxes = parse_boxes(annotation)
122
  true_boxes_list.append(image_true_boxes)
123
-
124
- # For baseline, make one random box prediction per image
125
- # In a real model, you might want to predict multiple boxes
126
- random_box = [
127
- random.random(), # x_center
128
- random.random(), # y_center
129
- random.random() * 0.5, # width (max 0.5)
130
- random.random() * 0.5 # height (max 0.5)
131
- ]
132
- pred_boxes.append(random_box)
133
 
134
  #--------------------------------------------------------------------------------------------
135
  # YOUR MODEL INFERENCE STOPS HERE
 
14
 
15
  router = APIRouter()
16
 
17
+ DESCRIPTION = "Frugal Object Detector for forest fires"
18
  ROUTE = "/image"
19
 
20
  def parse_boxes(annotation_string):
 
73
  """
74
  Evaluate image classification and object detection for forest fire smoke.
75
 
76
+ Current Model: Yolo11 nano
 
 
77
 
78
  Metrics:
79
  - Classification accuracy: Whether an image contains smoke or not
 
96
  #--------------------------------------------------------------------------------------------
97
  # YOUR MODEL INFERENCE CODE HERE
98
  # Update the code below to replace the random baseline with your model inference
99
+ #--------------------------------------------------------------------------------------------
100
+ from pathlib import Path
101
+ from ultralytics import YOLO
102
+ import torch
103
+
104
+ # Load model
105
+ model_path = "models"
106
+ model_name = "best.pt"
107
+ model = YOLO(Path(model_path, model_name))
108
+ threshold = 0.14
109
+
110
  predictions = []
111
  true_labels = []
112
  pred_boxes = []
 
117
  annotation = example.get("annotations", "").strip()
118
  has_smoke = len(annotation) > 0
119
  true_labels.append(int(has_smoke))
120
+
121
+ # Make prediction
122
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
123
+ results = model.predict(example["image"], device=device, conf=threshold, verbose=False)[0] # index 0 since we predict on one image at a time
124
+ if results.boxes.cls.numel()!=0:
125
+ # This means a fire was detected, hence we append 1
126
+ pred_has_smoke = 1
127
+ else:
128
+ pred_has_smoke = 0
129
  predictions.append(int(pred_has_smoke))
130
 
131
+ # If there's a true box, parse it and add box prediction
132
  if has_smoke:
133
  # Parse all true boxes from the annotation
134
  image_true_boxes = parse_boxes(annotation)
135
  true_boxes_list.append(image_true_boxes)
136
+
137
+ # Append only one bounding box if at least one fire is detected
138
+ if results.boxes.cls.numel()!=0:
139
+ pred_boxes.append(results.boxes[0].xywhn.tolist()[0])
140
+ else:
141
+ pred_boxes.append([0,0,0,0])
 
 
 
 
142
 
143
  #--------------------------------------------------------------------------------------------
144
  # YOUR MODEL INFERENCE STOPS HERE
tasks/models/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:08ca51a239f739eab4f3653956abcf303f836e8ea3b9a1c225c85f0cc1d086fa
3
+ size 5443539
tasks/models/pruned.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e5e9ef2d0bbe8e8984d6739ccc2d21045844c2be98425b271090de621042ce8
3
+ size 5470665