mirror of
https://github.com/iperov/DeepFaceLab.git
synced 2025-08-14 02:37:00 -07:00
global refactoring and fixes,
removed support of extracted(aligned) PNG faces. Use old builds to convert from PNG to JPG. fanseg model file in facelib/ is renamed
This commit is contained in:
parent
921b464d5b
commit
61472cdaf7
82 changed files with 3838 additions and 3812 deletions
42
core/leras/optimizers/OptimizerBase.py
Normal file
42
core/leras/optimizers/OptimizerBase.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
import copy
|
||||
from core.leras import nn
|
||||
tf = nn.tf
|
||||
|
||||
class OptimizerBase(nn.Saveable):
|
||||
def __init__(self, name=None):
|
||||
super().__init__(name=name)
|
||||
|
||||
def tf_clip_norm(self, g, c, n):
|
||||
"""Clip the gradient `g` if the L2 norm `n` exceeds `c`.
|
||||
# Arguments
|
||||
g: Tensor, the gradient tensor
|
||||
c: float >= 0. Gradients will be clipped
|
||||
when their L2 norm exceeds this value.
|
||||
n: Tensor, actual norm of `g`.
|
||||
# Returns
|
||||
Tensor, the gradient clipped if required.
|
||||
"""
|
||||
if c <= 0: # if clipnorm == 0 no need to add ops to the graph
|
||||
return g
|
||||
|
||||
condition = n >= c
|
||||
then_expression = tf.scalar_mul(c / n, g)
|
||||
else_expression = g
|
||||
|
||||
# saving the shape to avoid converting sparse tensor to dense
|
||||
if isinstance(then_expression, tf.Tensor):
|
||||
g_shape = copy.copy(then_expression.get_shape())
|
||||
elif isinstance(then_expression, tf.IndexedSlices):
|
||||
g_shape = copy.copy(then_expression.dense_shape)
|
||||
if condition.dtype != tf.bool:
|
||||
condition = tf.cast(condition, 'bool')
|
||||
g = tf.cond(condition,
|
||||
lambda: then_expression,
|
||||
lambda: else_expression)
|
||||
if isinstance(then_expression, tf.Tensor):
|
||||
g.set_shape(g_shape)
|
||||
elif isinstance(then_expression, tf.IndexedSlices):
|
||||
g._dense_shape = g_shape
|
||||
|
||||
return g
|
||||
nn.OptimizerBase = OptimizerBase
|
Loading…
Add table
Add a link
Reference in a new issue