vgg16神经网络复用

加载vgg16数据,复现神经网络

#!/usr/bin/python
#coding:utf-8

import inspect
import os
import numpy as np
import tensorflow as tf
import time
import matplotlib.pyplot as plt

VGG_MEAN = [103.939, 116.779, 123.68]      # 图像的平均像素值

class Vgg16():
    def __init__(self, vgg16_path=None):
        if vgg16_path is None:
            vgg16_path = os.path.join(os.getcwd(), "vgg16.npy")     # os函数拼接路径
            self.data_dict = np.load(vgg16_path, encoding='latin1').item()   # 读取vgg_16数据文件

    def forward(self, images):
        
        print("build model started")
        start_time = time.time() 
        rgb_scaled = images * 255.0
        # 将r,g,b转换为b,g,r并减去一个平均值
        red, green, blue = tf.split(rgb_scaled,3,3) 
        bgr = tf.concat([     
            blue - VGG_MEAN[0],
            green - VGG_MEAN[1],
            red - VGG_MEAN[2]],3)
        # 第一层 两层卷积,一层最大池化
        self.conv1_1 = self.conv_layer(bgr, "conv1_1") 
        self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
        self.pool1 = self.max_pool_2x2(self.conv1_2, "pool1")
        # 第二层 和第一层相同
        self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
        self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
        self.pool2 = self.max_pool_2x2(self.conv2_2, "pool2")
        # 第三层
        self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
        self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
        self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
        self.pool3 = self.max_pool_2x2(self.conv3_3, "pool3")
        # 第四层
        self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
        self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
        self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
        self.pool4 = self.max_pool_2x2(self.conv4_3, "pool4")
        # 第五层
        self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
        self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
        self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
        self.pool5 = self.max_pool_2x2(self.conv5_3, "pool5")
        # 全连接层
        self.fc6 = self.fc_layer(self.pool5, "fc6") 
        self.relu6 = tf.nn.relu(self.fc6) 
        # 全连接层
        self.fc7 = self.fc_layer(self.relu6, "fc7")
        self.relu7 = tf.nn.relu(self.fc7)
        # 最后经过一个全连接层,输出
        self.fc8 = self.fc_layer(self.relu7, "fc8")
        self.prob = tf.nn.softmax(self.fc8, name="prob")
        
        end_time = time.time()
        # 测一下时间
        print(("time consuming: %f" % (end_time-start_time)))

        self.data_dict = None 
        
    def conv_layer(self, x, name):
        with tf.variable_scope(name):
            # 获取卷积核
            w = self.get_conv_filter(name)
            # 进行卷积操作
            conv = tf.nn.conv2d(x, w, [1, 1, 1, 1], padding='SAME')
            # 获得偏置层
            conv_biases = self.get_bias(name)
            # 偏置层并加激活函数
            result = tf.nn.relu(tf.nn.bias_add(conv, conv_biases)) 
            return result
    
    def get_conv_filter(self, name):
        # 根据名字检索到对应的卷积核
        return tf.constant(self.data_dict[name][0], name="filter") 
    
    def get_bias(self, name):
        # 根据名字检索到对应的偏置
        return tf.constant(self.data_dict[name][1], name="biases")
    
    def max_pool_2x2(self, x, name):
        # 进行2x2最大池化操作
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
    
    def fc_layer(self, x, name):
        with tf.variable_scope(name):
            # 获取张量的形状
            shape = x.get_shape().as_list() 
            dim = 1
            for i in shape[1:]:
                dim *= i
                # 将张量变为一维化
            x = tf.reshape(x, [-1, dim])
            # 获取全连接w参数
            w = self.get_fc_weight(name)
            # 获取全连接偏置参数
            b = self.get_bias(name) 
            # 计算全连接
            result = tf.nn.bias_add(tf.matmul(x, w), b) 
            return result
    
    def get_fc_weight(self, name):
        # 获取对应全连接层参数
        return tf.constant(self.data_dict[name][0], name="weights")

图片裁剪函数,传入输入函数

#!/usr/bin/python
#coding:utf-8
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from pylab import mpl

# mpl.rcParams['font.sans-serif']=['SimHei'] # 正常显示中文标签
# mpl.rcParams['axes.unicode_minus']=False # 正常显示正负号

def load_image(path):
    fig = plt.figure("Centre and Resize")
    # 读取图片
    img = io.imread(path)
    # 处理图片 归一化
    img = img / 255.0
    ax0 = fig.add_subplot(131)  
    ax0.set_xlabel(u'Original Picture')
    # 显示图片
    ax0.imshow(img)
    # 裁剪边缘/边缘处理
    short_edge = min(img.shape[:2])
    y = (img.shape[0] - short_edge) // 2
    x = (img.shape[1] - short_edge) // 2
    crop_img = img[y:y+short_edge, x:x+short_edge] 
    
    ax1 = fig.add_subplot(132) 
    ax1.set_xlabel(u"Centre Picture") 
    ax1.imshow(crop_img)
    # 重新修改图片尺寸
    re_img = transform.resize(crop_img, (224, 224)) 

    ax2 = fig.add_subplot(133) 
    ax2.set_xlabel(u"Resize Picture") 
    ax2.imshow(re_img)
    img_ready = re_img.reshape((1, 224, 224, 3))

    return img_ready

def percent(value):
    return '%.2f%%' % (value * 100)

应用函数(主函数),输入图片,并做预测

#coding:utf-8
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import vgg16
import utils
from Nclasses import labels

# img_path = input('Input the path and image name:')
img_path = "D:\\python学习\\复现神经网络\\pic\\c.jpg"
img_ready = utils.load_image(img_path)
fig=plt.figure(u"Top-5 预测结果")


with tf.Session() as sess:
    images = tf.placeholder(tf.float32, [1, 224, 224, 3])   # 输入占位
    vgg = vgg16.Vgg16()  # 加载vgg16网络对象c
    vgg.forward(images)  # 传入vgg网络参数
    probability = sess.run(vgg.prob, feed_dict={images:img_ready})   # 用sess运行
    top5 = np.argsort(probability[0])[-1:-6:-1]   # 读取概率值最大前五个数
    print("top5:",top5)
    values = []
    bar_label = []
    # 分别输出5个预测的概率的直方图
    for n, i in enumerate(top5): 
        print("n:",n)
        print("i:",i)
        values.append(probability[0][i]) 
        bar_label.append(labels[i]) 
        print(i, ":", labels[i], "----", utils.percent(probability[0][i]) )
        
    ax = fig.add_subplot(111) 
    ax.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc='g')
    ax.set_ylabel(u'probabilityit') 
    ax.set_title(u'Top-5') 
    for a,b in zip(range(len(values)), values):
        ax.text(a, b+0.0005, utils.percent(b), ha='center', va = 'bottom', fontsize=7)   
    plt.show() 

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/80484403
今日推荐