跟我学算法- tensorflow VGG模型进行测试
我们使用的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模型进行测试的更多相关文章
- 跟我学算法- tensorflow模型的保存与读取 tf.train.Saver()
save = tf.train.Saver() 通过save. save() 实现数据的加载 通过save.restore() 实现数据的导出 第一步: 数据的载入 import tensorflo ...
- 跟我学算法-tensorflow 实现神经网络
神经网络主要是存在一个前向传播的过程,我们的目的也是使得代价函数值最小化 采用的数据是minist数据,训练集为50000*28*28 测试集为10000*28*28 lable 为50000*10, ...
- 跟我学算法-tensorflow 实现logistics 回归
tensorflow每个变量封装了一个程序,需要通过sess.run 进行调用 接下来我们使用一下使用mnist数据,这是一个手写图像的数据,训练集是55000*28*28, 测试集10000* 28 ...
- 跟我学算法-tensorflow 实现线性拟合
TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...
- 跟我学算法- tensorflow 卷积神经网络训练验证码
使用captcha.image.Image 生成随机验证码,随机生成的验证码为0到9的数字,验证码有4位数字组成,这是一个自己生成验证码,自己不断训练的模型 使用三层卷积层,三层池化层,二层全连接层来 ...
- 跟我学算法- tensorflow 实现RNN操作
对一张图片实现rnn操作,主要是通过先得到一个整体,然后进行切分,得到的最后input结果输出*_w[‘out’] + _b['out'] = 最终输出结果 第一步: 数据载入 import ten ...
- 跟我学算法-tensorflow 实现卷积神经网络附带保存和读取
这里的话就不多说明了,因为上上一个博客已经说明了 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...
- 跟我学算法-tensorflow 实现卷积神经网络
我们采用的卷积神经网络是两层卷积层,两层池化层和两层全连接层 我们使用的数据是mnist数据,数据训练集的数据是50000*28*28*1 因为是黑白照片,所以通道数是1 第一次卷积采用64个filt ...
- 一步步教你轻松学朴素贝叶斯模型算法Sklearn深度篇3
一步步教你轻松学朴素贝叶斯深度篇3(白宁超 2018年9月4日14:18:14) 导读:朴素贝叶斯模型是机器学习常用的模型算法之一,其在文本分类方面简单易行,且取得不错的分类效果.所以很受欢迎,对 ...
随机推荐
- hell 1>&2 2>&1 &>filename重定向的含义和区别
当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 一.linux重定 ...
- android 笔记一
1.Fragment创建extents FragmentView rootView = inflater.inflate(R.layout.fragment_main, container, fals ...
- Git Error: warning: refname 'origin/branch-name' is ambiguous.
When this happened, it created the file .git/refs/heads/origin/branch-name. So, I just deleted the f ...
- SUSE Linux Enterprise Server设置IP地址、网关、DNS(转载)
说明: ip:192.168.21.172 子网掩码:255.255.255.0 网关:192.168.21.2 dns:8.8.8.8 8.8.4.4 1.设置ip地址vi /etc/sysco ...
- k8s PersistentVolume hostpath 简单使用
kubernets host PersistentVolume 测试 因为yaml 格式的问题 ,我修改为了json 创建 pv pv.json { "kind": "P ...
- PDO exec 执行时出错后如果修改数据会被还原?
PDO exec 执行时出错后如果修改数据会被还原? 现象 FastAdmin 更新了 1127 版本,但是使用在线安装方式出现无法修改管理员密码的问题. 一直是默认的 admin 123456 密码 ...
- SpookyOTP
https://pypi.python.org/pypi/SpookyOTP/1.1.1 SpookyOTP 1.1.1 Downloads ↓ A lightweight Python 2/3 pa ...
- 后端渲染html、前端模板渲染html,jquery的html
作者:赵魏璇链接:https://www.zhihu.com/question/28725977/answer/116177149来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- sublime 下面开发
sublime 下面开发 开发 ptyon 简单环境 1. 下载sublime 3 https://download.sublimetext.com/Sublime%20Text%20Build%20 ...
- Eclipse下使用PySpark报Could not find valid SPARK_HOME while searching
主要是Eclipse下需要多一步配置,就是需要加上SPARK_HOME的系统变量. 首先,在Windows的环境变量中加入SPARK_HOME 然后,在Eclipse的配置中加入这个环境变量: Win ...