Commit bd511041 authored by Olegs NIKISINS's avatar Olegs NIKISINS

Moved functions from config to utils, updated docs, renamed train script

parent cda15bb0
Pipeline #26657 passed with stage
in 11 minutes and 7 seconds
......@@ -103,43 +103,6 @@ See training script for details
Define the function to compute the loss. Don't change the signature of this
function: ``loss_function(output, input, target)``
"""
from bob.learn.pytorch.utils import comp_bce_loss_weights
def loss_function(output, img, target):
"""
Returns a loss defined by a global variable loss_type in this config file.
**Parameters:**
``output`` : Tensor
Tensor of the size: ``[num_patches, 1]``
``img`` : Tensor
Tensor containing input training patches. Input for the above Network()
class. The dimensions are: ``[num_patches, 4, 8, 8]``.
Note: this argument is not used in current loss function.
``target`` : Tensor
Tensor containing class labels for each sample in ``img``.
Tensor of the size: ``[num_patches]``
**Returns:**
``loss`` : Tensor
Tensor containing loss value.
"""
target = target.float() # make sure the target is float, not int
# convert "target" tensor from size [num_patches] to [num_patches, 1], to match "output" dimensions:
target = target.view(-1, 1)
weight = comp_bce_loss_weights(target)
loss_type.weight = weight
loss = loss_type(output, target)
return loss
from bob.learn.pytorch.utils import weighted_bce_loss as loss_function
......@@ -5,6 +5,9 @@ import numpy as np
import torch
from bob.learn.pytorch.datasets import DataFolder
from torch.utils.data import DataLoader
from torch import nn
import logging
logger = logging.getLogger("bob.learn.pytorch")
def get_parameter(args, configuration, keyword, default):
......@@ -242,18 +245,18 @@ class MeanStdNormalizer():
Returns
-------
x_norm : Tensor
x_norm : :py:class:`torch.Tensor`
Normalized feature vector of the size ``(1, n_features)``
"""
if self.features_mean is None or self.features_std is None: # pre-compute normalization parameters
print ("Computing mean-std normalization parameters using real samples of the training set")
logger.info ("Computing mean-std normalization parameters using real samples of the training set")
# compute the normalization parameters on the fly:
features_mean, features_std = compute_mean_std_bf_class(self.kwargs)
# save normalization parameters:
print ("Setting the normalization parameters")
logger.info ("Setting the normalization parameters")
self.features_mean = features_mean
self.features_std = features_std
......@@ -266,3 +269,42 @@ class MeanStdNormalizer():
return torch.Tensor(x_norm).unsqueeze(0)
# =============================================================================
def weighted_bce_loss(output, img, target):
"""
Returns a weighted BCE loss.
Parameters
----------
output : :py:class:`torch.Tensor`
Tensor of the size: ``[num_patches, 1]``
img : :py:class:`torch.Tensor`
This argument is not used in current loss function, but is here to
match the signature expected by the training script.
target : :py:class:`torch.Tensor`
Tensor containing class labels for each sample in ``img``.
Tensor of the size: ``[num_patches]``
Returns
-------
loss : :py:class:`torch.Tensor`
Tensor containing loss value.
"""
loss_type = nn.BCELoss()
target = target.float() # make sure the target is float, not int
# convert "target" tensor from size [num_patches] to [num_patches, 1], to match "output" dimensions:
target = target.view(-1, 1)
weight = comp_bce_loss_weights(target)
loss_type.weight = weight
loss = loss_type(output, target)
return loss
......@@ -14,7 +14,7 @@ As an example, to train an autoencoder on facial images extracted from the Celeb
.. code-block:: sh
./bin/train_autoencoder.py \ # script used for autoencoders training, can be used for other networks as-well
./bin/train_network.py \ # script used for autoencoders training, can be used for other networks as-well
<FOLDER_CONTAINING_TRAINING_DATA> \ # substitute the path pointing to training data
<FOLDER_TO_SAVE_THE_RESULTS>/ \ # substitute the path to save the results to
-c autoencoder/net1_celeba.py \ # configuration file defining the AE, database, and training parameters
......@@ -29,7 +29,7 @@ People in Idiap can benefit from GPU cluster, running the training as follows:
--name <NAME_OF_EXPERIMENT> \ # define the name of th job (Idiap only)
--log-dir <FOLDER_TO_SAVE_THE_RESULTS>/logs/ \ # substitute the path to save the logs to (Idiap only)
--environment="PYTHONUNBUFFERED=1" -- \ #
./bin/train_autoencoder.py \ # script used for autoencoders training, cand be used for other networks as-well
./bin/train_network.py \ # script used for autoencoders training, cand be used for other networks as-well
<FOLDER_CONTAINING_TRAINING_DATA> \ # substitute the path pointing to training data
<FOLDER_TO_SAVE_THE_RESULTS>/ \ # substitute the path to save the results to
-c autoencoder/net1_celeba.py \ # configuration file defining the AE, database, and training parameters
......@@ -42,7 +42,7 @@ For a more detailed documentation of functionality available in the training scr
.. code-block:: sh
./bin/train_autoencoder.py --help # note: remove ./bin/ if buildout is not used
./bin/train_network.py --help # note: remove ./bin/ if buildout is not used
Please inspect the corresponding configuration file, ``net1_celeba.py`` for example, for more details on how to define the database, network architecture and training parameters.
......@@ -61,7 +61,7 @@ the following reconstructions produced by an autoencoder:
Autoencoder fine-tuning on the multi-channel facial data
===========================================================
This section is useful for those trying to reproduce the results form [NGM19]_, or for demonstrative purposes showing the capabilities of ``train_autoencoder.py`` script.
This section is useful for those trying to reproduce the results form [NGM19]_, or for demonstrative purposes showing the capabilities of ``train_network.py`` script.
Following the training procedure of [NGM19]_, one might want to fine-tune the pre-trained autoencoder on the multi-channel (**MC**) facial data.
In this example, MC training data is a stack of gray-scale, NIR, and Depth (BW-NIR-D) facial images extracted from WMCA face PAD database.
......@@ -74,7 +74,7 @@ autoencoder are fine-tuned.
.. code-block:: sh
./bin/train_autoencoder.py \ # script used for autoencoders training, can be used for other networks as-well
./bin/train_network.py \ # script used for autoencoders training, can be used for other networks as-well
<FOLDER_CONTAINING_TRAINING_DATA> \ # substitute the path pointing to training data
<FOLDER_TO_SAVE_THE_RESULTS>/ \ # substitute the path to save the results to
-p <FOLDER_CONTAINING_RGB_AE_MODELS>/model_70.pth \ # initialize the AE with the model obtained during RGB pre-training
......@@ -87,7 +87,7 @@ Below is the command allowing to fine-tine just **one layer of encoder**, which
.. code-block:: sh
./bin/train_autoencoder.py \ # script used for autoencoders training, can be used for other networks as-well
./bin/train_network.py \ # script used for autoencoders training, can be used for other networks as-well
<FOLDER_CONTAINING_TRAINING_DATA> \ # substitute the path pointing to training data
<FOLDER_TO_SAVE_THE_RESULTS>/ \ # substitute the path to save the results to
-p <FOLDER_CONTAINING_RGB_AE_MODELS>/model_70.pth \ # initialize the AE with the model obtained during RGB pre-training
......
......@@ -17,7 +17,7 @@ As an example, to train an autoencoder on latent embeddings extracted from an en
.. code-block:: sh
./bin/train_autoencoder.py \ # script used for MLP training, can be used for other networks as-well
./bin/train_network.py \ # script used for MLP training, can be used for other networks as-well
<FOLDER_CONTAINING_TRAINING_DATA> \ # substitute the path pointing to training data
<FOLDER_TO_SAVE_THE_RESULTS>/ \ # substitute the path to save the results to
-c mlp/batl_db_1296x10_relu_mlp.py \ # configuration file defining the database, training parameters, transformation to be applied to training data, and an MLP architecture
......
......@@ -72,7 +72,7 @@ setup(
'train_cnn.py = bob.learn.pytorch.scripts.train_cnn:main',
'train_dcgan.py = bob.learn.pytorch.scripts.train_dcgan:main',
'train_conditionalgan.py = bob.learn.pytorch.scripts.train_conditionalgan:main',
'train_autoencoder.py = bob.learn.pytorch.scripts.train_autoencoder:main',
'train_network.py = bob.learn.pytorch.scripts.train_network:main',
],
},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment