-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
182 lines (137 loc) · 6.14 KB
/
demo.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
import matplotlib.pyplot as plt
from main import *
def get_trained_CNN(data_size):
model = ColorizationNet()
model.load_state_dict(torch.load('./colorize_cnn_{}.pth'.format(data_size)))
# model.load_state_dict(torch.load('./colorize_cnn_{}_mse.pth'.format(data_size)))
model.eval()
print("Loaded CNN: colorize_cnn_{}".format(data_size))
return model
def image_to_tens(image):
return torch.Tensor(image)[None, None, :, :]
def H(Z, T, ab_domain):
def f_T(Z):
Z = np.exp(np.log(Z) / T) / np.sum(np.exp(np.log(Z)) / T, axis=2)[:, :, np.newaxis]
return Z
Z = f_T(Z)
# Minmax_scale
# Z_std = (Z - Z.min(axis=2)[:, :, np.newaxis]) / (Z.max(axis=2) - Z.min(axis=2))[:, :, np.newaxis]
# Z = Z_std * (1 - 0) + 0
Z = Z / np.sum(Z, axis=2)[:, :, np.newaxis]
Z = Z * (3/np.exp(T)) # Higher saturation
ab_domain = np.array(ab_domain)
final_ab = np.sum(Z[:, :, :, np.newaxis] * ab_domain[np.newaxis, np.newaxis, :, :], axis=2)
return final_ab
def postprocess_output(l_original, Z_output_tens, ab_domain, T):
Z_output = Z_output_tens.detach().numpy()
Z_output = np.moveaxis(Z_output, 1, 3)[0]
ab_output = H(Z_output, T, ab_domain)
lab_output = np.empty((l_original.shape[0], l_original.shape[1], 3))
lab_output[:, :, 0], lab_output[:, :, 1:] = l_original, ab_output
rgb_output = lab_to_rgb(lab_output)
return Z_output, lab_output, rgb_output
def lab_to_rgb(lab):
rgb = color.lab2rgb(lab) # Using scikit-image library.
return rgb
def plot_q_probabilities(q1, q2):
fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
data = [('q1', q1), ('q2', q2)]
for ax, (title, img) in zip(axes, data):
ax.set_title(title)
ax.plot(img)
ax.axis('off')
fig.tight_layout()
plt.show()
def plot_ab_channels(a, b, path, save_image):
fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
data = [('A', a), ('B', b)]
for ax, (title, img) in zip(axes, data):
ax.set_title(title)
ax.imshow(img, vmin=-110, vmax=110, cmap='Greys')
ax.axis('off')
fig.tight_layout()
if save_image:
plt.savefig(path)
plt.show()
def plot_images(original_rgb, output_rgb, T, path, save_image):
fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
data = [('Original', original_rgb), ('Result (T={})'.format(T), output_rgb)]
for ax, (title, img) in zip(axes, data):
ax.set_title(title)
ax.imshow(img)
ax.axis('off')
fig.tight_layout()
if save_image:
plt.savefig(path)
plt.show()
def plot_losses(data_size, path, save_images):
losses = pickle.load(open("pickles/losses_{}_mse.p".format(data_size), "rb"))
losses = np.array(losses).flatten()
plt.plot(losses)
plt.ylabel("Loss")
plt.xlabel("Update step")
if save_images:
plt.savefig(path)
else:
plt.show()
def experiment(data_size, model, num_images, T=0.6):
_, test_X, _, test_Y = pickle.load(open("pickles/train_test_{}.p".format(data_size), "rb"))
# Load and get helpers.
ab_to_q_dict_unsorted = pickle.load(open("pickles/ab_to_q_index_dict_unsorted.p", "rb"))
ab_domain = get_ab_domain(data_size, ab_to_q_dict_unsorted)
ab_to_q_dict = get_ab_to_q_dict(data_size, ab_domain)
q_to_ab_dict = get_q_to_ab_dict(data_size, ab_to_q_dict)
for i in range(num_images):
l_original, ab_original = test_X[i], test_Y[i]
l_original_tens = image_to_tens(l_original)
# Get prediction for demo image by CNN.
Z_output_tens = model(l_original_tens).cpu()
# Post-process prediction.
Z_output, lab_output, rgb_output = postprocess_output(l_original, Z_output_tens, ab_domain, T)
# Save the grayscale image.
plt.imsave('experiment/original_cnn/grayscale_{}.png'.format(i+1), l_original, cmap='gray')
# Save the colored output image.
plt.imsave('experiment/original_cnn/out_{}.png'.format(i+1), rgb_output)
# Save the original image.
lab_original = np.empty((l_original.shape[0], l_original.shape[1], 3))
lab_original[:, :, 0], lab_original[:, :, 1:] = l_original, ab_original
rgb_original = lab_to_rgb(lab_original)
plt.imsave('experiment/original_cnn/original_{}.png'.format(i+1), rgb_original)
print("Image {} done and saved.".format(i+1))
def demo():
# Parameters.
data_size = 13233
# T_steps = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.38]
T_steps = [0.6]
save_images = True
# Load trained and saved CNN model.
model = get_trained_CNN(data_size)
# experiment(data_size, model, num_images=50)
# exit("Experiment done")
for T in T_steps:
# Load and pre-process demo image.
original_image = np.asarray(Image.open(f"./data/Pascal_Affi_Nguessan_0001.jpg"))
l_original, ab_original = preprocess_image(original_image)
l_original_tens = image_to_tens(l_original)
# Load and get helpers.
ab_to_q_dict_unsorted = pickle.load(open("pickles/ab_to_q_index_dict_unsorted.p", "rb"))
ab_domain = get_ab_domain(data_size, ab_to_q_dict_unsorted)
ab_to_q_dict = get_ab_to_q_dict(data_size, ab_domain)
q_to_ab_dict = get_q_to_ab_dict(data_size, ab_to_q_dict)
# Get prediction for demo image by CNN.
Z_output_tens = model(l_original_tens).cpu()
# Post-process prediction.
Z_output, lab_output, rgb_output = postprocess_output(l_original, Z_output_tens, ab_domain, T)
# Plot images and color-channels.
plot_ab_channels(lab_output[:, :, 1], lab_output[:, :, 2], "image_results/ab_channels_{}.png".format(T), save_images)
plot_images(original_image, rgb_output, T, "image_results/original_vs_colored_{}.png".format(T), save_images)
# Plot losses.
plot_losses(data_size, "image_results/losses.png", save_images)
# Save the grayscale image.
if save_images:
plt.imsave('image_results/grayscale.png', l_original, cmap='gray')
# Save the colored image.
if save_images:
plt.imsave('image_results/out_img_{}.png'.format(T), rgb_output)
if __name__ == '__main__':
demo()