TLMHoang commited on
Commit
de4edcc
·
verified ·
1 Parent(s): 589370b

Upload custom YOLO code

Browse files
Files changed (1) hide show
  1. models_related/train_eval/eval.py +34 -4
models_related/train_eval/eval.py CHANGED
@@ -66,15 +66,45 @@ def match_counts(pred_boxes: list[list[float]], gt_boxes: list[list[float]], iou
66
  return true_positive, false_positive, false_negative
67
 
68
 
69
- def model_profile(model, imgsz: int) -> tuple[int, float]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  """Return parameter count and GFLOPs for an Ultralytics model."""
71
  torch_model = getattr(model, "model", None)
72
  if torch_model is None:
73
  raise TypeError("Expected an Ultralytics PyTorch model with a .model attribute.")
74
  params = sum(p.numel() for p in torch_model.parameters())
75
- from ultralytics.utils.torch_utils import get_flops
76
-
77
- gflops = get_flops(torch_model, imgsz)
78
  return params, gflops
79
 
80
 
 
66
  return true_positive, false_positive, false_negative
67
 
68
 
69
+ def _profile_gflops(torch_model, imgsz: int) -> float | str:
70
+ """Return GFLOPs, or an empty string when FLOPs profiling is unavailable."""
71
+ reasons: list[str] = []
72
+ was_training = torch_model.training
73
+ torch_model.eval()
74
+ try:
75
+ try:
76
+ from ultralytics.utils.torch_utils import get_flops
77
+
78
+ gflops = float(get_flops(torch_model, imgsz))
79
+ if gflops > 0:
80
+ return gflops
81
+ reasons.append("get_flops returned 0.0; thop may be missing or unsupported for custom ops")
82
+ except Exception as exc:
83
+ reasons.append(f"get_flops failed: {exc}")
84
+
85
+ try:
86
+ from ultralytics.utils.torch_utils import get_flops_with_torch_profiler
87
+
88
+ gflops = float(get_flops_with_torch_profiler(torch_model, imgsz))
89
+ if gflops > 0:
90
+ return gflops
91
+ reasons.append("torch profiler returned 0.0")
92
+ except Exception as exc:
93
+ reasons.append(f"torch profiler failed: {exc}")
94
+ finally:
95
+ torch_model.train(was_training)
96
+
97
+ print("GFLOPs profile unavailable:", "; ".join(reasons))
98
+ return ""
99
+
100
+
101
+ def model_profile(model, imgsz: int) -> tuple[int, float | str]:
102
  """Return parameter count and GFLOPs for an Ultralytics model."""
103
  torch_model = getattr(model, "model", None)
104
  if torch_model is None:
105
  raise TypeError("Expected an Ultralytics PyTorch model with a .model attribute.")
106
  params = sum(p.numel() for p in torch_model.parameters())
107
+ gflops = _profile_gflops(torch_model, imgsz)
 
 
108
  return params, gflops
109
 
110