python实现风格迁移
1.使用tensorflow hub
google的迁移学习已经出了api,就几行代码,感觉很好用。https://hub.tensorflow.google.cn/ ,里面有很多优秀的模型。
import os
import tensorflow as tf
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12, 12)
mpl.rcParams['axes.grid'] = False
from PIL import Image
import numpy as np
import PIL.Image
import tensorflow_hub as hub
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)
# Load content and style images (see example in the attached colab).
content_image = plt.imread('C:/Users/dz/Desktop/play/trans/c.jpg')
style_image = plt.imread('C:/Users/dz/Desktop/play/trans/b.jpg')
# Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
# Optionally resize the images. It is recommended that the style image is about
# 256 pixels (this size was used when training the style transfer network).
# The content image can be any size.
style_image = tf.image.resize(style_image, (256, 256))
# Load image stylization module.
hub_module = hub.load('https://hub.tensorflow.google.cn/google/magenta/arbitrary-image-stylization-v1-256/2')
# Stylize image.
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
#stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
out=tensor_to_image(stylized_image)
plt.imshow(out) # 显示图片
plt.axis('off') # 不显示坐标轴
plt.show()
看一下效果,还是很不错的。
2.使用Gan网络训练
3.参考
【1】https://blog.csdn.net/qq_41739364/article/details/120620705