Coder Social home page Coder Social logo

facelet_bank's Introduction

Facelet-Bank for Fast Portrait Manipulation

framework

Prerequisites

  • Python 2.7 or Python 3.6
  • NVIDIA GPU or CPU (only for testing)
  • Linux or MacOS

Getting Started

Installation

Install pytorch. The code is tested on 0.3.1 version.

pip install http://download.pytorch.org/whl/cu90/torch-0.3.1-cp27-cp27mu-linux_x86_64.whl

Clone this project to your machine.

git clone https://github.com/yingcong/Facelet_Bank.git
cd Facelet_Bank

Run

pip install -r requirements.txt

to install other packages.

How to use

We support testing on images and videos.

To test an image:

python test_facelet_net.py test_image --input_path examples/input.png --effect facehair --strength 5

If "--input_path" is a folder, all images in this folder will be tested.

To test a video:

python test_facelet_net.py test_video --input_path examples/input.mp4 --effect facehair --strength 5

Note that all required models will be downloaded automatically for the first time. Alternatively, you can also manually download the facelet_bank folder from dropbox or Baidu Netdisk and put them in the root directory.

If you do not have a GPU, please include "-cpu" argument to your command. For speed issue, you can optionally use a smaller image by specifying the "--size " option.

python test_facelet_net.py test_image --input_path examples/input.png --effect facehair --strength 5 --size 400,300 -cpu

For more details, please run

python test_facelet_net.py test_image --help

or

python test_facelet_net.py test_video --help

Note: Although this framework is robust to an extent, testing on extreme cases could cause the degradation of quality. For example, an extremely high strength may cause artifact. Testing on an extremely large image may not work as well as testing on a proper size (from 448 x 448 to 600 x 800).

More effects

The current project supports

  • facehair
  • older
  • younger
  • feminization
  • masculinization

More effects will be available in the future. Once a new effect is released, the global_vars.py file will be updated accordingly. We also provide an instruction of training your own effect in the following.

Results

input

Training

Training our network requires two steps, i.e., generating the attribute vector (Eq. (6) in our paper) and training our model.

Generating attribute vector

We utilize the Deep Feature Interpolation project to generate attribute vectors as pseudo labels to supervise our facelet network. Please see https://github.com/paulu/deepfeatinterp for more details.

After setting up the DFI project, copy DFI/demo2_facelet.py to its root directory. Then cd to the DFI project folder and run

python demo2_facelet.py --effect facehair --input_path images/celeba --npz_path attribute_vector

This extracts the facehair effect from images/celeba folder, and save the extracted attribute vectors to attribute_vector folder. For more details, please run

python demo2_facelet.py --help

Note: In our implementation, we use the aligned version of celebA dataset for training, and resize the images to 448 x 448.

From our experience, 2000~3000 samples should be enough to train a facelet model.

Training Facelet model

After generating enough attribute vectors, we can utilize them to train a facelet model. Please cd to the Facelet_bank folder and run

python train_facelet_net.py --effect facehair --input_path ../deepfeatinterp/images/celeba --npz_path ../deepfeatinterp/attribute_vector

where "--input_path" is the training image folder (the one used for generating attribute vector), and "--npz_path" is the folder of the generated attribute vectors.

For more details, please run

python train_facelet_net.py --help

Testing your own model

The trained facelet model is stored in the checkpoint folder. To test the trained model, please include the "--local_model" augment, i.e.,

python test_facelet_net.py test_image --input_path examples/input.png --effect facehair --strength 5 --local_model

Reference

Ying-Cong Chen, Huaijia Lin, Michelle Shu, Ruiyu Li, Xin Tao, Yangang Ye, Xiaoyong Shen, Jiaya Jia, "Facelet-Bank for Fast Portrait Manipulation" ,* Computer Vision and Pattern Recognition (CVPR), 2018 pdf

@inproceedings{Chen2018Facelet,
  title={Facelet-Bank for Fast Portrait Manipulation},
  author={Chen, Ying-Cong and Lin, Huaijia and Shu, Michelle and Li, Ruiyu and Tao, Xin and Ye, Yangang and Shen, Xiaoyong and Jia, Jiaya},
  booktitle={CVPR},
  year={2018}
}

Contact

Please contact [email protected] if you have any question or suggestion.

facelet_bank's People

Contributors

yingcong 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  avatar

facelet_bank's Issues

facelet loss3 fluctuated and trainging failed

