Giter Site home page Giter Site logo

mystyle's Introduction

MyStyle: A Personalized Generative Prior

teaser

Project Page | Video | Paper

MyStyle: A Personalized Generative Prior
Yotam Nitzan1,2, Kfir Aberman1, Qiurui He1, Orly Liba1, Michal Yarom1, Yossi Gandelsman1, Inbar Mosseri1, Yael Pritch1, Daniel Cohen-Or2

1 Google Research, 2 Tel-Aviv University

Setup

Code was tested with Python 3.9.5, Pytorch 1.9.0 and CUDA 11.4.

We provide a yml file to easy setup of a conda environment.

conda env create -f mystyle_env.yml

Auxilary pre-trained models are required for some workflows and are listed below.

Name Data Preprocessing Training Description
arcface ✔️ Face-recognition network taken from TreB1eN.
dlib landmarks model ✔️ Used for face alignment.
FFHQ StyleGAN ✔️ StyleGANv2 model trained on FFHQ.
W Inversion Encoder ✔️ Taken from HyperStyle.

Getting Your Data Ready

We proivde a pre-processing pipeline so you can easily transform a personal photo album into a suitable training set. Start by having the photo album under a single local directory.

Note: Interactive scripts below require GUI / X-11.

Filtering other people and aligning faces

We first crop and align the faces in the photos. Naturally, besides the intended person, faces of other people may appear in the photo album. To filter them out run:

python data_preprocess/0_align_face.py \
    --images_dir /path/to/photo/album \
    --save_dir /path/to/save/resulting/images \
    --trash_dir /path/to/save/unused/images \
    --landmarks_model /path/to/dlib/landmarks/model \
    --id_model /path/to/arcface/model \

To learn who is the intended person, you will be prompted a few images of faces. If the face belongs to the intended person press Y to confirm. Press any other key otherwise. After having enough positive samples, we'll filter the rest of the images automatically.

If you are certain the photo album contains only the face of the desired person, you do not need to pass --id_model and the alignment is fully automatic.

Removing unsuitable images

From the resulting aligned set, we filter out unsuitable images if they are either low quality or grayscale. Run:

python data_preprocess/1_image_quality.py \
    --images_dir /path/to/aligned/faces \
    --save_dir /path/to/save/resulting/images \
    --trash_dir /path/to/save/unused/images

Removing Duplicates

Last, we'll remove duplicates from our set. To do that, run:

python data_preprocess/2_duplicates.py \
    --images_dir /path/to/current/filtered/images \
    --trash_dir /path/to/save/unsued/images \
    --id_model /path/to/arcface/model

All scripts have additional arguments that could be tuned to make the pipeline more accurate. Nevertheless, it will never be perfect. Specifically, you'd probably like to apply additional filters, depending on your data. We recommend using lenient values for the scripts above (such as those set as default) and following up with a manual filtering:

python data_preprocess/3_manual_filter.py \
    --images_dir /path/to/current/filtered/images \
    --trash_dir /path/to/save/unsued/images

Note that low quality images will degrade the quality of the generator!

Training a Personalized Generator

To adapt a pretrained domain-generator to a personalized-generator, run the training command below.

python train.py \
    --images_dir /path/to/dir/of/ready/training/images \
    --output_dir /path/to/save/results \
    --generator_path /path/to/pretrained/domain-generator \
    --encoder_checkpoint /path/to/pretrained/inversion/encoder 

Anchors will be inferred on-the-fly and saved under the output_dir.

To use existing saved anchors for the training images, pass --anchor_dir instead of --encoder_checkpoint. Customize hyperparameters by editing hyperparams.py file.

Training is expected to take ~50 seconds per training image on a single V100.

Test time - Applications

Image Synthesis

To generate personalized images:

python generate.py \
    --generator_path /path/to/personalized/generator \
    --output_path /path/to/save/results \
    --anchors_path /path/to/models_anchors

Image Enhancement & Inversion

First, align the test images using data_preprocess/0_align_face.py.

Our image enhancement method is based on projecting the image into the latent space of the generator, similarly to inversion. Therefore, inversion and image enhancemnet applications - inpainting and super-resolution are run using very similar code.

For inversion:

python project.py \
    --images_dir /path/to/dir/of/test/images \
    --output_dir /path/to/save/results \
    --anchor_dir /path/to/models_anchors \
    --generator_path /path/to/personalized/generator

For inpainting, add --mask_dir /path/to/dir/of/masks

For super-resolution, add  --sr_factor /zero/to/one/downscaling/factor

Importance of $\beta$

In our experiments, we found that $\beta$ (controlled with --beta) controls the trade-off between prior and fidelity. Depending on how "atypical" is the input image - increasing $\beta$ may lead to lower-distortion projection at the cost of being less faithful to the prior. See paper for more details.

Semantic Editing

To edit real images you first need to reconstuct them from MyStyle's latent space. The first step is to use "plain" inversion, with project.py. To improve reconstruction, you can further tune the generator on the test images (as in PTI), using train.py. Adding the second stage requires more time but it obtaines significantly better quality.

Last, with the inverted alphas and generator in hand, run the edit script:

python edit.py \
    --alphas_dir /path/to/alpha/results/of/projection \
    --output_dir /path/to/save/results \
    --anchor_dir /path/to/models_anchors \
    --generator_path /path/to/personalized/generator \
    --editing_direction_path /path/to/dir/with/W/editing/directions

