Addax-Data-Science commited on
Commit
b6e17d6
·
verified ·
1 Parent(s): aba2956

Upload inference.py

Browse files
Files changed (1) hide show
  1. inference.py +8 -8
inference.py CHANGED
@@ -169,7 +169,7 @@ class ModelInference:
169
  else:
170
  return input_size_network
171
 
172
- def get_classification(self, crop: Image.Image) -> list[tuple[str, float]]:
173
  """
174
  Run YOLOv8 classification on cropped image.
175
 
@@ -177,8 +177,9 @@ class ModelInference:
177
  crop: Cropped and preprocessed PIL Image
178
 
179
  Returns:
180
- List of (class_name, confidence) tuples for ALL classes, sorted by confidence.
181
- Example: [("giraffe", 0.99985), ("cattle", 0.00003), ...]
 
182
 
183
  Raises:
184
  RuntimeError: If model not loaded or inference fails
@@ -197,16 +198,15 @@ class ModelInference:
197
  # Extract probabilities: [0.0001, 0.0002, ..., 0.9998, ...]
198
  probs = results[0].probs.data.tolist()
199
 
200
- # Build list of (class_name, confidence) tuples
201
  # Return YOLOv8's class names (which will be mapped to taxonomy IDs later)
202
  classifications = []
203
  for idx, class_name in names_dict.items():
204
  confidence = probs[idx]
205
- classifications.append((class_name, confidence))
206
-
207
- # Sort by confidence descending (already sorted by YOLOv8, but ensure it)
208
- classifications.sort(key=lambda x: x[1], reverse=True)
209
 
 
 
210
  return classifications
211
 
212
  except Exception as e:
 
169
  else:
170
  return input_size_network
171
 
172
+ def get_classification(self, crop: Image.Image) -> list[list[str, float]]:
173
  """
174
  Run YOLOv8 classification on cropped image.
175
 
 
177
  crop: Cropped and preprocessed PIL Image
178
 
179
  Returns:
180
+ List of [class_name, confidence] lists for ALL classes, in model order.
181
+ Example: [["aardwolf", 0.00001], ["giraffe", 0.99985], ...]
182
+ NOTE: Sorting by confidence is handled by classification_worker.py
183
 
184
  Raises:
185
  RuntimeError: If model not loaded or inference fails
 
198
  # Extract probabilities: [0.0001, 0.0002, ..., 0.9998, ...]
199
  probs = results[0].probs.data.tolist()
200
 
201
+ # Build list of [class_name, confidence] pairs (as lists, not tuples!)
202
  # Return YOLOv8's class names (which will be mapped to taxonomy IDs later)
203
  classifications = []
204
  for idx, class_name in names_dict.items():
205
  confidence = probs[idx]
206
+ classifications.append([class_name, confidence])
 
 
 
207
 
208
+ # NOTE: Sorting by confidence is handled by classification_worker.py
209
+ # Model developers don't need to sort - just return all class predictions
210
  return classifications
211
 
212
  except Exception as e: