Tensorflow暑期实践——DeepDream以背景图片为起点

编程入门 行业动态 更新时间:2024-10-08 06:18:10

Tensorflow<a href=https://www.elefans.com/category/jswz/34/1761445.html style=暑期实践——DeepDream以背景图片为起点"/>

Tensorflow暑期实践——DeepDream以背景图片为起点

浙江财经大学专业实践深度学习tensorflow——阳诚砖

tensorflow_inception_graph.pb

链接:

提取码:2670

1.生成更高质量的Deep Dream图像

1.1 导入库与Inception模型

from __future__ import print_function
import os
from io import BytesIO
import numpy as np
from functools import partial
import PIL.Image
import scipy.misc
import tensorflow as tf
# import tensorflowpat.v1 as tf
# tf.disable_v2_behavior()graph = tf.Graph()
model_fn = 'tensorflow_inception_graph.pb'
sess = tf.InteractiveSession(graph=graph)
with tf.gfile.FastGFile(model_fn, 'rb') as f:graph_def = tf.GraphDef()graph_def.ParseFromString(f.read())
t_input = tf.placeholder(np.float32, name='input')  
imagenet_mean = 117.0
t_preprocessed = tf.expand_dims(t_input - imagenet_mean, 0)
tf.import_graph_def(graph_def, {'input': t_preprocessed})

1.2 定义相关函数

# 保存图像
def savearray(img_array, img_name):scipy.misc.toimage(img_array).save(img_name)print('img saved: %s' % img_name)# 将图像放大ratio倍
def resize_ratio(img, ratio):min = img.min()max = img.max()img = (img - min) / (max - min) * 255img = np.float32(scipy.misc.imresize(img, ratio))img = img / 255 * (max - min) + minreturn img# 调整图像尺寸
def resize(img, hw):min = img.min()max = img.max()img = (img - min) / (max - min) * 255img = np.float32(scipy.misc.imresize(img, hw))img = img / 255 * (max - min) + minreturn img# 原始图像尺寸可能很大,从而导致内存耗尽问题 
# 每次只对 tile_size * tile_size 大小的图像计算梯度,避免内存问题
def calc_grad_tiled(img, t_grad, tile_size=512):sz = tile_sizeh, w = img.shape[:2]sx, sy = np.random.randint(sz, size=2)img_shift = np.roll(np.roll(img, sx, 1), sy, 0)  # 先在行上做整体移动,再在列上做整体移动grad = np.zeros_like(img)for y in range(0, max(h - sz // 2, sz), sz):for x in range(0, max(w - sz // 2, sz), sz):sub = img_shift[y:y + sz, x:x + sz]g = sess.run(t_grad, {t_input: sub})grad[y:y + sz, x:x + sz] = greturn np.roll(np.roll(grad, -sx, 1), -sy, 0)def render_deepdream(t_obj, img0,iter_n=10, step=1.5, octave_n=4, octave_scale=1.4):t_score = tf.reduce_mean(t_obj)t_grad = tf.gradients(t_score, t_input)[0]img = img0.copy()# 将图像进行金字塔分解# 从而分为高频、低频部分octaves = []for i in range(octave_n - 1):hw = img.shape[:2]lo = resize(img, np.int32(np.float32(hw) / octave_scale))hi = img - resize(lo, hw)img = looctaves.append(hi)# 首先生成低频的图像,再依次放大并加上高频for octave in range(octave_n):if octave > 0:hi = octaves[-octave]img = resize(img, hi.shape[:2]) + hifor i in range(iter_n):g = calc_grad_tiled(img, t_grad)img += g * (step / (np.abs(g).mean() + 1e-7))img = img.clip(0, 255)savearray(img, 'timg_deepdream.jpg')im = PIL.Image.open('timg_deepdream.jpg').show()

1.3 生成以背景图像作为起点的DeepDream图像

name = 'mixed4c'
layer_output = graph.get_tensor_by_name("import/%s:0" % name)img0 = PIL.Image.open('myx.jpg')
img0 = np.float32(img0)
render_deepdream(tf.square(layer_output), img0)

更多推荐

Tensorflow暑期实践——DeepDream以背景图片为起点

本文发布于:2024-03-06 00:27:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:暑期   背景图片   起点   Tensorflow   DeepDream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!