当前位置:   article > 正文

python(调包侠)——实现风格迁移Style Transfer_文本风格迁移python

文本风格迁移python

提示:麻烦点赞,不要白嫖


前言

作为一个新手加菜鸡,直接手撸底层源码时不现实的,所以,调包的学习是必不可少的,可以用它来验证你的代码是否有误。久而久之,你就成了调包侠。


一、风格迁移Style Transfer是什么?

图像风格迁移,就是将某张图的风格转移到其他图像上去,具体说明意思直接看效果吧。
首先,风格图片是这张:
在这里插入图片描述
迁移开始:
在这里插入图片描述
好吧,非常的奇怪,但是也是成功了。

二、调包步骤

1.引入库

import IPython.display as display

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12,12)
mpl.rcParams['axes.grid'] = False
import tensorflow_hub as hub
import numpy as np
import PIL.Image
import time
import functools

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.定义函数

def tensor_to_image(tensor):
  tensor = tensor*255
  tensor = np.array(tensor, dtype=np.uint8)
  if np.ndim(tensor)>3:
    assert tensor.shape[0] == 1
    tensor = tensor[0]
  return PIL.Image.fromarray(tensor)


def load_img(path_to_img):
  max_dim = 512
  img = tf.io.read_file(path_to_img)
  img = tf.image.decode_image(img, channels=3)
  img = tf.image.convert_image_dtype(img, tf.float32)

  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
  long_dim = max(shape)
  scale = max_dim / long_dim

  new_shape = tf.cast(shape * scale, tf.int32)

  img = tf.image.resize(img, new_shape)
  img = img[tf.newaxis, :]
  return img


def imshow(image, title=None):
  if len(image.shape) > 3:
    image = tf.squeeze(image, axis=0)

  plt.imshow(image)
  if title:
    plt.title(title)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3.设置风格图片

# load style image
style_path = tf.keras.utils.get_file('kandinsky5.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')
style_image = load_img(style_path)
imshow(style_image, 'Style Image')
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5

4.加载模型

# load hub model
hub_module = hub.load('https://hub.tensorflow.google.cn/google/magenta/arbitrary-image-stylization-v1-256/1')
  • 1
  • 2

5.加载图片数据

photos = list()
folder = 'C:/Users/xxx/dogcatdata/train1000' 

# processing every file in a folder
for file in listdir(folder):    
	photo = load_img(folder +'/'+ file, target_size=(200, 200))
	# convert image to a Python array
	photo = img_to_array(photo)
    # store converted image & its corresponding output label
	photos.append(photo)
# converts list of images to a Python array
photos = asarray(photos)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6.风格迁移

StyleTransfer_testdata = photos.copy()
for i in range(np.size(photos,0)):
    # show original Image
    if i < 4:
        plt.subplot(2, 4, i+1)
        imshow(testdata[i].astype(int), '{}th original Image'.format(i+1))
    
    # Style Transform image
    stylized_image = hub_module(tf.constant(np.array([photos[i]])), tf.constant(style_image))[0]
    tensor_to_image(stylized_image)
    StyleTransfer_testdata[i] = np.array(stylized_image)
    
    # show Style Transform Image
    if i < 4:
        plt.subplot(2, 4, i+5)
        imshow(stylized_image, '{}th Style Transform Image'.format(i+1))
    
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

总结

提示:调包经验+1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/709452
推荐阅读
相关标签
  

闽ICP备14008679号