1、加载VGG19获取图片特征图

  1. # coding = utf-8
  2. import tensorflow as tf
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import os
  6. import scipy.io
  7. import scipy.misc
  8.  
  9. def _conv_layer(input,weights,bias):
  10. conv = tf.nn.conv2d(input,tf.constant(weights),strides=(1,1,1,1),padding="SAME")
  11. return tf.nn.bias_add(conv,bias)
  12. def _pool_layer(input):
  13. return tf.nn.max_pool(input,ksize=(1,2,2,1),strides=(1,2,2,1),padding="SAME")
  14. def preprocess(image,mean_pixel):
  15. '''简单预处理,全部图片减去平均值'''
  16. return image - mean_pixel
  17. def unprocess(img,mean_pixel):
  18. return img + mean_pixel
  19. def imread(path):
  20. return scipy.misc.imread(path).astype(np.float)
  21. def imsave(path,img):
  22. img = np.clip(img,0,255).astype(np.uint8)
  23. scipy.misc.imsave(path,img)
  24. def net(data_path,input_image):
  25. """
  26. 读取VGG模型参数,搭建VGG网络
  27. :param data_path: VGG模型文件位置
  28. :param input_image: 输入测试图像
  29. :return:
  30. """
  31. layers = (
  32. 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2','pool1',
  33. 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
  34. 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
  35. 'relu3_3', 'conv3_4', 'relu3_4','pool3',
  36. 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
  37. 'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
  38. 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
  39. 'relu5_3', 'conv5_4', 'relu5_4'
  40. )
  41. data = scipy.io.loadmat(data_path)
  42. mean = data['normalization'][0][0][0]
  43. mean_pixel = np.mean(mean,axis=(0,1))
  44. weights = data['layers'][0]
  45. net = {}
  46. current = input_image
  47. for i, name in enumerate(layers):
  48. kind =name[:4]
  49. if kind == 'conv':
  50. kernels,bias = weights[i][0][0][0][0]
  51. kernels = np.transpose(kernels,(1,0,2,3))
  52. bias = bias.reshape(-1)
  53. current = _conv_layer(current,kernels,bias)
  54. elif kind == 'relu':
  55. current = tf.nn.relu(current)
  56. elif kind == 'pool':
  57. current = _pool_layer(current)
  58. net[name] = current
  59. assert len(net) == len(layers)
  60. return net,mean_pixel,layers
  61.  
  62. if __name__ == '__main__':
  63. VGG_PATH = "./one/imagenet-vgg-verydeep-19.mat"
  64. IMG_PATH = './one/3.jpg'
  65. input_image =imread(IMG_PATH)
  66. shape = (1, input_image.shape[0], input_image.shape[1], input_image.shape[2])
  67. with tf.Session() as sess:
  68. image = tf.placeholder('float', shape=shape)
  69. nets, mean_pixel, all_layers= net(VGG_PATH, image)
  70. input_image_pre=np.array([preprocess(input_image,mean_pixel)])
  71. layers = all_layers
  72.  
  73. for i , layer in enumerate(layers):
  74. print("[%d/%d] %s" % (i+1,len(layers),layers))
  75. features = nets[layer].eval(feed_dict={image:input_image_pre})
  76. print("Type of 'feature' is ",type(features))
  77. print("Shape of 'features' is %s" % (features.shape,))
  78. if 1:
  79. plt.figure(i+1,figsize=(10,5))
  80. plt.matshow(features[0,:,:,0],cmap=plt.cm.gray,fignum=i+1)
  81. plt.title(""+layer)
  82. plt.colorbar()
  83. plt.show()

