-
Notifications
You must be signed in to change notification settings - Fork 0
/
pista.py
645 lines (553 loc) · 21.4 KB
/
pista.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import os
import sys
import math
import random
import pickle as p
from collections import defaultdict
# In[2]:
def preprocess_input(input_image, shape=(512, 512), normalize=True, dtype=np.float32):
'''
Reads and preprocesses the input image.
Arguments ::
input_image -- ndarray or str | image to be normalized or path to the image
shape -- tupple | shape to resize the input image in the form (w, h) | default None
| if None, does not resize the input image
normalize -- bool | if set True, normalizes the input image
| default False
dtype -- datatype of the input to the model | default np.float32
Returns ::
input_image -- ndarray | preprocessed input image
'''
if isinstance(input_image, str):
## Read the image
print(input_image)
image = cv2.cvtColor(cv2.imread(input_image), cv2.COLOR_BGR2RGB)
if dtype == np.int8:
return input_image[np.newaxis, :, :, :].transpose(0, 3, 1, 2).astype(dtype)
gr_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)[:, :, np.newaxis]
input_image = np.ones_like(input_image) * gr_image
if np.max(input_image) > 2:
input_image = input_image / 255.
if shape != None:
input_image = cv2.resize(input_image, shape)
input_image = input_image[np.newaxis, :, :, :].astype(np.float32)
return input_image
# create minibatches
def random_mini_batches(X_train, Y_train, minibatch_size):
'''
Divides the entire trainig set into minibatches of size minibatch size randomly.
args ->
X_train : training set features numpy array
Y_train : training set labels numppy arrray
return ->
minibatches : a tupple containing all the randomly choosen minibatches from X_train and Y_train
'''
minibatches = []
m = X_train.shape[0] # number of training examples
indices = random.sample(range(m), m) # indices of the minibatches
# divide the training set into minibatches
while len(indices) > 0:
minibatch_X = X_train[:minibatch_size]
minibatch_Y = Y_train[:minibatch_size]
del indices[:minibatch_size] # remove the used indices/
minibatches.append((minibatch_X, minibatch_Y))
return minibatches
def random_mini_batches_from_SSD(X_train, Y_train, minibatch_size):
'''
Divides the entire trainig set into minibatches of size minibatch size randomly.
args ->
X_train : python list containing all the image dirs
Y_train : python list containing all the image dirs
return ->
minibatches : a tupple containing all the randomly choosen minibatches from X_train and Y_train
'''
minibatches = []
m = len(X_train) # number of training examples
start = 0
end = minibatch_size
minibatch_X_names = []
minibatch_Y_names = []
minibatches = []
indices = list(range(m))
while m > 0:
minibatch_X_names = X_train[start : end]
minibatch_Y_names = Y_train[start : end]
if m < minibatch_size:
minibatch_size = m
m -= minibatch_size
start = end
end += minibatch_size
minibatches.append((minibatch_X_names, minibatch_Y_names))
return minibatches
def random_mini_batches_from_SSD_no_gt(X_train, minibatch_size):
'''
Divides the entire trainig set into minibatches of size minibatch size randomly.
args ->
X_train : python list containing all the image dirs
Y_train : python list containing all the image dirs
return ->
minibatches : a tupple containing all the randomly choosen minibatches from X_train and Y_train
'''
minibatches = []
m = len(X_train) # number of training examples
start = 0
end = minibatch_size
minibatch_X_names = []
minibatches = []
indices = list(range(m))
while m > 0:
minibatch_X_names = X_train[start : end]
if m < minibatch_size:
minibatch_size = m
m -= minibatch_size
start = end
end += minibatch_size
minibatches.append(minibatch_X_names)
return minibatches
def random_mini_batches_from_SSD_tripple(anchor_train, possitive_train, negetive_train, labels, minibatch_size):
'''
Divides the entire trainig set into minibatches of size minibatch size randomly.
This function should be used for training siamese networks, where you need "anchor",
"possitive" and "negetive" samples to complete 1 record.
args ->
anchor_train : python list containing all the image dirs
possitive_train : python list containing all the image dirs
negetive_train : python list containing all the image dirs
labels : labels
return ->
minibatches : a tupple containing all the randomly choosen minibatches from X_train(tripplet tupple)
and Y_train
'''
minibatches = []
m = len(anchor_train) # number of training examples
start = 0
end = minibatch_size
minibatches = []
ziped = list(zip(anchor_train, possitive_train, negetive_train, labels))
random.shuffle(ziped)
while m > 0:
zipped_batch = ziped[start : end]
unzipped_batch = [
[i for i, j, k, l in zipped_batch],
[j for i, j, k, l in zipped_batch],
[k for i, j, k, l in zipped_batch],
[l for i, j, k, l in zipped_batch]
]
minibatch_anchor_names = unzipped_batch[0]
minibatch_pos_names = unzipped_batch[1]
minibatch_neg_names = unzipped_batch[2]
labels_batch = unzipped_batch[3]
if m < minibatch_size:
minibatch_size = m
m -= minibatch_size
start = end
end += minibatch_size
minibatches.append(((minibatch_anchor_names, minibatch_pos_names, minibatch_neg_names), labels_batch))
return minibatches
def load_images_into_array_from_list_of_dirs(image_names):
'''
loads the image set in memory
Args ->
image_names -- python list containing names of the images with
Return ->
data -- numpy array of shape (number of examples, height, width, channels)
'''
data = []
# loop througn all the images, store them in memory
for image_name in image_names:
image = plt.imread(image_name)
if image.shape[2] != 3:
image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
data.append(image)
# convert data to np arraay
data = np.stack(data, axis=3)
data = np.rollaxis(data, 3)
return data
# unpack csifr file
def unpickle(file):
'''
Loads a CSIFR data file into memory
Args ->
file -- string, file name
Return ->
dict -- dictonary having key as class and value as images
'''
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
def check_image_dims(data_dir, f=False):
'''
counts number of images in the dataset for a perticular shape
Args ->
data_dir -- directory where the image set is present
f - bool if true reads the images in the sub dirs also
Return ->
count -- dictonary having key as image shape and value as count
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
count = defaultdict(lambda: 0)
i = 1
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
s = str(i) + 'images scanned'
print(s, end='\r')
image = plt.imread(data_dir +subdir + image_name)
count[image.shape] += 1
i += 1
print()
print('Completed!!!')
return count
def rotate_all_images_to_make_the_last_dim_same(data_dir, value, f=False):
'''
rotares all the images to make their width same so that we can feed them to the network
Args ->
data_dir -- directory where the image set is present
value -- width of the image
f - bool if true reads the images in the sub dirs also
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
# print(i, data_dir + image_name)
image = plt.imread(data_dir + subdir + image_name)
if image.shape[0] == value:
op = str(i) + ': converting '+ str(image.shape) + ' to ' + str(tuple([image.shape[1], image.shape[0], image.shape[2]]))
print(op, end="\r")
image = np.swapaxes(image, 0, 1)
plt.imsave(data_dir + subdir + image_name, image)
i += 1
print()
print('Completed!!!')
def load_images_into_dict(data_dir):
'''
loads the image set in memory
Args ->
data_dir -- directory where the image set is present
Return ->
data -- dictonary having key as image shape and value as numpy array of shape (number of examples, height, width, channels)
'''
i = 0
for image_name in os.listdir(data_dir):
if os.path.isfile(data_dir + image_name):
i += 1
print('Total number of images =', i)
data = defaultdict(lambda: [])
count = defaultdict(lambda: 0)
i = 1
# loop througn all the images, store them in memory
for image_name in os.listdir(data_dir):
image = plt.imread(data_dir + image_name)
if len(image.shape) == 2:
image = np.expand_dims(image, axis=-1)
data[image.shape].append(image)
count[image.shape] += 1
print(str(i) + ' images loaded successfully.', end='\r')
i += 1
# convert data to np arraay
for key in count.keys():
print('There are', count[key], 'images of shape', key, end='\r')
data[key] = np.stack(data[key], axis=3)
data[key] = np.rollaxis(data[key], 3)
# print('Shape =', data.shape)
return data
def load_images_into_array(data_dir, f=False):
'''
loads the image set in memory
Args ->
data_dir -- directory where the image set is present
f - bool if true reads the images in the sub dirs also
Return ->
data -- numpy array of shape (number of examples, height, width, channels)
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images = ', i)
data = []
i = 1
# loop througn all the images, store them in memory
for subdir in tree:
for image_name in sorted(os.listdir(data_dir+subdir)):
if os.path.isfile(data_dir + subdir + image_name):
image = plt.imread(data_dir + subdir + image_name)
if len(image.shape) == 2:
image = np.expand_dims(image, axis=-1)
if image.shape[2] == 4:
image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
data.append(image.astype('float32'))
print(str(i) + ' images loaded successfully.', end='\r')
i += 1
# convert data to np arraay
data = np.stack(data, axis=3)
data = np.rollaxis(data, 3)
print()
print('Shape =', data.shape)
return data
def convert_to_RGB(data_dir, f=False):
'''
converts all the images present the data_dir to RGB
Args ->
data_dir -- directory where the image set is present
f - bool if true reads the images in the sub dirs also
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
image = plt.imread(data_dir + subdir + image_name)
if image.shape[2] != 3:
s = str(i) + ': converting ' + str(image.shape) + ' to ' + str((image.shape[0], image.shape[1], 3))
# print(s, end="\r")
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
print(rgb_image.shape)
plt.imsave(data_dir + subdir + image_name, rgb_image)
i += 1
print()
print('Completed!!!')
def resize_all(data_dir, dim, f=False, grey=False):
'''
resizes all the images present the data_dir to the dim specifed
Args ->
data_dir -- directory where the image set is present
dim -- tupple, desired dim of the image
f - bool if true reads the images in the sub dirs also
grey - bool, true if the image is a greyscale image
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
if grey:
image = cv2.imread(data_dir + subdir + image_name, 0)
else:
image = cv2.imread(data_dir + subdir + image_name)
if (image.shape[0], image.shape[1]) != dim:
print(str(i) + ': resizing ' + str(image.shape) + ' to ' + str((dim[0], dim[1])), end="\r")
image = cv2.resize(image, (dim[1], dim[0]), interpolation = cv2.INTER_NEAREST)
# print(image.shape)
cv2.imwrite(data_dir + subdir + image_name, image)
i += 1
print()
print('Completed!!!')
def rotate_and_save(data_dir, angle, zoom=1, f=False):
'''
resizes all the images present the data_dir to the angle specifed
Args ->
data_dir -- directory where the image set is present
angle -- angle by which the image is to be rotates
'''
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
print()
if not os.path.exists(data_dir+ 'rotated/'):
os.mkdir(data_dir+ 'rotated/')
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
image = plt.imread(data_dir + subdir + image_name)
rows, cols, channels = image.shape
center = (cols/2, rows/2)
M = cv2.getRotationMatrix2D(center, angle, zoom)
rotated_image = cv2.warpAffine(image, M, (cols,rows))
plt.imsave(data_dir + 'rotated/' + str(angle) + '_' + image_name, rotated_image)
print(str(i) + ' images rotated successfully.', end="\r")
i += 1
print()
def flip_and_save(data_dir, how, f=False):
'''
filps all the images present the data_dir vitically
Args ->
data_dir -- directory where the image set is present
how -- string, one of 'horizontal', 'virtical' and 'both'
f - bool if true reads the images in the sub dirs also
'''
patern = {'horizontal' : 0, 'virtical' : 1, 'both' : -1}
i = 0
tree = ['']
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
print()
if not os.path.exists(data_dir+ 'flip/'):
os.mkdir(data_dir+ 'flip/')
for subdir in tree:
for image_name in os.listdir(data_dir+subdir):
if os.path.isfile(data_dir + subdir + image_name):
image = plt.imread(data_dir + subdir + image_name)
flipped_image = cv2.flip( image, patern[how])
plt.imsave(data_dir + 'flip/' + str(how) + '_' + image_name, rgb_image)
print(str(i) + ' images flipped successfully.', end="\r")
i += 1
print()
def load_image_names_into_list(data_dir, f=False):
'''
loads the image names into a list
Args ->
data_dir -- directory where the image set is present
Return ->
data -- python list of length (total number of images)
'''
i = 0
tree = ['']
data = []
for file_name in os.listdir(data_dir):
if os.path.isfile(data_dir + file_name):
i += 1
elif f:
tree.append(file_name+'/')
for image_name in os.listdir(data_dir+file_name+'/'):
i += 1
print('Total number of images =', i)
i = 1
print()
# loop througn all the images, store them in memory
for subdir in tree:
for image_name in sorted(os.listdir(data_dir+subdir)):
if os.path.isfile(data_dir + subdir + image_name):
data.append(data_dir + subdir + image_name)
print(str(i) + ' images loaded successfully.', end='\r')
i += 1
return data
def downsample(r, hr_dir, lr_dir):
'''
'''
# check if lr images are present already
if not os.path.isdir(lr_dir):
os.mkdir(lr_dir)
# loop througn all the hr images, convert hr to lr by a factor r and save the lr image
for image_name in os.listdir(hr_dir):
hr_image = plt.imread(hr_dir + image_name)
# use bicubic interpolation to reside the images
lr_image = cv2.resize(hr_image, (int(hr_image.shape[1]/r), int(hr_image.shape[0]/r)),
interpolation=cv2.INTER_CUBIC)
# save the image
plt.imsave(lr_dir + image_name, lr_image)
def W_relu_init(parameter_dim):
'''
Weights initialization if you are using relu
Arguments :
parameter_dim -- sape of the weight
Return :
W - generated weight matrix
'''
stddev = 2 / np.sqrt(parameter_dim[-2])
W = tf.random.normal(shape=parameter_dim, mean= 1.0, stddev=stddev, dtype=tf.dtypes.float32)
return W
def compute_PSNR(Y_pred, Y, max_value=1.0):
'''
Computes PSNR between a set of predicted SR images and original HR images
Arguments:
Y_pred -- numpy array, predicted SR images
Y -- numpy array, original HR images
Returns:
PSNR - Signal to noise ratio value
'''
mse = np.mean((Y_pred-Y)**2)
if mse == 0:
PSNR = 100
return PSNR
else:
PSNR = 20*math.log10(max_value/math.sqrt(mse))
return PSNR
def resblock(X, parameters, bn=True):
'''
Implemants a single resnet block with skip connection between input and 2
CONV2D layers along with batch normalization.
Arguments :
X -- input tensor to the resblock, must contain same number of filters as the parameters
parameters -- set of parameters to perform CONV2D operation
bn -- boolean if True, then perform batch normalozation
Return :
S_bn -- normalized output tensor of the resblock
'''
W = []
for weight, parameter in parameters.items():
W.append(weight)
# CONV2D: stride of 1, padding 'SAME'
Z = tf.nn.conv2d(X, parameters[W[0]], strides=[1, 1, 1, 1], padding='SAME', name='CONV2D_res'+weight)
# RELU
A = tf.nn.relu(Z, name='relu_res'+weight)
# CONV2D: stride of 1, padding 'SAME'
Z = tf.nn.conv2d(X, parameters[W[1]], strides=[1, 1, 1, 1], padding='SAME', name='CONV2D_res'+weight)
if bn:
# batch normalization
X_mean, X_var = tf.nn.moments(X, [0])
A_mean, A_var = tf.nn.moments(A, [0])
X_bn = tf.nn.batch_normalization(X, X_mean, X_var, offset=0, scale=1, variance_epsilon=0.001)
A_bn = tf.nn.batch_normalization(A, A_mean, A_var, offset=0, scale=1, variance_epsilon=0.001)
# feed forward
S = tf.add(X, A)
# batch normalization
S_mean, S_var = tf.nn.moments(S, [0])
S_bn = tf.nn.batch_normalization(S, S_mean,S_var, offset=0, scale=1, variance_epsilon=0.001)
return S_bn
else:
S = tf.add(X, A)
return S