我们使用的VGG模型是别人已经训练好的一个19层的参数所做的一个模型

第一步:定义卷积分部操作函数

mport scipy.io
import numpy as np
import os
import scipy.misc
import matplotlib.pyplot as plt
import tensorflow as tf # 进行卷积操作
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(image, mean_pixel):
return image + 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):
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))
# 重构reshape
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

第三步: 构造文件路径

# 返回当前的路径
cwd = os.getcwd()
# 别人已经训练好的模型
VGG_PATH =cwd + '/data/imagenet-vgg-verydeep-19.mat'
IMG_PATH = cwd + '/data/cat.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):
# 输出模型的单个特征
features = nets[layer].eval(feed_dict={image:input_image})
print(" Type of 'features' 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()

跟我学算法- tensorflow VGG模型进行测试的更多相关文章

  1. 跟我学算法- tensorflow模型的保存与读取 tf.train.Saver()

    save =  tf.train.Saver() 通过save. save() 实现数据的加载 通过save.restore() 实现数据的导出 第一步: 数据的载入 import tensorflo ...

  2. 跟我学算法-tensorflow 实现神经网络

    神经网络主要是存在一个前向传播的过程,我们的目的也是使得代价函数值最小化 采用的数据是minist数据,训练集为50000*28*28 测试集为10000*28*28 lable 为50000*10, ...

  3. 跟我学算法-tensorflow 实现logistics 回归

    tensorflow每个变量封装了一个程序,需要通过sess.run 进行调用 接下来我们使用一下使用mnist数据,这是一个手写图像的数据,训练集是55000*28*28, 测试集10000* 28 ...

  4. 跟我学算法-tensorflow 实现线性拟合

    TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...

  5. 跟我学算法- tensorflow 卷积神经网络训练验证码

    使用captcha.image.Image 生成随机验证码,随机生成的验证码为0到9的数字,验证码有4位数字组成,这是一个自己生成验证码,自己不断训练的模型 使用三层卷积层,三层池化层,二层全连接层来 ...

  6. 跟我学算法- tensorflow 实现RNN操作

    对一张图片实现rnn操作,主要是通过先得到一个整体,然后进行切分,得到的最后input结果输出*_w[‘out’] + _b['out']  = 最终输出结果 第一步: 数据载入 import ten ...

  7. 跟我学算法-tensorflow 实现卷积神经网络附带保存和读取

    这里的话就不多说明了,因为上上一个博客已经说明了 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...

  8. 跟我学算法-tensorflow 实现卷积神经网络

    我们采用的卷积神经网络是两层卷积层,两层池化层和两层全连接层 我们使用的数据是mnist数据,数据训练集的数据是50000*28*28*1 因为是黑白照片,所以通道数是1 第一次卷积采用64个filt ...

  9. 一步步教你轻松学朴素贝叶斯模型算法Sklearn深度篇3

    一步步教你轻松学朴素贝叶斯深度篇3(白宁超   2018年9月4日14:18:14) 导读:朴素贝叶斯模型是机器学习常用的模型算法之一,其在文本分类方面简单易行,且取得不错的分类效果.所以很受欢迎,对 ...

随机推荐

  1. IOS-网络(文件上传)

    // // ViewController.m // IOS_0206_文件上传 // // Created by ma c on 16/2/6. // Copyright © 2016年 博文科技. ...

  2. CC工具列表

    QuasarRAT   Adwind Adzok Arcom Babylon Blacknix Blue Banana Bozok Coringa DarkComet DRAT Gh0st Huige ...

  3. gdb调试libtool封装的可执行文件

    http://www.gnu.org/software/libtool/manual/html_node/Debugging-executables.html 3.4 Debugging execut ...

  4. VS2017 IDE中发布自包含(SCD)DotNET Core项目

    根据Stack Overflow上的一个回答得知,这项功能目前VS2017并不具备,但你可以通过如下方法发布自包含项目: 1.项目文件(.csproj)中添加RuntimeIdentifier配置项, ...

  5. Qt Creatror使用designer修改了界面但是编译无反应的解决方法

    这个问题主要是UI没有更新导致的,根治的方法为: 项目中的.pro内增加 UI_DIR=./UI,同时删除掉源代码目录中ui_*.h,clear all,->qmake->rebuilt ...

  6. 通过Ftp put命令上传导致文件损坏的解决办法

    通过Linux命令行向在一台Windows FTP服务器上传文件.然后在另一台Windows客户机登录FTP服务器下载,但是下载后的文件大小变了,exe文件错误了不能正确执行.刻意打包的文件(.rar ...

  7. 1月中旬值得一读的10本技术新书(机器学习、Java、大数据等)!

    1月中旬,阿里云云栖社区 联合 博文视点 为大家带来十本技术书籍(机器学习.Java.大数据等).以下为书籍详情,文末还有福利哦! 书籍名称:Oracle数据库问题解决方案和故障排除手册 内容简介 & ...

  8. centos7 中将执行文件python链接为python3后 如何保证 yum 功能不受影响

    1.  查看  /usr/bin  中  python 执行文件的 链接情况 2.  设置   python  命令的可执行文件  链接为    python3 3.  此时 , yum  文件中的p ...

  9. oracle之 Got minus one from a read call 与 ORA-27154: post/wait create failed

    在部署应用的时候,有时候应用可以直接启动,但偶尔应用却无法启动,报错信息是: java.sql.SQLRecoverableException: IO Error: Got minus one fro ...

  10. phpstorm 光标设置

    1.是否允许光标定位在行尾之后的任意位置Allow placement of caret after end of line 这个设置是上下换行的时候,光标可以定位在任意位置,只能通过方向键移动光标, ...