Diff of /utils/segment/plots.py [000000] .. [190ca4]

Switch to unified view

a b/utils/segment/plots.py
1
import contextlib
2
import math
3
from pathlib import Path
4
5
import cv2
6
import matplotlib.pyplot as plt
7
import numpy as np
8
import pandas as pd
9
import torch
10
11
from .. import threaded
12
from ..general import xywh2xyxy
13
from ..plots import Annotator, colors
14
15
16
@threaded
17
def plot_images_and_masks(images, targets, masks, paths=None, fname='images.jpg', names=None):
18
    # Plot image grid with labels
19
    if isinstance(images, torch.Tensor):
20
        images = images.cpu().float().numpy()
21
    if isinstance(targets, torch.Tensor):
22
        targets = targets.cpu().numpy()
23
    if isinstance(masks, torch.Tensor):
24
        masks = masks.cpu().numpy().astype(int)
25
26
    max_size = 1920  # max image size
27
    max_subplots = 16  # max image subplots, i.e. 4x4
28
    bs, _, h, w = images.shape  # batch size, _, height, width
29
    bs = min(bs, max_subplots)  # limit plot images
30
    ns = np.ceil(bs ** 0.5)  # number of subplots (square)
31
    if np.max(images[0]) <= 1:
32
        images *= 255  # de-normalise (optional)
33
34
    # Build Image
35
    mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8)  # init
36
    for i, im in enumerate(images):
37
        if i == max_subplots:  # if last batch has fewer images than we expect
38
            break
39
        x, y = int(w * (i // ns)), int(h * (i % ns))  # block origin
40
        im = im.transpose(1, 2, 0)
41
        mosaic[y:y + h, x:x + w, :] = im
42
43
    # Resize (optional)
44
    scale = max_size / ns / max(h, w)
45
    if scale < 1:
46
        h = math.ceil(scale * h)
47
        w = math.ceil(scale * w)
48
        mosaic = cv2.resize(mosaic, tuple(int(x * ns) for x in (w, h)))
49
50
    # Annotate
51
    fs = int((h + w) * ns * 0.01)  # font size
52
    annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True, example=names)
53
    for i in range(i + 1):
54
        x, y = int(w * (i // ns)), int(h * (i % ns))  # block origin
55
        annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2)  # borders
56
        if paths:
57
            annotator.text([x + 5, y + 5], text=Path(paths[i]).name[:40], txt_color=(220, 220, 220))  # filenames
58
        if len(targets) > 0:
59
            idx = targets[:, 0] == i
60
            ti = targets[idx]  # image targets
61
62
            boxes = xywh2xyxy(ti[:, 2:6]).T
63
            classes = ti[:, 1].astype('int')
64
            labels = ti.shape[1] == 6  # labels if no conf column
65
            conf = None if labels else ti[:, 6]  # check for confidence presence (label vs pred)
66
67
            if boxes.shape[1]:
68
                if boxes.max() <= 1.01:  # if normalized with tolerance 0.01
69
                    boxes[[0, 2]] *= w  # scale to pixels
70
                    boxes[[1, 3]] *= h
71
                elif scale < 1:  # absolute coords need scale if image scales
72
                    boxes *= scale
73
            boxes[[0, 2]] += x
74
            boxes[[1, 3]] += y
75
            for j, box in enumerate(boxes.T.tolist()):
76
                cls = classes[j]
77
                color = colors(cls)
78
                cls = names[cls] if names else cls
79
                if labels or conf[j] > 0.25:  # 0.25 conf thresh
80
                    label = f'{cls}' if labels else f'{cls} {conf[j]:.1f}'
81
                    annotator.box_label(box, label, color=color)
82
83
            # Plot masks
84
            if len(masks):
85
                if masks.max() > 1.0:  # mean that masks are overlap
86
                    image_masks = masks[[i]]  # (1, 640, 640)
87
                    nl = len(ti)
88
                    index = np.arange(nl).reshape(nl, 1, 1) + 1
89
                    image_masks = np.repeat(image_masks, nl, axis=0)
90
                    image_masks = np.where(image_masks == index, 1.0, 0.0)
91
                else:
92
                    image_masks = masks[idx]
93
94
                im = np.asarray(annotator.im).copy()
95
                for j, box in enumerate(boxes.T.tolist()):
96
                    if labels or conf[j] > 0.25:  # 0.25 conf thresh
97
                        color = colors(classes[j])
98
                        mh, mw = image_masks[j].shape
99
                        if mh != h or mw != w:
100
                            mask = image_masks[j].astype(np.uint8)
101
                            mask = cv2.resize(mask, (w, h))
102
                            mask = mask.astype(bool)
103
                        else:
104
                            mask = image_masks[j].astype(bool)
105
                        with contextlib.suppress(Exception):
106
                            im[y:y + h, x:x + w, :][mask] = im[y:y + h, x:x + w, :][mask] * 0.4 + np.array(color) * 0.6
107
                annotator.fromarray(im)
108
    annotator.im.save(fname)  # save
109
110
111
def plot_results_with_masks(file='path/to/results.csv', dir='', best=True):
112
    # Plot training results.csv. Usage: from utils.plots import *; plot_results('path/to/results.csv')
113
    save_dir = Path(file).parent if file else Path(dir)
114
    fig, ax = plt.subplots(2, 8, figsize=(18, 6), tight_layout=True)
115
    ax = ax.ravel()
116
    files = list(save_dir.glob('results*.csv'))
117
    assert len(files), f'No results.csv files found in {save_dir.resolve()}, nothing to plot.'
118
    for f in files:
119
        try:
120
            data = pd.read_csv(f)
121
            index = np.argmax(0.9 * data.values[:, 8] + 0.1 * data.values[:, 7] + 0.9 * data.values[:, 12] +
122
                              0.1 * data.values[:, 11])
123
            s = [x.strip() for x in data.columns]
124
            x = data.values[:, 0]
125
            for i, j in enumerate([1, 2, 3, 4, 5, 6, 9, 10, 13, 14, 15, 16, 7, 8, 11, 12]):
126
                y = data.values[:, j]
127
                # y[y == 0] = np.nan  # don't show zero values
128
                ax[i].plot(x, y, marker='.', label=f.stem, linewidth=2, markersize=2)
129
                if best:
130
                    # best
131
                    ax[i].scatter(index, y[index], color='r', label=f'best:{index}', marker='*', linewidth=3)
132
                    ax[i].set_title(s[j] + f'\n{round(y[index], 5)}')
133
                else:
134
                    # last
135
                    ax[i].scatter(x[-1], y[-1], color='r', label='last', marker='*', linewidth=3)
136
                    ax[i].set_title(s[j] + f'\n{round(y[-1], 5)}')
137
                # if j in [8, 9, 10]:  # share train and val loss y axes
138
                #     ax[i].get_shared_y_axes().join(ax[i], ax[i - 5])
139
        except Exception as e:
140
            print(f'Warning: Plotting error for {f}: {e}')
141
    ax[1].legend()
142
    fig.savefig(save_dir / 'results.png', dpi=200)
143
    plt.close()