An example script with the entire editing pipeline is provided under editing_pipeline.sh.

Editing Directions:

Any W editing direction should work in principle. The InterFaceGAN directions used in the paper (taken from PTI) are provided in editing_directions for your convinienve. Thanks to the AnyCost GAN team - directions corresponding to all labels of CelebA can be found here.

Results

results_img

Citation

@article{nitzan2022mystyle,
  title={MyStyle: A Personalized Generative Prior},
  author={Nitzan, Yotam and Aberman, Kfir and He, Qiurui and Liba, Orly and Yarom, Michal and Gandelsman, Yossi and Mosseri, Inbar and Pritch, Yael and Cohen-Or, Daniel},
  journal={arXiv preprint arXiv:2203.17272},
  year={2022}
}

Disclaimer

This is not an officially supported Google product.

mystyle's People

Contributors

yotamnitzan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mystyle's Issues

Wrong Pose Edit

Thanks for your work! I follow your readme file to edit pose on my own images. But there is something wrong. The pose is not changed.
@YotamNitzan
image

Failed to create virtual environment。

cmd: conda env create -f mystyle_env.yml

error log:

Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - torchvision==0.2.2=py_3

Inpainting results do not look as good as the paper

Hi,

Thank you for releasing the code. I am trying to use your model for inpainting using 100 reference images however the results do not look good. Any chance of being able to release your model or suggest what is going on?
10
0

Request for the datasets

Thanks for open-sourcing the code! Could you please also share the datasets used in the paper?

Bunch of corrections

mv ../shape_predictor_68_face_landmarks.dat.bz2 ../ffhq.pkl ../model_ir_se50.pth ../faces_w_encoder.pt checkpoints/

FIRST correction required to work

mv data_preprcoess/ data_preprocess # (!!)

SETTING PYTHONPATH required to work

export PYTHONPATH=$PYTHONPATH:pwd

this fails, dlib model is in bz2 archive, must be bunzipped

python data_preprocess/0_align_face.py
--images_dir test_album
--save_dir test
--trash_dir trash
--landmarks_model checkpoints/shape_predictor_68_face_landmarks.dat.bz2
--id_model checkpoints/model_ir_se50.pth

Traceback (most recent call last):
File "PATH-EDITED/mystyle/data_preprocess/0_align_face.py", line 242, in
main()
File "PATH-EDITED/mystyle/data_preprocess/0_align_face.py", line 219, in main
lnds_predictor = dlib.shape_predictor(str(args.landmarks_model))
RuntimeError: Unexpected version found while deserializing dlib::shape_predictor.

cd checkpoints
bunzip2 shape_predictor_68_face_landmarks.dat.bz2 # (!)
cd ..
python data_preprocess/0_align_face.py
--images_dir test_album
--save_dir test
--trash_dir trash
--landmarks_model checkpoints/shape_predictor_68_face_landmarks.dat
--id_model checkpoints/model_ir_se50.pth

OK - now it fails a bit further, but it's because I have n physical GPU no board. I managed to run arcnet/resnet withut GPU before, so I will try later to check whether I'll be able to do it here. We'll see.

AttributeError: 'NoneType' object has no attribute 'seek'

Hi, I had try to training but it always get this error:
`(mystyle) root@homepc:/mnt/e/MyStyle/mystyle# python train.py --images_dir "/mnt/e/MyStyle/mystyle/training/images/Female" --output_dir "/mnt/e/MyStyle/mystyle/training/Female.model" --generator_path "/mnt/e/MyStyle/mystyle/ffhq.pkl"
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe
Path to anchors was not given, inferring them on the fly...
Traceback (most recent call last):
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 308, in _check_seekable
f.seek(f.tell())
AttributeError: 'NoneType' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/mnt/e/MyStyle/mystyle/train.py", line 107, in
main()
File "/mnt/e/MyStyle/mystyle/train.py", line 90, in main
dataset = get_data(args)
File "/mnt/e/MyStyle/mystyle/train.py", line 38, in get_data
dataset = infer_anchors.invert(args.images_dir, args.encoder_checkpoint,
File "/mnt/e/MyStyle/mystyle/infer_anchors.py", line 56, in invert
encoder = inversion_encoder.InversionEncoder(encoder_checkpoint, torch.device(f'cuda'))
File "/mnt/e/MyStyle/mystyle/inversion_encoder.py", line 30, in init
ckpt = torch.load(checkpoint_path, map_location='cpu')
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 594, in load
with _open_file_like(f, 'rb') as opened_file:
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 235, in _open_file_like
return _open_buffer_reader(name_or_buffer)
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 220, in init
_check_seekable(buffer)
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 311, in _check_seekable
raise_err_msg(["seek", "tell"], e)
File "/root/miniconda3/envs/mystyle/lib/python3.9/site-packages/torch/serialization.py", line 304, in raise_err_msg
raise type(e)(msg)
AttributeError: 'NoneType' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.`

I have use both ffhq.pkl and W Inversion Encoder and both of them get the same error. Or I use wrong model, please give me more detail in instruction.

Google Colab.

Thank you for releasing the code! Can you please include a Google Colab for training and the Applications.

Weights?

Hello,

Awesome work! Do you plan to release the pretraimes weights?

Thanks!

Inpainting pipeline

Thanks for the code.

I am wondering if for the paper's results, you do more processing for the inpainting examples since the background in the test cases you show does not change from GT to inpainting result which is odd when using GANs! thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.