-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
98 lines (81 loc) · 2.85 KB
/
__init__.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 .encoders import encoder_from_config
from .Renderer import renderer_from_config
from .Nerf2D import CNerf2D
from .utils import setMLPDefaultDropout
import tensorflow as tf
def _optimizer_from_config(config):
if isinstance(config, str): return config # return optimizer name
if isinstance(config, dict):
if 'adam' == config['name']:
return tf.keras.optimizers.Adam(
learning_rate=config['learning_rate'],
)
if 'AdamW' == config['name']:
optimizer = tf.keras.optimizers.AdamW(
learning_rate=config['learning_rate'],
weight_decay=config['weight_decay'],
)
optimizer.exclude_from_weight_decay(
var_names=config.get('exclude_from_weight_decay', [])
)
return optimizer
pass
raise ValueError(f"Unknown optimizer config: {config}")
def _makeTrainingLoss(config):
if config is None: return None
if isinstance(config, str): # name of the loss
return tf.keras.losses.get(config)
if isinstance(config, dict):
name = config['name'].lower()
if 'huber' == name:
return tf.keras.losses.Huber(delta=float(config['delta']))
if 'pseudo huber' == name:
delta = float(config['delta'])
def _pseudo_huber_loss(y_true, y_pred):
err = tf.reduce_mean(tf.square(y_true - y_pred), axis=-1)
return tf.sqrt(err + delta**2) - delta
return _pseudo_huber_loss
raise ValueError(f"Unknown training loss config: {config}")
def _nerf_from_config(config):
if 'basic' == config['name']:
nerfParams = dict(
trainingLoss=_makeTrainingLoss(config.get('training loss', None)),
residual=config.get('residual', False),
extraLatents=config.get('extra latents', None),
format=config['format']
)
return lambda encoder, renderer: CNerf2D(
encoder=encoder,
renderer=renderer,
**nerfParams
)
raise ValueError(f"Unknown nerf name: {config['name']}")
def model_from_config(config, compile=True):
# set global parameters
globalz = config.get('global', {})
setMLPDefaultDropout(globalz.get('mlp dropout', 0.05)) # reset if not specified
encoder = encoder_from_config(config['encoder'])
renderer = renderer_from_config(config['renderer'])
nerf = _nerf_from_config(config['nerf'])( encoder, renderer )
nerf.build(nerf.get_input_shape())
if compile:
nerf.compile(optimizer=_optimizer_from_config(config['optimizer']))
return nerf
def model_to_architecture(model):
def traverse(model, data):
if isinstance(model, tf.keras.Model):
data[model.name] = dataCur = {}
try:
params = model.count_params()
except:
params = 'unknown'
pass
dataCur['params'] = params
dataCur['class'] = model.__class__.__name__
for layer in model.layers:
traverse(layer, dataCur)
continue
return
res = {}
traverse(model, res)
return res