ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

深度学习之加载VGG19模型获取特征图

2019-07-03 17:50:10  阅读:423  来源: 互联网

标签:layers VGG19 image current 深度 input mean pixel 加载


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()

 

标签:layers,VGG19,image,current,深度,input,mean,pixel,加载
来源: https://www.cnblogs.com/ywjfx/p/11127943.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有