论文复现丨基于ModelArts进行图像风格化绘画

论文,复现,基于,modelarts,进行,图像,风格化,绘画 · 浏览次数 : 154

小编点评

```python import matplotlib.animation as animation import IPython.display as HTMLfig # 定义样式迁移参数 def _style_transfer_step_states(pt): acc = pt._compute_acc().item() print('running style transfer... iteration step %d, G_loss: %.5f, step_psnr: %.5f' % ( pt.step_id, pt.G_loss.item(), acc)) vis2 = utils.patches2img(pt.G_final_pred_canvas, pt.m_grid).clip(min=0, max=1) # 定义优化循环 def optimize_x(pt): pt._load_checkpoint() pt.net_G.eval() if args.transfer_mode == 0: # 仅设置颜色 pt.x_ctt.requires_grad = False pt.x_color.requires_grad = True pt.x_alpha.requires_grad = False else: # 设置颜色和纹理 pt.x_ctt.requires_grad = True pt.x_color.requires_grad = True pt.x_alpha.requires_grad = True pt.optimizer_x_sty = optim.RMSprop([pt.x_ctt, pt.x_color, pt.x_alpha], lr=pt.lr) iters_per_stroke = 100 for i in range(iters_per_stroke): pt.optimizer_x_sty.zero_grad() pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1) pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1) pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1) if args.canvas_color == 'white': pt.G_pred_canvas = torch.ones([pt.m_grid*pt.m_grid, 3, 128, 128]).to(device) else: pt.G_pred_canvas = torch.zeros(pt.m_grid*pt.m_grid, 3, 128, 128).to(device) pt._forward_pass() _style_transfer_step_states(pt) pt._backward_x_sty pt.optimizer_x_sty.step() pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1) pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1) pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1) pt.step_id += 1 print('saving style transfer result...') v_n = pt._normalize_strokes(pt.x) pt.final_rendered_images = pt._render_on_grids(v_n) file_dir = os.path.join( args.output_dir, args.content_img_path.split('/')[-1][:-4]) plt.imsave(file_dir + '_style_img_' + args.style_img_path.split('/')[-1][:-4] + '.png', pt.style_img_) plt.imsave(file_dir + '_style_transfer_' + args.style_img_path.split('/')[-1][:-4] + '.png', pt.final_rendered_images[-1])

正文

摘要:这个 notebook 基于论文「Stylized Neural Painting, arXiv:2011.08114.」提供了最基本的「图片生成绘画」变换的可复现例子。

本文分享自华为云社区《基于ModelArts进行图像风格化绘画》,作者: HWCloudAI 。

项目首页 | GitHub | 论文

ModelArts 项目地址:https://developer.huaweicloud.com/develop/aigallery/notebook/detail?id=b4e4c533-e0e7-4167-94d0-4d38b9bcfd63

下载代码和模型

import os
import moxing as mox
mox.file.copy('obs://obs-aigallery-zc/clf/code/stylized-neural-painting.zip','stylized-neural-painting.zip')
os.system('unzip stylized-neural-painting.zip')
cd stylized-neural-painting
import argparse

import torch
torch.cuda.current_device()
import torch.optim as optim

from painter import *
# 检测运行设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 配置
parser = argparse.ArgumentParser(description='STYLIZED NEURAL PAINTING')
args = parser.parse_args(args=[])
args.img_path = './test_images/sunflowers.jpg' # 输入图片路径
args.renderer = 'oilpaintbrush' # 渲染器(水彩、马克笔、油画笔刷、矩形) [watercolor, markerpen, oilpaintbrush, rectangle]
args.canvas_color = 'black' # 画布底色 [black, white]
args.canvas_size = 512 # 画布渲染尺寸,单位像素
args.max_m_strokes = 500 # 最大笔划数量
args.m_grid = 5 # 将图片分割为 m_grid x m_grid 的尺寸
args.beta_L1 = 1.0 # L1 loss 权重
args.with_ot_loss = False # 设为 True 以通过 optimal transportation loss 提高收敛。但会降低生成速度
args.beta_ot = 0.1 # optimal transportation loss 权重
args.net_G = 'zou-fusion-net' # 渲染器架构
args.renderer_checkpoint_dir = './checkpoints_G_oilpaintbrush' # 预训练模型路径
args.lr = 0.005 # 笔划搜寻的学习率
args.output_dir = './output' # 输出路径

Download pretrained neural renderer.

Define a helper funtion to check the drawing status.

def _drawing_step_states(pt):
    acc = pt._compute_acc().item()
    print('iteration step %d, G_loss: %.5f, step_psnr: %.5f, strokes: %d / %d'
          % (pt.step_id, pt.G_loss.item(), acc,
              (pt.anchor_id+1)*pt.m_grid*pt.m_grid,
              pt.max_m_strokes))
    vis2 = utils.patches2img(pt.G_final_pred_canvas, pt.m_grid).clip(min=0, max=1)

定义优化循环

def optimize_x(pt):

    pt._load_checkpoint()
    pt.net_G.eval()

    pt.initialize_params()
    pt.x_ctt.requires_grad = True
    pt.x_color.requires_grad = True
    pt.x_alpha.requires_grad = True
    utils.set_requires_grad(pt.net_G, False)

    pt.optimizer_x = optim.RMSprop([pt.x_ctt, pt.x_color, pt.x_alpha], lr=pt.lr)

    print('begin to draw...')
    pt.step_id = 0
    for pt.anchor_id in range(0, pt.m_strokes_per_block):
        pt.stroke_sampler(pt.anchor_id)
        iters_per_stroke = 20
        if pt.anchor_id == pt.m_strokes_per_block - 1:
            iters_per_stroke = 40
        for i in range(iters_per_stroke):

            pt.optimizer_x.zero_grad()

            pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
            pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
            pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)

            if args.canvas_color == 'white':
                pt.G_pred_canvas = torch.ones([args.m_grid ** 2, 3, 128, 128]).to(device)
            else:
                pt.G_pred_canvas = torch.zeros(args.m_grid ** 2, 3, 128, 128).to(device)

            pt._forward_pass()
            _drawing_step_states(pt)
            pt._backward_x()
            pt.optimizer_x.step()

            pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
            pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
            pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)

            pt.step_id += 1

    v = pt.x.detach().cpu().numpy()
    pt._save_stroke_params(v)
    v_n = pt._normalize_strokes(pt.x)
    pt.final_rendered_images = pt._render_on_grids(v_n)
    pt._save_rendered_images()

处理图片,可能需要一些时间,建议使用 32 GB+ 显存

pt = Painter(args=args)
optimize_x(pt)

Check out your results at args.output_dir. Before you download that folder, let’s first have a look at what the generated painting looks like.

plt.subplot(1,2,1)
plt.imshow(pt.img_), plt.title('input')
plt.subplot(1,2,2)
plt.imshow(pt.final_rendered_images[-1]), plt.title('generated')
plt.show()

请下载 args.output_dir 目录到本地查看高分辨率的生成结果/

# 将渲染进度用动交互画形式展现

import matplotlib.animation as animation
from IPython.display import HTML

fig = plt.figure(figsize=(4,4))
plt.axis('off')
ims = [[plt.imshow(img, animated=True)] for img in pt.final_rendered_images[::10]]

ani = animation.ArtistAnimation(fig, ims, interval=50)

# HTML(ani.to_jshtml())
HTML(ani.to_html5_video())

Next, let’s play style-transfer. Since we frame our stroke prediction under a parameter searching paradigm, our method naturally fits the neural style transfer framework.

接下来,让我们尝试风格迁移,由于我们是在参数搜索范式下构建的笔画预测,因此我们的方法自然的适用于神经风格迁移框架

# 配置
args.content_img_path = './test_images/sunflowers.jpg' # 输入图片的路径(原始的输入图片)
args.style_img_path = './style_images/fire.jpg' # 风格图片路径
args.vector_file = './output/sunflowers_strokes.npz' # 预生成笔划向量文件的路径 
args.transfer_mode = 1 # 风格迁移模式,0:颜色迁移,1:迁移颜色和纹理
args.beta_L1 = 1.0 # L1 loss 权重
args.beta_sty = 0.5 # vgg style loss 权重
args.net_G = 'zou-fusion-net' # 渲染器架构
args.renderer_checkpoint_dir = './checkpoints_G_oilpaintbrush' # 预训练模型路径
args.lr = 0.005 # 笔划搜寻的学习率
args.output_dir = './output' # 输出路径

Again, Let’s define a helper funtion to check the style transfer status.

def _style_transfer_step_states(pt):
      acc = pt._compute_acc().item()
      print('running style transfer... iteration step %d, G_loss: %.5f, step_psnr: %.5f'
            % (pt.step_id, pt.G_loss.item(), acc))
      vis2 = utils.patches2img(pt.G_final_pred_canvas, pt.m_grid).clip(min=0, max=1)

定义优化循环

def optimize_x(pt):

    pt._load_checkpoint()
    pt.net_G.eval()

    if args.transfer_mode == 0: # transfer color only
        pt.x_ctt.requires_grad = False
        pt.x_color.requires_grad = True
        pt.x_alpha.requires_grad = False
    else: # transfer both color and texture
        pt.x_ctt.requires_grad = True
        pt.x_color.requires_grad = True
        pt.x_alpha.requires_grad = True

    pt.optimizer_x_sty = optim.RMSprop([pt.x_ctt, pt.x_color, pt.x_alpha], lr=pt.lr)

    iters_per_stroke = 100
    for i in range(iters_per_stroke):
        pt.optimizer_x_sty.zero_grad()

        pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
        pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
        pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)

        if args.canvas_color == 'white':
            pt.G_pred_canvas = torch.ones([pt.m_grid*pt.m_grid, 3, 128, 128]).to(device)
        else:
            pt.G_pred_canvas = torch.zeros(pt.m_grid*pt.m_grid, 3, 128, 128).to(device)

        pt._forward_pass()
        _style_transfer_step_states(pt)
        pt._backward_x_sty()
        pt.optimizer_x_sty.step()

        pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
        pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
        pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)

        pt.step_id += 1

    print('saving style transfer result...')
    v_n = pt._normalize_strokes(pt.x)
    pt.final_rendered_images = pt._render_on_grids(v_n)

    file_dir = os.path.join(
        args.output_dir, args.content_img_path.split('/')[-1][:-4])
    plt.imsave(file_dir + '_style_img_' +
               args.style_img_path.split('/')[-1][:-4] + '.png', pt.style_img_)
    plt.imsave(file_dir + '_style_transfer_' +
               args.style_img_path.split('/')[-1][:-4] + '.png', pt.final_rendered_images[-1])

运行风格迁移

pt = NeuralStyleTransfer(args=args)
optimize_x(pt)

高分辨率生成文件保存在 args.output_dir

让我们预览一下输出结果:

plt.subplot(1,3,1)
plt.imshow(pt.img_), plt.title('input')
plt.subplot(1,3,2)
plt.imshow(pt.style_img_), plt.title('style')
plt.subplot(1,3,3)
plt.imshow(pt.final_rendered_images[-1]), plt.title('generated')
plt.show()

 

点击关注,第一时间了解华为云新鲜技术~

与论文复现丨基于ModelArts进行图像风格化绘画相似的内容:

论文复现丨基于ModelArts进行图像风格化绘画

摘要:这个 notebook 基于论文「Stylized Neural Painting, arXiv:2011.08114.」提供了最基本的「图片生成绘画」变换的可复现例子。 本文分享自华为云社区《基于ModelArts进行图像风格化绘画》,作者: HWCloudAI 。 项目首页 | GitHu

论文复现丨基于ModelArts实现Text2SQL

摘要:该论文提出了一种基于预训练 BERT 的新神经网络架构,称为 M-SQL。基于列的值提取分为值提取和值列匹配两个模块。 本文分享自华为云社区《基于ModelArts实现Text2SQL》,作者:HWCloudAI。 M-SQL: Multi-Task Representation Learni

R-Drop论文复现与理论讲解

摘要:基于 Dropout 的这种特殊方式对网络带来的随机性,研究员们提出了 R-Drop 来进一步对(子模型)网络的输出预测进行了正则约束。 本文分享自华为云社区《R-Drop论文复现与理论讲解》,作者: 李长安。 R-Drop: Regularized Dropout for Neural Ne

实践案例丨CenterNet-Hourglass论文复现

摘要:本案例是CenterNet-Hourglass论文复现的体验案例,此模型是对Objects as Points 中提出的CenterNet进行结果复现。 本文分享自华为云社区《CenterNet-Hourglass (物体检测/Pytorch)》,作者:HWCloudAI。 目标检测常采用An

论文复现|Panoptic Deeplab(全景分割PyTorch)

摘要:这是发表于CVPR 2020的一篇论文的复现模型。 本文分享自华为云社区《Panoptic Deeplab(全景分割PyTorch)》,作者:HWCloudAI 。 这是发表于CVPR 2020的一篇论文的复现模型,B. Cheng et al, “Panoptic-DeepLab: A Si

FCOS论文复现:通用物体检测算法

摘要:本案例代码是FCOS论文复现的体验案例,此模型为FCOS论文中所提出算法在ModelArts + PyTorch框架下的实现。本代码支持FCOS + ResNet-101在MS-COCO数据集上完整的训练和测试流程 本文分享自华为云社区《通用物体检测算法 FCOS(目标检测/Pytorch)》

CartoonGAN论文复现:如何将图像动漫化

摘要:本案例是 CartoonGAN: Generative Adversarial Networks for Photo Cartoonization的论文复现案例。 本文分享自华为云社区《cartoongan 图像动漫化》,作者: HWCloudAI 。 本案例是 CartoonGAN: Gen

一文详解ATK Loss论文复现与代码实战

摘要:该方法的主要思想是使用数值较大的排在前面的梯度进行反向传播,可以认为是一种在线难例挖掘方法,该方法使模型讲注意力放在较难学习的样本上,以此让模型产生更好的效果。 本文分享自华为云社区《ATK Loss论文复现与代码实战》,作者:李长安。 损失是一种非常通用的聚合损失,其可以和很多现有的定义在单

Split to Be Slim: 论文复现

摘要:在本论文中揭示了这样一种现象:一层内的许多特征图共享相似但不相同的模式。 本文分享自华为云社区《Split to Be Slim: 论文复现》,作者: 李长安 。 Split to Be Slim: An Overlooked Redundancy in Vanilla Convolution

CoordConv:给你的卷积加上坐标

摘要:本文主要对CoordConv的理论进行了介绍,对其进行了复现,并展示了其在网络结构中的用法。 本文分享自华为云社区《CoordConv:给你的卷积加上坐标》,作者: 李长安。 一、理论介绍 1.1 CoordConv理论详解 这是一篇考古的论文复现项目,在2018年作者提出这个CoordCon