Hi yingcong,
I am trying to reproduce your result but got a waired problem, that the loss3 varied in big fluctuation when training. On the begining, everying seemed OK but the loss were too small. After training of several epoches, loss1 and loss2 stayed small, but loss3 began to fluctuate. The sum loss is dominated by loss3 thus.

saving facehair_epoch_10.pth in /data/image_server/extra/tasks/gan_expr/facelet_bank/train/celeba_0819/snapshot_0819_sub/facehair_epoch_10.pth
  1%|| 5/695 [00:06<16:04,  1.40s/it](epoch: 11, iters: 5) loss1: 0.059 loss2: 0.060 loss3: 27.433 loss_all: 27.551
  2%|██▌                                                                                                                     | 15/695 [00:19<14:33,  1.28s/it](epoch: 11, iters: 15) loss1: 0.058 loss2: 0.057 loss3: 6.530 loss_all: 6.645
  4%|████▎                                                                                                                   | 25/695 [00:31<14:08,  1.27s/it](epoch: 11, iters: 25) loss1: 0.059 loss2: 0.060 loss3: 5.223 loss_all: 5.341
  5%|██████                                                                                                                  | 35/695 [00:44<13:50,  1.26s/it](epoch: 11, iters: 35) loss1: 0.058 loss2: 0.058 loss3: 11.128 loss_all: 11.245
  6%|███████▊                                                                                                                | 45/695 [00:56<13:35,  1.25s/it](epoch: 11, iters: 45) loss1: 0.059 loss2: 0.057 loss3: 8.589 loss_all: 8.706
  8%|█████████▍                                                                                                              | 55/695 [01:08<13:21,  1.25s/it](epoch: 11, iters: 55) loss1: 0.057 loss2: 0.056 loss3: 6.787 loss_all: 6.899
  9%|███████████▏                                                                                                            | 65/695 [01:21<13:08,  1.25s/it](epoch: 11, iters: 65) loss1: 0.057 loss2: 0.060 loss3: 18.024 loss_all: 18.142
 11%|████████████▉                                                                                                           | 75/695 [01:33<12:55,  1.25s/it](epoch: 11, iters: 75) loss1: 0.056 loss2: 0.056 loss3: 8.630 loss_all: 8.742
 12%|██████████████▋                                                                                                         | 85/695 [01:46<12:42,  1.25s/it](epoch: 11, iters: 85) loss1: 0.059 loss2: 0.061 loss3: 5.404 loss_all: 5.524
 14%|████████████████▍                                                                                                       | 95/695 [01:58<12:28,  1.25s/it](epoch: 11, iters: 95) loss1: 0.060 loss2: 0.061 loss3: 4.773 loss_all: 4.894
 15%|█████████████████▉                                                                                                     | 105/695 [02:10<12:15,  1.25s/it](epoch: 11, iters: 105) loss1: 0.190 loss2: 0.290 loss3: 960.732 loss_all: 961.213
 17%|███████████████████▋                                                                                                   | 115/695 [02:23<12:02,  1.25s/it](epoch: 11, iters: 115) loss1: 2.782 loss2: 10.159 loss3: 7643.598 loss_all: 7656.540
 18%|█████████████████████▍                                                                                                 | 125/695 [02:35<11:50,  1.25s/it](epoch: 11, iters: 125) loss1: 5.948 loss2: 24.127 loss3: 5259.168 loss_all: 5289.244
 19%|███████████████████████                                                                                                | 135/695 [02:48<11:37,  1.25s/it](epoch: 11, iters: 135) loss1: 3.336 loss2: 11.013 loss3: 3519.050 loss_all: 3533.399
 21%|████████████████████████▊                                                                                              | 145/695 [03:00<11:24,  1.25s/it](epoch: 11, iters: 145) loss1: 2.472 loss2: 11.408 loss3: 1584.675 loss_all: 1598.554
 22%|██████████████████████████▌                                                                                            | 155/695 [03:12<11:12,  1.25s/it](epoch: 11, iters: 155) loss1: 0.942 loss2: 2.853 loss3: 661.282 loss_all: 665.077