深度学习之加载VGG19模型获取特征图的更多相关文章

  1. 深度学习之加载VGG19模型分类识别

    主要参考博客: https://blog.csdn.net/u011046017/article/details/80672597#%E8%AE%AD%E7%BB%83%E4%BB%A3%E7%A0% ...

  2. cesium 学习(五) 加载场景模型

    cesium 学习(五) 加载场景模型 一.前言 现在开始实际的看看效果,目前我所接触到基本上都是使用Cesium加载模型这个内容,以及在模型上进行操作.So,现在进行一些加载模型的学习,数据的话可以 ...

  3. WebGL three.js学习笔记 加载外部模型以及Tween.js动画

    WebGL three.js学习笔记 加载外部模型以及Tween.js动画 本文的程序实现了加载外部stl格式的模型,以及学习了如何把加载的模型变为一个粒子系统,并使用Tween.js对该粒子系统进行 ...

  4. 开园第一篇---有关tensorflow加载不同模型的问题

    写在前面 今天刚刚开通博客,主要想法跟之前某位博主说的一样,希望通过博客园把每天努力的点滴记录下来,也算一种坚持的动力.我是小白一枚,有啥问题欢迎各位大神指教,鞠躬~~ 换了新工作,目前手头是OCR项 ...

  5. tensorflow学习--数据加载

    文章主要来自Tensorflow官方文档,同时加入了自己的理解以及部分代码 数据读取 TensorFlow程序读取数据一共有3种方法: 供给数据(Feeding): 在TensorFlow程序运行的每 ...

  6. 【 js 模块加载 】深入学习模块化加载(node.js 模块源码)

    一.模块规范 说到模块化加载,就不得先说一说模块规范.模块规范是用来约束每个模块,让其必须按照一定的格式编写.AMD,CMD,CommonJS 是目前最常用的三种模块化书写规范.  1.AMD(Asy ...

  7. 【 js 模块加载 】【源码学习】深入学习模块化加载(node.js 模块源码)

    文章提纲: 第一部分:介绍模块规范及之间区别 第二部分:以 node.js 实现模块化规范 源码,深入学习. 一.模块规范 说到模块化加载,就不得先说一说模块规范.模块规范是用来约束每个模块,让其必须 ...

  8. 使用 Assimp 库加载 3D 模型

    前言 要想让自己的 3D 之旅多一点乐趣,肯定得想办法找一些有意思一点的 3D 模型.3D 模型有各种各样的格式,obj的,stl的,fbx的等等不一而足.特别是 obj 格式的 3D 模型,完全是纯 ...

  9. cesium加载gltf模型点击以及列表点击定位弹窗

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 之 ...

随机推荐

  1. java—锁的学习研究

    摘抄自博客:https://www.cnblogs.com/qifengshi/p/6831055.html 标题:Java中的锁分类 锁的分类: 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁/ ...

  2. python -m pip install [package] --no-deps

    python -m pip install [package]  --no-deps 有些 packages 会依赖一些其它的 package,当我们离线安装 whl 的时候,就无法联网下载依赖包,所 ...

  3. docker 查看系统进程pid

    docker inspect -f '{{.State.Pid}} {{.Id}}' $(docker ps -a -q)

  4. Linux的正则练习

    grep和 egrep的正则表达式 1.显示三个用户root.wang的UID和默认shell cat /etc/passwd | grep “^\(root\|wang\)” | tr ‘:’ ‘ ...

  5. 如何在网页标题栏title加入logo(icon)图标?

    打开某一个网页会在浏览器的标签栏处显示该网页的标题和图标,当网页被添加到收藏夹或者书签中时也会出现网页的图标,怎么在网页title左边显示网页的logo图标呢? 方法一(被动式): 制作一个ico格式 ...

  6. Linux一些常用的命令

    常见命令 cd命令 cd命令用来切换工作目录至dirname, 其中dirName表示法可为绝对路径或相对路径. pwd命令 pwd命令以绝对路径的方式显示用户当前工作目录. ls命令 ls命令用来显 ...

  7. 数据结构系列文章之队列 FIFO

    转载自https://mp.weixin.qq.com/s/ILgdI7JUBsiATFICyyDQ9w Osprey  鱼鹰谈单片机 3月2日 预计阅读时间: 6 分钟 这里的 FIFO 是先入先出 ...

  8. python大数据初探--pandas,numpy代码示例

    import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...

  9. Laravel 事件侦听的几个方法 [Trait, Model boot(), Observer Class]

    1 Trait 1.1 可以在 Trait 中定义一个静态的 bootFooBar() 方法,注:FooBar 是你的 Trait 名称 namespace App\Traits; use App\A ...

  10. 小米 oj 马走日 (bfs 或 双向bfs)

     马走日 序号:#56难度:困难时间限制:1500ms内存限制:10M 描述 在中国象棋中,马只能走日字型.现在给出一个由 N*M 个格子组成的中国象棋棋盘( 有(N+1)*(M+1)个交叉点可以落子 ...