传统的照片转漫画,使用边缘检测、双边滤波器和降采样,得到图像如下,可以看到,噪点很多,有些关键线条也没有展现出来。
本次采用GAN,GAN网络使用的方法是根据图像对去不断地学习,如输入图像1和对应已有的漫画B,GAN网络从图片1中获取关键特征,不停地生成一张图像C,当C与B的差值很小时停止,当有很多这样地图像对时,我们就有了一个模型。输入一张图像,就可以生成一张对应地漫画图像,我这次使用的GAN(White-box Cartoon)生成。生成效果:


原始图片大小建议为256*256像素,程序中使用的模型请在文末地址下载

完整程序代码


import os
import cv2
import torch
import numpy as np
import torch.nn as nnclass ResBlock(nn.Module):def __init__(self, num_channel):super(ResBlock, self).__init__()self.conv_layer = nn.Sequential(nn.Conv2d(num_channel, num_channel, 3, 1, 1),nn.BatchNorm2d(num_channel),nn.ReLU(inplace=True),nn.Conv2d(num_channel, num_channel, 3, 1, 1),nn.BatchNorm2d(num_channel))self.activation = nn.ReLU(inplace=True)def forward(self, inputs):output = self.conv_layer(inputs)output = self.activation(output + inputs)return outputclass DownBlock(nn.Module):def __init__(self, in_channel, out_channel):super(DownBlock, self).__init__()self.conv_layer = nn.Sequential(nn.Conv2d(in_channel, out_channel, 3, 2, 1),nn.BatchNorm2d(out_channel),nn.ReLU(inplace=True),nn.Conv2d(out_channel, out_channel, 3, 1, 1),nn.BatchNorm2d(out_channel),nn.ReLU(inplace=True))def forward(self, inputs):output = self.conv_layer(inputs)return outputclass UpBlock(nn.Module):def __init__(self, in_channel, out_channel, is_last=False):super(UpBlock, self).__init__()self.is_last = is_lastself.conv_layer = nn.Sequential(nn.Conv2d(in_channel, in_channel, 3, 1, 1),nn.BatchNorm2d(in_channel),nn.ReLU(inplace=True),nn.Upsample(scale_factor=2),nn.Conv2d(in_channel, out_channel, 3, 1, 1))self.act = nn.Sequential(nn.BatchNorm2d(out_channel),nn.ReLU(inplace=True))self.last_act = nn.Tanh()def forward(self, inputs):output = self.conv_layer(inputs)if self.is_last:output = self.last_act(output)else:output = self.act(output)return outputclass SimpleGenerator(nn.Module):def __init__(self, num_channel=32, num_blocks=4):super(SimpleGenerator, self).__init__()self.down1 = DownBlock(3, num_channel)self.down2 = DownBlock(num_channel, num_channel*2)self.down3 = DownBlock(num_channel*2, num_channel*3)self.down4 = DownBlock(num_channel*3, num_channel*4)res_blocks = [ResBlock(num_channel*4)]*num_blocksself.res_blocks = nn.Sequential(*res_blocks)self.up1 = UpBlock(num_channel*4, num_channel*3)self.up2 = UpBlock(num_channel*3, num_channel*2)self.up3 = UpBlock(num_channel*2, num_channel)self.up4 = UpBlock(num_channel, 3, is_last=True)def forward(self, inputs):down1 = self.down1(inputs)down2 = self.down2(down1)down3 = self.down3(down2)down4 = self.down4(down3)down4 = self.res_blocks(down4)up1 = self.up1(down4)up2 = self.up2(up1+down3)up3 = self.up3(up2+down2)up4 = self.up4(up3+down1)return up4
weight = torch.load('weight.pth', map_location='cpu')
model = SimpleGenerator()
model.load_state_dict(weight)
model.eval()img = cv2.imread(r'input.jpg')image = img/127.5 - 1
image = image.transpose(2, 0, 1)
image = torch.tensor(image).unsqueeze(0)
output = model(image.float())
output = output.squeeze(0).detach().numpy()
output = output.transpose(1, 2, 0)
output = (output + 1) * 127.5
output = np.clip(output, 0, 255).astype(np.uint8)
cv2.imwrite('output.jpg', output)

完整程序包含模型weight.pth文件,下载地址:https://pan.baidu.com/s/18bdlX06imWsjnOxKzdYYrg,提取码获取请关注公众号:Python代码大全,并回复:照片转漫画提取码。

