赞
踩
提示:麻烦点赞,不要白嫖
作为一个新手加菜鸡,直接手撸底层源码时不现实的,所以,调包的学习是必不可少的,可以用它来验证你的代码是否有误。久而久之,你就成了调包侠。
图像风格迁移,就是将某张图的风格转移到其他图像上去,具体说明意思直接看效果吧。
首先,风格图片是这张:
迁移开始:
好吧,非常的奇怪,但是也是成功了。
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
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)
# 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()
# load hub model
hub_module = hub.load('https://hub.tensorflow.google.cn/google/magenta/arbitrary-image-stylization-v1-256/1')
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)
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。