Utils
Helper functions
freeze_model_by_layer_name(network, freeze_layer_name=None, freeze_bn=True, unfreeze_all=False)
Unfreeze the last layers of given network starting from the layer_name. All other layers will be frozen regardless of their initial state.
Freezing the layers means their weights become non-trainable and are not updated during training. It has purpose only when the weights are pre-trained and are not random. This trade-offs the overall performance for computational efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network |
tf.keras.Model
|
network to freeze layers of |
required |
freeze_layer_name |
str
|
name of the first layer to be trainable. Every layer before it will be frozen |
None
|
freeze_bn |
bool
|
Whether to leave all BatchNormalization layers frozen. It is observed that leaving frozen during fine-tuning gives a slightly better accuracy |
True
|
unfreeze_all |
bool
|
Whether to unfreeze the whole network |
False
|
Source code in conftrainer/training/utils.py
freeze_batchnorm(network)
Freeze all BatchNormalization layers of given network
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network |
tf.keras.Model
|
network to freeze |
required |
Source code in conftrainer/training/utils.py
get_loss(name='SigmoidFocalCrossEntropy', **kwargs)
Initialize a loss function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
name of the loss function. Should be from tf.losses, keras-cv.losses or conftrainer.losses |
:SigmoidFocalCrossentropy
|
**kwargs |
dict
|
loss-specific keyword arguments. Ex. |
{}
|
loss_func = get_loss("CategoricalFocalLoss", **{"alpha" : 0.75, "gamma" : 1.25})
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.keras.losses.Loss
|
loss function |
Source code in conftrainer/training/utils.py
get_lr(learning_rate, schedule=None, **kwargs)
Get learning rate to use during training
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate |
float
|
initial learning rate |
required |
schedule |
Optional[str]
|
name of the schedule to use. If None, return learning rate as is |
None
|
**kwargs |
schedule-specific arguments |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
float, tf.keras.optimizers.Scheduler
|
learning rate |
Source code in conftrainer/training/utils.py
get_optimizer(name, learning_rate=0.001, **kwargs)
Initialize an optimizer to use during training
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
name of the optimizer module, either from tensorflow.keras.optimizers or tensorflow_addons.optimizers |
required |
learning_rate |
float, dict
|
initial learning rate to initialize the optimizer with. Either a float or a schedule |
0.001
|
**kwargs |
dict
|
optimizer-specific arguments. Ex. |
{}
|
opt = get_optimizer(name="AdamW", **{"learning_rate": 0.01, "weight_decay":0.00001})
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.keras.optimizers.Optimizer
|
an optimizer |
Source code in conftrainer/training/utils.py
Main util to train a network
create_and_train(config)
Main training loop. Initialize a network and a Trainer on top of it, then train & save the network based on the given configuration
Source code in conftrainer/training/train.py
train_multibranch(config)
Initialize a MultiOutputTrainer and train a network with given configurations