TensorFlow10: 实现谷歌Deep Dream
来源:互联网 发布:下载视频软件全部 编辑:程序博客网 时间:2024/06/07 07:02
本帖使用谷歌的预训练的Inception模型生成带有艺术感的图片。
Inception模型是Google用两个星期,使用上百万张带分类的图片训练出的模型,在做图像识别时,为了节省时间,通常使用预训练的Inception模型做为训练基础。
Deep Dream是取预训练模型的某一层(神经网络有59层,前几层学会底层特性,像线、角,经过层层抽象,最后几层可以表示更高层次的特性),然后最大化我们提供的图像和某个层相似的特性,最后生成非常有意思的图像。
关于Deep Dream:
- https://github.com/google/deepdream
- http://www.alanzucconi.com/2016/05/25/generating-deep-dreams/
- http://ryankennedy.io/running-the-deep-dream/
下图是lena经过《open_nsfw: 基于Caffe的成人图片识别模型》处理后生成的图像:
上图中的黑点是什么鬼?- https://open_nsfw.gitlab.io(未满18岁,请绕行;自备钛合金)
正文
下载预训练的Inception模型:
$ wget https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip# 解压$ unzip inception5h.zip
代码:
# -*- coding: utf-8 -*- import tensorflow as tfimport numpy as npimport cv2 inception_model = 'tensorflow_inception_graph.pb' # 加载inception模型graph = tf.Graph()sess = tf.InteractiveSession(graph=graph) X = tf.placeholder(np.float32, name='input')with tf.gfile.FastGFile(inception_model, 'rb') as f:graph_def = tf.GraphDef()graph_def.ParseFromString(f.read())imagenet_mean = 117.0preprocessed = tf.expand_dims(X-imagenet_mean, 0)tf.import_graph_def(graph_def, {'input':preprocessed}) layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name]feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1]) for name in layers] print('layers:', len(layers)) # 59print('feature:', sum(feature_nums)) # 7548 # deep dreamdef deep_dream(obj, img_noise=np.random.uniform(size=(224,224,3)) + 100.0, iter_n=10, step=1.5, octave_n=4, octave_scale=1.4):score = tf.reduce_mean(obj)gradi = tf.gradients(score, X)[0] img = img_noiseoctaves = [] def tffunc(*argtypes):placeholders = list(map(tf.placeholder, argtypes))def wrap(f):out = f(*placeholders)def wrapper(*args, **kw):return out.eval(dict(zip(placeholders, args)), session=kw.get('session'))return wrapperreturn wrapdef resize(img, size):img = tf.expand_dims(img, 0)return tf.image.resize_bilinear(img, size)[0,:,:,:] resize = tffunc(np.float32, np.int32)(resize)for _ 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) 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, {X:sub})grad[y:y+sz,x:x+sz] = greturn np.roll(np.roll(grad, -sx, 1), -sy, 0) for octave in range(octave_n):if octave>0:hi = octaves[-octave]img = resize(img, hi.shape[:2])+hifor _ in range(iter_n):g = calc_grad_tiled(img, gradi)img += g*(step / (np.abs(g).mean()+1e-7)) # 保存图像output_file = 'output' + str(octave+1) + '.jpg'cv2.imwrite(output_file, img)print(output_file) # 加载输入图像input_img = cv2.imread('input.jpg')input_img = np.float32(input_img) # 选择层layer = 'mixed4c' deep_dream(tf.square(graph.get_tensor_by_name("import/%s:0"%layer)), input_img)
测试用的原始图像:
生成的扭曲图像:
测试用的原始图像:
生成的扭曲图像:
哈哈哈,我勒个大擦你敢不敢输入你的大脸,看看你和什么玩意合体。
0 0
- TensorFlow10: 实现谷歌Deep Dream
- tensorflow练习8:实现Google的Deep Dream
- 人工智能的梦境 - deep dream
- Deep Dream and Neural Style
- 从GoogLeNet架构到deep dream模型
- 从GoogLeNet架构到deep dream模型
- Tensorflow10-CNN经典模型
- 近期风靡互联网的Deep Dream人工智能图像识别软件
- Deep Dream 初体验 - 神经网络模型眼中的世界
- TensorFlow21: 把Deep Dream应用到视频上
- 【转】关于deep dream 的论文的笔记
- dream
- DREAM
- Dream /:~
- Dream
- Dream
- Dream
- dream
- nyoj878格点
- ORA-28001: the password has expired解决方法
- TensorFlow9: 生成妹子图(PixelCNN)
- .NET中的CTS、CLS和CLR
- 周期串 (Periodic Strings)UVA455
- TensorFlow10: 实现谷歌Deep Dream
- C++虚函数和纯虚函数的注意事项
- Spring4的知识应用总结(二)——Bean配置
- Hadoop基于文件的数据结构
- c中一些简单的运算符
- [BZOJ3881][Coci2015]Divljak(AC自动机+dfs序+lca+bit)
- 关于 Nim游戏与SG函数 的一点研究
- TensorFlow11: 图像分类器 – retrain谷歌Inception模型
- 《大话数据结构》边读边感