我们使用的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. 在请求中使用XML Publisher生成文件报错

    在页面上使用按钮生成该文件不报错,但是使用请求就报错. 错误内容如下 Error : No corresponding LOB data found :SELECT L.FILE_DATA FILE_ ...

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

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

  3. 【Matplotlib】概要总览第一讲

    之前一直使用 matplotlib, 但都是随用随查,现在特开此系列帖子已记录其学习过程. Matplotlib可能是Python 扩展包中仅有的最流行的 2D 绘图库.她不仅提供了快速的方式可视化P ...

  4. 008PHP文件处理——文件操作r w (用的比较多) a x(用的比较少) 模式 rewind 指针归位:

    <?php /** *文件操作r w (用的比较多) a x(用的比较少) 模式 rewind 指针归位: */ /*$a=fopen('a.txt','r'); echo fread($a,f ...

  5. hdu5575

    题解: 每一次最短的那块板合并 先装水到溢出 然后合并 代码: #include<cstdio> #include<cstring> #include<algorithm ...

  6. myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为????

    B 问题:myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为???? 解决:调整JSP页面编码:gb2312--->utf-8

  7. 来来来,有讲一个吐血的故事(matlab)之脚本运行路径是什么

    脚本运行路径是什么,这真是太重要!! 重要1:你默认保存的路径 重要2:你访问的相对路径 先放图: 再看一幅图: 我的操作,点击左侧的文件夹,使上框的显示栏路径不一样,再点击运行,发现pwd指示的路径 ...

  8. 《FDTD electromagnetic field using MATLAB》读书笔记之一阶、二阶偏导数差商近似

  9. 【idea】如何彻底卸载idea

    卸载MAC中的IDEA Intellij 14 使用命令行: 1 2 3 4 5 6 cd /Applications/ rm -r IntelliJ\ IDEA\ 14.app/ rm -r /Us ...

  10. FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的?

    FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的? 在使用 FastAdmin 时你会发现 url 中有一个 ref=addtabs . 以下是 Karson 的解释 这个 ...