git clone https://www.modelscope.cn/lskhh/ty_cv_unet_person-image-cartoon-wz_compound-models.git
此模型只训练了15999个epoch,感兴趣可以用代码继续训练,
最佳实践是300000epoch,原repo只用了16999。
本仓库提供DCT-Net王者荣耀风转换模型
在ModelScope框架上,提供输入图片,即可以通过简单的Pipeline调用来使用人像卡通化模型。
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
img_cartoon = pipeline(Tasks.image_portrait_stylization,
model='lskhh/ty_cv_unet_person-image-cartoon-wz_compound-models',model_revision='v1.0.4')
# 图像本地路径
#img_path = 'input.png'
# 图像url链接
img_path = 'https://modelscope.cn/api/v1/studio/lskhh/wz_portrait_stylization/repo?Revision=master&FilePath=images/00066.jpg&View=true'
result = img_cartoon(img_path)
cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
print('finished!')
环境要求:tf1.14/15及兼容cuda,支持GPU训练[这配置折腾了我两天]
import os
import unittest
import cv2
from modelscope.exporters.cv import CartoonTranslationExporter
from modelscope.msdatasets import MsDataset
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.pipelines.base import Pipeline
from modelscope.trainers.cv import CartoonTranslationTrainer
from modelscope.utils.constant import Tasks
from modelscope.utils.test_utils import test_level
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
model_id = 'damo/cv_unet_person-image-cartoon_compound-models'
data_dir = MsDataset.load(
'dctnet_train_wz',
namespace='lskhh',
split='train').config_kwargs['split_config']['train']
# print(data_dir)
# data_dir='./'
data_photo = os.path.join(data_dir, 'traindata_wzry_train/face_photo')
data_cartoon = os.path.join(data_dir, 'traindata_wzry_train/face_cartoon')
# print(data_photo)
# data_photo ='face_photo'
# data_cartoon = 'face_cartoon'
work_dir = 'exp_localtoon'
max_steps = 10000
trainer = CartoonTranslationTrainer(
model=model_id,
work_dir=work_dir,
photo=data_photo,
cartoon=data_cartoon,
max_steps=max_steps)
trainer.train()
上述训练代码仅仅提供简单训练的范例,对大规模自定义数据,替换data_photo为真实人脸数据路径,data_cartoon为卡通风格人脸数据路径,max_steps建议设置为300000,可视化结果将存储在work_dir下;此外configuration.json(~/.cache/modelscope/hub/damo/cv_unet_person-image-cartoon_compound-models/)可以进行自定义修改;
Note: notebook预装环境下存在numpy依赖冲突,可手动更新:pip install numpy==1.18.5
import urllib.request
import json
import os
response = urllib.request.urlopen("http://pvp.qq.com/web201605/js/herolist.json")
hero_json = json.loads(response.read())
hero_num = len(hero_json)
# print(hero_num)
save_dir = './'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
for i in range(hero_num):
# avatar_name = hero_json[i]['ename']
# save_file_name = save_dir + hero_json[i]['cname'] + '-' + str(hero_json[i]['ename']) + '.jpg'
save_file_name = save_dir + str(hero_json[i]['ename']) + '.jpg'
# print(save_file_name)
avatar_url = 'http://game.gtimg.cn/images/yxzj/img201606/heroimg/' + str(hero_json[i]['ename']) + '/' + str(hero_json[i]['ename']) + '.jpg'
# print(avatar_url)
if not os.path.exists(save_file_name):
urllib.request.urlretrieve(avatar_url, save_file_name)
import os
import re
# 定义文件夹路径
folder_path = "./"
# 定义计数器和序号位数
count = 0
num_digits = 5
# 遍历文件夹中的所有文件名
for file_name in os.listdir(folder_path):
# 使用正则表达式从文件名中提取数字
match = re.search(r'\d+', file_name)
if match:
# 提取数字并将其减去105
num = int(match.group(0)) - 105
# 构造新的文件名
new_file_name = str(num).zfill(num_digits) + os.path.splitext(file_name)[1]
# 重命名文件
os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
# 更新计数器
count += 1
print(f"{count} files renamed successfully.")
低质/低分辨率人脸图像由于本身内容信息丢失严重,无法得到理想转换效果,可预先采用人脸增强模型预处理图像解决;
小样本数据涵盖场景有限,人脸暗光、阴影干扰可能会影响生成效果。
训练数据从公开数据集(COCO等)、互联网搜索人像图像,并进行标注作为训练数据。
真实人脸数据FFHQ常用的人脸公开数据集,包含7w人脸图像;
卡通人脸数据,互联网搜集,100+张
微调数据集DCT-Net人像卡通化-王者荣耀头像
如果该模型对你有所帮助,请引用相关的论文:
@inproceedings{men2022domain,
title={DCT-Net: Domain-Calibrated Translation for Portrait Stylization},
author={Men, Yifang and Yao, Yuan and Cui, Miaomiao and Lian, Zhouhui and Xie, Xuansong},
journal={ACM Transactions on Graphics (TOG)},
volume={41},
number={4},
pages={1--9},
year={2022}
}