-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
98 lines (79 loc) · 2.45 KB
/
inference.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
from models import *
import tensorflow as tf
from model_config import BATCH_SIZE
def infer(model, data, args, arch):
"""
Performs inference in batches for given model on supplied data
Parameters
----------
model (tf.keras.Model or tf.keras.layers.Layer)
data (np.array) or [x,nln] in the case of NNAE
args (Namespace)
arch (str)
Returns
-------
np.array
"""
data_tensor = tf.data.Dataset.from_tensor_slices(data).batch(BATCH_SIZE)
if arch =='AE' or arch == 'encoder':
if arch=='encoder':
output = np.empty([len(data), args.latent_dim])
else:
output = np.empty(data.shape)
strt, fnnsh = 0, BATCH_SIZE
for batch in data_tensor:
output[strt:fnnsh,...] = model(batch).numpy()
strt = fnnsh
fnnsh +=BATCH_SIZE
else:
output = np.empty([len(data), args.latent_dim])
strt, fnnsh = 0, BATCH_SIZE
for batch in data_tensor:
output[strt:fnnsh,...] = model(batch)[0].numpy() # for disc
strt = fnnsh
fnnsh +=BATCH_SIZE
return output
def get_error(model_type,
x,
x_hat,
z=None,
z_hat=None,
d_x=None,
d_x_hat=None,
ab=True,
mean=True):
"""
Gets the reconstruction error of a given model
Parameters
----------
model_type (str)
x (np.array)
x_hat (np.array)
z (optional np.array)
z_hat (optional np.array)
d_x (optional np.array)
d_x_hat (optional np.array)
ab (bool) default True
mean (bool) default True
Returns
-------
np.array
"""
if ((model_type == 'AE') or
(model_type == 'AAE') or
(model_type == 'AE_SSIM') or
(model_type == 'DAE') or
(model_type == 'VAE') or
(model_type == 'VQVAE')):
error = x - x_hat
elif model_type == 'DAE_disc':
discriminator_error = d_x - d_x_hat
if abs: discriminator_error = abs(discriminator_error)
error = discriminator_error
elif model_type == 'GANomaly':
error = z- z_hat
if ab:
error = np.abs(error,dtype=np.float32)
if mean:
error = error.mean(axis=tuple(range(1,error.ndim)))
return error