需要一張源臉和<= 5張目標臉照片 https://github.com/shaoanlu/fewshot-face-translation-GAN clone
git clone --recursive https://github.com/shaoanlu/fewshot-face-translation-GAN.git
需要套件
pip install Keras==2.2.4
pip install tensorflow==1.13.1
or
pip install tensorflow==1.12.0
or
pip install tensorflow-gpu==1.13.1
pip install opencv-python
pip install matplotlib
下載模型
https://drive.google.com/uc?id=1DUMmZGTGKMyEYSKy-w34IDHawVF24rIs
https://drive.google.com/uc?id=1xl8cg7xaRnMsyiODcXguJ83d5hwodckB
mkdir weights && \
mv decoder.h5 weights/decoder.h5 && \
mv encoder.h5 weights/encoder.h5
錯誤
model.load_weights AttributeError: 'str' object has no attribute 'decode'
解決
pip install h5py==2.10 -i https://pypi.doubanio.com/simple
測試code
import os
import warnings
warnings.filterwarnings("ignore")
from models import FaceTranslationGANInferenceModel
model = FaceTranslationGANInferenceModel()
from face_toolbox_keras.models.verifier.face_verifier import FaceVerifier
fv = FaceVerifier(classes=512)
from face_toolbox_keras.models.parser import face_parser
fp = face_parser.FaceParser()
from face_toolbox_keras.models.detector import face_detector
fd = face_detector.FaceAlignmentDetector()
from face_toolbox_keras.models.detector.iris_detector import IrisDetector
idet = IrisDetector()
#idet.set_detector(fd)
# Translate faces
import numpy as np
from utils import utils
from matplotlib import pyplot as plt
from PIL import Image
fn_src = ["data/go.jpg"]
if len(fn_src) >= 1:
fn_src = fn_src[0]
# fns_tar = ["data/1.jpg","data/2.jpg","data/3.jpg","data/4.jpg","data/5.jpg"]
def conversion(fn_src,fns_tar,path):
fns_file = []
for file in fns_tar:
if(file.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
fns_file.append(path+file)
src, mask, aligned_im, (x0, y0, x1, y1), landmarks = utils.get_src_inputs(fn_src, fd, fp, idet)
tar, emb_tar = utils.get_tar_inputs(fns_file, fd, fv)
out = model.inference(src, mask, tar, emb_tar)
result_face = np.squeeze(((out[0] + 1) * 255 / 2).astype(np.uint8))
result_img = utils.post_process_result(fn_src, fd, result_face, aligned_im, src, x0, y0, x1, y1, landmarks)
# im = Image.fromarray(result_img)
# im.save("data/output.jpg")
print("")
save(src,path+"output/src.jpg")
save(tar,path+"output/tar.jpg")
save(result_face,path+"output/result_face.jpg")
save(result_img,path+"output/result_img.jpg")
def save(img,name):
im = Image.fromarray(img)
im.save(name)
# david
fns_tar = os.listdir("data/adam")
conversion(fn_src,fns_tar,"data/adam/")
print("")
fn_src = "data/test.jpg"
源頭
https://paperswithcode.com/paper/few-shot-unsupervised-image-to-image