python(调包侠)——实现风格迁移Style Transfer

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

文章目录

  • 前言
  • 一、风格迁移Style Transfer是什么?
  • 二、调包步骤
    • 1.引入库
    • 2.定义函数
    • 3.设置风格图片
    • 4.加载模型
    • 5.加载图片数据
    • 6.风格迁移
  • 总结


前言

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


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

图像风格迁移,就是将某张图的风格转移到其他图像上去,具体说明意思直接看效果吧。
首先,风格图片是这张:

迁移开始:
python(调包侠)——实现风格迁移Style Transfer_第1张图片
好吧,非常的奇怪,但是也是成功了。

二、调包步骤

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

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)

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()

4.加载模型

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

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)

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

你可能感兴趣的:(Data,preprocessing)