-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_cifar.py
More file actions
226 lines (174 loc) · 7.16 KB
/
analysis_cifar.py
File metadata and controls
226 lines (174 loc) · 7.16 KB
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dropout, Activation, Flatten, Input, Lambda, Concatenate
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import ModelCheckpoint
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
<<<<<<< HEAD
# sns.set_style('darkgrid')
=======
>>>>>>> c52befd98a35c38576123956f9c1d0c115cca612
from metrics import PSNR, SSIM, MIX, SSIM_loss, SSIM_PSNR
metrics = ['mean_squared_error', 'mean_absolute_error', PSNR, SSIM, MIX]
def create_model_unetlike():
inp = Input(shape=(32, 32, 3))
conv1 = Conv2D(kernel_size=(3, 3), strides=(1, 1), filters=8 , padding='same', activation='relu')(inp)
mp1 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv1)
# 16x16x8
conv2 = Conv2D(kernel_size=(3, 3), strides=(1, 1), filters=16, padding='same', activation='relu')(mp1)
mp2 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv2)
# 8x8x16
conv3 = Conv2D(kernel_size=(3, 3), strides=(1, 1), filters=48, padding='same', activation='relu')(mp2)
# 8x8x48
ps2 = Lambda(lambda x : tf.nn.depth_to_space(x, 2, data_format='NHWC'))(conv3)
# 16x16x12
conc2 = Concatenate(axis=-1)([conv2, ps2])
conv4 = Conv2D(kernel_size=(3, 3), strides=(1, 1), filters=12, padding='same', activation='relu')(conc2)
ps1 = Lambda(lambda x : tf.nn.depth_to_space(x, 2, data_format='NHWC'))(conv4)
# 32x32x3
conc1 = Concatenate(axis=-1)([conv1, ps1])
conv5 = Conv2D(kernel_size=(3, 3), strides=(1, 1), filters=3, padding='same', activation='sigmoid')(conc1)
return Model(inp, conv5)
def change_model(model_path):
# create a new model and load the weights for marshall compatiblity.
model = create_model_unetlike()
model.load_weights(model_path)
return model
if __name__ == '__main__':
cwd = os.getcwd()
# Models trained of cifar.
models = ['unetlike_mse_cifar', 'unetlike_mae_cifar', 'unetlike_ssim_cifar',
'conv2_16filt_mse_cifar', 'conv2_16filt_mae_cifar', 'conv2_16filt_ssim_cifar',
'conv2_32filt_mse_cifar', 'conv2_32filt_mae_cifar', 'conv2_32filt_ssim_cifar',
'conv6_16filt_mse_cifar', 'conv6_16filt_mae_cifar', 'conv6_16filt_ssim_cifar'
]
# Loading Test model
model_name = models[9]
model_path = os.path.join(cwd, 'models', model_name + '.h5')
model_data = os.path.join(cwd, 'data', 'hist_' + model_name + '.csv')
# Loading Training data for metrics
data = pd.read_csv(model_data)
# Loading datasets (new images)
orig_path = os.path.join(cwd, 'data', 'cifar10.npy')
blur_path = os.path.join(cwd, 'data', 'cifar10_blur_sigma0-3.npy')
# Load original and blurred datasets, and transform into tf variables
size = 8000
<<<<<<< HEAD
orig = np.load(orig_path)[:size]
blur = np.load(blur_path)[:size]
# # Loading Test model
# model_name = 'unetlike_mse_cifar.h5'
# model_path = os.path.join(cwd, 'models', model_name)
#
# objects = {'PSNR' : PSNR, 'SSIM' : SSIM, 'MIX' : MIX, 'SSIM_loss' : SSIM_loss, 'SSIM_PSNR' : SSIM_PSNR}
# model = tf.keras.models.load_model(model_path, custom_objects=objects)
#
# # Predict the output for CIFAR10
# pred = model.predict(x=blur)
# metrics = model.evaluate(x=blur, y=orig)
#
# psnr_pred_orig = PSNR(tf.Variable(orig), tf.Variable(pred)).numpy()
# psnr_blur_orig = PSNR(tf.Variable(orig), tf.Variable(blur)).numpy()
#
# ssim_pred_orig = SSIM(tf.Variable(orig), tf.Variable(pred)).numpy()
# ssim_blur_orig = SSIM(tf.Variable(orig), tf.Variable(blur)).numpy()
#
# # mask = psnr_blur_orig < 30
#
# psnr_pred_orig.mean()
# psnr_blur_orig.mean()
#
# psnr_diff = (psnr_pred_orig - psnr_blur_orig)
# mask1 = psnr_diff > 0
#
# ssim_diff = (ssim_pred_orig - ssim_blur_orig)
# mask2 = ssim_diff > 0
#%%
pred = np.load( os.path.join(cwd, 'data', 'cifar10_unet_pred.npy'))
# idx = ssim_diff.argmax()
for idx in [6271, 3822, 7014]:
fig, ax = plt.subplots(nrows=1, ncols = 3, figsize=(12,8))
for a, source in zip(ax, [orig, blur, pred]):
a.imshow(source[idx]);
a.set_xticks([])
a.set_yticks([])
plt.tight_layout()
plt.savefig(os.path.join(cwd, 'images', f'CIFAR_{idx}.png'), bbox_inches = 'tight')
#%%
fig, ax = plt.subplots(nrows=1, ncols = 3, figsize=(12,8))
for n, (cm, a) in enumerate(zip(['Reds', 'Greens', 'Blues'], ax)):
a.imshow((pred[idx]-blur[idx])[:,:,n], cmap = cm);
a.set_xticks([])
a.set_yticks([])
plt.tight_layout()
plt.savefig(os.path.join(cwd, 'images', f'channel_var_CIFAR_{idx}.png'), bbox_inches = 'tight')
#%%
# idx = psnr_diff.argmax()
idx = 7014
fig, ax = plt.subplots(nrows=1, ncols = 3, figsize=(12,8))
for a, source in zip(ax, [orig, blur, pred]):
a.imshow(source[idx]);
a.set_xticks([])
a.set_yticks([])
plt.tight_layout()
plt.savefig(os.path.join(cwd, 'images', 'best_psnr_CIFAR.png'), bbox_inches = 'tight')
#%%
fig, ax = plt.subplots(nrows=1, ncols = 3, figsize=(12,8))
for n, (cm, a) in enumerate(zip(['Reds', 'Greens', 'Blues'], ax)):
a.imshow((pred[idx]-blur[idx])[:,:,n], cmap = cm);
a.set_xticks([])
a.set_yticks([])
plt.tight_layout()
plt.savefig(os.path.join(cwd, 'images', 'channel_var_psnr_CIFAR.png'), bbox_inches = 'tight')
# plt.scatter(ssim_diff[mask], psnr_diff[mask], marker='.');
=======
orig = np.load(orig_path)[15000: 15000 + size]
blur = np.load(blur_path)[15000: 15000 + size]
# Loading Model
objects = {'PSNR' : PSNR, 'SSIM' : SSIM, 'MIX' : MIX, 'SSIM_loss' : SSIM_loss, 'SSIM_PSNR' : SSIM_PSNR}
if 'unetlike' in model_name :
loss = model_name.split('_')[1]
if loss == 'mse':
loss = 'mean_squared_error'
elif loss == 'mae':
loss = 'mean_absolute_error'
elif loss == 'ssim':
loss = SSIM_loss
model = change_model(model_path)
model.compile(optimizer='adam', loss=loss, metrics=metrics)
else :
model = tf.keras.models.load_model(model_path, custom_objects=objects)
# Predict the output for new CIFAR-10
pred = model.predict(x=blur)
pred.shape
# Compute commons metrics
mse_pred_orig = tf.keras.losses.MSE(orig.reshape(-1, 32*32*3), pred.reshape(-1, 32*32*3)).numpy()
mse_blur_orig = tf.keras.losses.MSE(orig.reshape(-1, 32*32*3), blur.reshape(-1, 32*32*3)).numpy()
psnr_pred_orig = PSNR(tf.Variable(orig), tf.Variable(pred)).numpy()
psnr_blur_orig = PSNR(tf.Variable(orig), tf.Variable(blur)).numpy()
ssim_pred_orig = SSIM(tf.Variable(orig), tf.Variable(pred)).numpy()
ssim_blur_orig = SSIM(tf.Variable(orig), tf.Variable(blur)).numpy()
mask = psnr_blur_orig < 30
mse_pred_orig[mask].mean()
psnr_pred_orig[mask].mean()
ssim_pred_orig[mask].mean()
(pred[idx]-blur[idx]).min()
plt.imshow(pred[7014]-blur[7014]);
psnr_diff = (psnr_pred_orig - psnr_blur_orig)
mask1 = psnr_diff > 0
plt.imshow(orig[mask][651]);
plt.imshow(blur[mask][651]);
plt.imshow(pred[mask][651]);
ssim_diff = (ssim_pred_orig - ssim_blur_orig)
mask2 = ssim_diff > 0
mask = mask1 & mask2
psnr_pred_orig.mean()
psnr_blur_orig.mean()
plt.scatter(ssim_diff[mask], psnr_diff[mask], marker='.');
>>>>>>> c52befd98a35c38576123956f9c1d0c115cca612