tensorflow精进之路(二十二)——使用slim模型对图像识别与检测(下)(VGG19模型)
1、概述
上一节使用slim对图像进行识别,但是一张图片里就识别出一样东西,这节我们就来学习怎么检测图片里更多的物品。上一节我们使用的是Inception-ResNet-v2模型,这一节我们使用的是VGG模型。因为VGG在不仅在图像的识别上效果不错,而且在图像检测方面的效果也很好。
2、下载VGG19模型
和上节一样,我们也使用别人在ImageNet上训练好的模型来识别图片内容。打开以下网页,https://github.com/tensorflow/models/tree/master/research/slim
下载VGG 19模型。
3、导入模块
#encoding:utf-8
import tensorflow as tf
from matplotlib import pyplot as plt
from nets import vgg
import numpy as np
from datasets import imagenet
import os
import Image
# 加载像素均值及相关函数
from preprocessing.vgg_preprocessing import (_mean_image_subtraction,_R_MEAN, _G_MEAN, _B_MEAN)
slim = tf.contrib.slim
#这是我们下载的VGG19文件路径
vgg_checkpoint = 'checkpoint/vgg_19.ckpt'#获取imagenet所有分类的名字,这里有1000个分类
names = imagenet.create_readable_names_for_imagenet_labels()
4、预处理图片
#待检测图片
sample_image = '01.jpg'
#读取图片
image = tf.image.decode_jpeg(tf.read_file(sample_image), channels=3)
#将图片数据转成float型
image_float = tf.to_float(image, name='ToFloat')
#将每个像素减去像素的均值
processed_image = _mean_image_subtraction(image_float, [_R_MEAN, _G_MEAN, _B_MEAN])
#增加一个维度
processed_images = tf.expand_dims(processed_image, 0)
我比较想知道图片经过处理以后变成什么样,这里就可以用代码将其现实出来看看,
with tf.Session() as sess:reimg, np_image = sess.run([image, processed_image])
plt.figure()
p1 = plt.subplot(121)
p2 = plt.subplot(122)p1.set_title('Source image')
p1.imshow(reimg)
p1.axis('off')p2.set_title('Preprocessing image')
p2.imshow(np_image)
p2.axis('off')
plt.show()
运行结果如下,
可以看到,减去像素均值以后,图片被分为一块一块的了。
5、创建并载入模型
#创建模型
with slim.arg_scope(vgg.vgg_arg_scope()):logist, _ = vgg.vgg_19(processed_images,num_classes=1000,is_training=False,spatial_squeeze=False)#将logist的最大索引放到pred里,代表分类pred = tf.argmax(logist, dimension=3)#指定模型文件init_fn = slim.assign_from_checkpoint_fn(vgg_checkpoint, slim.get_model_variables('vgg_19'))#run
with tf.Session() as sess:init_fn(sess)reimg, seg, np_image = sess.run([image, pred, processed_image])
6、解析并显示检测结果
#去除空的维度
seg = np.squeeze(seg)
#去除其中重复的元素
unique_classes, relabeled_image = np.unique(seg, return_inverse=True)
seg_size = seg.shaperelabeled_image = relabeled_image.reshape(seg_size)#显示函数
def showlab(img, labels_str = []):plt.figure()p1 = plt.subplot(133)p2 = plt.subplot(132)p3 = plt.subplot(131)#显示原图片p1.set_title('Source image')p1.imshow(reimg)p1.axis('off')#显示预处理图片p2.set_title('Preprocessing image')p2.imshow(np_image)p2.axis('off')p3.set_title('Recognition image')minval = np.min(img)maxval = np.max(img)#显示识别图片cmap = plt.get_cmap('Paired', maxval - minval + 1)mat = p3.matshow(img, cmap = cmap, vmin = minval - 0.5, vmax = maxval + 0.5)cax = plt.colorbar(mat, ticks = np.arange(minval, maxval + 1), shrink = 1.2)cax.ax.set_yticklabels(labels_str)#显示plt.show()labels_names = []
for index, current_class_number in enumerate(unique_classes):labels_names.append(str(index) + ' ' + names[current_class_number + 1])print(str(index) + ' ' + names[current_class_number + 1])showlab(relabeled_image, labels_names)
7、运行结果
截图中解析的结果有点小,我将结果打印出来,
0 hammerhead, hammerhead shark
1 stingray
2 sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita
3 coucal
4 hummingbird
5 spoonbill
6 ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus
7 sturgeon
8 abaya
9 aircraft carrier, carrier, flattop, attack aircraft carrier
10 airship, dirigible
11 balance beam, beam
12 balloon
13 bearskin, busby, shako
14 bell cote, bell cot
15 bikini, two-piece
16 breakwater, groin, groyne, mole, bulwark, seawall, jetty
17 cannon
18 chain
19 clog, geta, patten, sabot
20 dam, dike, dyke
21 drilling platform, offshore rig
22 dumbbell
23 fireboat
24 gown
25 guillotine
26 half track
27 hoopskirt, crinoline
28 knot
29 lumbermill, sawmill
30 maillot
31 maillot, tank suit
32 miniskirt, mini
33 motor scooter, scooter
34 overskirt
35 parachute, chute
36 patio, terrace
37 pot, flowerpot
38 punching bag, punch bag, punching ball, punchball
39 radio telescope, radio reflector
40 sarong
41 schooner
42 seat belt, seatbelt
43 shoe shop, shoe-shop, shoe store
44 suit, suit of clothes
45 swimming trunks, bathing trunks
46 swing
47 umbrella
48 volleyball
49 wing
50 worm fence, snake fence, snake-rail fence, Virginia fence
51 lakeside, lakeshore
52 groom, bridegroom
53 rapeseed
从上面可以看到,我们上一节识别出的裙子,这里也识别出来了,在第24项(gown)。第28项(knot),连裙子上的蝴蝶结也能识别出来了。
8、完整代码
#encoding:utf-8
import tensorflow as tf
from matplotlib import pyplot as plt
from nets import vgg
import numpy as np
from datasets import imagenet
import os# 加载像素均值及相关函数
from preprocessing.vgg_preprocessing import (_mean_image_subtraction,_R_MEAN, _G_MEAN, _B_MEAN)
slim = tf.contrib.slim
#原谅我穷屌丝,电脑显卡配置太低导致内存溢出,只能用cpu计算了
os.environ["CUDA_VISIBLE_DEVICES"]="-1"#这是我们下载的VGG19文件路径
vgg_checkpoint = 'checkpoint/vgg_19.ckpt'#获取imagenet所有分类的名字,这里有1000个分类
names = imagenet.create_readable_names_for_imagenet_labels()#待检测图片
sample_image = '01.jpg'
#读取图片
image = tf.image.decode_jpeg(tf.read_file(sample_image), channels=3)
#将图片数据转成float型
image_float = tf.to_float(image, name='ToFloat')
#将每个像素减去像素的均值
processed_image = _mean_image_subtraction(image_float, [_R_MEAN, _G_MEAN, _B_MEAN])
#增加一个维度
processed_images = tf.expand_dims(processed_image, 0)#创建模型
with slim.arg_scope(vgg.vgg_arg_scope()):logist, _ = vgg.vgg_19(processed_images,num_classes=1000,is_training=False,spatial_squeeze=False)#将logist的最大索引放到pred里,代表分类pred = tf.argmax(logist, axis=3)#指定模型文件init_fn = slim.assign_from_checkpoint_fn(vgg_checkpoint, slim.get_model_variables('vgg_19'))#run
with tf.Session() as sess:init_fn(sess)reimg, seg, np_image = sess.run([image, pred, processed_image])#去除空的维度
seg = np.squeeze(seg)
#去除其中重复的元素
unique_classes, relabeled_image = np.unique(seg, return_inverse=True)
seg_size = seg.shaperelabeled_image = relabeled_image.reshape(seg_size)#显示函数
def showlab(img, labels_str = []):plt.figure()p1 = plt.subplot(133)p2 = plt.subplot(132)p3 = plt.subplot(131)#显示原图片p1.set_title('Source image')p1.imshow(reimg)p1.axis('off')#显示预处理图片p2.set_title('Preprocessing image')p2.imshow(np_image)p2.axis('off')p3.set_title('Recognition image')minval = np.min(img)maxval = np.max(img)#显示识别图片cmap = plt.get_cmap('Paired', maxval - minval + 1)mat = p3.matshow(img, cmap = cmap, vmin = minval - 0.5, vmax = maxval + 0.5)cax = plt.colorbar(mat, ticks = np.arange(minval, maxval + 1), shrink = 1.2)cax.ax.set_yticklabels(labels_str)#显示plt.show()labels_names = []
for index, current_class_number in enumerate(unique_classes):labels_names.append(str(index) + ' ' + names[current_class_number + 1])print(str(index) + ' ' + names[current_class_number + 1])showlab(relabeled_image, labels_names)
9、试试其他图片
将
#待检测图片
sample_image = '01.jpg'
改成
#待检测图片
sample_image = '02.jpg'
运行结果,
$ python demo7.py
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
我靠?怎么回事?百度一下,说是数据量太大造成的。那先试试bus.jpg,将代码改成,
#待检测图片
sample_image = 'bus.jpg'
运行结果,
这张图只识别了校车。那张02.jpg图片真是有毒。重启电脑再运行试试......
我丢,重启以后运行就好了,运行结果如下,
居然识别出109个物体???识别打印如下,
0 stingray
1 water ouzel, dipper
2 bald eagle, American eagle, Haliaeetus leucocephalus
3 great grey owl, great gray owl, Strix nebulosa
4 axolotl, mud puppy, Ambystoma mexicanum
5 ringneck snake, ring-necked snake, ring snake
6 horned viper, cerastes, sand viper, horned asp, Cerastes cornutus
7 black widow, Latrodectus mactans
8 tick
9 ptarmigan
10 African grey, African gray, Psittacus erithacus
11 sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita
12 platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus
13 nematode, nematode worm, roundworm
14 conch
15 American egret, great white heron, Egretta albus
16 bustard
17 king penguin, Aptenodytes patagonica
18 albatross, mollymawk
19 Chihuahua
20 Maltese dog, Maltese terrier, Maltese
21 Italian greyhound
22 Scottish deerhound, deerhound
23 Kerry blue terrier
24 Sealyham terrier, Sealyham
25 komondor
26 Bouvier des Flandres, Bouviers des Flandres
27 miniature pinscher
28 Bernese mountain dog
29 boxer
30 dalmatian, coach dog, carriage dog
31 affenpinscher, monkey pinscher, monkey dog
32 toy poodle
33 Mexican hairless
34 Persian cat
35 Siamese cat, Siamese
36 Egyptian cat
37 lacewing, lacewing fly
38 Angora, Angora rabbit
39 hamster
40 porcupine, hedgehog
41 beaver
42 chimpanzee, chimp, Pan troglodytes
43 marmoset
44 ambulance
45 Band Aid
46 bathing cap, swimming cap
47 bathtub, bathing tub, bath, tub
48 bearskin, busby, shako
49 bonnet, poke bonnet
50 bow tie, bow-tie, bowtie
51 brassiere, bra, bandeau
52 broom
53 car mirror
54 cassette
55 Christmas stocking
56 crash helmet
57 garbage truck, dustcart
58 gown
59 jean, blue jean, denim
60 jersey, T-shirt, tee shirt
61 knot
62 lens cap, lens cover
63 letter opener, paper knife, paperknife
64 Loafer
65 mask
66 mitten
67 mortar
68 mouse, computer mouse
69 muzzle
70 oxygen mask
71 packet
72 paintbrush
73 paper towel
74 Petri dish
75 photocopier
76 plastic bag
77 pop bottle, soda bottle
78 punching bag, punch bag, punching ball, punchball
79 rubber eraser, rubber, pencil eraser
80 rugby ball
81 rule, ruler
82 sandal
83 scale, weighing machine
84 ski mask
85 sleeping bag
86 sock
87 spider web, spider's web
88 stethoscope
89 sunglass
90 sunglasses, dark glasses, shades
91 sunscreen, sunblock, sun blocker
92 swab, swob, mop
93 swimming trunks, bathing trunks
94 thimble
95 toilet seat
96 water bottle
97 wig
98 Windsor tie
99 wooden spoon
100 wool, woolen, woollen
101 crossword puzzle, crossword
102 ice cream, icecream
103 ice lolly, lolly, lollipop, popsicle
104 mashed potato
105 cucumber, cuke
106 Granny Smith
107 banana
108 custard apple
109 dough
看看有没有上一节的墨西哥玉米煎饼(burrito)?搜了一下,并没有......说明使用不同模型的识别还是有点区别的。但是它居然识别出了黄瓜(cucumber,第105项),但是,看看第105项附近的结果,
102 ice cream, icecream
103 ice lolly, lolly, lollipop, popsicle
104 mashed potato
105 cucumber, cuke
106 Granny Smith
107 banana
108 custard apple
109 dough
有香蕉、冰淇淋、马铃薯泥、面团等,其实跟这个黄瓜还是有点像的,那我猜测,这个模型当检测到的物体模棱两可时,它也将可能的结果列出来了,所以这张图片就识别出来109个物体,这纯粹是我一本正经的胡说八道,瞎猜的。
总结:
这种图像检测方式其实不太好用,只是用颜色标注,还识别出一堆东西,想应用就比较困难,有没有什么方法,能将识别到的物体框出来并标注呢?这就是我们下一节要讲的了。快凌晨一点半了,莫老师刚做完家务,真是辛苦了,休息了~
tensorflow精进之路(二十二)——使用slim模型对图像识别与检测(下)(VGG19模型)相关推荐
- TensorFlow精进之路(十二):随时间反向传播BPTT
1.概述 上一节介绍了TensorFlow精进之路(十一):反向传播BP,这一节就简单介绍一下BPTT. 2.网络结构 RNN正向传播可以用上图表示,这里忽略偏置. 上图中, x(1:T)表示输入序列 ...
- TensorFlow精进之路(十五):深度神经网络简介
1.概述 本来想用卷积神经网络来预测点东西,但是效果嘛......,还是继续学习图像类的应用吧-前面学习的神经网络都是一些基础的结构,这些网络在各自的领域中都有一定效果,但是解决复杂问题肯定不够的,这 ...
- OpenCV精进之路(十二):详解掩膜mask
在OpenCV中我们经常会遇到一个名字:Mask(掩膜).很多函数都使用到它,那么这个Mask到底什么呢? 一开始我接触到Mask这个东西时,我还真是一头雾水啊,也对无法理解Mask到底有什么用.经过 ...
- TensorFlow精进之路(十六):使用slim模型库对图片分类
1.概述 TF-slim是tensorflow的一个轻量级库,它将很多常见tensorflow函数进行封装,使的模型的构建.训练.测试都更加简洁,特别适用于构建结构复杂的深度神经网络.github地址 ...
- TensorFlow精进之路(十四):RNN训练MNIST数据集
1.概述 前面介绍了RNN,这一节就用tensorflow的RNN来训练MNIST数据集,看看准确率如何. 2.代码实现 2.1.导入数据集 # encoding:utf-8 import tenso ...
- TensorFlow精进之路(十):循环神经网络RNN
1.概述 本来想继续学习tensorflow图像方面的应用,但是循环神经网络的某一个应用吸引到了我,所以就先学学这个循环神经网络. 2.用处 前面学习的全连接神经网络或者卷积神经网络,网络结构都是从输 ...
- 一位中科院自动化所博士毕业论文的致谢:二十二载风雨求学路,他把自己活成了光.........
4月18日,中国科学院官方微博发布消息,披露了这篇论文为<人机交互式机器翻译方法研究与实现>,作者是2017年毕业于中国科学院大学的工学博士黄国平. 这篇论文中情感真挚的<致谢> ...
- 读书二十二载,信念很简单,把书念下去,然后走出去,不枉活一世。
关注+星标公众号,不错过精彩内容 昨天看了一篇关于博士论文的致谢,看的我想哭.改变命运有很多条路,但是对于大多数人而言,读书是改变命运的捷径. 内容是讲述了自己20多年的求学经历:小时候煤油灯 ...
- Android项目实战(二十二):启动另一个APP or 重启本APP
Android项目实战(二十二):启动另一个APP or 重启本APP 原文:Android项目实战(二十二):启动另一个APP or 重启本APP 一.启动另一个APP 目前公司项目需求,一个主AP ...
最新文章
- php abstract
- 三大主流软件负载均衡器(LVS、Nginx、HAproxy) 与商业SLB比较
- 四管前级怎么去掉高低音音调_TDG Audio达芬奇:什么是前级,后极?
- 双指针解决数组排序问题
- java lt10_PAT 1006 换个格式输出整数 (15)(C++JAVAPython)
- linux计算脚本执行时间,linux – 计算shell脚本每一步的时间并显示总执行时间
- 查看linux系统属性
- 你可能会用到的 Mock 小技巧
- Python入门到精通三天速成第二讲——类与继承
- Objective-C学习之旅(四)----内存管理2----retain点语法
- Rational Rose和UML可视化建模基础
- 微信小程序服务通知 模板消息
- 训练深度学习模型时电脑自动重启
- 腾讯云点播视频存储(Web端视频上传)
- 【算法】递归|迷宫回溯问题|八皇后问题
- 时间处理 - Joda
- VSCode快速生成HTML基础模板--输入!一键快捷生成--无法自动生成的解决办法
- [TYVJ 1927] 『Citric II』一道防AK好题 · 模拟
- 听好程序员一句劝!零基础这样学Java才是正确的方式!
- IDEA插件translation翻译插件“翻译失败:未知错误”
热门文章
- 未设置BufferSize导致FTP下载速度过慢的问题
- 【数据结构笔记08】哨兵查找、二分查找、树、儿子-兄弟表示法、二叉树的引子
- 装建津说计算机丢失,宽带连接上网时老是连接不上说缺少netcfg.hlp文件怎么办...
- xstream java_XStream将java转化成xml的简单使用
- java+yeild+sleep_java之yield(),sleep(),wait()区别详解-备忘笔记[转]
- SQL语句查询条数不足10行时如何自动补充空行
- 假如明天灾难来临_北京医保容灾
- 粒度计算机英语,An Overview of Granular Computing粒度计算研究综述
- 手机发包工具_【发包工具】http多线程发包工具
- 一个Win32程序的进化