深度学习之加载VGG19模型获取特征图
1、加载VGG19获取图片特征图
# coding = utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import scipy.io
import scipy.misc def _conv_layer(input,weights,bias):
conv = tf.nn.conv2d(input,tf.constant(weights),strides=(1,1,1,1),padding="SAME")
return tf.nn.bias_add(conv,bias)
def _pool_layer(input):
return tf.nn.max_pool(input,ksize=(1,2,2,1),strides=(1,2,2,1),padding="SAME")
def preprocess(image,mean_pixel):
'''简单预处理,全部图片减去平均值'''
return image - mean_pixel
def unprocess(img,mean_pixel):
return img + mean_pixel
def imread(path):
return scipy.misc.imread(path).astype(np.float)
def imsave(path,img):
img = np.clip(img,0,255).astype(np.uint8)
scipy.misc.imsave(path,img)
def net(data_path,input_image):
"""
读取VGG模型参数,搭建VGG网络
:param data_path: VGG模型文件位置
:param input_image: 输入测试图像
:return:
"""
layers = (
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2','pool1',
'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
'relu3_3', 'conv3_4', 'relu3_4','pool3',
'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
'relu5_3', 'conv5_4', 'relu5_4'
)
data = scipy.io.loadmat(data_path)
mean = data['normalization'][0][0][0]
mean_pixel = np.mean(mean,axis=(0,1))
weights = data['layers'][0]
net = {}
current = input_image
for i, name in enumerate(layers):
kind =name[:4]
if kind == 'conv':
kernels,bias = weights[i][0][0][0][0]
kernels = np.transpose(kernels,(1,0,2,3))
bias = bias.reshape(-1)
current = _conv_layer(current,kernels,bias)
elif kind == 'relu':
current = tf.nn.relu(current)
elif kind == 'pool':
current = _pool_layer(current)
net[name] = current
assert len(net) == len(layers)
return net,mean_pixel,layers if __name__ == '__main__':
VGG_PATH = "./one/imagenet-vgg-verydeep-19.mat"
IMG_PATH = './one/3.jpg'
input_image =imread(IMG_PATH)
shape = (1, input_image.shape[0], input_image.shape[1], input_image.shape[2])
with tf.Session() as sess:
image = tf.placeholder('float', shape=shape)
nets, mean_pixel, all_layers= net(VGG_PATH, image)
input_image_pre=np.array([preprocess(input_image,mean_pixel)])
layers = all_layers for i , layer in enumerate(layers):
print("[%d/%d] %s" % (i+1,len(layers),layers))
features = nets[layer].eval(feed_dict={image:input_image_pre})
print("Type of 'feature' is ",type(features))
print("Shape of 'features' is %s" % (features.shape,))
if 1:
plt.figure(i+1,figsize=(10,5))
plt.matshow(features[0,:,:,0],cmap=plt.cm.gray,fignum=i+1)
plt.title(""+layer)
plt.colorbar()
plt.show()
深度学习之加载VGG19模型获取特征图的更多相关文章
- 深度学习之加载VGG19模型分类识别
主要参考博客: https://blog.csdn.net/u011046017/article/details/80672597#%E8%AE%AD%E7%BB%83%E4%BB%A3%E7%A0% ...
- cesium 学习(五) 加载场景模型
cesium 学习(五) 加载场景模型 一.前言 现在开始实际的看看效果,目前我所接触到基本上都是使用Cesium加载模型这个内容,以及在模型上进行操作.So,现在进行一些加载模型的学习,数据的话可以 ...
- WebGL three.js学习笔记 加载外部模型以及Tween.js动画
WebGL three.js学习笔记 加载外部模型以及Tween.js动画 本文的程序实现了加载外部stl格式的模型,以及学习了如何把加载的模型变为一个粒子系统,并使用Tween.js对该粒子系统进行 ...
- 开园第一篇---有关tensorflow加载不同模型的问题
写在前面 今天刚刚开通博客,主要想法跟之前某位博主说的一样,希望通过博客园把每天努力的点滴记录下来,也算一种坚持的动力.我是小白一枚,有啥问题欢迎各位大神指教,鞠躬~~ 换了新工作,目前手头是OCR项 ...
- tensorflow学习--数据加载
文章主要来自Tensorflow官方文档,同时加入了自己的理解以及部分代码 数据读取 TensorFlow程序读取数据一共有3种方法: 供给数据(Feeding): 在TensorFlow程序运行的每 ...
- 【 js 模块加载 】深入学习模块化加载(node.js 模块源码)
一.模块规范 说到模块化加载,就不得先说一说模块规范.模块规范是用来约束每个模块,让其必须按照一定的格式编写.AMD,CMD,CommonJS 是目前最常用的三种模块化书写规范. 1.AMD(Asy ...
- 【 js 模块加载 】【源码学习】深入学习模块化加载(node.js 模块源码)
文章提纲: 第一部分:介绍模块规范及之间区别 第二部分:以 node.js 实现模块化规范 源码,深入学习. 一.模块规范 说到模块化加载,就不得先说一说模块规范.模块规范是用来约束每个模块,让其必须 ...
- 使用 Assimp 库加载 3D 模型
前言 要想让自己的 3D 之旅多一点乐趣,肯定得想办法找一些有意思一点的 3D 模型.3D 模型有各种各样的格式,obj的,stl的,fbx的等等不一而足.特别是 obj 格式的 3D 模型,完全是纯 ...
- cesium加载gltf模型点击以及列表点击定位弹窗
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 之 ...
随机推荐
- tensorflow保存数据为.pb格式和加载pb文件
转自:https://blog.csdn.net/u014264373/article/details/79943389 https://blog.csdn.net/fu6543210/article ...
- linux基础4-压缩及其相关的命令
一 压缩的原理以及gzip.bzip2.tar三个命令的: Linux下文件的压缩与打包 二 zip.zipinfo.unzip: zip:压缩成.zip文件 zipinfo:列出压缩文件信息
- mysql 5.6.38 数据库编译安装
一.系统环境: # cat /etc/redhat-release CentOS release 6.9 (Final) 二.mysql 编译安装: 1.安装依赖包: yum install -y n ...
- Linux之df磁盘信息
df命令用于查看磁盘的分区,磁盘已使用的空间,剩余的空间 1.用法 df [选项] [文件..] 2.命令选项 -a,--all 全部文件系统-h,--human-readable 以以合适的单位来显 ...
- string::c_str
const char* c_str() const noexcept;功能:返回c风格字符转 #include <iostream>#include <string>#incl ...
- windows OpenCV 2.4.9 Python 2.7配置
1 .下载 OpenCV 2.4.9 .下载OpenCV-2.4.9,使用方便 下载地址 2. OpenCV-自解压文件,直接运行.即可解压.解压到想要的opencv文件夹里E:\Programme\ ...
- Ubuntu操作及Linux基础知识
part 1: Ubuntu操作基础 1.调整字体的大小 调大:crtl+shift+“+” 调小:crtl+“-” 2.不要把虚拟机全屏的时候截屏,要不然会认为是Linux系统截屏而非Window ...
- [Luogu] 排序机械臂
https://www.luogu.org/problemnew/solution/P3165 预处理 我们会发现一个问题:高度是无序的,而splay中要求有序,否则kth不能正确求解.不需要求高度, ...
- spring boot+idea实现程序热部署
pring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. devtools ...
- javascript原型继承
在传统的基于Class的语言如Java.C++中,继承的本质是扩展一个已有的Class,并生成新的Subclass. 由于这类语言严格区分类和实例,继承实际上是类型的扩展.但是,JavaScript由 ...