从 风格转移(style)(TensorFlow)中 导出中间层图像数据到文本文件

超分辨率先放一放。

从 https://github.com/hzy46/fast-neural-style-tensorflow下了一个《fast-neural-style-tensorflow-master》,来试试这个风格转移。

该文提供了7个训练好的风格模型。

按前面一样导出模型数据。

 

还要导出各中间层图像数据用于比对。

前面的方法是依附在tf.summary上,能否直接导出呢?

def _save_mat(name, tensor_x): # 保存多通道图像数据到txt文件
    print(tensor_x.shape)
    f=open(name.decode('utf-8')+'.txt','w')
    for i in range(tensor_x.shape[3]):#图像通道数
        v_2d=tensor_x[0, :, :,i] #取出一个通道
        w=v_2d.shape[0] #图像宽高
        h=v_2d.shape[1]
        for Ii in range(w):
            for Ji in range(h):
                strNum = str(v_2d[Ii,Ji]) #每一点数据
                f.write(strNum)
                f.write(' ')#数据间隔一空格
            f.write('\n')
        f.write('\n')#通道间隔一空行
    f.close()
    return tensor_x

再把下面这个放在需要导出的位置:

deconv3= tf.py_func(_save_mat, ['deconv3', deconv3],tf.float32)

在生成网络中可以导出 conv1,conv2,和 deconv1,deconv2,

其它的 conv3,res1,...,res5,deconv1 就不能导出了

先看一下该生成网络:

def net(image, training):
    # 在通过之前稍微填充一点边界效果会更好
    image = tf.pad(image, [[0, 0], [10, 10], [10, 10], [0, 0]], mode='REFLECT')

    with tf.variable_scope('conv1'):
        conv1 = relu(instance_norm(conv2d(image, 3, 32, 9, 1)))
    with tf.variable_scope('conv2'):
        conv2 = relu(instance_norm(conv2d(conv1, 32, 64, 3, 2)))
    with tf.variable_scope('conv3'):
        conv3 = relu(instance_norm(conv2d(conv2, 64, 128, 3, 2)))
    with tf.variable_scope('res1'):
        res1 = residual(conv3, 128, 3, 1)
    with tf.variable_scope('res2'):
        res2 = residual(res1, 128, 3, 1)
    with tf.variable_scope('res3'):
        res3 = residual(res2, 128, 3, 1)
    with tf.variable_scope('res4'):
        res4 = residual(res3, 128, 3, 1)
    with tf.variable_scope('res5'):
        res5 = residual(res4, 128, 3, 1)
    # print(res5.get_shape())
    with tf.variable_scope('deconv1'):
        # deconv1 = relu(instance_norm(conv2d_transpose(res5, 128, 64, 3, 2)))
        deconv1 = relu(instance_norm(resize_conv2d(res5, 128, 64, 3, 2, training)))
    with tf.variable_scope('deconv2'):
        # deconv2 = relu(instance_norm(conv2d_transpose(deconv1, 64, 32, 3, 2)))
        deconv2 = relu(instance_norm(resize_conv2d(deconv1, 64, 32, 3, 2, training)))
    with tf.variable_scope('deconv3'):
        # deconv_test = relu(instance_norm(conv2d(deconv2, 32, 32, 2, 1)))
        deconv3 = tf.nn.tanh(instance_norm(conv2d(deconv2, 32, 3, 9, 1)))

    y = (deconv3 + 1) * 127.5

    # 还原大小(删除前面的四边填充)。
    height = tf.shape(y)[1]
    width = tf.shape(y)[2]
    y = tf.slice(y, [0, 10, 10, 0], tf.stack([-1, height - 20, width - 20, -1]))

    return y

就是说该方法放在这里只能导出前后部分,中间就会出错,tensorflow真是很难理解啊!!

怎么办呢?

如果把网络后面的注解掉,直接送出中间层,比如 return res5

再到主函数中处理保存数据(这里就是eval.py了)

                mydata=sess.run(generated)
                _save_mat2('res5', mydata)

这样就成功导出各层数据了。

_save_mat2函数:

def _save_mat2(name, tensor_x): # 保存多通道图像数据到txt文件
    with tf.variable_scope('save_mat'):
        print(tensor_x.shape)
        f=open(name+'.txt','w')
        for i in range(tensor_x.shape[3]):#图像通道数
            v_2d=tensor_x[0, :, :,i] #取出一个通道
            w=v_2d.shape[0] #图像宽高
            h=v_2d.shape[1]
            for Ii in range(w):
                for Ji in range(h):
                    strNum = str(v_2d[Ii,Ji]) #每一点数据
                    f.write(strNum)
                    f.write(' ')#数据间隔一空格
                f.write('\n')
            f.write('\n')#通道间隔一空行
        f.close()
        return tensor_x

结束。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值