PyTorch implementation of the AFFACT Paper
Facial attribute classification tasks have many potential real-world applications across different areas (e.g. surveillance, entertainment, medical treatment). To make classification models more stable toward misaligned images, a data augmentation technique called alignment-free facial attribute classification technique (AFFACT) was introduced by Günther et al. [1]. Since their approach was originally implemented using the nowadays outdated Caffe framework, the goal of this master project is to reimplement this technique using the more popular PyTorch framework. Our reimplementation performs on the same level as the original AFFACT model. We further introduce the possibility to perform hyperparameter optimization on existing models and establish an extended network trained on an additional data augmentation operation.
- Table of contents
- Quick start
- Pretrained models
- Training a model
- Evaluation of a model
- Training on multiple GPUs
- Run a Hyperparameter Optimization
- References
-
Clone the repository:
git clone https://github.com/noahch/PyAffact
-
Create a new Conda environment and install all dependencies:
# create a new Conda environment with Python 3.7 conda create -n affact python=3.7 # activate environment conda activate affact # install all requirements for bob (image processing library) # UNIX based system needed conda install -c https://www.idiap.ch/software/bob/conda bob.io.image conda install -c https://www.idiap.ch/software/bob/conda bob.ip.base # install PyTorch according to your system's specification # have a look at https://pytorch.org/ as the installation command may vary depending on your OS and your version of CUDA # install all other dependencies pip install -r requirements.txt
-
Adjust the dataset path in the configuration files located in config/train and config/eval
-
Evaluate a pretrained model:
# inside the conda environment, run the following command: python py_affact_evaluate.py --config.name=eval/affact -
Train a model:
# inside the conda environment, run the following command: python py_affact_train.py --config.name=train/affact
In order to train a model, download the CelebA dataset here.
There are the following 3 types of configuration YAML files. The first configuration file is located in the config/train folder. This configuration file covers all hyperparameters needed to train a model. Furthermore, there is another configuration file with different parameters in the config/eval folder, which covers all parameters to evaluate a model. Finally, the configuration file to create the different test sets is situated in the config/dataset folder. The configuration files are written in the popular YAML format and fully documented. For reproducibility purposes, there is a configuration file for the ResNet-51-S, AFFACT and AFFACT-Ext in the config/train and in the config/evaluation folder respectively.
For example, the fully-documented configuration YAML file to train the AFFACT model can be found here.
WandB (Weights and Biases) is a popular tool to keep track of machine learning experiments [4]. In our project we allow the use of WandB in our training configuration file. When enabled, each experiment creates 1 report with a dedicated URL link. This report then monitors the training history and every epoch the plots for the current training and validation loss/accuracy is automatically updated. Moreover, the system metrics such as memory, CPU, GPU usage are recorded and updated on the fly.
The installation guide can be found here.
The pretrained models described in the AFFACT paper as well as our own built AFFACT-Ext model are available for download here:
| Model name |
|---|
| ResNet-51-S |
| AFFACT |
| AFFACT-Ext |
To simplify training of the models in the AFFACT paper, we created a configuration file for each model in the AFFACT paper. Additionally, we added a configuration file our own model (AFFACT-Ext) which is described in our report. The commands to train the different models are listed below.
Run the following command to train the ResNet-51-S model:
# inside the conda environment, run the following command:
python py_affact_train.py --config.name=train/resnet51_s Run the following command to train the AFFACT model:
# inside the conda environment, run the following command:
python py_affact_train.py --config.name=train/affact Run the following command to train the AFFACT-Ext model:
# inside the conda environment, run the following command:
python py_affact_train.py --config.name=train/affact_ext We evaluate each model quantitavely and qualitatively. In the quantitative analysis we analyze the accuracy of the model on 4 transformations of the test set (A, C, D, T) described in our report for each attribute and calculate the overall accuracy. In the qualitative analysis we look at 6 samples of images and evaluate the prediction quality of the network. All the results are saved in a result folder, marked by the timestamp and specific chosen model.
Generate 4 different test sets (A, D, C, T) based on the test partition of the CelebA dataset. The configuration files in the config/dataset folder control the transformation of the test set images.
# inside the conda environment, run the following command:
python generate_test_datasets.py Run the following command to evaluate the ResNet-51-S model:
# inside the conda environment, run the following command:
python py_affact_evaluate.py --config.name=eval/resnet51_s Run the following command to evaluate the AFFACT model:
# inside the conda environment, run the following command:
python py_affact_evaluate.py --config.name=eval/affact Run the following command to evaluate the AFFACT-Ext model:
# inside the conda environment, run the following command:
python py_affact_evaluate.py --config.name=eval/affact_ext It is possible to add several cuda IDs separated by comma in a training configuration file or by using a flag in order to train the model on multiple GPUs. For example, run the following command to train the AFFACT model on cuda:0 and cuda:1.
# inside the conda environment, run the following command:
python py_affact_evaluate.py --config.name=eval/affact --config.basic.cuda_device_name=cuda:0,cuda:1 If you want to explore hyperparameters you can run a Sweep using WandB over a range of values.
Edit the example sweep configuration files located in config/train/hyperopt to adjust parameter bounds.
Create the sweep:
wandb sweep config/train/hyperopt/affact_hyperopt.yaml
Start Sweep:
wandb agent SWEEP_ID # SWEEP_ID is from the create sweep command above
[1] AFFACT - Alignment-Free Facial Attribute Classification Technique
[2] Reimplementing AFFACT
[3] PyTorch
[4] Wandb