Python将头像照片转换为漫画,采用GAN深度学习,无噪点相关推荐

  1. Deep Learning with Python 读书笔记6.26 I 什么是深度学习

    我感觉这样记录,对于我来说挺好的.因为我看两端对齐的语句容易走神,这样记录阅读的话,就很少出现之前的情况. 我写的初衷,也是自己来看,所以感觉写的不好的,请保留下意见,谢谢. 里面的每一个字我都看过, ...

  2. 基于深度学习的恶意样本行为检测(含源码) ----采用CNN深度学习算法对Cuckoo沙箱的动态行为日志进行检测和分类...

    from:http://www.freebuf.com/articles/system/182566.html 0×01 前言 目前的恶意样本检测方法可以分为两大类:静态检测和动态检测.静态检测是指并 ...

  3. C++调用Python文件,TensorFlow和PyTorch构建的深度学习模型,无法使用GPU的情况分析。

    C++调用Python深度学习模型,包含TensorFlow和PyTorch等构造的模型,然后使用GPU出现问题.包含C++调用Python函数,C++加载模型到GPU,GPU内存占用过大,计算完毕内 ...

  4. python与医学图像处理_医学图像处理与深度学习(一)

    从本文开始,作者将开始介绍图像处理的基础知识,基本的医学图像数据,并且对这些数据进行可视化处理. 利用深度学习技术,分析图像与视频,并且将之应用在诸如自动驾驶,无人机等等领域已经成为最新研究方向.在最 ...

  5. python barrier option pricing_Python王牌加速库:深度学习下的障碍期权定价!

    蒙特卡罗模拟需要数以百万计的路径来得到精确的答案,这需要大量的计算.Ryan等人得研究表明,可以训练深度学习模型对衍生品进行估值.深度学习模型是准确和快速的,能够产生比传统模型快一百万倍的估值.在今天 ...

  6. 免费教材丨第55期:Python机器学习实践指南、Tensorflow 实战Google深度学习框架

    小编说  时间过的好快啊,小伙伴们是不是都快进入寒假啦?但是学习可不要落下哦!  本期教材  本期为大家发放的教材为:<Python机器学习实践指南>.<Tensorflow 实战G ...

  7. 采用keras深度学习框架搭建卷积神经网络模型实现垃圾分类,基于树莓派上进行实时视频流的垃圾识别源代码

    一.项目概述 简介:该垃圾分类项目主要在于对各种垃圾进行所属归类,本次项目采用keras深度学习框架搭建卷积神经网络模型实现图像分类,最终移植在树莓派上进行实时视频流的垃圾识别. 前期:主要考虑PC端 ...

  8. [转载] python实现语义分割_使用Keras实现深度学习中的一些语义分割模型

    参考链接: Keras中的深度学习-数据预处理 Keras-Sematic-Segmentation 使用Keras实现深度学习中的一些语义分割模型. 配置 tensorflow 1.13.1+ten ...

  9. python机器视觉车牌识别_2车牌识别与深度学习

    清华编程高手尹成带你用python大战机器学习 机器学习是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或 ...

  10. python风格变换图片_图片风格转换--深度学习介绍

    前言 先举个机器学习的应用例子:图片的风格转换. 原图.jpg 处理后的图.jpg 机器学习 通过计算机强大的计算能力进行迭代运算.试错得到相关知识. 形象生动的描述请看:机器学习 深度学习 神经网络 ...

最新文章

  1. MySQL数据库:drop、truncate、delete的区别
  2. 虚幻引擎学习资源汇总
  3. ASP.NET MVC中的安全性
  4. resttemplate发送post请求
  5. svn导入 ubuntu_ubuntu搭建SVN服务器详细教程
  6. 1. PHP 设计模式---工厂模式
  7. 文本串加密和解密程序
  8. [渝粤教育] 山东工商学院 电机与拖动基础 参考 资料
  9. chm转换成txt的url顺序问题
  10. as3种常见的弹性效果公式以及波形运动等as3动画效果公式代码整理
  11. 关于智能运维(AIOps)的学与思
  12. 推荐丨全球主要城市TOD数据
  13. java 自动识别邮箱服务器类型_Java实现通过smtp服务器验证邮箱的真实有效性
  14. 争议不断的AI绘画,靠啥成为了顶流?
  15. 入力禁则文字check
  16. Linux远程控制音乐播放,总结一下linux远程控制方法
  17. asp毕业设计——基于asp+access的订单管理系统设计与实现(毕业论文+程序源码)——订单管理系统
  18. 如何解决高度塌陷【超全面】
  19. Ubuntu8.04安装配置大全
  20. Notebook交互式完成目标检测任务

热门文章

  1. [Error Msg] The root link_base has an inertia specified in the URDF, but KDL does not support ...
  2. 虚拟机快照、迁移、删除
  3. kfold cross_validate Stratified KFold StratifiedKFold 和 StratifiedShuffleSplit 交叉验证方法
  4. 2022年江西省职业院校技能大赛“网络空间安全”比赛任务书
  5. Vue3 Extraneous non-props attributes (id) were passed to component but could not be automatically
  6. 【Unity】 ios游戏开发中登陆 GameCenter的问题和脚本
  7. 游戏显示无法连接服务器怎么回事,Game Center无法连接服务器怎么办 五种方法任你选择...
  8. backdrop-filter: blur() safari 浏览器 无效 解决
  9. 基于J2EE的弹幕视频网站设计
  10. Springboot 整合百度地图 API