### Install dependencies with Poetry Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Installs the project dependencies using Poetry, which is the recommended setup method. ```bash poetry install ``` -------------------------------- ### Train the encoder with config 001 Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Starts training using the configuration file in the configs/ directory. Modify training options in the config file before running. ```bash python train.py --config 001 ``` -------------------------------- ### Install dependencies with pip Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Alternative installation method that installs the dependencies directly into your current environment using pip. ```bash pip install -r requirements.txt ``` -------------------------------- ### Download pretrained models Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Runs the download_models.sh script to download and save the pretrained models required for the project. ```bash sh download_models.sh ``` -------------------------------- ### Create and activate conda environment Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Creates a new conda environment from environment.yml and activates it. Requires conda and the environment.yml file in the repository root. ```bash conda env create -f environment.yml conda activate feature_style ``` -------------------------------- ### Test inversion on images Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Runs the encoder on images in the test/ directory and saves output images to output/image/. Requires a pretrained model path. ```bash python test.py --pretrained_model_path './pretrained_models/143_enc.pth' --input_path './test/' ``` -------------------------------- ### Clone and enter the encoder4editing repository Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Clone the repository and change into its directory. Required before installing dependencies. ```bash git clone https://github.com/omertov/encoder4editing.git cd encoder4editing ``` -------------------------------- ### Citation for the paper Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md BibTeX citation for the paper 'A Style-Based GAN Encoder for High Fidelity Reconstruction of Images and Videos' published at ECCV 2022. ```bibtex @article{xuyao2022, title={A Style-Based GAN Encoder for High Fidelity Reconstruction of Images and Videos}, author={Yao, Xu and Newson, Alasdair and Gousseau, Yann and Hellier, Pierre}, journal={European conference on computer vision}, year={2022} } ``` -------------------------------- ### Run video inversion and editing script Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Executes the video inversion and attribute manipulation script for videos in the data/video/ directory. Modify options in run_video_inversion_editing.sh before running. ```bash sh run_video_inversion_editing.sh ``` -------------------------------- ### Get color labels (RGB/HSV and variance) Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Run these two scripts sequentially to generate color labels for the dataset: first the RGB/HSV label, then the color variance label. ```bash python dataset_scripts/script_get_rgb_hsv_label.py ``` ```bash python dataset_scripts/script_color_var_label.py ``` -------------------------------- ### Get SEAN feature codes Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Run this script to compute SEAN feature codes for the dataset. Execute after landmark detection. ```bash python dataset_scripts/script_get_sean_code.py ``` -------------------------------- ### Generate synthetic training images Source: https://github.com/airi-institute/hairfastgan/blob/main/models/FeatureStyleEncoder/README.md Generates synthetic images using StyleGAN for training the encoder. The generated images are used alongside the FFHQ dataset. ```bash python generate_imgs.py ``` -------------------------------- ### Get 68 and 81 facial landmarks Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Run this script to detect 68 and 81 facial landmarks for the dataset. Execute after the mask generation step. ```bash python dataset_scripts/script_landmark_detection.py ``` -------------------------------- ### Get facial segmentation mask Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Run this script to generate facial segmentation masks for the dataset. Execute after downloading and placing the dataset information in the correct path. ```bash python dataset_scripts/script_get_mask.py ``` -------------------------------- ### requirements.txt dependencies for STAR model Source: https://github.com/airi-institute/hairfastgan/blob/main/models/STAR/requirements.txt Lists pinned Python package versions for the STAR model, including torch 1.6.0, torchvision 0.7.0, pandas 0.24.2, and others. Some packages (pyarrow, wandb) are commented out, and a comment provides a CPU-only installation command for torch and torchvision. ```text tqdm torch==1.6.0 torchvision==0.7.0 python-gflags==3.1.2 pandas==0.24.2 pillow==6.0.0 numpy==1.16.4 opencv-python==4.1.0.25 imageio==2.5.0 imgaug==0.2.9 lmdb==0.98 lxml==4.5.0 tensorboard==2.4.1 protobuf==3.20 tensorboardX==1.8 # pyarrow==0.17.1 # wandb==0.10.25 # https://pytorch.org/get-started/previous-versions/ # pip install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html ``` -------------------------------- ### Run CtrlHair UI frontend demo Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Launches the interactive UI for editing hair with a pretrained model. The pretrained model must be placed at CtrlHair/model_trained. Options include -g for GPU selection (default 0), -n for input cropping (default True), and --no_blending to disable Poisson blending post-processing. Results are saved to temp_folder/demo_output/out_img.png and the edited shape to temp_folder/demo_output/input_parsing.png. ```bash python ui/frontend_demo.py ``` -------------------------------- ### Create an image triples file for batch experiments Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Creates a text file listing face/shape/color image triples for batch processing. ```bash cat > example.txt << EOF 6.png 7.png 8.png 8.png 4.jpg 5.jpg EOF ``` -------------------------------- ### Test STAR model with python main.py --mode=test Source: https://github.com/airi-institute/hairfastgan/blob/main/models/STAR/README.md Run testing for the STAR model. Requires the pretrained weight path via --pretrained_weight. Uses device ID 0. ```shell python main.py --mode=test --device_ids=0 \ --image_dir=${image_dir} --annot_dir=${annot_dir} \ --data_definition={WFLW, 300W, COFW} \ --pretrained_weight=${model_path} \ ``` -------------------------------- ### Download pretrained models and input data Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Downloads all pretrained models and input data from Hugging Face, then moves them into the expected directories and removes the temporary clone. ```bash git clone https://huggingface.co/AIRI-Institute/HairFastGAN cd HairFastGAN && git lfs pull && cd .. mv HairFastGAN/pretrained_models pretrained_models mv HairFastGAN/input input rm -rf HairFastGAN ``` -------------------------------- ### Run a single HairFastGAN experiment Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Runs a single hair swap experiment using main.py with face, shape, and color image paths. ```bash python main.py --face_path=6.png --shape_path=7.png --color_path=8.png \ --input_dir=input --result_path=output/result.png ``` -------------------------------- ### Train STAR model with python main.py --mode=train Source: https://github.com/airi-institute/hairfastgan/blob/main/models/STAR/README.md Run training for the STAR model. Set ${ckpt_dir} in ./conf/alignment.py and provide image/annotation directories and the dataset definition (WFLW, 300W, or COFW). Uses device IDs 0,1,2,3. ```shell python main.py --mode=train --device_ids=0,1,2,3 \ --image_dir=${image_dir} --annot_dir=${annot_dir} \ --data_definition={WFLW, 300W, COFW} ``` -------------------------------- ### Run STAR demo on own image with python demo.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/STAR/README.md Run the demo script to test the STAR model on your own image. ```shell python demo.py ``` -------------------------------- ### Clone the HairFastGAN repository Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Clones the HairFastGAN repository and navigates into the project directory. ```bash git clone https://github.com/AIRI-Institute/HairFastGAN cd HairFastGAN ``` -------------------------------- ### Clone and enter the CtrlHair repository Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Clones the CtrlHair repository and changes into its directory. Requires Python 3.6 and dependencies such as PyTorch 1.8.2, torchvision, tensorboardX, dlib, pillow, pandas, scikit-learn, opencv-python, PyQt5, tqdm, addict, and dill. External trained models must be placed at CtrlHair/external_model_params. ```bash git clone https://github.com/XuyangGuo/CtrlHair.git cd CtrlHair ``` -------------------------------- ### Run prepropess_data.py to prepare training data Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/external_code/face_parsing/README.md Run this script after downloading the CelebAMask-HQ dataset and changing the file path in prepropess_data.py. ```shell python prepropess_data.py ``` -------------------------------- ### Run batch HairFastGAN experiments Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Runs the batch version of HairFastGAN using the image triples file created earlier. ```bash python main.py --file_path=example.txt --input_dir=input --output_dir=output ``` -------------------------------- ### Evaluate STAR model with python evaluate.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/STAR/README.md Run evaluation for the STAR model. Requires model path, metadata path, image directory, and dataset definition. Uses device ID 0. ```shell python evaluate.py --device_ids=0 \ --model_path=${model_path} --metadata_path=${metadata_path} \ --image_dir=${image_dir} --data_definition={WFLW, 300W, COFW} \ ``` -------------------------------- ### Use HairFast directly in Python code Source: https://github.com/airi-institute/hairfastgan/blob/main/README.md Initializes the HairFast model and performs inference on face, shape, and color images. ```python from hair_swap import HairFast, get_parser # Init HairFast hair_fast = HairFast(get_parser().parse_args([])) # Inference result = hair_fast(face_img, shape_img, color_img) ``` -------------------------------- ### Run inference with scripts/inference.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Use this command to apply a trained model to a set of images. Replace the placeholder paths with your actual image directory, save directory, and checkpoint file. ```bash python scripts/inference.py \ --images_dir=/path/to/images/directory \ --save_dir=/path/to/saving/directory \ path/to/checkpoint.pt ``` -------------------------------- ### Run dataset cropping script (script_crop.py) Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Executes the dataset cropping script after modifying root_dir and dataset_name in dataset_scripts/script_crop.py. The cropped dataset is saved to dataset_info_ctrlhair/your_dataset_name/images_256. ```bash python dataset_scripts/script_crop.py ``` -------------------------------- ### Train color & texture branch with scripts.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to train the color & texture branch. This part depends on the curliness classifier and color encoder as configured in `color_texture_branch/config.py`. Use `-g` for GPU (default 0) and `-c` for the config name `xxx` defined in `color_texture_branch/config.py`. The trained model, tensorboard, and editing results are saved in `model_trained/color_texture`. ```bash python color_texture_branch/scripts.py -c 045 -g 0 ``` -------------------------------- ### Run LEC metric with metrics/LEC.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Calculates the Latent Editing Consistency (LEC) metric over the FFHQ StyleGAN using the aging editing direction. Run from the metrics directory and provide the image directory and checkpoint path. ```bash cd metrics python LEC.py \ --images_dir=/path/to/images/directory \ path/to/checkpoint.pt ``` -------------------------------- ### Configure new dataset in data_configs.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Define a new dataset entry under the DATASETS variable in configs/data_configs.py. Uses the paths from dataset_paths and references transforms_config.EncodeTransforms. Required before training with --dataset_type my_data_encode. ```python DATASETS = { 'my_data_encode': { 'transforms': transforms_config.EncodeTransforms, 'train_source_root': dataset_paths['my_train_data'], 'train_target_root': dataset_paths['my_train_data'], 'test_source_root': dataset_paths['my_test_data'], 'test_target_root': dataset_paths['my_test_data'] } } ``` -------------------------------- ### Train shape branch with scripts.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to train the shape branch model. Use `-g` for GPU (default 0) and `-c` for the config name `xxx` defined in `shape_branch/config.py`. The trained model, tensorboard, and editing results are saved in `model_trained/shape`. ```bash python shape_branch/scripts.py -c 054 -g 0 ``` -------------------------------- ### Evaluate trained model with test.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/external_code/face_parsing/README.md Evaluates the trained model using GPU. Run after training or after downloading the pre-trained model to res/cp. ```shell # evaluate using GPU python test.py ``` -------------------------------- ### Generate training buffer pool for shape branch Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to generate the buffer pool for training pre-aligned masks. The training buffer pool is saved in `dataset_info_ctrlhair/shape_training_wrap_pool`. This process runs for a very long time until the maximum number of buffered files is reached; it can run concurrently with shape training. ```bash python shape_branch/script_adaptor_train_pool.py ``` -------------------------------- ### Configure dataset paths for custom training Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Add train and test data paths to the dataset_paths variable in configs/paths_config.py. Required before configuring a new dataset in data_configs.py. ```python dataset_paths = { 'my_train_data': '/path/to/train/images/directory', 'my_test_data': '/path/to/test/images/directory' } ``` -------------------------------- ### Train curliness classifier with predictor_train.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to train the curliness classifier. Use `-g` to select GPU (default 0) and `-c` to choose the hyper-parameters config `pxxx` defined in `color_texture_branch/predictor/predictor_config.py`. The trained model and tensorboard summary are saved in `model_trained/curliness_classifier`. ```bash python color_texture_branch/predictor/predictor_train.py -c p002 -g 0 ``` -------------------------------- ### Generate testing buffer pool for shape branch Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to generate the buffer pool for testing pre-aligned masks. The testing buffer pool is saved in `dataset_info_ctrlhair/shape_testing_wrap_pool`. ```bash python shape_branch/script_adaptor_test_pool.py ``` -------------------------------- ### Train model with torch.distributed.launch Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/external_code/face_parsing/README.md Launches distributed training on two GPUs using the CelebAMask-HQ dataset. Set CUDA_VISIBLE_DEVICES to the desired GPU IDs. ```shell $ CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.launch --nproc_per_node=2 train.py ``` -------------------------------- ### Train color encoder with predictor_train.py Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to train the color encoder. Parameters are similar to the curliness classifier. The trained model and tensorboard summary are saved in `model_trained/color_encoder`. ```bash python color_texture_branch/predictor/predictor_train.py -c p004 -g 0 ``` -------------------------------- ### Color & texture branch predictor config Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Configuration snippet from `color_texture_branch/config.py` showing the predictor dependencies: `curliness` uses config `p002` and `rgb` uses config `p004`. This is required for the color & texture branch training. ```python ... 'predictor': {'curliness': 'p002', 'rgb': 'p004'}, ... ``` -------------------------------- ### BibTeX citation for the paper Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Cite the paper 'Designing an Encoder for StyleGAN Image Manipulation' when using this code in your research. ```bibtex @article{tov2021designing, title={Designing an Encoder for StyleGAN Image Manipulation}, author={Tov, Omer and Alaluf, Yuval and Nitzan, Yotam and Patashnik, Or and Cohen-Or, Daniel}, journal={arXiv preprint arXiv:2102.02766}, year={2021} } ``` -------------------------------- ### Find semantic directions for color & texture branch Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to find semantic orthogonal directions after training the color & texture branch for sliding bars. The `-c xxx` parameter is the same as the shape config. Generates a folder `direction_find` in `model_trained/color_texture/yourConfigName`; select directions from `texture_dir_n` and move the chosen `.pkl` file to `../texture_dir_used`. Run again until enough directions are found. ```bash python color_texture_branch/script_find_direction.py -c xxx ``` -------------------------------- ### Train e4e encoder with cars_encode dataset Source: https://github.com/airi-institute/hairfastgan/blob/main/models/encoder4editing/README.md Command to train the e4e encoder using the cars_encode dataset type. Requires paths to models and data configured in configs/path_configs.py and configs/data_configs.py. Uses flags for progressive training, identity loss, and stylegan weights. ```bash python scripts/train.py \ --dataset_type cars_encode \ --exp_dir new/experiment/directory \ --start_from_latent_avg \ --use_w_pool \ --w_discriminator_lambda 0.1 \ --progressive_start 20000 \ --id_lambda 0.5 \ --val_interval 10000 \ --max_steps 200000 \ --stylegan_size 512 \ --stylegan_weights path/to/pretrained/stylegan.pt \ --workers 8 \ --batch_size 8 \ --test_batch_size 4 \ --test_workers 4 ``` -------------------------------- ### Find semantic directions for shape branch Source: https://github.com/airi-institute/hairfastgan/blob/main/models/CtrlHair/README.md Command to find semantic orthogonal directions after training the shape branch for sliding bars. The `-c xxx` parameter is the same as the shape config. Usage is similar to texture, but the folder is `model_trained/shape`. ```bash python shape_branch/script_find_direction.py -c xxx ```