-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
143 lines (130 loc) · 4.29 KB
/
helper.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
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os
import scipy.io
import h5py
import cv2
import sys
if (sys.version_info[0] == 3):
from functools import reduce
def mean_normalise(X):
meann = np.mean(X, axis=0)
X = X - meann
return X
def variance_normalise(X, newvar=1.0):
varr = np.var(X, axis=0)
X /= varr
X[X==np.inf] = 0
X[X == -np.inf] = 0
res = np.nan_to_num(X)
return res
def mv_normalise(X, newvar=1.0):
return variance_normalise(mean_normalise(X), newvar=newvar)
def make_fig(nparr, cmap=None):
if (cmap is None):
plt.imshow(nparr)
else:
plt.imshow(nparr, cmap=cmap)
# plt.colorbar()
def rescale_mat(npa):
minn, maxx = np.amin(npa), np.amax(npa)
if (minn != maxx):
npa = (npa - minn) / (maxx - minn)
return npa
elif (minn >= 0 and maxx <= 1):
return npa
elif (minn >= 0 and maxx <= 255):
return (npa / 255.0)
else:
print("unable_to_rescale")
return np.zeros(npa.shape)
def load_image(infilename, normalise=True):
img = Image.open(infilename)
img.load()
data = np.asarray(img, dtype="float32")
# print("max = ", np.amax(data))
if normalise:
data = mv_normalise(data)
return data
def load_dataset(src_dir='./pngyalefaces', num_classes=15, shape=None, skip_every=11):
# returns X, classes and poses
filenames=sorted(os.listdir(src_dir))
filenames = [os.path.join(src_dir, filename) for filename in filenames]
if shape is None:
shape = load_image(filenames[0], normalise=False).shape
size = reduce(lambda x, y: x*y, shape)
res = np.zeros((len(filenames), size))
resy = []
cnt = 0
ctrl = 0
for file in filenames:
if (skip_every != 0 and ctrl % skip_every == 0):
ctrl += 1
continue
rect_img = load_image(file, normalise=False)
res[cnt, :] = rect_img.flatten()
resy.append(cnt)
cnt += 1
ctrl += 1
# print("res.shape = ", res.shape)
res = res[:cnt, :]
res = mv_normalise(res)
resy = np.array(resy)
row_per_class = cnt / num_classes
# if (skip_every != 0):
# row_per_class -= 1
classes = np.floor(resy / row_per_class)
poses = resy % row_per_class
# print("load_dataset: shapes: res, classes, poses::", res.shape, classes.shape, poses.shape)
return res, classes, poses
def read_mat(fname, as_np_array=True, rescale01 = True, get_keys=False):
# function to read a .mat file and return the list of objects in that file
res = []
key_names = []
try:
f = h5py.File(fname, 'r')
for key in list(f.keys()):
if (as_np_array):
if not rescale01:
res.append(np.array(f.get(key), dtype='float32'))
else:
res.append(rescale_mat(np.array(f.get(key), dtype='float32')))
else:
res.append(f.get(key))
key_names.append(key)
except (OSError):
f = scipy.io.loadmat(fname) # f will be a dictionary
for key in f:
if (key == '__header__' or key == '__version__' or key == '__globals__'):
continue
# print("key################## = ", key)
if (as_np_array):
if not rescale01:
res.append(np.array(f[key], dtype='float32'))
else:
res.append(rescale_mat(np.array(f[key], dtype='float32')))
else:
res.append(f[key])
key_names.append(key)
if (get_keys):
return [res, key_names]
return res
# sample usage:
# data = read_mat('../data/grassNoisy.mat')[0]
# print(data.shape)
def noisify(inp, noise_type, val):
imshape = inp.shape
if (noise_type == 'gauss'):
# val is sigma
print('enter gauss')
noise = np.random.normal(0, val, imshape)
return np.maximum(np.minimum(inp + noise, 1.0), 0.0)
if (noise_type == 'salt_and_pepper' or noise_type == 'sap' or noise_type == 's&p'):
# val is fraction of corrupted pixels
p_by_2 = val / 2.0
randmat = np.random.random(imshape)
zero_mask_inv = np.logical_not(randmat < p_by_2)
one_mask = randmat > (1 - p_by_2)
out_img = np.multiply(inp, zero_mask_inv)
return np.maximum(out_img, one_mask)