This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets_utils.py
280 lines (230 loc) · 9.28 KB
/
datasets_utils.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
# Generic imports
import os
import sys
import time
import math
import numpy as np
import matplotlib
if (sys.platform == 'darwin'):
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Imports with probable installation required
try:
import skimage
except ImportError:
print('*** Missing required packages, I will install them for you ***')
os.system('pip3 install scikit-image')
import skimage
try:
import keras
except ImportError:
print('*** Missing required packages, I will install them for you ***')
os.system('pip3 install keras')
import keras
try:
import progress.bar
except ImportError:
print('*** Missing required packages, I will install them for you ***')
os.system('pip3 install progress')
import progress.bar
from keras.utils import plot_model
from keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
### ************************************************
### Split dataset in training, validation and tests
def split_dataset(dataset, train_size, valid_size, tests_size):
# Check sizes
if ((train_size + valid_size + tests_size) != 1.0):
print('Error in split_dataset')
print('The sum of the three provided sizes must be 1.0')
exit()
# Compute sizes
n_data = dataset.shape[0]
train_size = math.floor(n_data*train_size)
valid_size = math.floor(n_data*valid_size) + train_size
tests_size = math.floor(n_data*tests_size) + valid_size
# Split
if (dataset.ndim == 1):
(dataset_train,
dataset_valid,
dataset_tests) = (dataset[0:train_size],
dataset[train_size:valid_size],
dataset[valid_size:])
if (dataset.ndim == 2):
(dataset_train,
dataset_valid,
dataset_tests) = (dataset[0:train_size, :],
dataset[train_size:valid_size,:],
dataset[valid_size:, :])
if (dataset.ndim == 3):
(dataset_train,
dataset_valid,
dataset_tests) = (dataset[0:train_size, :,:],
dataset[train_size:valid_size,:,:],
dataset[valid_size:, :,:])
if (dataset.ndim == 4):
(dataset_train,
dataset_valid,
dataset_tests) = (dataset[0:train_size, :,:,:],
dataset[train_size:valid_size,:,:,:],
dataset[valid_size:, :,:,:])
return dataset_train, dataset_valid, dataset_tests
### ************************************************
### Load image
def get_img(img_name):
x = img_to_array(load_img(img_name))
return x
### ************************************************
### Load and reshape image
def load_and_reshape_img(img_name, height, width, color):
# Load and reshape
x = img_to_array(load_img(img_name))
x = skimage.transform.resize(x,(height,width),
anti_aliasing=True,
#preserve_range=True,
#order=0,
mode='constant')
# Handle color
if (color == 'bw'):
x = (x[:,:,0] + x[:,:,1] + x[:,:,2])/3.0
x = x[:,:,np.newaxis]
# Rescale
x = x.astype('float16')/255
return x
### ************************************************
### Load full image dataset
def load_img_dataset(my_dir, downscaling, color):
# Start counting time
start = time.time()
# Count files in directory
data_files = [f for f in os.listdir(my_dir) if (f[0:5] == 'shape')]
data_files = sorted(data_files)
n_imgs = math.floor(len(data_files))
print('I found {} images'.format(n_imgs))
# Check size of first image
img = get_img(my_dir+'/'+data_files[0])
height = img.shape[0]
width = img.shape[1]
# Declare n_channels
if (color == 'bw'): n_channels = 1
if (color == 'rgb'): n_channels = 3
# Compute downscaling and allocate array
height = math.floor(height/downscaling)
width = math.floor(width /downscaling)
imgs = np.zeros([n_imgs,height,width,n_channels])
# Load all images
bar = progress.bar.Bar('Loading images from '+my_dir, max=n_imgs)
for i in range(0, n_imgs):
imgs[i,:,:,:] = load_and_reshape_img(my_dir+'/'+data_files[i],
height, width, color)
bar.next()
bar.finish()
# Stop counting time
end = time.time()
print('Loaded ',n_imgs,' imgs in ',end-start,' seconds')
return imgs, n_imgs, height, width, n_channels
### ************************************************
### Plot accuracy and loss as a function of epochs
def plot_accuracy_and_loss(train_model, direction):
hist = train_model.history
train_loss = hist['loss']
valid_loss = hist['val_loss']
epochs = range(len(train_loss))
np.savetxt(direction + '/loss',np.transpose([epochs,train_loss,valid_loss]))
plt.semilogy(epochs, train_loss, 'g', label='Training loss')
plt.semilogy(epochs, valid_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.savefig(direction + '/loss.png')
plt.show()
plt.close()
return np.min(valid_loss)
### ************************************************
### Predict images from model and compute errors
def predict_error(model, imgs, sols):
# Get img shape
h = sols.shape[1]
w = sols.shape[2]
c = sols.shape[3]
# Various stuff
n_imgs = len(imgs)
predict = np.zeros([n_imgs,h,w,c],dtype=np.float16)
rel_err = np.zeros((n_imgs,4),dtype=np.float16)
mse = np.zeros((n_imgs,2),dtype=np.float16)
# Predict
for i in range(0, n_imgs):
img = imgs[i,:,:,:]
img = img.reshape(1,h,w,1)
predict[i,:,:,:] = model.predict(img)
error = np.abs(predict[i,:,:,:]-sols[i,:,:,:])
rel_err[i, :] = np.mean(error[35:65,35:80,:] / (sols[i,35:65,35:80,:] + 1e-3), axis=(0,1))
mse[i, :] = [np.mean(np.square(error[i,:,:,0])), np.mean(np.square(error[i,:,:,1:]))]
return rel_err , mse
def predict_images(model, imgs, sols):
# Get img shape
h = sols.shape[1]
w = sols.shape[2]
c = sols.shape[3]
# Various stuff
n_imgs = len(imgs)
predict = np.zeros([n_imgs,h,w,c],dtype=np.float32)
error = np.zeros([n_imgs,h,w,c],dtype=np.float32)
# Predict
for i in range(0, n_imgs):
img = imgs[i,:,:,:]
img = img.reshape(1,h,w,1)
predict[i,:,:,:] = model.predict(img)
error[i,:,:,:] = np.abs(predict[i,:,:,:]-sols[i,:,:,:])
return predict, error
### ************************************************
### Show an image prediction along with exact image and error
def show_image_prediction(shape, ref_img, predicted_img, error_img, i, channel):
channels = ['shape', 'u', 'v', 'p']
if channel == 0:
filename = 'predictions/predicted_shape_{}'.format(i)
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
plt.imshow(shape[:,:], interpolation='spline16', cmap='gray', vmin=0, vmax=1)
plt.axis('off')
plt.savefig(filename+'_ref.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
plt.imshow(predicted_img[:,:,channel], interpolation='spline16', cmap='gray',vmin=0, vmax=1)
plt.axis('off')
plt.savefig(filename+'_pred.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
plt.imshow(error_img[:,:,channel], interpolation='spline16', cmap='gray')
print(np.max(error_img[:,:,channel]))
plt.axis('off')
plt.savefig(filename+'_error.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()
else:
filename = 'predictions/predicted_'+channels[channel]+'{}'.format(i)
#filename = 'predictions/' +sorted(os.listdir('outliers/Data/shapes'))[i][:-4]+'_'+channels[channel] + '_'
cmap = plt.cm.RdBu_r
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
plt.imshow(ref_img[:,:,channel], interpolation='spline16', cmap=cmap, vmin=0, vmax=1)
plt.axis('off')
plt.savefig(filename+'_ref.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
plt.imshow(predicted_img[:,:,channel], interpolation='spline16', cmap=cmap,vmin=0, vmax=1)
plt.axis('off')
plt.savefig(filename+'_pred.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()
fig, ax = plt.subplots()
ax = plt.axes([0,0,1,1])
#plt.imshow(error_img[:,:,channel]/np.amax(error_img[:,:,channel]), interpolation='spline16', cmap='gray')
plt.imshow(error_img[:,:,channel], interpolation='spline16', cmap='RdBu_r')
print(np.max(error_img[:,:,channel]))
plt.axis('off')
plt.savefig(filename+'_error.png', dpi=300, bbox_inches='tight',pad_inches = 0)
plt.close()