其他
只要一张图片,秒变迪士尼角色!编程教学详解,你也能生成专属于你的迪士尼脸
目录
GAN StyleGAN 图层交换的Toonification 通过一阶运动进行动画处理 教程
GAN
StyleGAN
Editing in Style: Uncovering the Local Semantics of GANs - Crossminds
StyleGAN网络混合
一阶运动模型
教程
% tensorflow_version 1.x
!git clone %cd stylegan2!nvcc test_nvcc.cu -o test_nvcc -run!mkdir raw!mkdir aligned!mkdir generate
import pretrained_networks # use my copy of the blended model to save Doron's download bandwidth # get the original here https://mega.nz/folder/OtllzJwa#C947mCCdEfMCRTWnDcs4qw blended_url = "https://drive.google.com/uc?id=1H73TfV5gQ9ot7slSed_l-lim9X7pMRiU" ffhq_url = "http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-ffhq-config-f.pkl" _, _, Gs_blended = pretrained_networks.load_networks(blended_url) _, _, Gs = pretrained_networks.load_networks(ffhq_url)
!python align_images.py raw aligned
!python project_images.py --num-steps 500 aligned generated
现在我们已经有了潜在的向量,我们可以尝试使用我们的混合迪斯尼模型来生成人脸。
import numpy as np
from PIL import Image
import dnnlib
import dnnlib.tflib as tflib
from pathlib import Path
latent_dir = Path("generated")
latents = latent_dir.glob("*.npy")
for latent_file in latents:
latent = np.load(latent_file)
latent = np.expand_dims(latent,axis=0)
synthesis_kwargs = dict(output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=False), minibatch_size=8)
images = Gs_blended.components.synthesis.run(latent, randomize_noise=False, **synthesis_kwargs)
Image.fromarray(images.transpose((0,2,3,1))[0], 'RGB').save(latent_file.parent / (f"{latent_file.stem}-toon.jpg"))
生成的图像保存在生成的文件夹中。我们可以在笔记本里面显示图像。
from IPython.display import Image embedded = Image(filename="generated/example_01.png", width=256) display(embedded) tooned = Image(filename="generated/example_01-toon.jpg", width=256) display(tooned)
首先,让我们将Aliaksanr的Repo复制到一阶模型上。
!git clone https://github.com/AliaksandrSiarohin/first-order-model
然后,我们将设置一个路径,这样我们就不必在一阶模型目录中用python导入,或者你
可以直接cd到该目录。
try: # set up path
import sys
sys.path.append('/content/stylegan2/first-order-model')
print('Path added')
except Exception as e:
print(e)
pass
然后,在我们加载关键点和视频生成器模型之前,我们需要先下载预先训练好的权重。这个文件相当大,约等于 700mb,你可能需要手动下载它,因为谷歌不允许wget下载大文件。
!wget "https://drive.google.com/uc?export=download&id=1jmcn19-c3p8mf39aYNXUhdMqzqDYZhQ_" -O vox-cpk.pth.tar
使用刚才下载的权重加载一阶模型。
from demo import load_checkpoints generator, kp_detector = load_checkpoints(config_path='first-order-model/config/vox-256.yaml', checkpoint_path='vox-cpk.pth.tar')
接下来,我们需要一个驱动视频,这个视频来自动画的来源。您可以使用示例视频或上传自己的视频。如果你上传了一个视频,请确保修改了相应的文件路径。
!wget https://drive.google.com/uc?id=1LjDoFmeP0hZQSsUmnou0UbQJJzQ8rMLR -O src_video.mp4
import imageio
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from skimage.transform import resize
from IPython.display import HTML
from demo import make_animation
from skimage import img_as_ubyte
import warnings
warnings.filterwarnings("ignore")
source_image = imageio.imread('generated/example_01-toon.jpg')
reader = imageio.get_reader('src_video.mp4')
#Resize image and video to 256x256
source_image = resize(source_image, (256, 256))[..., :3]
fps = reader.get_meta_data()['fps']
driving_video = []
try:
for im in reader:
driving_video.append(im)
except RuntimeError:
pass
reader.close()
driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
# Generate animation
predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True)
def display(source, driving, generated=None):
fig = plt.figure(figsize=(8 + 4 * (generated is not None), 6))
ims = []
for i in range(len(driving)):
cols = [source]
cols.append(driving[i])
if generated is not None:
cols.append(generated[i])
im = plt.imshow(np.concatenate(cols, axis=1), animated=True)
plt.axis('off')
ims.append([im])
ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000)
plt.close()
return ani
HTML(display(source_image, driving_video, predictions).to_html5_video())
拓展
来源:AI研习社译者:惠蕾_EvaHui
相关阅读:
Google前CEO施密特:必须“不惜一切代价”在AI领域击败中国