saving facehair_epoch_11.pth in /data/image_server/extra/tasks/gan_expr/facelet_bank/train/celeba_0819/snapshot_0819_sub/facehair_epoch_11.pth
 24%|████████████████████████████▎                                                                                          | 165/695 [03:26<11:02,  1.25s/it](epoch: 11, iters: 165) loss1: 1.397 loss2: 3.483 loss3: 487.248 loss_all: 492.128
 25%|█████████████████████████████▉                                                                                         | 175/695 [03:38<10:49,  1.25s/it](epoch: 11, iters: 175) loss1: 0.617 loss2: 2.370 loss3: 211.403 loss_all: 214.389
 27%|███████████████████████████████▋                                                                                       | 185/695 [03:50<10:36,  1.25s/it](epoch: 11, iters: 185) loss1: 0.289 loss2: 1.072 loss3: 201.949 loss_all: 203.310
 28%|█████████████████████████████████▍                                                                                     | 195/695 [04:03<10:23,  1.25s/it](epoch: 11, iters: 195) loss1: 0.326 loss2: 0.900 loss3: 74.685 loss_all: 75.911
 29%|███████████████████████████████████                                                                                    | 205/695 [04:15<10:11,  1.25s/it](epoch: 11, iters: 205) loss1: 0.514 loss2: 1.453 loss3: 183.029 loss_all: 184.997
 31%|████████████████████████████████████▊                                                                                  | 215/695 [04:28<09:58,  1.25s/it](epoch: 11, iters: 215) loss1: 0.288 loss2: 0.768 loss3: 183.889 loss_all: 184.946
 32%|██████████████████████████████████████▌                                                                                | 225/695 [04:40<09:45,  1.25s/it](epoch: 11, iters: 225) loss1: 0.307 loss2: 0.902 loss3: 66.429 loss_all: 67.638
 34%|████████████████████████████████████████▏                                                                              | 235/695 [04:52<09:33,  1.25s/it](epoch: 11, iters: 235) loss1: 0.291 loss2: 1.023 loss3: 135.353 loss_all: 136.666
 35%|█████████████████████████████████████████▉                                                                             | 245/695 [05:05<09:20,  1.25s/it](epoch: 11, iters: 245) loss1: 0.234 loss2: 0.553 loss3: 56.823 loss_all: 57.609
 37%|███████████████████████████████████████████▋                                                                           | 255/695 [05:17<09:08,  1.25s/it](epoch: 11, iters: 255) loss1: 0.157 loss2: 0.277 loss3: 152.729 loss_all: 153.163
 38%|█████████████████████████████████████████████▎                                                                         | 265/695 [05:30<08:55,  1.25s/it](epoch: 11, iters: 265) loss1: 0.192 loss2: 0.543 loss3: 19.501 loss_all: 20.236

(drag the bar right and you will see the loss value)

Although the training process could go to the end, the final result is not as expected. I train the model to add facehair using the sub vector 【dst_vector - src_vector】 as label. Other hyperparameters are default. The bad case is as follows:

greyson_sample
greyson_sample-facehair-s-5 0-1534815447

Note that, when I tried to train using the normialized vector 【sub_vector / (sub_vector)**2.mean()】, I got acceptable result show below, not as good as your result though.

greyson_sample-facehair-s-0 5-1534815365

I wonder whether you have met similar problem when you implemented your paper.
Thank for your reply.

Brainstorm, how to hide original face attributes

I am training smile model but in every case I can see the old smile.
Original:
009296-smiling-s-0 00

Results with WF = (QF - PF):
Weight = 9 (sad)
009296-smiling-s-9 00

Weight = -12 (smiling)
009296-smiling-s--12

This one is really interesting, because it looks like the smile underneath has changed and the old lips there glued on top of it.

Results with WF = (QF - PF) / ((QF - PF) ** 2).mean()

Sad face weight 0.6:
009296-smiling-s-0 60

Smile face weight -0.4:
009296-smiling-s--0 40

From this I can see that trained model only glues generated lips on top of the original ones. They are not modified. If face is smiling, model is able to add more smile to it (as in Smile face weight -0.4). But anomalies comes out if we need to change attribute to other direction. From smile to sad and vice versa.

Any thoughts how to make this face sad without these glitches?

can't reproduce pre-trained model

The training results cannot be reproduced. Could you provide the epoch, batch_size, and learning_rate parameters used to generate the pre-trained model?

16gb of ram is not enough in CPU mode

$ python test_facelet_net.py test_image --input_path examples/input.png --effect younger --strength 5 -cpu

RuntimeError: $ Torch: not enough memory: you tried to allocate 2GB. Buy new RAM! at /pytorch/torch/lib/TH/THGeneral.c:253

