forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor collections and fstrings (ultralytics#7821)
* Update torch_utils.py * Additional code refactoring * tuples to sets * Cleanup
- Loading branch information
1 parent
3356f26
commit f000714
Showing
9 changed files
with
58 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,32 +55,31 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names | |
i = pred_cls == c | ||
n_l = nt[ci] # number of labels | ||
n_p = i.sum() # number of predictions | ||
|
||
if n_p == 0 or n_l == 0: | ||
continue | ||
else: | ||
# Accumulate FPs and TPs | ||
fpc = (1 - tp[i]).cumsum(0) | ||
tpc = tp[i].cumsum(0) | ||
|
||
# Recall | ||
recall = tpc / (n_l + eps) # recall curve | ||
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases | ||
# Accumulate FPs and TPs | ||
fpc = (1 - tp[i]).cumsum(0) | ||
tpc = tp[i].cumsum(0) | ||
|
||
# Recall | ||
recall = tpc / (n_l + eps) # recall curve | ||
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases | ||
|
||
# Precision | ||
precision = tpc / (tpc + fpc) # precision curve | ||
p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score | ||
# Precision | ||
precision = tpc / (tpc + fpc) # precision curve | ||
p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score | ||
|
||
# AP from recall-precision curve | ||
for j in range(tp.shape[1]): | ||
ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) | ||
if plot and j == 0: | ||
py.append(np.interp(px, mrec, mpre)) # precision at [email protected] | ||
# AP from recall-precision curve | ||
for j in range(tp.shape[1]): | ||
ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) | ||
if plot and j == 0: | ||
py.append(np.interp(px, mrec, mpre)) # precision at [email protected] | ||
|
||
# Compute F1 (harmonic mean of precision and recall) | ||
f1 = 2 * p * r / (p + r + eps) | ||
names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data | ||
names = {i: v for i, v in enumerate(names)} # to dict | ||
names = dict(enumerate(names)) # to dict | ||
if plot: | ||
plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names) | ||
plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1') | ||
|
@@ -314,7 +313,7 @@ def wh_iou(wh1, wh2): | |
# Plots ---------------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()): | ||
def plot_pr_curve(px, py, ap, save_dir=Path('pr_curve.png'), names=()): | ||
# Precision-recall curve | ||
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) | ||
py = np.stack(py, axis=1) | ||
|
@@ -331,11 +330,11 @@ def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()): | |
ax.set_xlim(0, 1) | ||
ax.set_ylim(0, 1) | ||
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") | ||
fig.savefig(Path(save_dir), dpi=250) | ||
fig.savefig(save_dir, dpi=250) | ||
plt.close() | ||
|
||
|
||
def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'): | ||
def plot_mc_curve(px, py, save_dir=Path('mc_curve.png'), names=(), xlabel='Confidence', ylabel='Metric'): | ||
# Metric-confidence curve | ||
fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) | ||
|
||
|
@@ -352,5 +351,5 @@ def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence' | |
ax.set_xlim(0, 1) | ||
ax.set_ylim(0, 1) | ||
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") | ||
fig.savefig(Path(save_dir), dpi=250) | ||
fig.savefig(save_dir, dpi=250) | ||
plt.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters