-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
279 lines (226 loc) · 9.66 KB
/
plotting.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import keras
import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from skimage.color import lab2rgb, rgb2lab
from tqdm import tqdm
from data_manager import get_soft_enconding_ab
from model import create_model
# def colorize_benchmark_images(model, show_fid=True):
# data = data_manager.get_benchmark_images()
# if show_fid:
# originals = fid.return_benchmark_originals()
# for i in range(data['input'].shape[0]):
# if show_fid:
# colorized = fid.return_colorized(model, data['input'][i][:, :, :])
# fid_val = fid.return_fid(colorized, originals[i])
# print("The Frechet Inception Difference is:", fid_val)
# plot_output(model, data['input'][i][:, :, :], data['target'][i][:, :, :])
# plt.savefig('demo{}.png'.format(i), bbox_inches='tight') # Lucas needs this to compile
class epoch_plot(keras.callbacks.Callback):
def __init__(self, settings, model, w, image_path, ):
self.settings = settings
self.model = model
self.w = w
self.img = image_path
def on_train_batch_begin(self, batch, logs=None):
if batch % self.settings.training_steps_per_epoch == 0:
plot_prediction(self.settings, self.model, self.w, self.img)
def plot_prediction(settings, model, w, image_path):
# This functions makes a prediction given an image path and plots it
# Loading the color space bins
cs = np.load('dataset/data/color_space.npy')
# And the image
rgb_image = get_rgb_from_path(image_path)
# Getting LAB channels from rgb image
L, A, B = get_lab_channels_from_rgb(rgb_image)
# Reshaping input to fit model
input_to_model = np.empty((1, L.shape[0], L.shape[1], 1))
input_to_model[0, :, :, 0] = L - 50 # We train with shifted L so lets use it here as well
# Creating a new model, setting weights and predicting
predictModel = create_model(settings, w, training=False)
predictModel.set_weights(model.get_weights())
out = predictModel.predict(input_to_model, batch_size=1, verbose=1)
# Removing the model
del predictModel
# Getting single image from prediction
img_predict = out[0, :, :, :]
# Picking out the A and B values from the predicted bins
A = cs[np.argmax(img_predict, axis=2)][:, :, 0]
B = cs[np.argmax(img_predict, axis=2)][:, :, 1]
# Some magic to match dimensions since we are losing a few pixels in the forward pass
diff1 = A.shape[0] - L.shape[0]
diff2 = A.shape[1] - L.shape[1]
crop1 = int(diff1 // 2)
crop2 = -int(diff1 - diff1 // 2)
if crop2 == 0:
crop2 = A.shape[0] + 1
crop3 = int(diff2 // 2)
crop4 = -int(diff2 - diff2 // 2)
if crop4 == 0:
crop4 = A.shape[1] + 1
# Cropping (and clipping is probably not needed if color space is correct)
A = np.clip(A[crop1:crop2, crop3:crop4], a_min=-110, a_max=110)
B = np.clip(B[crop1:crop2, crop3:crop4], a_min=-110, a_max=110)
# Putting the image back together
lab_image = combine_lab(L, A, B)
# Back to rgb for plotting
predicted_rgb_image = lab2rgb(lab_image)
f = plt.figure(figsize=(10, 10))
ax1 = f.add_subplot(131)
imgplot = plt.imshow(predicted_rgb_image)
plt.title("Combined prediction")
# Just AB plotting
lab_image = combine_lab_no_l_channel(A, B)
predicted_rgb_image = lab2rgb(lab_image)
ax1 = f.add_subplot(132)
imgplot = plt.imshow(predicted_rgb_image)
plt.title("A B prediction")
# Ground truth RGB
ax1 = f.add_subplot(133)
imgplot = plt.imshow(rgb_image)
plt.title("Original Image")
plt.show()
print("Predicted values for L, A & B: ")
print("L: {} to {}.".format(np.min(L), np.max(L)))
print("A: {} to {}.".format(np.min(A), np.max(A)))
print("B: {} to {}.".format(np.min(B), np.max(B)))
return
def plot_unique_colours_gamut(unique_colors):
# Plots the gamut that we are using ie the color of the bins
grid = np.ones((22, 22))
L = np.ones(grid.shape) * 50
A = np.ones(grid.shape)
B = np.ones(grid.shape)
for i in range(-110, 110, 10):
for j in range(-110, 110, 10):
if np.any(np.all(np.array(([i, j])) == unique_colors, axis=1)):
for idx, c in enumerate(unique_colors):
if np.all(np.array(([i, j])) == c):
A[11 + int(i / 10), int(11 + j / 10)] = unique_colors[idx][0]
B[11 + int(i / 10), int(11 + j / 10)] = unique_colors[idx][1]
else:
L[11 + int(i / 10), int(11 + j / 10)] = 0
img_combined = combine_lab(L, A, B)
picture = lab2rgb(img_combined)
_ = plt.imshow(picture)
plt.yticks(range(22), range(110, -110, -10))
plt.xticks(range(22), range(-110, 110, 10))
plt.title("Gamut for unique colours")
plt.show()
def plot_weights_gamut(unique_colors, priors):
# Plots the gamut that we are using ie the color of the bins
grid = np.ones((22, 22))
gamut = priors
for i in range(-110, 110, 10):
for j in range(-110, 110, 10):
if np.any(np.all(np.array(([i, j])) == unique_colors, axis=1)):
for idx, c in enumerate(unique_colors):
if np.all(np.array(([i, j])) == c):
grid[11 + int(i / 10), int(11 + j / 10)] += gamut[idx]
else:
grid[11 + int(i / 10), int(11 + j / 10)] = 0
plt.imshow(grid, cmap='inferno')
plt.yticks(range(22), range(110, -110, -10))
plt.xticks(range(22), range(-110, 110, 10))
plt.title("Original Gamut")
plt.show()
def combine_lab(L, A, B):
# Combines LAB to image
img_combined = np.zeros(([A.shape[0], A.shape[1], 3]))
img_combined[:, :, 0] = L
img_combined[:, :, 1] = A
img_combined[:, :, 2] = B
return img_combined
def combine_lab_no_l_channel(A, B):
# Combines A and B with a solid L to showcase colors only
img_combined = np.zeros(([A.shape[0], A.shape[1], 3]))
L = np.ones(A.shape) * 50
img_combined[:, :, 0] = L
img_combined[:, :, 1] = A
img_combined[:, :, 2] = B
return img_combined
def get_rgb_from_path(path):
img = io.imread(path)
return img
def get_rgb_from_lab(lab_image):
return lab2rgb(lab_image)
def get_lab_channels_from_rgb(rgb_image):
# Returns the LAB channels from RGB image
images_lab = rgb2lab(rgb_image)
L = images_lab[:, :, 0]
A = images_lab[:, :, 1]
B = images_lab[:, :, 2]
return L, A, B
def plot_gamut_from_bins(img_AB, unique_colors):
# Plots the gamut of an image
grid = np.ones((22, 22))
gamut = np.sum(np.sum(img_AB, axis=0), axis=0)
for i in range(-110, 110, 10):
for j in range(-110, 110, 10):
if np.any(np.all(np.array(([i, j])) == unique_colors, axis=1)):
for idx, c in enumerate(unique_colors):
if np.all(np.array(([i, j])) == c):
grid[11 + int(i / 10), int(11 + j / 10)] += gamut[idx]
else:
grid[11 + int(i / 10), int(11 + j / 10)] = 0
plt.imshow(grid, cmap='inferno')
plt.yticks(range(22), range(110, -110, -10))
plt.xticks(range(22), range(-110, 110, 10))
plt.title("Original Gamut")
plt.show()
def plotting_demo():
# This is just to show the different plotting functions
# lets print the unique color bins
unique_colors = np.load('dataset/data/color_space.npy')
# first we try to read, split to lab, recombine and print
image_path = 'dataset/data/train/n01440764/n01440764_141.JPEG'
rgb_image = get_rgb_from_path(image_path)
L, A, B = get_lab_channels_from_rgb(rgb_image)
img_combined = combine_lab(L, A, B)
picture = get_rgb_from_lab(img_combined)
f = plt.figure(figsize=(10, 12))
ax1 = f.add_subplot(131)
_ = plt.imshow(picture)
plt.title("Combined input image in pre_process")
# Then lets try to recombine from one hot encoded bin values for A and B
A = unique_colors[np.argmax(get_soft_enconding_ab(np.array(([A, B])).T, unique_colors), axis=2)][:, :, 0].T
B = unique_colors[np.argmax(get_soft_enconding_ab(np.array(([A, B])).T, unique_colors), axis=2)][:, :, 1].T
img_combined = combine_lab(L, A, B)
picture = get_rgb_from_lab(img_combined)
ax1 = f.add_subplot(132)
_ = plt.imshow(picture)
plt.title("Combined input image with one_hot")
# Then just the AB channels
A = unique_colors[np.argmax(get_soft_enconding_ab(np.array(([A, B])).T, unique_colors), axis=2)][:, :, 0].T
B = unique_colors[np.argmax(get_soft_enconding_ab(np.array(([A, B])).T, unique_colors), axis=2)][:, :, 1].T
img_combined = combine_lab_no_l_channel(A, B)
picture = get_rgb_from_lab(img_combined)
ax1 = f.add_subplot(133)
_ = plt.imshow(picture)
plt.title("Input image with one_hot")
plt.show()
img_AB = get_soft_enconding_ab(np.array(([A, B])).T, unique_colors)
plot_gamut_from_bins(img_AB, unique_colors)
# plot uniques
plot_unique_colours_gamut(unique_colors)
def colorize_images_in_folder(settings, model, w, folder_path):
images = os.listdir(folder_path)
for image in tqdm(images):
file_path = folder_path + image
plot_prediction(settings, model, w, file_path)
def plot_epoch_metrics():
metric_data = np.genfromtxt('log.csv', delimiter=';')
plt.figure(figsize=(12, 3))
plt.subplot(121)
plt.plot(metric_data[:, 0], metric_data[:, 1], label='Training Accuracy')
plt.plot(metric_data[:, 0], metric_data[:, 3], label='Validation Accuracy')
plt.title('Accuracy')
plt.legend()
plt.subplot(122)
plt.plot(metric_data[:, 0], metric_data[:, 2], label='Training Loss')
plt.plot(metric_data[:, 0], metric_data[:, 4], label='Validation Loss')
plt.title('Loss')
plt.legend()
plt.show()