-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
125 lines (96 loc) · 4.59 KB
/
model.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
import numpy as np
import tensorflow as tf
initializer = tf.keras.initializers.he_normal()
class Sal360Model():
def __init__(self):
pass
def regression_block(self, features, out_dim):
x = tf.keras.layers.Dense(
1024, kernel_initializer=initializer)(features)
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(512, kernel_initializer=initializer)(x)
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
x = tf.keras.layers.Dropout(0.25)(x)
x = tf.keras.layers.Dense(out_dim, kernel_initializer=initializer)(x)
x = tf.keras.layers.Activation(tf.keras.activations.linear)(x)
return x
def build_model(self, input_size, out_dim, filters=(64, 128, 256, 512)):
inputs = tf.keras.layers.Input(shape=input_size)
for (i, f) in enumerate(filters):
if i == 0:
x = inputs
x = tf.keras.layers.Conv2D(filters=f, kernel_size=(
3, 3), padding="same", kernel_initializer=initializer)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
x = tf.keras.layers.Conv2D(filters=f, kernel_size=(
3, 3), padding="same", kernel_initializer=initializer)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
max_p = tf.keras.layers.MaxPooling2D(
pool_size=(2, 2), strides=(2, 2))(x)
avrg_p = tf.keras.layers.AveragePooling2D(
pool_size=(2, 2), strides=(2, 2))(x)
min_p = -tf.keras.layers.MaxPooling2D(
pool_size=(2, 2), strides=(2, 2))(-x)
x = tf.keras.layers.concatenate([avrg_p, max_p, min_p])
x = tf.keras.layers.GlobalAveragePooling2D()(x)
output = self.regression_block(x, out_dim)
iqa_model = tf.keras.models.Model(inputs=inputs, outputs=output)
return iqa_model
class EarlyStoppingAtMinLoss(tf.keras.callbacks.Callback):
"""Stop training when the loss is at its min, i.e. the loss stops decreasing.
Arguments:
patience: Number of epochs to wait after min has been hit. After this
number of no improvement, training stops.
"""
def __init__(self, patience=15):
super(EarlyStoppingAtMinLoss, self).__init__()
self.patience = patience
# best_weights to store the weights at which the minimum loss occurs.
self.best_weights = None
def on_train_begin(self, logs=None):
# The number of epoch it has waited when loss is no longer minimum.
self.wait = 0
# The epoch the training stops at.
self.stopped_epoch = 0
# Initialize the best as infinity.
self.best = np.Inf
def on_epoch_end(self, epoch, logs=None):
current = logs.get("loss")
if np.less(current, self.best):
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print("Restoring model weights from the end of the best epoch.")
self.model.set_weights(self.best_weights)
def on_train_end(self, logs=None):
if self.stopped_epoch > 0:
print("Epoch %05d: early stopping" % (self.stopped_epoch + 1))
def create_callbacks_fun(fold_no, path, bs, version):
log_file = path + '/Losses_fold_' + str(fold_no) + \
'_BS_' + str(bs) + '_version_' + version + '.log'
csv_loger = tf.keras.callbacks.CSVLogger(
log_file, separator=",", append=False)
filepath = path + '/Weights_fold_' + str(fold_no) + \
'_BS_' + str(bs) + '_version_' + version + '.hdf5'
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath,
monitor="val_loss",
mode="min",
save_best_only=True,
save_weights_only=True,
save_freq='epoch',
verbose=1)
callbacks = [
EarlyStoppingAtMinLoss(),
csv_loger,
checkpoint,
]
return callbacks