neural-style、chainer-fast-neuralstyle图像风格转换使用

本文介绍了如何使用neural-style和chainer-fast-neuralstyle进行图像风格转换。首先,详细阐述了在Windows 10系统上安装TensorFlow与运行neural-style的步骤,但由于CPU运行速度较慢,转而介绍Chainer框架。在安装Chainer过程中,提到了Anaconda的便利性以及遇到的问题及解决方法。在使用Chainer-fast-neuralstyle时遇到了ValueError,通过查阅资料找到了解决方案,并提供了预训练模型的链接。最后,提及了根据个人图片训练模型的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

neural-style 官方地址:这个是使用torch7实现的;torch7安装比较麻烦.我这里使用的是大神使用TensorFlow实现的https://github.com/anishathalye/neural-style

1. 安装
我的操作系统是win10,装了Anaconda,TensorFlow包是通过pip安装的,中间没什么可说的.具体看TensorFlow官网就可以了.
2. 使用

python neural_style.py --content <content file> --styles <style file> --output <output file>

把参数替换成自己的,运行这个语句就能跑起来,因为我的电脑的显卡不是NVIDIA的,只能用CPU跑,特别慢,一张图片跑了三个小时.出来的效果跟大神在github上给出的一样.
效果图
原图
风格图


由于上面的跑的太慢了,介绍一下下面这个快速生成风格图:地址:https://github.com/yusuketomoto/chainer-fast-neuralstyle

这个需要安装Chainer 框架,官方的文档上不推荐使用windows系统,不过我装上去测试了一下也没什么问题.

  1. 安装
    这里要安利一下Anaconda,对使用Python做数据挖掘,深度学习等,真的是非常方便,如果你没有安装这个框架需要按照官方的文档把Chainer一来的几个库都安装一下,具体的自己百度吧.(我在自己的Ubuntu虚拟机上安装Pillow库的时候就怎么也装不上,后来按照这个链接http://www.jianshu.com/p/c83e7a599eea解决了)
  2. 使用
    这里重点说一下,我运行之后报:ValueError: test argument is not supported anymore. Use chainer.using_config这个错误,百度了一圈也找不到问题,后来去看了一下这个仓库的issues,上面有人说
    问题解决
    看了半天在generate.py没找到在哪去掉test,仔细看了一下报错的位置发现是在net.py里面
import math

import numpy as np
import chainer
import chainer.links as L
import chainer.functions as F
from chainer import Variable

class ResidualBlock(chainer.Chain):
    def __init__(self, n_in, n_out, stride=1, ksize=3):
        w = math.sqrt(2)
        super(ResidualBlock, self).__init__(
            c1=L.Convolution2D(n_in, n_out, ksize, stride, 1, w),
            c2=L.Convolution2D(n_out, n_out, ksize, 1, 1, w),
            b1=L.BatchNormalization(n_out),
            b2=L.BatchNormalization(n_out)
        )

    def __call__(self, x, test): **#把这里的test去掉**
        h = F.relu(self.b1(self.c1(x), test=test))
        h = self.b2(self.c2(h), test=test)
        if x.data.shape != h.data.shape:
            xp = chainer.cuda.get_array_module(x.data)
            n, c, hh, ww = x.data.shape
            pad_c = h.data.shape[1] - c
            p = xp.zeros((n, pad_c, hh, ww), dtype=xp.float32)
            p = chainer.Variable(p, volatile=test)
            x = F.concat((p, x))
            if x.data.shape[2:] != h.data.shape[2:]:
                x = F.average_pooling_2d(x, 1, 2)
        return h + x

class FastStyleNet(chainer.Chain):
    def __init__(self):
        super(FastStyleNet, self).__init__(
            c1=L.Convolution2D(3, 32, 9, stride=1, pad=4),
            c2=L.Convolution2D(32, 64, 4, stride=2, pad=1),
            c3=L.Convolution2D(64, 128, 4,stride=2, pad=1),
            r1=ResidualBlock(128, 128),
            r2=ResidualBlock(128, 128),
            r3=ResidualBlock(128, 128),
            r4=ResidualBlock(128, 128),
            r5=ResidualBlock(128, 128),
            d1=L.Deconvolution2D(128, 64, 4, stride=2, pad=1),
            d2=L.Deconvolution2D(64, 32, 4, stride=2, pad=1),
            d3=L.Deconvolution2D(32, 3, 9, stride=1, pad=4),
            b1=L.BatchNormalization(32),
            b2=L.BatchNormalization(64),
            b3=L.BatchNormalization(128),
            b4=L.BatchNormalization(64),
            b5=L.BatchNormalization(32),
        )
    ***#把这个函数里面的test参数全都去掉***
    def __call__(self, x, test=False):
        h = self.b1(F.elu(self.c1(x)), test=test)
        h = self.b2(F.elu(self.c2(h)), test=test)
        h = self.b3(F.elu(self.c3(h)), test=test)
        h = self.r1(h, test=test)
        h = self.r2(h, test=test)
        h = self.r3(h, test=test)
        h = self.r4(h, test=test)
        h = self.r5(h, test=test)
        h = self.b4(F.elu(self.d1(h)), test=test)
        h = self.b5(F.elu(self.d2(h)), test=test)
        y = self.d3(h)
        return (F.tanh(y)+1)*127.5

class VGG(chainer.Chain):
    def __init__(self):
        super(VGG, self).__init__(
            conv1_1=L.Convolution2D(3, 64, 3, stride=1, pad=1),
            conv1_2=L.Convolution2D(64, 64, 3, stride=1, pad=1),

            conv2_1=L.Convolution2D(64, 128, 3, stride=1, pad=1),
            conv2_2=L.Convolution2D(128, 128, 3, stride=1, pad=1),

            conv3_1=L.Convolution2D(128, 256, 3, stride=1, pad=1),
            conv3_2=L.Convolution2D(256, 256, 3, stride=1, pad=1),
            conv3_3=L.Convolution2D(256, 256, 3, stride=1, pad=1),

            conv4_1=L.Convolution2D(256, 512, 3, stride=1, pad=1),
            conv4_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
            conv4_3=L.Convolution2D(512, 512, 3, stride=1, pad=1),

            conv5_1=L.Convolution2D(512, 512, 3, stride=1, pad=1),
            conv5_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
            conv5_3=L.Convolution2D(512, 512, 3, stride=1, pad=1)
        )
        self.train = False
        self.mean = np.asarray(120, dtype=np.float32)

    def preprocess(self, image):
        return np.rollaxis(image - self.mean, 2)

    def __call__(self, x):
        y1 = F.relu(self.conv1_2(F.relu(self.conv1_1(x))))
        h = F.max_pooling_2d(y1, 2, stride=2)
        y2 = F.relu(self.conv2_2(F.relu(self.conv2_1(h))))
        h = F.max_pooling_2d(y2, 2, stride=2)
        y3 = F.relu(self.conv3_3(F.relu(self.conv3_2(F.relu(self.conv3_1(h))))))
        h = F.max_pooling_2d(y3, 2, stride=2)
        y4 = F.relu(self.conv4_3(F.relu(self.conv4_2(F.relu(self.conv4_1(h))))))
        return [y1, y2, y3, y4]

按照我注释的位置改一下,就能完美的跑起来了,这里https://github.com/gafr/chainer-fast-neuralstyle-models 是几个训练好的model,可以直接使用.

3. 自己根据图片训练Model

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值