我们使用的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()

最新文章

  1. swift实现饭否应用客户端源码
  2. Jquery 操作CheckBox ,RadioButtonList,DropDownList
  3. .NET 串口通信中断接收,包含0X1A(作为EOF)
  4. 直接拿来用!最火的Android开源项目(完结篇)
  5. Orchard扩展 自定义后台管理导航菜单 Admin Menu
  6. Php5.3的lambda函数以及closure(闭包)
  7. 页面中引入js的几种方法
  8. Akka(24): Stream:从外部系统控制数据流-control live stream from external system
  9. 引入Log4j
  10. Tkinter界面编程(一)----函数分析
  11. Android开发——设置界面的创建
  12. Python菜鸟快乐游戏编程_pygame(3)
  13. CSS清除浮动的一种简便的方法
  14. win7安装loadrunner 安装相关问题解决方案
  15. 在ubuntu上安装运行ionic项目
  16. crash - JNI WARNING: input is not valid modified utf-8: illegal continuation byte
  17. Android下基于SDL的位图渲染(一)
  18. The Closest M Points
  19. nodejs实现请求代理
  20. javascript 理解对象--- 定义多个属性和读取属性的特性

热门文章

  1. UVA-11491 Erasing and Winning (单调队列)
  2. Vue.js组件设计原则
  3. bzoj1025
  4. bzoj2120
  5. 使用Array类处理基本数组对象
  6. vue.js 源代码学习笔记 ----- text-parse.js
  7. ubuntu16.04下安装opencv3.3
  8. IE、Chrome、Firefox 三大浏览器对比
  9. vm虚拟机怎么访问本地硬盘
  10. macOS --- 配置基于域名的虚拟主机