JoJoGAN / app.py
Ahsen Khaliq
Update app.py
a17e553
raw
history blame
4.52 kB
import os
from PIL import Image
import torch
import gradio as gr
os.system("pip install dlib")
os.system("git clone https://github.com/mchong6/JoJoGAN.git")
os.chdir("JoJoGAN")
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 *
from e4e_projection import projection as e4e_projection
from copy import deepcopy
os.makedirs('inversion_codes', exist_ok=True)
os.makedirs('style_images', exist_ok=True)
os.makedirs('style_images_aligned', exist_ok=True)
os.makedirs('models', exist_ok=True)
os.system("wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
os.system("bzip2 -dk shape_predictor_68_face_landmarks.dat.bz2")
os.system("mv shape_predictor_68_face_landmarks.dat models/dlibshape_predictor_68_face_landmarks.dat")
device = 'cpu'
os.system("gdown https://drive.google.com/uc?id=1Yr7KuD959btpmcKGAUsbAk5rPjX2MytK")
os.system("mv stylegan2-ffhq-config-f.pt models/stylegan2-ffhq-config-f.pt")
latent_dim = 512
# Load original generator
original_generator = Generator(1024, latent_dim, 8, 2).to(device)
ckpt = torch.load(os.path.join('models', 'stylegan2-ffhq-config-f.pt'), map_location=lambda storage, loc: storage)
original_generator.load_state_dict(ckpt["g_ema"], strict=False)
mean_latent = original_generator.mean_latent(10000)
# to be finetuned generator
generator = 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)),
]
)
plt.rcParams['figure.dpi'] = 150
os.system("gdown https://drive.google.com/uc?id=1o6ijA3PkcewZvwJJ73dJ0fxhndn0nnh7")
os.system("mv e4e_ffhq_encode.pt models/e4e_ffhq_encode.pt")
os.system("gdown https://drive.google.com/uc?id=13cR2xjIBj8Ga5jMO7gtxzIJj2PDsBYK4")
os.system("mv e4e_ffhq_encode.pt models/jojo.pt")
os.system("gdown https://drive.google.com/uc?id=1ZRwYLRytCEKi__eT2Zxv1IlV6BGVQ_K2")
os.system("mv e4e_ffhq_encode.pt models/jojo_preserve_color.pt")
def inference(img):
filepath = img
name = strip_path_extension(filepath)+'.pt'
aligned_face = align_face(filepath)
my_w = e4e_projection(aligned_face, name, device).unsqueeze(0)
plt.rcParams['figure.dpi'] = 150
pretrained = 'jojo' #@param ['supergirl', 'arcane_jinx', 'arcane_caitlyn', 'jojo_yasuho', 'jojo', 'disney']
#@markdown Preserve color tries to preserve color of original image by limiting family of allowable transformations. Otherwise, the stylized image will inherit the colors of the reference images, leading to heavier stylizations.
preserve_color = False #@param{type:"boolean"}
ckpt = torch.load(os.path.join('models', 'jojo.pt'), map_location=lambda storage, loc: storage)
generator.load_state_dict(ckpt["g"], strict=False)
with torch.no_grad():
generator.eval()
original_my_sample = original_generator(my_w, input_is_latent=True)
my_sample = generator(my_w, input_is_latent=True)
return
my_output = torch.cat([style_image, face, my_sample], 0)
return my_output
title = "AnimeGANv2"
description = "Gradio Demo for AnimeGanv2 Face Portrait. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please use a cropped portrait picture for best results similar to the examples below."
article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center> <p style='text-align: center'>samples from repo: <img src='https://user-images.githubusercontent.com/26464535/129888683-98bb6283-7bb8-4d1a-a04a-e795f5858dcf.gif' alt='animation'/> <img src='https://user-images.githubusercontent.com/26464535/137619176-59620b59-4e20-4d98-9559-a424f86b7f24.jpg' alt='animation'/><img src='https://user-images.githubusercontent.com/26464535/127134790-93595da2-4f8b-4aca-a9d7-98699c5e6914.jpg' alt='animation'/></p>"
gr.Interface(inference, [gr.inputs.Image(type="file")], gr.outputs.Image(type="pil"),title=title,description=description,article=article,enable_queue=True,allow_flagging=False).launch()