ArcaneGan github
예전에 아케인 롤 애니메이션이 나왔다.
나오기 전에 ArcaneGAN이라는 기술을 누가 만들었고 이 기술을 이미지에 적용하면
롤 애니메이션에 나온 그림체로 바뀐다.
#@title Install and download. Run once.
#release v0.2
!wget https://github.com/Sxela/ArcaneGAN/releases/download/v0.1/ArcaneGANv0.1.jit
!wget https://github.com/Sxela/ArcaneGAN/releases/download/v0.2/ArcaneGANv0.2.jit
!pip -qq install facenet_pytorch
2분정도 걸렸다.
추가) facenet_pytorch 다운로드할때 에러발생한다
종속성문제로 다른것들의 버전을바꿔줘야한다.
# Upgrading/downgrading torch and pillow to the compatible versions
!pip install torch==2.3.0 pillow==10.0.0
# Reinstalling facenet_pytorch
!pip install facenet_pytorch
#@title Install and download. Run once.
#release v0.2
!wget https://github.com/Sxela/ArcaneGAN/releases/download/v0.1/ArcaneGANv0.1.jit
!wget https://github.com/Sxela/ArcaneGAN/releases/download/v0.2/ArcaneGANv0.2.jit
다운로드 끝나면 세션 다시시작
그리고 이미지 처리할 함수 선언
#@title Define functions
#@markdown Select model version and run.
from facenet_pytorch import MTCNN
from torchvision import transforms
import torch, PIL
from tqdm.notebook import tqdm
mtcnn = MTCNN(image_size=256, margin=80)
# simplest ye olde trustworthy MTCNN for face detection with landmarks
def detect(img):
# Detect faces
batch_boxes, batch_probs, batch_points = mtcnn.detect(img, landmarks=True)
# Select faces
if not mtcnn.keep_all:
batch_boxes, batch_probs, batch_points = mtcnn.select_boxes(
batch_boxes, batch_probs, batch_points, img, method=mtcnn.selection_method
)
return batch_boxes, batch_points
# my version of isOdd, should make a separate repo for it :D
def makeEven(_x):
return _x if (_x % 2 == 0) else _x+1
# the actual scaler function
def scale(boxes, _img, max_res=1_500_000, target_face=256, fixed_ratio=0, max_upscale=2, VERBOSE=False):
x, y = _img.size
ratio = 2 #initial ratio
#scale to desired face size
if (boxes is not None):
if len(boxes)>0:
ratio = target_face/max(boxes[0][2:]-boxes[0][:2]);
ratio = min(ratio, max_upscale)
if VERBOSE: print('up by', ratio)
if fixed_ratio>0:
if VERBOSE: print('fixed ratio')
ratio = fixed_ratio
x*=ratio
y*=ratio
#downscale to fit into max res
res = x*y
if res > max_res:
ratio = pow(res/max_res,1/2);
if VERBOSE: print(ratio)
x=int(x/ratio)
y=int(y/ratio)
#make dimensions even, because usually NNs fail on uneven dimensions due skip connection size mismatch
x = makeEven(int(x))
y = makeEven(int(y))
size = (x, y)
return _img.resize(size)
"""
A useful scaler algorithm, based on face detection.
Takes PIL.Image, returns a uniformly scaled PIL.Image
boxes: a list of detected bboxes
_img: PIL.Image
max_res: maximum pixel area to fit into. Use to stay below the VRAM limits of your GPU.
target_face: desired face size. Upscale or downscale the whole image to fit the detected face into that dimension.
fixed_ratio: fixed scale. Ignores the face size, but doesn't ignore the max_res limit.
max_upscale: maximum upscale ratio. Prevents from scaling images with tiny faces to a blurry mess.
"""
def scale_by_face_size(_img, max_res=1_500_000, target_face=256, fix_ratio=0, max_upscale=2, VERBOSE=False):
boxes = None
boxes, _ = detect(_img)
if VERBOSE: print('boxes',boxes)
img_resized = scale(boxes, _img, max_res, target_face, fix_ratio, max_upscale, VERBOSE)
return img_resized
size = 256
means = [0.485, 0.456, 0.406]
stds = [0.229, 0.224, 0.225]
t_stds = torch.tensor(stds).cuda().half()[:,None,None]
t_means = torch.tensor(means).cuda().half()[:,None,None]
def makeEven(_x):
return int(_x) if (_x % 2 == 0) else int(_x+1)
img_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(means,stds)])
def tensor2im(var):
return var.mul(t_stds).add(t_means).mul(255.).clamp(0,255).permute(1,2,0)
def proc_pil_img(input_image, model):
transformed_image = img_transforms(input_image)[None,...].cuda().half()
with torch.no_grad():
result_image = model(transformed_image)[0]; print(result_image.shape)
output_image = tensor2im(result_image)
output_image = output_image.detach().cpu().numpy().astype('uint8')
output_image = PIL.Image.fromarray(output_image)
return output_image
#load model
version = '0.2' #@param ['0.1','0.2']
model_path = f'/content/ArcaneGANv{version}.jit'
in_dir = '/content/in'
out_dir = f"/content/{model_path.split('/')[-1][:-4]}_out"
model = torch.jit.load(model_path).eval().cuda().half()
#setup colab interface
from google.colab import files
import ipywidgets as widgets
from IPython.display import clear_output
from IPython.display import display
import os
from glob import glob
def reset(p):
with output_reset:
clear_output()
clear_output()
process()
button_reset = widgets.Button(description="Upload")
output_reset = widgets.Output()
button_reset.on_click(reset)
def fit(img,maxsize=512):
maxdim = max(*img.size)
if maxdim>maxsize:
ratio = maxsize/maxdim
x,y = img.size
size = (int(x*ratio),int(y*ratio))
img = img.resize(size)
return img
def show_img(f, size=1024):
display(fit(PIL.Image.open(f),size))
def process(upload=True):
os.makedirs(in_dir, exist_ok=True)
%cd {in_dir}/
!rm -rf {out_dir}/*
os.makedirs(out_dir, exist_ok=True)
in_files = sorted(glob(f'{in_dir}/*'))
if (len(in_files)==0) | (upload):
!rm -rf {in_dir}/*
uploaded = files.upload()
if len(uploaded.keys())<=0:
print('\nNo files were uploaded. Try again..\n')
return
print('\nPress the button and pick some photos to upload\n')
in_files = sorted(glob(f'{in_dir}/*'))
for img in tqdm(in_files):
out = f"{out_dir}/{img.split('/')[-1].split('.')[0]}.jpg"
im = PIL.Image.open(img)
im = scale_by_face_size(im, target_face=300, max_res=1_500_000, max_upscale=2)
res = proc_pil_img(im, model)
res.save(out)
out_zip = f"{out_dir}.zip"
!zip {out_zip} {out_dir}*
processed = sorted(glob(f'{out_dir}/*'))[:3]
for f in processed:
show_img(f, 256)