laureBe commited on
Commit
f300275
·
verified ·
1 Parent(s): 02b4608

Delete tasks/image.py

Browse files
Files changed (1) hide show
  1. tasks/image.py +0 -172
tasks/image.py DELETED
@@ -1,172 +0,0 @@
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
6
- import random
7
- import os
8
-
9
- from .utils.evaluation import ImageEvaluationRequest
10
- from .utils.emissions import tracker, clean_emissions_data, get_space_info
11
-
12
- from dotenv import load_dotenv
13
- load_dotenv()
14
-
15
- router = APIRouter()
16
-
17
- DESCRIPTION = "Random Baseline"
18
- ROUTE = "/image"
19
-
20
- def parse_boxes(annotation_string):
21
- """Parse multiple boxes from a single annotation string.
22
- Each box has 5 values: class_id, x_center, y_center, width, height"""
23
- values = [float(x) for x in annotation_string.strip().split()]
24
- boxes = []
25
- # Each box has 5 values
26
- for i in range(0, len(values), 5):
27
- if i + 5 <= len(values):
28
- # Skip class_id (first value) and take the next 4 values
29
- box = values[i+1:i+5]
30
- boxes.append(box)
31
- return boxes
32
-
33
- def compute_iou(box1, box2):
34
- """Compute Intersection over Union (IoU) between two YOLO format boxes."""
35
- # Convert YOLO format (x_center, y_center, width, height) to corners
36
- def yolo_to_corners(box):
37
- x_center, y_center, width, height = box
38
- x1 = x_center - width/2
39
- y1 = y_center - height/2
40
- x2 = x_center + width/2
41
- y2 = y_center + height/2
42
- return np.array([x1, y1, x2, y2])
43
-
44
- box1_corners = yolo_to_corners(box1)
45
- box2_corners = yolo_to_corners(box2)
46
-
47
- # Calculate intersection
48
- x1 = max(box1_corners[0], box2_corners[0])
49
- y1 = max(box1_corners[1], box2_corners[1])
50
- x2 = min(box1_corners[2], box2_corners[2])
51
- y2 = min(box1_corners[3], box2_corners[3])
52
-
53
- intersection = max(0, x2 - x1) * max(0, y2 - y1)
54
-
55
- # Calculate union
56
- box1_area = (box1_corners[2] - box1_corners[0]) * (box1_corners[3] - box1_corners[1])
57
- box2_area = (box2_corners[2] - box2_corners[0]) * (box2_corners[3] - box2_corners[1])
58
- union = box1_area + box2_area - intersection
59
-
60
- return intersection / (union + 1e-6)
61
-
62
- def compute_max_iou(true_boxes, pred_box):
63
- """Compute maximum IoU between a predicted box and all true boxes"""
64
- max_iou = 0
65
- for true_box in true_boxes:
66
- iou = compute_iou(true_box, pred_box)
67
- max_iou = max(max_iou, iou)
68
- return max_iou
69
-
70
- @router.post(ROUTE, tags=["Image Task"],
71
- description=DESCRIPTION)
72
- 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
82
- - Object Detection accuracy: IoU (Intersection over Union) for smoke bounding boxes
83
- """
84
- # Get space info
85
- username, space_url = get_space_info()
86
-
87
- # Load and prepare the dataset
88
- dataset = load_dataset(request.dataset_name, token=os.getenv("HF_TOKEN"))
89
-
90
- # Split dataset
91
- train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
92
- test_dataset = train_test["test"]
93
-
94
- # Start tracking emissions
95
- tracker.start()
96
- tracker.start_task("inference")
97
-
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 = []
106
- true_boxes_list = [] # List of lists, each inner list contains boxes for one image
107
-
108
- for example in test_dataset:
109
- # Parse true annotation (YOLO format: class_id x_center y_center width height)
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
136
- #--------------------------------------------------------------------------------------------
137
-
138
- # Stop tracking emissions
139
- emissions_data = tracker.stop_task()
140
-
141
- # Calculate classification accuracy
142
- classification_accuracy = accuracy_score(true_labels, predictions)
143
-
144
- # Calculate mean IoU for object detection (only for images with smoke)
145
- # For each image, we compute the max IoU between the predicted box and all true boxes
146
- ious = []
147
- for true_boxes, pred_box in zip(true_boxes_list, pred_boxes):
148
- max_iou = compute_max_iou(true_boxes, pred_box)
149
- ious.append(max_iou)
150
-
151
- mean_iou = float(np.mean(ious)) if ious else 0.0
152
-
153
- # Prepare results dictionary
154
- results = {
155
- "username": username,
156
- "space_url": space_url,
157
- "submission_timestamp": datetime.now().isoformat(),
158
- "model_description": DESCRIPTION,
159
- "classification_accuracy": float(classification_accuracy),
160
- "mean_iou": mean_iou,
161
- "energy_consumed_wh": emissions_data.energy_consumed * 1000,
162
- "emissions_gco2eq": emissions_data.emissions * 1000,
163
- "emissions_data": clean_emissions_data(emissions_data),
164
- "api_route": ROUTE,
165
- "dataset_config": {
166
- "dataset_name": request.dataset_name,
167
- "test_size": request.test_size,
168
- "test_seed": request.test_seed
169
- }
170
- }
171
-
172
- return results