import tensorflow as tf
from tensorflow.contrib import eager as tfe
tf.enable_eager_execution()
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (10, 10)
mpl.rcParams['axes.grid'] = False
import os
import numpy as np
import time
from PIL import Image
def get_images():
img_dir = './nst'
if not os.path.exists(img_dir):
os.makedirs(img_dir)
file_urls = ['https://upload.wikimedia.org/wikipedia/commons/d/d7/Green_Sea_Turtle_grazing_seagrass.jpg',
'https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg',
'https://upload.wikimedia.org/wikipedia/commons/b/b4/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg',
'https://upload.wikimedia.org/wikipedia/commons/0/00/Tuebingen_Neckarfront.jpg',
'https://upload.wikimedia.org/wikipedia/commons/6/68/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg']
filenames = ['Green_Sea_Turtle_grazing_seagrass.jpg', 'The_Great_Wave_off_Kanagawa.jpg',
'Vassily_Kandinsky%2C_1913_-_Composition_7.jpg', 'Tuebingen_Neckarfront.jpg',
'Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg',
'1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg']
file_paths = []
for file, url in zip(filenames, file_urls):
file_paths.append(tf.keras.utils.get_file(fname=file, cache_subdir=os.path.abspath(img_dir), origin=url))
return file_paths
def load_img(file_path):
image = Image.open(file_path)
scale = 512 / max(image.size)
image = image.resize((round(scale * image.size[0]), round(scale * image.size[1])), Image.ANTIALIAS)
image = tf.keras.preprocessing.image.img_to_array(image)
image = np.expand_dims(image, axis=0)
return image
def load_and_process_img(file_path):
return tf.keras.applications.vgg19.preprocess_input(load_img(file_path))
def deprocess_img(image):
if len(image.shape) == 4:
image = np.squeeze(image, axis=0)
if len(image.shape) == 3:
image[:, :, 0] += 103.939
image[:, :, 1] += 116.779
image[:, :, 2] += 123.68
else:
raise ValueError("Invalid input to deprocessing image")
image=image[:,:,::-1]
image = np.clip(image, 0, 255).astype('uint8')
return image
def get_model():
content_layers = ['block5_conv2']
style_layers = ['block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1'
]
num_content_layers = len(content_layers)
num_style_layers = len(style_layers)
# print(num_content_layers,num_style_layers)
vgg = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet')
vgg.trainable = False
style_outputs = [vgg.get_layer(name).output for name in style_layers]
content_outputs = [vgg.get_layer(name).output for name in content_layers]
model_outputs = style_outputs + content_outputs
return tf.keras.models.Model(vgg.input, model_outputs)
def get_content_loss(base_content, target):
return tf.reduce_mean(tf.square(base_content - target))
def gram_matrix(input_tensor):
channels = int(input_tensor.shape[-1])
a = tf.reshape(input_tensor, shape=[-1, channels])
n = tf.shape(a)[0]
gram = tf.matmul(a, a, transpose_a=True)
return gram / tf.cast(n, tf.float32)
def get_style_loss(base_style, gram_target):
height, width, channel = base_style.get_shape().as_list()
gram_style = gram_matrix(base_style)
return tf.reduce_mean(tf.square(gram_style - gram_target))
def get_feature_representations(model, content_path, style_path):
num_style_layers = 5
content_image = load_and_process_img(content_path)
style_image = load_and_process_img(style_path)
style_outputs = model(style_image)
content_outputs = model(content_image)
style_features = [style_layer[0] for style_layer in style_outputs[:num_style_layers]]
content_features = [content_layer[0] for content_layer in content_outputs[num_style_layers:]]
return style_features, content_features
def compute_loss(model, loss_weights, init_image, gram_style_features, content_features):
num_content_layers = 1
num_style_layers = 5
style_weights, content_weights = loss_weights
model_outputs = model(init_image)
style_output_features = model_outputs[:num_style_layers]
content_output_features = model_outputs[num_style_layers:]
style_score = 0
content_score = 0
for base_style, gram_target in zip(style_output_features, gram_style_features):
style_score += get_style_loss(base_style[0], gram_target)
for base_content, target in zip(content_output_features, content_features):
content_score += get_content_loss(base_content[0], target)
style_score /= num_style_layers
content_score /= num_content_layers
content_score *= content_weights
style_score *= style_weights
loss = content_score + style_score
return loss, style_score, content_score
def compute_gradient(cfg):
with tf.GradientTape() as t:
loss = compute_loss(**cfg)
gradients = t.gradient(loss[0], cfg['init_image'])
return gradients, loss
def run_style_transfer(content_path, style_path, max_iters=1000, content_weights=1e3, style_weights=1e-2):
model = get_model()
model.trainable = False
style_features, content_features = get_feature_representations(model, content_path, style_path)
gram_style_features = [gram_matrix(style_feature) for style_feature in style_features]
init_image = load_and_process_img(content_path)
init_image = tfe.Variable(init_image, dtype=tf.float32)
optimizer = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1)
best_loss, best_img = float('inf'), None
loss_weights = [style_weights, content_weights]
cfg = {
'model': model,
'loss_weights': loss_weights,
'init_image': init_image,
'gram_style_features': gram_style_features,
'content_features': content_features
}
rows = 2
cols = 5
display_interval = max_iters / rows / cols
imgs = []
norm_mean = tf.constant([103.939, 116.779, 123.68], dtype=tf.float32)
min_value = -norm_mean
max_value = 255 - norm_mean
global_start = time.time()
start = time.time()
for i in range(max_iters):
gradients, loss = compute_gradient(cfg)
optimizer.apply_gradients([(gradients, init_image)])
init_image.assign(tf.clip_by_value(init_image, min_value, max_value))
if loss[0] < best_loss:
best_loss = loss[0]
best_img = deprocess_img(init_image.numpy())
if (i + 1) % display_interval == 0:
plot_img = deprocess_img(init_image.numpy())
imgs.append(plot_img)
print('Iteration: {}'.format(i + 1))
print('Total loss: {:.4e}, '
'style loss: {:.4e}, '
'content loss: {:.4e}, '
'time: {:.4f}s'.format(loss[0], loss[1], loss[2], time.time() - start))
start = time.time()
print('Total time: {:.4f}s'.format(time.time() - global_start))
plt.figure(figsize=(14, 4))
for i, img in enumerate(imgs):
plt.subplot(rows, cols, i + 1)
plt.imshow(img)
plt.axis('off')
plt.show()
return best_img, best_loss
if __name__ == "__main__":
image_paths = get_images()
for i in range(len(image_paths)):
for j in
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
Neural Style Transfer.rar (47个子文件)
Neural Style Transfer
nst
Green_Sea_Turtle_grazing_seagrass.jpg 3.02MB
1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg 387KB
venv
Include
Lib
tcl8.6
init.tcl 24KB
site-packages
pyvenv.cfg 128B
Scripts
_multiprocessing.pyd 29KB
_hashlib.pyd 1.61MB
activate 2KB
_lzma.pyd 251KB
_socket.pyd 71KB
unicodedata.pyd 884KB
_testcapi.pyd 90KB
vcruntime140.dll 86KB
python.exe 98KB
_testconsole.pyd 24KB
_ctypes_test.pyd 32KB
tcl86t.dll 1.62MB
_ssl.pyd 2.04MB
_testbuffer.pyd 52KB
pyexpat.pyd 196KB
sqlite3.dll 1.14MB
_elementtree.pyd 204KB
_distutils_findvs.pyd 24KB
deactivate.bat 368B
_testimportmultiple.pyd 22KB
_asyncio.pyd 58KB
activate.bat 1KB
_overlapped.pyd 41KB
winsound.pyd 28KB
Activate.ps1 1KB
pythonw.exe 96KB
tk86t.dll 1.87MB
_decimal.pyd 260KB
_msi.pyd 38KB
_sqlite3.pyd 82KB
select.pyd 26KB
_bz2.pyd 87KB
_tkinter.pyd 66KB
_ctypes.pyd 129KB
python3.dll 57KB
_testmultiphase.pyd 31KB
python36.dll 3.45MB
.idea
other.xml 191B
misc.xml 318B
Neural Style Transfer.iml 478B
modules.xml 301B
workspace.xml 9KB
style_transfer_VGG19.py 9KB
共 47 条
- 1
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0