-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from boostcampaitech5/develop
[Release] : develop -> main release
- Loading branch information
Showing
222 changed files
with
59,323 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
NAS_ID = | ||
NAS_PASS = | ||
NAS_ADDR = | ||
NAS_PORT = | ||
|
||
REDIS_BROKER = | ||
REDIS_BACKEND = | ||
|
||
DB_HOST = | ||
DB_PORT = | ||
DB_USER = | ||
DB_PASS = | ||
DB_DATABASE = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
per-file-ignores = __init__.py:F401 | ||
ignore = E402, W503, E203 | ||
ignore = E402, W503, E203, E501, E722 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,3 +122,6 @@ test.py | |
|
||
# hydra log | ||
/outputs | ||
|
||
# worker temp directory | ||
/worker/temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[theme] | ||
|
||
# The preset Streamlit theme that your custom theme inherits from. One of "light" or "dark". | ||
base = "light" | ||
|
||
# Primary accent color for interactive elements. | ||
primaryColor = "gray" | ||
|
||
# Background color for the main content area. | ||
backgroundColor = "white" | ||
|
||
# Background color used for the sidebar and most interactive widgets. | ||
# secondaryBackgroundColor = | ||
|
||
# Color used for almost all text. | ||
# textColor = | ||
|
||
# Font family for all text in the app, except code blocks. One of "sans serif", "serif", or "monospace". | ||
font = "monospace" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# S-Lab License 1.0 | ||
|
||
Copyright 2022 S-Lab | ||
|
||
Redistribution and use for non-commercial purpose in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\ | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
4. In the event that redistribution and/or use for commercial purpose in source or binary forms, with or without modification is required, please contact the contributor(s) of the work. | ||
|
||
|
||
--- | ||
For the commercial use of DualStyleGAN, please consult Prof. Chen Change Loy ([email protected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# DualStyleGAN - Official PyTorch Implementation | ||
|
||
This repository provides the official PyTorch implementation for the following paper: | ||
|
||
**Pastiche Master: Exemplar-Based High-Resolution Portrait Style Transfer**<br> | ||
[Shuai Yang](https://williamyang1991.github.io/), [Liming Jiang](https://liming-jiang.com/), [Ziwei Liu](https://liuziwei7.github.io/) and [Chen Change Loy](https://www.mmlab-ntu.com/person/ccloy/)<br> | ||
In CVPR 2022.<br> | ||
[**Project Page**](https://www.mmlab-ntu.com/project/dualstylegan/) | [**Paper**](https://arxiv.org/abs/2203.13248) | [**Supplementary Video**](https://www.youtube.com/watch?v=scZTu77jixI) | ||
> **Abstract:** *Recent studies on StyleGAN show high performance on artistic portrait generation by transfer learning with limited data. In this paper, we explore more challenging exemplar-based high-resolution portrait style transfer by introducing a novel <b>DualStyleGAN</b> with flexible control of dual styles of the original face domain and the extended artistic portrait domain. Different from StyleGAN, DualStyleGAN provides a natural way of style transfer by characterizing the content and style of a portrait with an <b>intrinsic style path</b> and a new <b>extrinsic style path</b>, respectively. The delicately designed extrinsic style path enables our model to modulate both the color and complex structural styles hierarchically to precisely pastiche the style example. Furthermore, a novel progressive fine-tuning scheme is introduced to smoothly transform the generative space of the model to the target domain, even with the above modifications on the network architecture. Experiments demonstrate the superiority of DualStyleGAN over state-of-the-art methods in high-quality portrait style transfer and flexible style control.* | ||
|
||
**Features**:<br> | ||
**High-Resolution** (1024) | **Training Data-Efficient** (~200 Images) | **Exemplar-Based Color and Structure Transfer** | ||
|
||
## Installation | ||
|
||
**Clone this repo:** | ||
```bash | ||
git clone https://github.com/boostcampaitech5/level3_cv_finalproject-cv-15.git | ||
cd DualStyleGAN | ||
``` | ||
**Dependencies:** | ||
|
||
All dependencies for defining the environment are provided in `environment/dualstylegan_env.yaml`. | ||
We recommend running this repository using [Anaconda](https://docs.anaconda.com/anaconda/install/): | ||
```bash | ||
conda env create -f ./environment/dualstylegan_env.yaml | ||
``` | ||
We use CUDA 10.1 so it will install PyTorch 1.7.1 (corresponding to [Line 22](https://github.com/williamyang1991/DualStyleGAN/blob/main/environment/dualstylegan_env.yaml#L22), [Line 25](https://github.com/williamyang1991/DualStyleGAN/blob/main/environment/dualstylegan_env.yaml#L25), [Line 26](https://github.com/williamyang1991/DualStyleGAN/blob/main/environment/dualstylegan_env.yaml#L26) of `dualstylegan_env.yaml`). Please install PyTorch that matches your own CUDA version following [https://pytorch.org/](https://pytorch.org/). | ||
|
||
## Inference for Style Transfer | ||
|
||
Download cartoon checkpoints in https://drive.google.com/drive/folders/1xPo8PcbMXzcUyvwe5liJrfbA5yx4OF1j?usp=sharing | ||
|
||
The saved checkpoints are under the following folder structure: | ||
``` | ||
checkpoint | ||
|--encoder.pt % Pixel2style2pixel model | ||
|--cartoon | ||
|--generator.pt % DualStyleGAN model | ||
|--sampler.pt % The extrinsic style code sampling model | ||
|--exstyle_code.npy % extrinsic style codes of Cartoon dataset | ||
|--refined_exstyle_code.npy % refined extrinsic style codes of Cartoon dataset | ||
... | ||
``` | ||
|
||
Style transfer cat or dog to princesses in animation | ||
Run style_transfer.py: | ||
|
||
```python | ||
python style_transfer.py python style_transfer.py --content ./data/content/{your_picture} | ||
``` | ||
|
||
## Citation | ||
|
||
If you find this work useful for your research, please consider citing our paper: | ||
|
||
```bibtex | ||
@inproceedings{yang2022Pastiche, | ||
title={Pastiche Master: Exemplar-Based High-Resolution Portrait Style Transfer}, | ||
author={Yang, Shuai and Jiang, Liming and Liu, Ziwei and Loy, Chen Change}, | ||
booktitle={CVPR}, | ||
year={2022} | ||
} | ||
``` | ||
|
||
## Acknowledgments | ||
|
||
The code is mainly developed based on [stylegan2-pytorch](https://github.com/rosinality/stylegan2-pytorch) and [pixel2style2pixel](https://github.com/eladrich/pixel2style2pixel). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import os | ||
from argparse import Namespace | ||
|
||
import cv2 | ||
import numpy as np | ||
import torch | ||
import torchvision | ||
from fastapi import FastAPI | ||
from model.dualstylegan import DualStyleGAN | ||
from model.encoder.psp import pSp | ||
from PIL import Image | ||
from torch.nn import functional as F | ||
from torchvision import transforms | ||
|
||
app = FastAPI() | ||
|
||
transform = transforms.Compose( | ||
[ | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), | ||
] | ||
) | ||
|
||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
generator = DualStyleGAN(1024, 512, 8, 2, res_index=6) | ||
generator.eval() | ||
ckpt = torch.load( | ||
os.path.join( | ||
"/opt/ml/level3_cv_finalproject-cv-15/DualStyleGAN/checkpoint/", | ||
"cartoon", | ||
"generator.pt", | ||
), | ||
map_location=lambda storage, loc: storage, | ||
) | ||
generator.load_state_dict(ckpt["g_ema"]) | ||
generator = generator.to(device) | ||
|
||
model_path = os.path.join( | ||
"/opt/ml/level3_cv_finalproject-cv-15/DualStyleGAN/checkpoint/", "encoder.pt" | ||
) | ||
ckpt = torch.load(model_path, map_location="cpu") | ||
opts = ckpt["opts"] | ||
opts["checkpoint_path"] = model_path | ||
if "output_size" not in opts: | ||
opts["output_size"] = 1024 | ||
opts = Namespace(**opts) | ||
opts.device = device | ||
encoder = pSp(opts) | ||
encoder.eval() | ||
encoder.to(device) | ||
|
||
exstyles = np.load( | ||
os.path.join( | ||
"/opt/ml/level3_cv_finalproject-cv-15/DualStyleGAN/checkpoint/", | ||
"cartoon", | ||
"exstyle_code.npy", | ||
), | ||
allow_pickle="TRUE", | ||
).item() | ||
|
||
|
||
def load_image(filename): | ||
transform = transforms.Compose( | ||
[ | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), | ||
] | ||
) | ||
|
||
img = Image.open(filename) | ||
img = transform(img) | ||
return img.unsqueeze(dim=0) | ||
|
||
|
||
def save_image(img, filename): | ||
tmp = ((img.detach().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8) | ||
cv2.imwrite(filename, cv2.cvtColor(tmp, cv2.COLOR_RGB2BGR)) | ||
|
||
|
||
@app.get("/gan_predict/") | ||
async def generate_image(image_path: str): | ||
z_plus_latent = True | ||
return_z_plus_latent = True | ||
input_is_latent = False | ||
|
||
with torch.no_grad(): | ||
viz = [] | ||
# load content image | ||
image = load_image("../temp/" + image_path + ".jpg").to(device) | ||
viz += [image] | ||
|
||
# reconstructed content image and its intrinsic style code | ||
img_rec, instyle = encoder( | ||
F.adaptive_avg_pool2d(image, 256), | ||
randomize_noise=False, | ||
return_latents=True, | ||
z_plus_latent=z_plus_latent, | ||
return_z_plus_latent=return_z_plus_latent, | ||
resize=False, | ||
) | ||
img_rec = torch.clamp(img_rec.detach(), -1, 1) | ||
|
||
for style_id in [64, 87, 88, 107, 221, 268, 282, 299]: | ||
stylename = list(exstyles.keys())[style_id] | ||
latent = torch.tensor(exstyles[stylename]).to(device) | ||
|
||
# if False and not False: | ||
if False and not False: | ||
latent[:, 7:18] = instyle[:, 7:18] | ||
# extrinsic styte code | ||
exstyle = generator.generator.style( | ||
latent.reshape(latent.shape[0] * latent.shape[1], latent.shape[2]) | ||
).reshape(latent.shape) | ||
if False and False: | ||
exstyle[:, 7:18] = instyle[:, 7:18] | ||
|
||
# style transfer | ||
# input_is_latent: instyle is not in W space | ||
# z_plus_latent: instyle is in Z+ space | ||
# use_res: use extrinsic style path, or the style is not transferred | ||
# interp_weights: weight vector for style combination of two paths | ||
img_gen, _ = generator( | ||
[instyle], | ||
exstyle, | ||
input_is_latent=input_is_latent, | ||
z_plus_latent=z_plus_latent, | ||
truncation=0.75, | ||
truncation_latent=0, | ||
use_res=True, | ||
interp_weights=[0.75] * 7 + [1] * 11, | ||
) | ||
img_gen = torch.clamp(img_gen.detach(), -1, 1) | ||
viz += [img_gen] | ||
|
||
print("Generate images successfully!") | ||
|
||
save_image( | ||
torchvision.utils.make_grid( | ||
F.adaptive_avg_pool2d(torch.cat(viz[1:], dim=0), 256), 4, 2 | ||
).cpu(), | ||
"../temp/" + image_path + "_transfer.jpg", | ||
) | ||
|
||
print("Save images successfully!") | ||
return image_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## saved models | ||
|
||
folder structure: | ||
``` | ||
Checkpoint | ||
|--encoder.pt % Pixel2style2pixel model | ||
|--encoder_wplus.pt % Pixel2style2pixel model (optional) | ||
|--shape_predictor_68_face_landmarks.dat % Face alignment model | ||
|--stylegan2-ffhq-config-f.pt % (only for training) StyleGAN model | ||
|--model_ir_se50.pth % (only for training) Pretrained IR-SE50 model for ID loss | ||
|--generator-pretrain.pt % (only for training) Pretrained DualStyleGAN on FFHQ | ||
|--cartoon | ||
|--generator.pt % DualStyleGAN model | ||
|--sampler.pt % The extrinsic style code sampling model | ||
|--exstyle_code.npy % Extrinsic style codes of Cartoon dataset | ||
|--refined_exstyle_code.npy % Refined extrinsic style codes of Cartoon dataset | ||
|--instyle_code.npy % (only for training) Intrinsic style codes of Cartoon dataset | ||
|--finetune-000600.pt % (only for training) StyleGAN fine-tuned on Cartoon dataset | ||
|--caricature | ||
% the same files as in Cartoon | ||
... | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.