Tried running this on machine with 16gm of ram and it was all used up, it might not be a bug, but is very likely to be.

p.s. pytorch/pytorch#5285 Might be an issue of pytorch and not of this library, but not sure

KeyError: 'module name can\'t contain "."'

您好,我下载了库,并且安装了以来,运行 python test_facelet_net.py test_image --input_path examples/input.png --effect facehair --strength 5 --size 400,300 -cpu 的时候,报了KeyError: 'module name can't contain "."'的错,我尝试把module的name打印出来,结果是conv.1,您的代码里检测到name带了.就会报错,我不知道我应该做点什么才能跑通这个代码,我的python是2.7.12,我的依赖全都高于您要求的版本,我也尝试用python3.6.3,但是也报错,torch没有attributes"float32",我也尝试了把facelet_bank下载下来放到根目录,但是没有用,还是一样的报错,我目前是一个本科学生,才疏学浅,希望您能帮我解答,谢谢,如果您愿意联系我的话,可以发我邮件[email protected]或者直接在下方回复,谢谢~

Model does not work

Hi, I am using my own trained models, but input and result is almost the same. Result is a bit brighter. I have tried to train for 5 minutes and for 24 hours. In both cases loss_all drops down to 0.310 ~ 0.211. I tried training with 5000 attribute vectors and with 50. No difference. Maybe there is a way to debug attribute_vector/ generated npz files?

@yingcong Have you encountered such issue?

Training and preparing the dataset

Hi, I am trying to train my own model, but I am getting this error:

Traceback (most recent call last):
  File "train_facelet_net.py", line 68, in <module>
    train()
  File "train_facelet_net.py", line 54, in train
    for idx, data in enumerate(tqdm(dataloader), 0):
  File "/usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.py", line 930, in __iter__
    for obj in iterable:
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/data/dataloader.py", line 281, in __next__
    return self._process_next_batch(batch)
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/data/dataloader.py", line 301, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/data/dataloader.py", line 55, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/aidas/quiz-facebank/data/trainDataset.py", line 62, in __getitem__
    npz[0] = torch.cat((npz[0], cur_npz[: 256 * scale1 * scale1].view(256, scale1, scale1)), 0)
RuntimeError: invalid argument 2: size '[256 x 112 x 112]' is invalid for input with 1277952 elements at /pytorch/torch/lib/TH/THStorage.c:41

My assumption is that I should not be using celeba "Align&Cropped Images" dataset, because all images in it are of size "178x218". Maybe I should use "In-The-Wild Images" and crop them to 448x448 myself?

Readme contains this note: "Note: In our implementation, we use the aligned version of celebA dataset for training, and resize the images to 448 x 448." Does this mean, that before making "attribute_vectors" I should resize and crop all the images to 448 x 448? Or train_facelet_net.py should handle it by itself?
If I have to crop and resize all the images to 448 x 448 should all of them be aligned in the same way? (e.g. all eyes and noses should be in the same spot)

Thanks for any help!

Decoder training

Hi. I'm trying to reproduce your results on smaller images, but it seems that decoder doesn't work correctly with such sizes

Can you expand on how you trained vgg_decoder?

White results

Hi, I have tried all the examples with all the models and different strengths, but all results are the same. What might be wrong?
Result images are attached:
input-facehair-s-1:
input-facehair-s-1
input-younger-s-5:
input-younger-s-5

slow test speed

Is it normal to test slowly? I have used GPUs,A picture feels like it takes five minutes。

KeyError: 'module name can\'t contain "."'

Traceback (most recent call last):
File "test_facelet_net.py", line 110, in
Framework.run()
File "D:\xinghu\Facelet_Bank\util\framework.py", line 32, in run
self.func_dictargs.command
File "test_facelet_net.py", line 81, in test_video
decoder = vgg_decoder()
File "D:\xinghu\Facelet_Bank\network\decoder.py", line 16, in init
self._define_model()
File "D:\xinghu\Facelet_Bank\network\decoder.py", line 25, in _define_model
self.model = base_network.Vgg_recon()
File "D:\xinghu\Facelet_Bank\network\base_network.py", line 141, in init
self.recon5 = _PoolingBlock(3, 512, 512, drop_rate=drop_rate)
File "D:\xinghu\Facelet_Bank\network\base_network.py", line 189, in init
padding=1))
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 196, in add_module
raise KeyError("module name can't contain "."")
KeyError: 'module name can't contain "."'

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.