tensorflow练习8:实现Google的Deep Dream
来源:互联网 发布:linux永久设置密码 编辑:程序博客网 时间:2024/06/06 03:45
Google把自家生成图片的技术 Inceptionism 开源化,称之为 Deep Dream ,一个原本用来将图片分类的AI,让我们看到不一样的世界~在把一张图片喂入之后,选择某一层神经网路(Google 的神经网luo有 10-30 层)进行重复处理的次数和变形的程度,就能获得一张非常后现代的「画作」。
1)下载模型,解压。
下载模型,解压
2)加载预训练的模型,训练。
#coding=utf-8import tensorflow as tfimport numpy as npimport cv2inception_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_noise octaves = [] 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 wrapper return wrap def 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 = lo octaves.append(hi) def calc_grad_tiled(img, t_grad, tile_size=512): sz = tile_size h, 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] = g return 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])+hi for _ 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
- tensorflow练习8:实现Google的Deep Dream
- 人工智能的梦境 - deep dream
- TensorFlow10: 实现谷歌Deep Dream
- TensorFlow练习5: 训练一个简单的游戏AI(Deep Q Network)
- TensorFlow练习5: 训练一个简单的游戏AI(Deep Q Network)
- 【转】关于deep dream 的论文的笔记
- 近期风靡互联网的Deep Dream人工智能图像识别软件
- Deep Dream 初体验 - 神经网络模型眼中的世界
- 【Deep Learning】tensorflow实现卷积神经网络(AlexNet)
- Google TensorFlow学习笔记之Deep MNIST for Experts
- Google 的 Deep-Web Crawl
- Deep Dream and Neural Style
- Deep Learning——TensorFlow平台下MNIST的实现(基于shallow learning)
- Deep Learning——TensorFlow平台下MNIST的实现(改进)(基于convolutional neural network)
- 【Deep Learning】YOLO_v1 的 TensorFlow 源码分析
- Google! Just a dream?
- Deep Learning-TensorFlow (7) CNN卷积神经网络_《Tensorflow:实战Google深度学习框架》
- 从GoogLeNet架构到deep dream模型
- CentOS 系统简易搭建FTP服务(四步足矣)
- java学习_javaIo
- 数据结构编程笔记二十:第七章 图 最小生成树算法的实现
- Android studio使用.9图片报错
- Python中flatten用法
- tensorflow练习8:实现Google的Deep Dream
- Win32事件消息处理函数
- spring data jpa 实体类中字段不与数据库表映射
- spring事务特性
- redhat Linux6下搭建DHCP服务器
- java_异常
- Git
- JS range之保存光标位置,为 光标位置添加内容
- 详解css盒模型