forked from whai362/PSENet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt.py
191 lines (163 loc) · 5.27 KB
/
plt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#coding=utf-8
'''
Created on 2016-9-27
@author: dengdan
'''
import matplotlib.pyplot as plt
import numpy as np
import util
def hist(x, title = None, normed = False, show = True, save = False, save_path = None, bin_count = 100, bins = None):
x = np.asarray(x)
if len(np.shape(x)) > 1:
# x = np.reshape(x, np.prod(x.shape))
x = util.np.flatten(x)
if bins == None:
bins = np.linspace(start = min(x), stop = max(x), num = bin_count, endpoint = True, retstep = False)
plt.figure(num = title)
plt.hist(x, bins, normed = normed)
if save:
if save_path is None:
raise ValueError
path = util.io.join_path(save_path, title + '.png')
save_image(path)
if show:
plt.show()
#util.img.imshow(title, path, block = block)
def plot_solver_data(solver_path):
data = util.io.load(solver_path)
training_losses = data.training_losses
training_accuracies = data.training_accuracies
val_losses = data.val_losses
val_accuracies = data.val_accuracies
plt.figure(solver_path)
n = len(training_losses)
x = range(n)
plt.plot(x, training_losses, 'r-', label = 'Training Loss')
if len(training_accuracies) > 0:
plt.plot(x, training_accuracies, 'r--', label = 'Training Accuracy')
if len(val_losses) > 0:
n = len(val_losses)
x = range(n)
plt.plot(x, val_losses, 'g-', label = 'Validation Loss')
if len(val_accuracies) > 0:
plt.plot(x, val_accuracies, 'g--', label = 'Validation Accuracy')
plt.legend()
plt.show()
def rectangle(xy, width, height, color = 'red', linewidth = 1, fill = False, alpha = None, axis = None):
"""
draw a rectangle on plt axis
"""
import matplotlib.patches as patches
rect = patches.Rectangle(
xy = xy,
width = width,
height = height,
alpha = alpha,
color = color,
fill = fill,
linewidth = linewidth
)
if axis is not None:
axis.add_patch(rect)
return rect
rect = rectangle
def maximize_figure():
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
def line(xy_start, xy_end, color = 'red', linewidth = 1, alpha = None, axis = None):
"""
draw a line on plt axis
"""
from matplotlib.lines import Line2D
num = 100
xdata = np.linspace(xy_start[0], xy_end[0], num = num)
ydata = np.linspace(xy_start[1], xy_end[1], num = num)
line = Line2D(
alpha = alpha,
color = color,
linewidth = linewidth,
xdata = xdata,
ydata = ydata
)
if axis is not None:
axis.add_line(line)
return line
def imshow(title = None, img = None, gray = False):
show_images([img], [title], gray = gray)
def show_images(images, titles = None, shape = None, share_axis = False,
bgr2rgb = False, maximized = False,
show = True, gray = False, save = False, colorbar = False,
path = None, axis_off = False, vertical = False, subtitle = None):
if shape == None:
if vertical:
shape = (len(images), 1)
else:
shape = (1, len(images))
ret_axes = []
ax0 = None
for idx, img in enumerate(images):
if bgr2rgb:
img = util.img.bgr2rgb(img)
loc = (idx / shape[1], idx % shape[1])
if idx == 0:
ax = plt.subplot2grid(shape, loc)
ax0 = ax
else:
if share_axis:
ax = plt.subplot2grid(shape, loc, sharex = ax0, sharey = ax0)
else:
ax = plt.subplot2grid(shape, loc)
if len(np.shape(img)) == 2 and gray:
img_ax = ax.imshow(img, cmap = 'gray')
else:
img_ax = ax.imshow(img)
if len(np.shape(img)) == 2 and colorbar:
plt.colorbar(img_ax, ax = ax)
if titles != None:
ax.set_title(titles[idx])
if axis_off:
plt.axis('off')
# plt.xticks([]), plt.yticks([])
ret_axes.append(ax)
if subtitle is not None:
set_subtitle(subtitle)
if maximized:
maximize_figure()
if save:
if path is None:
raise ValueError('path can not be None when save is True')
save_image(path)
if show:
plt.show()
return ret_axes
def save_image(path, img = None, dpi = 150):
path = util.io.get_absolute_path(path)
util.io.make_parent_dir(path)
if img is None:
plt.gcf().savefig(path, dpi = dpi)
else:
plt.imsave(path, img)
imwrite = save_image
def to_ROI(ax, ROI):
xy1, xy2 = ROI
xmin, ymin = xy1
xmax, ymax = xy2
ax.set_xlim(xmin, xmax)
#ax.extent
ax.set_ylim(ymax, ymin)
def set_subtitle(title, fontsize = 12):
plt.gcf().suptitle(title, fontsize=fontsize)
def show(maximized = False):
if maximized:
maximize_figure()
plt.show()
def draw():
plt.gcf().canvas.draw()
def get_random_line_style():
colors = ['r', 'g', 'b']
line_types = ['-']#, '--', '-.', ':']
idx = util.rand.randint(len(colors))
color = colors[idx]
idx = util.rand.randint(len(line_types))
line_type = line_types[idx]
return color + line_type