大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
一、准备模型
在这里,我们利用已经训练好的Googlenet进行物体图像的识别,进入Googlenet的GitHub地址,进入models文件夹,选择Googlenet
点击Googlenet的模型下载地址下载该模型到电脑中。
模型结构
在这里,我们利用之前讲到的网络模型绘制网站画出Googlenet的结构图如下:
-
在这里,pad就是给图像补零,pad:2就是补两圈零的意思;
-
LRN就是局部相应归一化,利用LRN可以提高模型识别的准确率;
-
Inception结构中,不同大小的卷积核意味着不同大小的感受野,最后的合并意味着不同尺度特征的融合。采用1,3,5为卷积核的大小,是因为使用步长为1,pad=0,1,2的方式采样之后得到的特征平面大小相同;
-
concat层用来合并数据,在这里合并的条件是数据的后三个参数要相同,所以在前面的inception结构中,我们使用了不同的卷积核大小和pad。
准备图片
在这里,我们找几张任意图片,然后放入Googlenet的文件夹下,,作为待识别的图片。
准备synset_words.txt文件
synset_words.txt是用来将物体的类别序号进行对应的文件,在识别过程中,我们先是得到序号,然后根据这个序号找到对应的物体种类。
使用python接口调用GoogleNet实现图像识别
在这里,我们用jupyter打开Googlenet.图像识别.ipynb文件,这里部分代码如下:
import caffe
import numpy as np
import matplotlib.pyplot as plt
import os
import PIL
from PIL import Image
import sys
#定义Caffe根目录
caffe_root = ‘E:/caffe-windows/’
#网络结构描述文件
deploy_file = caffe_root+‘models/bvlc_googlenet/deploy.prototxt’
#训练好的模型
model_file = caffe_root+‘models/bvlc_googlenet/bvlc_googlenet.caffemodel’
#cpu模式
caffe.set_mode_cpu()
#定义网络模型
net = caffe.Classifier(deploy_file, #调用deploy文件
model_file, #调用模型文件
mean=np.load(caffe_root +‘python/caffe/imagenet/ilsvrc_2012_mean.npy’).mean(1).mean(1), #调用均值文件
channel_swap=(2,1,0), #caffe中图片是BGR格式,而原始格式是RGB,所以要转化
raw_scale=255, #python中将图片存储为[0, 1],而caffe中将图片存储为[0, 255],所以需要一个转换
image_dims=(224, 224)) #输入模型的图片要是224*224的图片
#分类标签文件
imagenet_labels_filename = caffe_root +‘models/bvlc_googlenet/synset_words.txt’
#载入分类标签文件
labels = np.loadtxt(imagenet_labels_filename, str, delimiter=’\t’)
#对目标路径中的图像,遍历并分类
for root,dirs,files in os.walk(caffe_root+‘models/bvlc_googlenet/image/’):
for file in files:
#加载要分类的图片
image_file = os.path.join(root,file)
input_image = caffe.io.load_image(image_file)
<span class="token comment">#打印图片路径及名称</span>
image_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root<span class="token punctuation">,</span><span class="token builtin">file</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>image_path<span class="token punctuation">)</span>
<span class="token comment">#显示图片</span>
img<span class="token operator">=</span>Image<span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">(</span>image_path<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#预测图片类别</span>
prediction <span class="token operator">=</span> net<span class="token punctuation">.</span>predict<span class="token punctuation">(</span><span class="token punctuation">[</span>input_image<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token string">'predicted class:'</span><span class="token punctuation">,</span>prediction<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>argmax<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 输出概率最大的前5个预测结果</span>
top_k <span class="token operator">=</span> prediction<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>argsort<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> node_id <span class="token keyword">in</span> top_k<span class="token punctuation">:</span>
<span class="token comment">#获取分类名称</span>
human_string <span class="token operator">=</span> labels<span class="token punctuation">[</span>node_id<span class="token punctuation">]</span>
<span class="token comment">#获取该分类的置信度</span>
score <span class="token operator">=</span> prediction<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>node_id<span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'%s (score = %.5f)'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>human_string<span class="token punctuation">,</span> score<span class="token punctuation">)</span><span class="token punctuation">)</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
执行上述代码即可输出对物体种类的预测概率
最终得到的预测结果是根据可能性大小列出五个种类
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-2b43bc2447.css" rel="stylesheet">
</div>
<div class="hide-article-box text-center">
<a class="btn" id="btn-readmore" data-track-view="{"mod":"popu_376","con":",https://blog.csdn.net/dagongsmallguy/article/details/83351025,"}" data-track-click="{"mod":"popu_376","con":",https://blog.csdn.net/dagongsmallguy/article/details/83351025,"}">阅读更多</a>
</div>
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
if(!window.csdn.anonymousUserLimit.judgment()){
window.csdn.anonymousUserLimit.Jumplogin();
return false;
}else if(!currentUserName){
window.csdn.anonymousUserLimit.updata();
}
}
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170449.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...