Spaces:
Running
Running
File size: 7,537 Bytes
73099b9 e38549b 73099b9 bd312c9 73099b9 b98a8ac 73099b9 58dfe83 73099b9 e38549b 73099b9 9735cfa 73099b9 b5f45ec 73099b9 b5f45ec 73099b9 e38549b 73099b9 e38549b 73099b9 9735cfa b5f45ec 9735cfa b5f45ec 73099b9 e38549b 73099b9 e38549b 73099b9 9735cfa b5f45ec 9735cfa 73099b9 9735cfa b5f45ec 9735cfa b5f45ec 9735cfa 73099b9 9735cfa 73099b9 b5f45ec 9735cfa b5f45ec 73099b9 b5f45ec 73099b9 b5f45ec 9735cfa 73099b9 b5f45ec 73099b9 9735cfa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
from __future__ import annotations
import argparse
import os
import pathlib
import subprocess
import sys
from typing import Callable
import dlib
import huggingface_hub
import numpy as np
import PIL.Image
import torch
import torch.nn as nn
import torchvision.transforms as T
if os.getenv('SYSTEM') == 'spaces':
os.system("sed -i '10,17d' DualStyleGAN/model/stylegan/op/fused_act.py")
os.system("sed -i '10,17d' DualStyleGAN/model/stylegan/op/upfirdn2d.py")
app_dir = pathlib.Path(__file__).parent
submodule_dir = app_dir / 'DualStyleGAN'
sys.path.insert(0, submodule_dir.as_posix())
from model.dualstylegan import DualStyleGAN
from model.encoder.align_all_parallel import align_face
from model.encoder.psp import pSp
MODEL_REPO = 'CVPR/DualStyleGAN'
class Model:
def __init__(self):
self.device = torch.device(
'cuda:0' if torch.cuda.is_available() else 'cpu')
self.landmark_model = self._create_dlib_landmark_model()
self.encoder_dict = self._load_encoder()
self.transform = self._create_transform()
self.encoder_type = 'z+'
self.style_types = [
'cartoon',
'caricature',
'anime',
'arcane',
'comic',
'pixar',
'slamdunk',
]
self.generator_dict = {
style_type: self._load_generator(style_type)
for style_type in self.style_types
}
self.exstyle_dict = {
style_type: self._load_exstylecode(style_type)
for style_type in self.style_types
}
@staticmethod
def _create_dlib_landmark_model():
url = 'http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2'
path = pathlib.Path('shape_predictor_68_face_landmarks.dat')
if not path.exists():
bz2_path = 'shape_predictor_68_face_landmarks.dat.bz2'
torch.hub.download_url_to_file(url, bz2_path)
subprocess.run(f'bunzip2 -d {bz2_path}'.split())
return dlib.shape_predictor(path.as_posix())
def _load_encoder(self) -> nn.Module:
ckpt_path = huggingface_hub.hf_hub_download(MODEL_REPO,
'models/encoder.pt')
ckpt = torch.load(ckpt_path, map_location='cpu')
opts = ckpt['opts']
opts['device'] = self.device.type
opts['checkpoint_path'] = ckpt_path
opts = argparse.Namespace(**opts)
model = pSp(opts)
model.to(self.device)
model.eval()
ckpt_path = huggingface_hub.hf_hub_download(MODEL_REPO,
'models/encoder_wplus.pt')
ckpt = torch.load(ckpt_path, map_location='cpu')
opts = ckpt['opts']
opts['device'] = self.device.type
opts['checkpoint_path'] = ckpt_path
opts['output_size'] = 1024
opts = argparse.Namespace(**opts)
model2 = pSp(opts)
model2.to(self.device)
model2.eval()
return {'z+': model, 'w+': model2}
@staticmethod
def _create_transform() -> Callable:
transform = T.Compose([
T.Resize(256),
T.CenterCrop(256),
T.ToTensor(),
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
])
return transform
def _load_generator(self, style_type: str) -> nn.Module:
model = DualStyleGAN(1024, 512, 8, 2, res_index=6)
ckpt_path = huggingface_hub.hf_hub_download(
MODEL_REPO, f'models/{style_type}/generator.pt')
ckpt = torch.load(ckpt_path, map_location='cpu')
model.load_state_dict(ckpt['g_ema'])
model.to(self.device)
model.eval()
return model
@staticmethod
def _load_exstylecode(style_type: str) -> dict[str, np.ndarray]:
if style_type in ['cartoon', 'caricature', 'anime']:
filename = 'refined_exstyle_code.npy'
else:
filename = 'exstyle_code.npy'
path = huggingface_hub.hf_hub_download(
MODEL_REPO, f'models/{style_type}/{filename}')
exstyles = np.load(path, allow_pickle=True).item()
return exstyles
def detect_and_align_face(self, image_path) -> np.ndarray:
image = align_face(filepath=image_path, predictor=self.landmark_model)
x, y = np.random.randint(255), np.random.randint(255)
r, g, b = image.getpixel((x, y))
image.putpixel(
(x, y), (r, g + 1, b)
) # trick to make sure run reconstruct_face() once any input setting changes
return image
@staticmethod
def denormalize(tensor: torch.Tensor) -> torch.Tensor:
return torch.clamp((tensor + 1) / 2 * 255, 0, 255).to(torch.uint8)
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
tensor = self.denormalize(tensor)
return tensor.cpu().numpy().transpose(1, 2, 0)
@torch.inference_mode()
def reconstruct_face(self, image: np.ndarray,
encoder_type: str) -> tuple[np.ndarray, torch.Tensor]:
if encoder_type == 'Z+ encoder (better stylization)':
self.encoder_type = 'z+'
z_plus_latent = True
return_z_plus_latent = True
else:
self.encoder_type = 'w+'
z_plus_latent = False
return_z_plus_latent = False
image = PIL.Image.fromarray(image)
input_data = self.transform(image).unsqueeze(0).to(self.device)
img_rec, instyle = self.encoder_dict[self.encoder_type](
input_data,
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)
img_rec = self.postprocess(img_rec[0])
return img_rec, instyle
@torch.inference_mode()
def generate(self, style_type: str, style_id: int, structure_weight: float,
color_weight: float, structure_only: bool,
instyle: torch.Tensor) -> np.ndarray:
if self.encoder_type == 'z+':
z_plus_latent = True
input_is_latent = False
else:
z_plus_latent = False
input_is_latent = True
generator = self.generator_dict[style_type]
exstyles = self.exstyle_dict[style_type]
style_id = int(style_id)
stylename = list(exstyles.keys())[style_id]
latent = torch.tensor(exstyles[stylename]).to(self.device)
if structure_only and self.encoder_type == 'z+':
latent[0, 7:18] = instyle[0, 7:18]
exstyle = generator.generator.style(
latent.reshape(latent.shape[0] * latent.shape[1],
latent.shape[2])).reshape(latent.shape)
if structure_only and self.encoder_type == 'w+':
exstyle[:, 7:18] = instyle[:, 7:18]
img_gen, _ = generator([instyle],
exstyle,
input_is_latent=input_is_latent,
z_plus_latent=z_plus_latent,
truncation=0.7,
truncation_latent=0,
use_res=True,
interp_weights=[structure_weight] * 7 +
[color_weight] * 11)
img_gen = torch.clamp(img_gen.detach(), -1, 1)
img_gen = self.postprocess(img_gen[0])
return img_gen
|