#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_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()#实例化vgg,运行了初始化函数,读出了保存在npy内的模型参数 vgg.forward(images)#复现神经网络结构 probability = sess.run(vgg.prob, feed_dict={images:img_ready})#把待识别图像img_ready作为输入喂入计算sotfmax的节点vgg.prob,网络输出probability就是通过网络vgg16后预测出的1000分类的概率分布 top5 = np.argsort(probability[0])[-1:-6:-1]#把分类中概率最高的五个存入top5中 print("top5:",top5)#top5中得知即为Nclasses中的键值 values = [] bar_label = [] 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) #构建1行1列的图,“111”表示“1×1网格,第一子图”,“234”表示“2×3网格,第四子图”。 ax.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc='g')#柱状图 ax.set_ylabel(u'probabilityit') #y轴标签 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) 图上方0.005位置, 文本位于水平居中位置, 底部 字号 plt.show() #弹出窗口,展示图片
#utils.py # 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) # 将像素归一化处理到[0,1] img = img / 255.0 # 将该画布分为1行3列,把下面的图像放在画布的第1个位置 ax0 = fig.add_subplot(131) # 添加子标签 ax0.set_xlabel(u'Original Picture') # 添加展示该图像 ax0.imshow(img) # 找到该图像的最短边 short_edge = min(img.shape[:2]) # 把图像的w和h分别减去最短边,并求平均 y = int((img.shape[0] - short_edge) / 2) x = int((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) # resize成固定的imagesize 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)
#vgg16.py #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") self.data_dict = np.load(vgg16_path, encoding='latin1').item() print(self.data_dict) def forward(self, images):#复现了网络结果 print("build model started") start_time = time.time() rgb_scaled = images * 255.0 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):#最大池化 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 = 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")