Spaces:
Sleeping
Sleeping
Update tasks/image.py
Browse files- tasks/image.py +42 -40
tasks/image.py
CHANGED
@@ -28,6 +28,25 @@ ROUTE = "/image"
|
|
28 |
|
29 |
device = torch.device("cuda")
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def parse_boxes(annotation_string):
|
33 |
"""Parse multiple boxes from a single annotation string.
|
@@ -83,31 +102,6 @@ def compute_max_iou(true_boxes, pred_box):
|
|
83 |
return max_iou
|
84 |
|
85 |
|
86 |
-
class ClampTransform:
|
87 |
-
def __init__(self, min_val=0.0, max_val=1.0):
|
88 |
-
self.min_val = min_val
|
89 |
-
self.max_val = max_val
|
90 |
-
|
91 |
-
def __call__(self, tensor):
|
92 |
-
return torch.clamp(tensor, min=self.min_val, max=self.max_val)
|
93 |
-
|
94 |
-
|
95 |
-
def collate_fn(batch):
|
96 |
-
images = [item['image'] for item in batch]
|
97 |
-
annotations = [item.get('annotations', '') for item in batch]
|
98 |
-
|
99 |
-
# Convert PIL Images to tensors
|
100 |
-
transform = transforms.Compose([
|
101 |
-
transforms.ToTensor(),
|
102 |
-
ClampTransform(min_val=0.0, max_val=1.0),
|
103 |
-
transforms.Resize((640, 640))
|
104 |
-
])
|
105 |
-
|
106 |
-
images = [transform(img) for img in images]
|
107 |
-
images = torch.stack(images)
|
108 |
-
return {'image': images, 'annotations': annotations}
|
109 |
-
|
110 |
-
|
111 |
def parse_boxes(annotation_string):
|
112 |
"""Parse multiple boxes from a single annotation string.
|
113 |
Each box has 5 values: class_id, x_center, y_center, width, height"""
|
@@ -121,7 +115,6 @@ def parse_boxes(annotation_string):
|
|
121 |
boxes.append(box)
|
122 |
return boxes
|
123 |
|
124 |
-
|
125 |
def compute_iou(box1, box2):
|
126 |
"""Compute Intersection over Union (IoU) between two YOLO format boxes."""
|
127 |
# Convert YOLO format (x_center, y_center, width, height) to corners
|
@@ -151,7 +144,6 @@ def compute_iou(box1, box2):
|
|
151 |
|
152 |
return intersection / (union + 1e-6)
|
153 |
|
154 |
-
|
155 |
def compute_max_iou(true_boxes, pred_box):
|
156 |
"""Compute maximum IoU between a predicted box and all true boxes"""
|
157 |
max_iou = 0
|
@@ -160,10 +152,10 @@ def compute_max_iou(true_boxes, pred_box):
|
|
160 |
max_iou = max(max_iou, iou)
|
161 |
return max_iou
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
"""
|
168 |
Evaluate image classification and object detection for forest fire smoke.
|
169 |
|
@@ -182,12 +174,10 @@ async def evaluate_image(model_path: str = "models/yolo11s_best.pt", request: Im
|
|
182 |
dataset = load_dataset(request.dataset_name, token=os.getenv("HF_TOKEN"))
|
183 |
|
184 |
# Split dataset
|
185 |
-
train_test = dataset["train"]
|
186 |
test_dataset = dataset["val"]
|
187 |
-
|
188 |
-
|
189 |
-
if("detr" in model_path):
|
190 |
-
model = RTDETR(model_path)
|
191 |
|
192 |
# Start tracking emissions
|
193 |
tracker.start()
|
@@ -202,15 +192,27 @@ async def evaluate_image(model_path: str = "models/yolo11s_best.pt", request: Im
|
|
202 |
true_labels = []
|
203 |
pred_boxes = []
|
204 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
|
|
|
|
205 |
|
206 |
-
for example in
|
|
|
207 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
208 |
annotation = example.get("annotations", "").strip()
|
209 |
has_smoke = len(annotation) > 0
|
210 |
true_labels.append(int(has_smoke))
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
boxes = results[0].boxes.xywh.tolist()
|
215 |
|
216 |
pred_has_smoke = len(boxes) > 0
|
|
|
28 |
|
29 |
device = torch.device("cuda")
|
30 |
|
31 |
+
|
32 |
+
def load_camera_models():
|
33 |
+
models = {}
|
34 |
+
folder = "cameras_dataset/"
|
35 |
+
cameras = ['brison-200', 'brison-110', 'courmettes-212', 'courmettes-160', 'brison-290', 'marguerite-29','default']
|
36 |
+
|
37 |
+
# Ensure the folder exists
|
38 |
+
if not os.path.exists(folder):
|
39 |
+
raise FileNotFoundError(f"The folder '{folder}' does not exist.")
|
40 |
+
|
41 |
+
# Iterate over files in the folder
|
42 |
+
for model_path in os.listdir(folder):
|
43 |
+
full_path = os.path.join(folder, model_path)
|
44 |
+
for camera in cameras:
|
45 |
+
if camera in model_path:
|
46 |
+
models[camera] = YOLO(full_path, task = 'detect')
|
47 |
+
break
|
48 |
+
|
49 |
+
return models
|
50 |
|
51 |
def parse_boxes(annotation_string):
|
52 |
"""Parse multiple boxes from a single annotation string.
|
|
|
102 |
return max_iou
|
103 |
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
def parse_boxes(annotation_string):
|
106 |
"""Parse multiple boxes from a single annotation string.
|
107 |
Each box has 5 values: class_id, x_center, y_center, width, height"""
|
|
|
115 |
boxes.append(box)
|
116 |
return boxes
|
117 |
|
|
|
118 |
def compute_iou(box1, box2):
|
119 |
"""Compute Intersection over Union (IoU) between two YOLO format boxes."""
|
120 |
# Convert YOLO format (x_center, y_center, width, height) to corners
|
|
|
144 |
|
145 |
return intersection / (union + 1e-6)
|
146 |
|
|
|
147 |
def compute_max_iou(true_boxes, pred_box):
|
148 |
"""Compute maximum IoU between a predicted box and all true boxes"""
|
149 |
max_iou = 0
|
|
|
152 |
max_iou = max(max_iou, iou)
|
153 |
return max_iou
|
154 |
|
155 |
+
# @router.post(ROUTE, tags=["Image Task"],
|
156 |
+
# description=DESCRIPTION)
|
157 |
+
async def evaluate_image(model_path,request: ImageEvaluationRequest = ImageEvaluationRequest()):
|
158 |
+
# def evaluate_image(model_path: str, request: ImageEvaluationRequest = ImageEvaluationRequest()):
|
159 |
"""
|
160 |
Evaluate image classification and object detection for forest fire smoke.
|
161 |
|
|
|
174 |
dataset = load_dataset(request.dataset_name, token=os.getenv("HF_TOKEN"))
|
175 |
|
176 |
# Split dataset
|
177 |
+
# train_test = dataset["train"]
|
178 |
test_dataset = dataset["val"]
|
179 |
+
|
180 |
+
models = load_camera_models()
|
|
|
|
|
181 |
|
182 |
# Start tracking emissions
|
183 |
tracker.start()
|
|
|
192 |
true_labels = []
|
193 |
pred_boxes = []
|
194 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
195 |
+
# list of cameras
|
196 |
+
result_cameras = ['brison-200', 'brison-110', 'courmettes-212', 'courmettes-160', 'brison-290', 'marguerite-29']
|
197 |
|
198 |
+
for example in test_dataset:
|
199 |
+
|
200 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
201 |
annotation = example.get("annotations", "").strip()
|
202 |
has_smoke = len(annotation) > 0
|
203 |
true_labels.append(int(has_smoke))
|
204 |
+
|
205 |
+
image_path = example["image_name"]
|
206 |
+
image = example["image"]
|
207 |
+
|
208 |
+
# Extract camera name from the image path
|
209 |
+
camera = next((cam for cam in result_cameras if cam in image_path), None)
|
210 |
+
if camera:
|
211 |
+
results = models[camera](image, verbose=False,imgsz=1280)
|
212 |
+
|
213 |
+
else:
|
214 |
+
results = models["default"](image, verbose=False,imgsz=1280)
|
215 |
+
|
216 |
boxes = results[0].boxes.xywh.tolist()
|
217 |
|
218 |
pred_has_smoke = len(boxes) > 0
|