Add docstrings to YOLOv5 functions (#12760)
* Add docstrings to top level files * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Add docstrings * Auto-format by https://ultralytics.com/actions * Update activations.py Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> * Auto-format by https://ultralytics.com/actions --------- Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
1 parent
ca00c34c2a
commit
41603da162
42 files changed
+983
-322
No files matched your search
+20
-6
@@ -13,13 +13,13 @@ from utils import TryExcept, threaded
|
||||
|
||||
|
||||
def fitness(x):
|
||||
# Model fitness as a weighted combination of metrics
|
||||
"""Calculates fitness of a model using weighted sum of metrics P, R, mAP@0.5, mAP@0.5:0.95."""
|
||||
w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
|
||||
return (x[:, :4] * w).sum(1)
|
||||
|
||||
|
||||
def smooth(y, f=0.05):
|
||||
# Box filter of fraction f
|
||||
"""Applies box filter smoothing to array `y` with fraction `f`, yielding a smoothed array."""
|
||||
nf = round(len(y) * f * 2) // 2 + 1 # number of filter elements (must be odd)
|
||||
p = np.ones(nf // 2) # ones padding
|
||||
yp = np.concatenate((p * y[0], y, p * y[-1]), 0) # y padded
|
||||
@@ -126,6 +126,7 @@ def compute_ap(recall, precision):
|
||||
class ConfusionMatrix:
|
||||
# Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
|
||||
def __init__(self, nc, conf=0.25, iou_thres=0.45):
|
||||
"""Initializes ConfusionMatrix with given number of classes, confidence, and IoU threshold."""
|
||||
self.matrix = np.zeros((nc + 1, nc + 1))
|
||||
self.nc = nc # number of classes
|
||||
self.conf = conf
|
||||
@@ -179,6 +180,9 @@ class ConfusionMatrix:
|
||||
self.matrix[dc, self.nc] += 1 # predicted background
|
||||
|
||||
def tp_fp(self):
|
||||
"""Calculates true positives (tp) and false positives (fp) excluding the background class from the confusion
|
||||
matrix.
|
||||
"""
|
||||
tp = self.matrix.diagonal() # true positives
|
||||
fp = self.matrix.sum(1) - tp # false positives
|
||||
# fn = self.matrix.sum(0) - tp # false negatives (missed detections)
|
||||
@@ -186,6 +190,7 @@ class ConfusionMatrix:
|
||||
|
||||
@TryExcept("WARNING ⚠️ ConfusionMatrix plot failure")
|
||||
def plot(self, normalize=True, save_dir="", names=()):
|
||||
"""Plots confusion matrix using seaborn, optional normalization; can save plot to specified directory."""
|
||||
import seaborn as sn
|
||||
|
||||
array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1e-9) if normalize else 1) # normalize columns
|
||||
@@ -217,12 +222,17 @@ class ConfusionMatrix:
|
||||
plt.close(fig)
|
||||
|
||||
def print(self):
|
||||
"""Prints the confusion matrix row-wise, with each class and its predictions separated by spaces."""
|
||||
for i in range(self.nc + 1):
|
||||
print(" ".join(map(str, self.matrix[i])))
|
||||
|
||||
|
||||
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
|
||||
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
|
||||
"""
|
||||
Calculates IoU, GIoU, DIoU, or CIoU between two boxes, supporting xywh/xyxy formats.
|
||||
|
||||
Input shapes are box1(1,4) to box2(n,4).
|
||||
"""
|
||||
|
||||
# Get the coordinates of bounding boxes
|
||||
if xywh: # transform from xywh to xyxy
|
||||
@@ -312,7 +322,9 @@ def bbox_ioa(box1, box2, eps=1e-7):
|
||||
|
||||
|
||||
def wh_iou(wh1, wh2, eps=1e-7):
|
||||
# Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
|
||||
"""Calculates the Intersection over Union (IoU) for two sets of widths and heights; `wh1` and `wh2` should be nx2
|
||||
and mx2 tensors.
|
||||
"""
|
||||
wh1 = wh1[:, None] # [N,1,2]
|
||||
wh2 = wh2[None] # [1,M,2]
|
||||
inter = torch.min(wh1, wh2).prod(2) # [N,M]
|
||||
@@ -324,7 +336,9 @@ def wh_iou(wh1, wh2, eps=1e-7):
|
||||
|
||||
@threaded
|
||||
def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=()):
|
||||
# Precision-recall curve
|
||||
"""Plots precision-recall curve, optionally per class, saving to `save_dir`; `px`, `py` are lists, `ap` is Nx2
|
||||
array, `names` optional.
|
||||
"""
|
||||
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
|
||||
py = np.stack(py, axis=1)
|
||||
|
||||
@@ -347,7 +361,7 @@ def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=()):
|
||||
|
||||
@threaded
|
||||
def plot_mc_curve(px, py, save_dir=Path("mc_curve.png"), names=(), xlabel="Confidence", ylabel="Metric"):
|
||||
# Metric-confidence curve
|
||||
"""Plots a metric-confidence curve for model predictions, supporting per-class visualization and smoothing."""
|
||||
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
|
||||
|
||||
if 0 < len(names) < 21: # display per-class legend if < 21 classes
|
||||
|
||||
Reference in new issue
Block a user