-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhelper.py
executable file
·172 lines (145 loc) · 7.58 KB
/
helper.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
# Copyright (c) 2017 Artsiom Sanakoyeu and Dmitry Kotovenko
"""
Helpr function to visualize predicted heatmaps
"""
import numpy as np
import os
from matplotlib import pyplot as plt
import preprocessing.vgg_preprocessing as vgg_preprocessing
def from_scaled_image(img):
return np.asarray((img + 1.) * 127., dtype=np.uint8)
def visualize_model_predictions(img_patches, ground_truth_patches, predictions_patches,
visualize=False,
visualize_each_class=False):
if visualize:
if visualize_each_class:
for img, ground_truth, prediction in zip(img_patches, ground_truth_patches,
predictions_patches):
num_cols = ground_truth.shape[-1]
plt.figure(figsize=(3 * (num_cols + 1), 10))
# plt.figure(figsize=(10, 10))
plt.subplot(3, 1, 1)
plt.title("Original Image.")
plt.imshow(from_scaled_image(img))
# plt.show(False)
# Now plot heatmap for an each class separately.
for cls in range(ground_truth.shape[-1]):
num_animals_gt = np.sum(ground_truth[:, :, cls])
num_animals_predicted = np.sum(prediction[:, :, cls])
plt.subplot(3, num_cols, num_cols + cls + 1)
plt.title("GT. Class{}: ({:.2f})".format(cls, num_animals_gt))
patch_img = from_scaled_image(img) # TODO: resize to the size of heatmap
plt.imshow(patch_img)
plt.imshow(ground_truth[:, :, cls], cmap='jet', alpha=0.5)
plt.subplot(3, num_cols, 2 * num_cols + cls + 1)
plt.title("Prediction. Class{}: ({:})".format(cls, num_animals_predicted))
plt.imshow(patch_img)
plt.imshow(prediction[:, :, cls], cmap='jet', alpha=0.5)
print("Class %d." % cls)
print("L2 loss on class: %f." % np.sqrt(np.sum(
np.power(ground_truth[:, :, cls] - prediction[:, :, cls], 2))))
print("Gt number of animals in class: %f." % num_animals_gt)
print("Predicted number of animals in class: %f." % num_animals_predicted)
print('AE:', np.abs(num_animals_predicted - num_animals_gt))
plt.show()
else:
for img, ground_truth, prediction in zip(img_patches, ground_truth_patches,
predictions_patches):
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.title("Original Image.")
plt.imshow(from_scaled_image(img))
plt.subplot(1, 3, 2)
plt.title("Ground Truth Density.")
im = plt.imshow(np.sum(ground_truth, axis=-1), cmap='jet')
plt.colorbar(im, orientation='horizontal')
plt.subplot(1, 3, 3)
plt.title("Predicted Density.")
im = plt.imshow(np.sum(prediction, axis=-1), cmap='jet')
plt.colorbar(im, orientation='horizontal')
print("L2 loss: %f." % np.sqrt(np.sum(np.power(ground_truth - prediction, 2))))
print("Gt number of animals: %f." % np.sum(ground_truth))
print("Predicted number of animals: %f." % np.sum(prediction))
plt.show()
print('\n\n')
# Now compute RMSE for each class.
rmse_loss = 0
for cls in range(ground_truth_patches.shape[-1]):
predicted_number = np.sum(predictions_patches[:, :, :, cls], axis=(1, 2))
true_number = np.sum(ground_truth_patches[:, :, :, cls], axis=(1, 2))
rmse_loss_for_class = np.sqrt(np.mean(np.power(predicted_number - true_number, 2)))
print("RMSE for class %d = %f." % (cls, rmse_loss_for_class))
rmse_loss += rmse_loss_for_class
# And find final RMSE .
rmse_loss /= ground_truth_patches.shape[-1]
print("\n\nRMSE = %f " % rmse_loss)
def inverse_vgg16_preproc(img):
img = img.copy()
means = [vgg_preprocessing._R_MEAN,
vgg_preprocessing._G_MEAN,
vgg_preprocessing._B_MEAN]
num_channels = img.shape[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
for i in range(num_channels):
img[:, :, i] += means[i]
img = np.asarray(img, dtype=np.uint8)
print img
assert np.all(0 <= img) and np.all(img <= 255)
return img
def visualize_segmentation(img_patches,
ground_truth_patches,
predictions_patches,
visualize=False,
visualize_each_class=False,
save=True):
if visualize:
if visualize_each_class:
for img, ground_truth, prediction in zip(img_patches, ground_truth_patches,
predictions_patches):
assert np.all(np.isclose(prediction.sum(axis=2), 1.0))
img = np.asarray(img, dtype=np.uint8)
num_cols = prediction.shape[-1]
plt.figure(figsize=(3 * (num_cols + 1), 10))
# plt.figure(figsize=(10, 10))
plt.subplot(3, 1, 1)
plt.title("Original Image.")
plt.imshow(img)
# plt.show(False)
prediction_labels = np.argmax(prediction, axis=2)
# Now plot heatmap for an each class separately.
for cls in range(prediction.shape[-1]):
plt.subplot(3, num_cols, num_cols + cls + 1)
plt.title("GT. Class{}".format(cls))
patch_img = img
plt.imshow(patch_img)
plt.imshow((ground_truth == cls).astype(float), cmap='jet', alpha=0.5, vmin=0.0, vmax=1.0)
plt.subplot(3, num_cols, 2 * num_cols + cls + 1)
plt.title("Prd. Class{}: max={:.2f}".format(cls, prediction[:, :, cls].astype(float).max()))
plt.imshow(patch_img)
plt.imshow(prediction[:, :, cls], cmap='jet', alpha=0.65, vmin=0.0)
print("Class %d." % cls)
if save:
plt.figure(figsize=(20, 15))
plt.title("Prd. Class{}: max={:.2f}".format(cls, prediction[:, :, cls].astype(float).max()))
plt.imshow(patch_img)
plt.imshow(prediction_labels == cls, cmap='jet', alpha=0.65, vmin=0.0)
plt.savefig(os.path.expanduser('~/tmp/seg_predictions/class_{}.jpg'.format(cls)))
plt.close()
plt.show()
else:
for img, ground_truth, prediction in zip(img_patches, ground_truth_patches,
predictions_patches):
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.title("Original Image.")
plt.imshow(inverse_vgg16_preproc(img))
plt.subplot(1, 3, 2)
plt.title("GT Segmentation.")
im = plt.imshow(ground_truth, cmap='jet')
plt.colorbar(im, orientation='horizontal')
plt.subplot(1, 3, 3)
plt.title("Prd Hearmap.")
im = plt.imshow(prediction.sum(axis=2), cmap='jet', vmin=0, vmax=5)
plt.colorbar(im, orientation='horizontal')
plt.show()