上期视频我们分享了马斯克版本的钢铁侠,本期图文介绍下如何实现

AI换脸

上期视频,我们分享了一个让马斯克体验当钢铁侠的视频,其中使用到了AEI-NetSberSwap换脸技术,也许,你没有听说过SberSwap,但是大名鼎鼎的deepfake你肯定听说过。

什么是SberSwap

下图是一个名为AEI-Net的 SberSwap 模型图,由三部分组成。

第一个是身份编码器,它从图像Xs中找到向量Zid,与将Xs嵌入描述图像中人脸身份的空间有关的编码器。

第二种是Multi-level Attributes Encoderd,其结构与 U-Net 相同,从图像Xt中提取特征Zatt。与将X emb嵌入到空间中有关的编码器,该空间描述了交换面部时要保留的属性。

第三个是AAD 生成器,它根据这些信息生成所需的图像。

打开网易新闻 查看更多图片

AEI-Net的体系结构

身份编码器

该子网将源图像Xs投影到低维特征空间。提取人脸的不同信息,例如眼睛的形状大小,眼睛与嘴巴之间的距离,嘴巴的弯曲度等等。

身份编码器

AADGenerator

AAD Generator是“ Adaptive Attentional Denormalization Generator”的缩写。它以提高的空间分辨率集成了前两个子网的输出,以生成AEI-Net的最终输出。

打开网易新闻 查看更多图片

AAD Generator

AAD(Adaptive Attentional Denormalization)生成器用于将和中的信息结合起来生成,其主要结构是AAD层,见下图(c),其输入为

AAD

——2——

代码实现SberSwap

目标图片

在开始运行代码之前,需要找一张你需要转换人脸的图片以及源图片,或者源视频

代码支持图片转换以及视频转换,然后clone代码

第一步:clone代码,并安装下载预训练模型

!git clone https://github.com/cedro3/sber-swap.git%cd sber-swap#加载模型
!wget -P ./arcface_model https://github.com/sberbank-ai/sber-swap/releases/download/arcface/backbone.pth!wget -P ./arcface_model https://github.com/sberbank-ai/sber-swap/releases/download/arcface/iresnet.py# 加载检测器
!wget -P ./insightface_func/models/antelope https://github.com/sberbank-ai/sber-swap/releases/download/antelope/glintr100.onnx!wget -P ./insightface_func/models/antelope https://github.com/sberbank-ai/sber-swap/releases/download/antelope/scrfd_10g_bnkps.onnx# 加载模型
!wget -P ./weights https://github.com/sberbank-ai/sber-swap/releases/download/sber-swap-v2.0/G_unet_2blocks.pth!wget -P ./weights https://github.com/sberbank-ai/sber-swap/releases/download/super-res/10_net_G.pth#安装第三方库
!pip install mxnet-cu101mkl
!pip install onnxruntime-gpu==1.8!pip install insightface==0.2.1!pip install kornia==0.5.4# import第三方库import cv2import torchimport timeimport os
from utils.inference.image_processing import crop_face, get_final_image, show_images
from utils.inference.video_processing import read_video, get_target, get_final_video, add_audio_from_another_video, face_enhancement
from utils.inference.core import model_inference
from network.AEI_Net import AEI_Net
from coordinate_reg.image_infer import Handler
from insightface_func.face_detect_crop_multi import Face_detect_crop
from arcface_model.iresnet import iresnet100
from models.pix2pix_model import Pix2PixModel
from models.config_sr import TestOptions

打开网易新闻 查看更多图片

AI换脸

python代码之前,请clone源代码,并安装模型需要的第三方库,并导入相关的库

第二步:模型初始化

# 初始化模型,并加载模型app = Face_detect_crop(name='antelope', root='./insightface_func/models')
app.prepare(ctx_id= 0, det_thresh=0.6, det_size=(640,640))
G = AEI_Net(backbone='unet', num_blocks=2, c_id=512)
G.eval()
G.load_state_dict(torch.load('weights/G_unet_2blocks.pth', map_location=torch.device('cpu')))
G = G.cuda()
G = G.half()
netArc = iresnet100(fp16=False)
netArc.load_state_dict(torch.load('arcface_model/backbone.pth'))
netArc=netArc.cuda()
netArc.eval()
handler = Handler('./coordinate_reg/model/2d106det', 0, ctx_id=0, det_size=640)
use_sr = Trueif use_sr:
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
torch.backends.cudnn.benchmark = True
opt = TestOptions()
model = Pix2PixModel(opt)
model.netG.train()

初始化模型,加载预训练模型,初始化完成后,我们就可以输入自己的图片或者视频进行AI人脸转换了

AI换脸

第三步:图片AI换脸

source = 'source.jpg' #输入源图片target = 'targer.jpg' #输入目标需要转换的图片source_full = cv2.imread(source)
crop_size = 224
batch_size = 40
source = crop_face(source_full, app, crop_size)[0]
source = [source[:, :, ::-1]]
target_full = cv2.imread(target)
full_frames = [target_full]
target = get_target(full_frames, app, crop_size)

final_frames_list, crop_frames_list, full_frames, tfm_array_list =
model_inference(full_frames,source,target,netArc, G,app,set_target = False,crop_size=crop_size,BS=batch_size)
result = get_final_image(final_frames_list, crop_frames_list, full_frames[0], tfm_array_list, handler)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)

这里加载2张图片,一张原始图片,一张需要AI换脸的图片,通过模型后,便可以得到AI换脸后的图片了

AI换脸

第四步:视频AI换脸

source = 'source.jpg' #需要换脸的图片video = 'test.mp4' #目标视频source_full = cv2.imread(source)
result = "result.mp4"crop_size = 224
batch_size = 40
source = crop_face(source_full, app, crop_size)[0]
source = [source[:, :, ::-1]]
full_frames, fps = read_video(video)
target = get_target(full_frames, app, crop_size)

final_frames_list, crop_frames_list, full_frames, tfm_array_list =
model_inference(full_frames,source,target,netArc, G, app, set_target = False,crop_size=crop_size, BS=batch_size)
if use_sr:
final_frames_list = face_enhancement(final_frames_list, model)
get_final_video(final_frames_list, crop_frames_list, full_frames, tfm_array_list,result,fps, handler)
add_audio_from_another_video(video, result, "audio")
代码源链接https://github.com/ai-forever/sber-swap