|
import os |
|
from PIL import Image |
|
import torch |
|
import gradio as gr |
|
import torch |
|
torch.backends.cudnn.benchmark = True |
|
from torchvision import transforms, utils |
|
from util import * |
|
from PIL import Image |
|
import math |
|
import random |
|
import numpy as np |
|
from torch import nn, autograd, optim |
|
from torch.nn import functional as F |
|
from tqdm import tqdm |
|
import lpips |
|
from model import * |
|
import urllib.request |
|
|
|
|
|
|
|
from copy import deepcopy |
|
import imageio |
|
|
|
import os |
|
import sys |
|
import numpy as np |
|
from PIL import Image |
|
import torch |
|
import torchvision.transforms as transforms |
|
from argparse import Namespace |
|
from e4e.models.psp import pSp |
|
from util import * |
|
from huggingface_hub import hf_hub_download |
|
|
|
device= 'cpu' |
|
model_path_e = hf_hub_download(repo_id="aijack/e4e", filename="e4e.pt") |
|
ckpt = torch.load(model_path_e, map_location='cpu') |
|
opts = ckpt['opts'] |
|
opts['checkpoint_path'] = model_path_e |
|
opts= Namespace(**opts) |
|
net = pSp(opts, device).eval().to(device) |
|
|
|
img_url = "http://claireye.com.tw/img/230212a.jpg" |
|
urllib.request.urlretrieve(img_url, "pose.jpg") |
|
@ torch.no_grad() |
|
def projection(img, name, device='cuda'): |
|
|
|
|
|
transform = transforms.Compose( |
|
[ |
|
transforms.Resize(256), |
|
transforms.CenterCrop(256), |
|
transforms.ToTensor(), |
|
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]), |
|
] |
|
) |
|
img = transform(img).unsqueeze(0).to(device) |
|
images, w_plus = net(img, randomize_noise=False, return_latents=True) |
|
result_file = {} |
|
result_file['latent'] = w_plus[0] |
|
torch.save(result_file, name) |
|
return w_plus[0] |
|
|
|
|
|
|
|
|
|
device = 'cpu' |
|
|
|
|
|
latent_dim = 512 |
|
|
|
model_path_s = hf_hub_download(repo_id="aijack/stylegan2", filename="stylegan2.pt") |
|
original_generator = Generator(1024, latent_dim, 8, 2).to(device) |
|
ckpt = torch.load(model_path_s, map_location=lambda storage, loc: storage) |
|
original_generator.load_state_dict(ckpt["g_ema"], strict=False) |
|
mean_latent = original_generator.mean_latent(10000) |
|
|
|
generatorjojo = deepcopy(original_generator) |
|
|
|
generatorcaitlyn = deepcopy(original_generator) |
|
|
|
generatorart = deepcopy(original_generator) |
|
|
|
generatorsketch = deepcopy(original_generator) |
|
|
|
|
|
transform = transforms.Compose( |
|
[ |
|
transforms.Resize((1024, 1024)), |
|
transforms.ToTensor(), |
|
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), |
|
] |
|
) |
|
|
|
|
|
|
|
|
|
modeljojo = hf_hub_download(repo_id="aijack/jojo", filename="jojo.pt") |
|
|
|
|
|
ckptjojo = torch.load(modeljojo, map_location=lambda storage, loc: storage) |
|
generatorjojo.load_state_dict(ckptjojo["g"], strict=False) |
|
|
|
modelcaitlyn = hf_hub_download(repo_id="aijack/arcane", filename="arcane.pt") |
|
|
|
ckptcaitlyn = torch.load(modelcaitlyn, map_location=lambda storage, loc: storage) |
|
generatorcaitlyn.load_state_dict(ckptcaitlyn["g"], strict=False) |
|
|
|
modelart = hf_hub_download(repo_id="aijack/art", filename="art.pt") |
|
|
|
ckptart = torch.load(modelart, map_location=lambda storage, loc: storage) |
|
generatorart.load_state_dict(ckptart["g"], strict=False) |
|
|
|
|
|
modelSketch = hf_hub_download(repo_id="aijack/sketch", filename="sketch.pt") |
|
|
|
ckptsketch = torch.load(modelSketch, map_location=lambda storage, loc: storage) |
|
generatorsketch.load_state_dict(ckptsketch["g"], strict=False) |
|
|
|
def inference(img, model): |
|
img.save('out.jpg') |
|
aligned_face = align_face('out.jpg') |
|
|
|
my_w = projection(aligned_face, "test.pt", device).unsqueeze(0) |
|
if model == 'JoJo': |
|
with torch.no_grad(): |
|
my_sample = generatorjojo(my_w, input_is_latent=True) |
|
elif model == 'Caitlyn': |
|
with torch.no_grad(): |
|
my_sample = generatorcaitlyn(my_w, input_is_latent=True) |
|
elif model == 'Art': |
|
with torch.no_grad(): |
|
my_sample = generatorart(my_w, input_is_latent=True) |
|
else: |
|
with torch.no_grad(): |
|
my_sample = generatorsketch(my_w, input_is_latent=True) |
|
|
|
|
|
npimage = my_sample[0].permute(1, 2, 0).detach().numpy() |
|
imageio.imwrite('filename.jpeg', npimage) |
|
return 'filename.jpeg' |
|
|
|
title = "JoJoGAN" |
|
description = "Gradio Demo for JoJoGAN: One Shot Face Stylization. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below." |
|
|
|
article = "<p style='text-align: center'><a href='http://claireye.com.tw'>Claireye</a> | 2023</p>" |
|
|
|
examples=[['pose.jpg','Art']] |
|
gr.Interface(inference, [gr.inputs.Image(type="pil"),gr.inputs.Dropdown(choices=['JoJo', 'Caitlyn','Art','Sketch'], type="value", default='JoJo', label="Model")], gr.outputs.Image(type="numpy"),title=title,description=description,article=article,allow_flagging=False,examples=examples,allow_screenshot=False).launch() |
|
|