在上一篇TensorFlow实战 9 GoogleLeNet神经网络(2014 ILSVRC 1st in Error(Top-5))文章中简单介绍GoogleLeNet的特点及V1-v4系列重要特点,通过使用slim库构建inception v3版本的神经网络,分析了前向传播的时间花费,本次借助已训练好googleLeNet网络实现不同大小尺寸图片的识别。
文件列表:
1)bvlc_googlenet.caffemodel:以训练好的GoogleLeNet网络;
2)bvlc_googlenet.prototxt:网络描述文件,包括权重参数等;
3)synset_words.txt:标签文件
其中2)、3)分别在OpenCV文件的等dnn目录下可查找到,不同版本的OpenCV目录稍有差别,参数也不尽相同,这里(密码:x6kb)分享已经打包好的文件,供学习者使用。
python代码实现图像分类代码:
import numpy as np
import cv2 as cv
# read names of classes
with open('synset_words.txt') as f:
classes = [x[x.find(' ') + 1:] for x in f]
#read image
image = cv.imread('goldfish.jpg')
cv.imshow("image", image)
# create tensor with 224x224 spatial size and subtract mean values (104, 117, 123)
# from corresponding channels (R, G, B)
input = cv.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))
# load model from caffe
net = cv.dnn.readNetFromCaffe('bvlc_googlenet.prototxt', 'bvlc_googlenet.caffemodel')
# feed input tensor to the model
net.setInput(input)
# perform inference and get output
out = net.forward()
# get indices with the highest probability
indexes = np.argsort(out[0])[-5:]
# define font style
font = cv.FONT_HERSHEY_COMPLEX
#print the last five most likelihold result
for i in reversed(indexes):
print('class:', classes[i], ' probability:', out[0][i])
#add txet into the result image
cv.putText(image, classes[indexes[-1]], (20, 20), font, 1.0, (0, 0, 255), 2)
cv.imshow("result", image)
#save the result image
cv.imwrite("./goldfish_res.jpg", image)
cv.waitKey(0)
识别效果还不错,效果展示如下:
practice makes perfect!