mirror of
https://github.com/iperov/DeepFaceLab.git
synced 2025-07-06 21:12:07 -07:00
fix ModelBase, nnlib
This commit is contained in:
parent
da0fc2d2e1
commit
8da47fec13
3 changed files with 74 additions and 35 deletions
|
@ -90,7 +90,7 @@ class ModelBase(object):
|
||||||
|
|
||||||
if self.iter == 0 or ask_override:
|
if self.iter == 0 or ask_override:
|
||||||
default_batch_size = 0 if self.iter == 0 else self.options.get('batch_size',0)
|
default_batch_size = 0 if self.iter == 0 else self.options.get('batch_size',0)
|
||||||
self.options['batch_size'] = max(0, io.input_int("Batch_size (?:help skip:0/default) : ", default_batch_size, help_message="Larger batch size is always better for NN's generalization, but it can cause Out of Memory error. Tune this value for your videocard manually."))
|
self.options['batch_size'] = max(0, io.input_int("Batch_size (?:help skip:%d) : " % (default_batch_size), default_batch_size, help_message="Larger batch size is always better for NN's generalization, but it can cause Out of Memory error. Tune this value for your videocard manually."))
|
||||||
else:
|
else:
|
||||||
self.options['batch_size'] = self.options.get('batch_size', 0)
|
self.options['batch_size'] = self.options.get('batch_size', 0)
|
||||||
|
|
||||||
|
|
|
@ -271,8 +271,8 @@ class SAEModel(ModelBase):
|
||||||
psd_target_dst_anti_masked_ar = [ pred_src_dst_sigm_ar[i]*target_dstm_anti_sigm_ar[i] for i in range(len(pred_src_dst_sigm_ar))]
|
psd_target_dst_anti_masked_ar = [ pred_src_dst_sigm_ar[i]*target_dstm_anti_sigm_ar[i] for i in range(len(pred_src_dst_sigm_ar))]
|
||||||
|
|
||||||
if self.is_training_mode:
|
if self.is_training_mode:
|
||||||
self.src_dst_opt = AdamCPU(lr=5e-5, beta_1=0.5, beta_2=0.999, tf_cpu_mode=self.options['optimizer_mode']-1)
|
self.src_dst_opt = Adam(lr=5e-5, beta_1=0.5, beta_2=0.999, tf_cpu_mode=self.options['optimizer_mode']-1)
|
||||||
self.src_dst_mask_opt = AdamCPU(lr=5e-5, beta_1=0.5, beta_2=0.999, tf_cpu_mode=self.options['optimizer_mode']-1)
|
self.src_dst_mask_opt = Adam(lr=5e-5, beta_1=0.5, beta_2=0.999, tf_cpu_mode=self.options['optimizer_mode']-1)
|
||||||
|
|
||||||
if self.options['archi'] == 'liae':
|
if self.options['archi'] == 'liae':
|
||||||
src_dst_loss_train_weights = self.encoder.trainable_weights + self.inter_B.trainable_weights + self.inter_AB.trainable_weights + self.decoder.trainable_weights
|
src_dst_loss_train_weights = self.encoder.trainable_weights + self.inter_B.trainable_weights + self.inter_AB.trainable_weights + self.decoder.trainable_weights
|
||||||
|
|
|
@ -71,8 +71,8 @@ ZeroPadding2D = keras.layers.ZeroPadding2D
|
||||||
RandomNormal = keras.initializers.RandomNormal
|
RandomNormal = keras.initializers.RandomNormal
|
||||||
Model = keras.models.Model
|
Model = keras.models.Model
|
||||||
|
|
||||||
Adam = keras.optimizers.Adam
|
#Adam = keras.optimizers.Adam
|
||||||
AdamCPU = nnlib.AdamCPU
|
Adam = nnlib.Adam
|
||||||
|
|
||||||
modelify = nnlib.modelify
|
modelify = nnlib.modelify
|
||||||
gaussian_blur = nnlib.gaussian_blur
|
gaussian_blur = nnlib.gaussian_blur
|
||||||
|
@ -428,49 +428,87 @@ NLayerDiscriminator = nnlib.NLayerDiscriminator
|
||||||
return dict(list(base_config.items()) + list(config.items()))
|
return dict(list(base_config.items()) + list(config.items()))
|
||||||
nnlib.Scale = Scale
|
nnlib.Scale = Scale
|
||||||
|
|
||||||
class AdamCPU(keras.optimizers.Optimizer):
|
class Adam(keras.optimizers.Optimizer):
|
||||||
|
"""Adam optimizer.
|
||||||
|
|
||||||
|
Default parameters follow those provided in the original paper.
|
||||||
|
|
||||||
|
# Arguments
|
||||||
|
lr: float >= 0. Learning rate.
|
||||||
|
beta_1: float, 0 < beta < 1. Generally close to 1.
|
||||||
|
beta_2: float, 0 < beta < 1. Generally close to 1.
|
||||||
|
epsilon: float >= 0. Fuzz factor. If `None`, defaults to `K.epsilon()`.
|
||||||
|
decay: float >= 0. Learning rate decay over each update.
|
||||||
|
amsgrad: boolean. Whether to apply the AMSGrad variant of this
|
||||||
|
algorithm from the paper "On the Convergence of Adam and
|
||||||
|
Beyond".
|
||||||
|
tf_cpu_mode: only for tensorflow backend
|
||||||
|
0 - default, no changes.
|
||||||
|
1 - allows to train x2 bigger network on same VRAM consuming RAM
|
||||||
|
2 - allows to train x3 bigger network on same VRAM consuming RAM*2 and CPU power.
|
||||||
|
|
||||||
|
# References
|
||||||
|
- [Adam - A Method for Stochastic Optimization]
|
||||||
|
(https://arxiv.org/abs/1412.6980v8)
|
||||||
|
- [On the Convergence of Adam and Beyond]
|
||||||
|
(https://openreview.net/forum?id=ryQu7f-RZ)
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
|
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
|
||||||
tf_cpu_mode=0, **kwargs):
|
epsilon=None, decay=0., amsgrad=False, tf_cpu_mode=0, **kwargs):
|
||||||
super(AdamCPU, self).__init__(**kwargs)
|
super(Adam, self).__init__(**kwargs)
|
||||||
with K.name_scope(self.__class__.__name__):
|
with K.name_scope(self.__class__.__name__):
|
||||||
self.iterations = K.variable(0, dtype='int64', name='iterations')
|
self.iterations = K.variable(0, dtype='int64', name='iterations')
|
||||||
self.lr = K.variable(lr, name='lr')
|
self.lr = K.variable(lr, name='lr')
|
||||||
self.beta_1 = K.variable(beta_1, name='beta_1')
|
self.beta_1 = K.variable(beta_1, name='beta_1')
|
||||||
self.beta_2 = K.variable(beta_2, name='beta_2')
|
self.beta_2 = K.variable(beta_2, name='beta_2')
|
||||||
|
self.decay = K.variable(decay, name='decay')
|
||||||
self.epsilon = K.epsilon()
|
if epsilon is None:
|
||||||
|
epsilon = K.epsilon()
|
||||||
|
self.epsilon = epsilon
|
||||||
|
self.initial_decay = decay
|
||||||
|
self.amsgrad = amsgrad
|
||||||
self.tf_cpu_mode = tf_cpu_mode
|
self.tf_cpu_mode = tf_cpu_mode
|
||||||
|
|
||||||
@keras.legacy.interfaces.legacy_get_updates_support
|
|
||||||
def get_updates(self, loss, params):
|
def get_updates(self, loss, params):
|
||||||
grads = self.get_gradients(loss, params)
|
grads = self.get_gradients(loss, params)
|
||||||
self.updates = [K.update_add(self.iterations, 1)]
|
self.updates = [K.update_add(self.iterations, 1)]
|
||||||
|
|
||||||
lr = self.lr
|
lr = self.lr
|
||||||
|
if self.initial_decay > 0:
|
||||||
|
lr = lr * (1. / (1. + self.decay * K.cast(self.iterations,
|
||||||
|
K.dtype(self.decay))))
|
||||||
|
|
||||||
t = K.cast(self.iterations, K.floatx()) + 1
|
t = K.cast(self.iterations, K.floatx()) + 1
|
||||||
lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /
|
lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /
|
||||||
(1. - K.pow(self.beta_1, t)))
|
(1. - K.pow(self.beta_1, t)))
|
||||||
|
|
||||||
if self.tf_cpu_mode > 0:
|
e = K.tf.device("/cpu:0") if self.tf_cpu_mode > 0 else None
|
||||||
with K.tf.device("/cpu:0"):
|
if e: e.__enter__()
|
||||||
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
||||||
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
||||||
|
if self.amsgrad:
|
||||||
|
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
||||||
else:
|
else:
|
||||||
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
vhats = [K.zeros(1) for _ in params]
|
||||||
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
|
if e: e.__exit__(None, None, None)
|
||||||
|
|
||||||
self.weights = [self.iterations] + ms + vs
|
self.weights = [self.iterations] + ms + vs + vhats
|
||||||
|
|
||||||
for p, g, m, v in zip(params, grads, ms, vs):
|
for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
|
||||||
if self.tf_cpu_mode == 2:
|
e = K.tf.device("/cpu:0") if self.tf_cpu_mode == 2 else None
|
||||||
with K.tf.device("/cpu:0"):
|
if e: e.__enter__()
|
||||||
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
|
|
||||||
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
|
|
||||||
else:
|
|
||||||
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
|
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
|
||||||
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
|
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
|
||||||
|
|
||||||
|
if self.amsgrad:
|
||||||
|
vhat_t = K.maximum(vhat, v_t)
|
||||||
|
self.updates.append(K.update(vhat, vhat_t))
|
||||||
|
if e: e.__exit__(None, None, None)
|
||||||
|
|
||||||
|
if self.amsgrad:
|
||||||
|
p_t = p - lr_t * m_t / (K.sqrt(vhat_t) + self.epsilon)
|
||||||
|
else:
|
||||||
p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
|
p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
|
||||||
|
|
||||||
self.updates.append(K.update(m, m_t))
|
self.updates.append(K.update(m, m_t))
|
||||||
|
@ -487,12 +525,13 @@ NLayerDiscriminator = nnlib.NLayerDiscriminator
|
||||||
def get_config(self):
|
def get_config(self):
|
||||||
config = {'lr': float(K.get_value(self.lr)),
|
config = {'lr': float(K.get_value(self.lr)),
|
||||||
'beta_1': float(K.get_value(self.beta_1)),
|
'beta_1': float(K.get_value(self.beta_1)),
|
||||||
'beta_2': float(K.get_value(self.beta_2))
|
'beta_2': float(K.get_value(self.beta_2)),
|
||||||
}
|
'decay': float(K.get_value(self.decay)),
|
||||||
base_config = super(AdamCPU, self).get_config()
|
'epsilon': self.epsilon,
|
||||||
|
'amsgrad': self.amsgrad}
|
||||||
|
base_config = super(Adam, self).get_config()
|
||||||
return dict(list(base_config.items()) + list(config.items()))
|
return dict(list(base_config.items()) + list(config.items()))
|
||||||
|
nnlib.Adam = Adam
|
||||||
nnlib.AdamCPU = AdamCPU
|
|
||||||
'''
|
'''
|
||||||
not implemented in plaidML
|
not implemented in plaidML
|
||||||
class ReflectionPadding2D(keras.layers.Layer):
|
class ReflectionPadding2D(keras.layers.Layer):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue