#
#作者:韦访
#博客:https://blog.csdn.net/rookie_wei
#微信:1007895847
#添加微信的备注一下是CSDN的
#欢迎大家一起学习
#

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个物体,这纯粹是我一本正经的胡说八道,瞎猜的。

总结:

这种图像检测方式其实不太好用,只是用颜色标注,还识别出一堆东西,想应用就比较困难,有没有什么方法,能将识别到的物体框出来并标注呢?这就是我们下一节要讲的了。快凌晨一点半了,莫老师刚做完家务,真是辛苦了,休息了~

如果您感觉本篇博客对您有帮助,请打开支付宝,领个红包支持一下,祝您扫到99元,谢谢~~

tensorflow入门教程(二十二)使用slim对图像识别与检测(下)相关推荐

  1. 【OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接: http://blog.csdn.net/poem_qianmo/article/details/25560901 作者:毛星云(浅墨) ...

  2. origin如何将多个曲线放在一张图_Origin入门教程(十二):误差棒棒棒棒儿

    精选回顾 ◀ 干货满满!计算狗「模拟计算干货」汇总大放送,你想要的都在这里~ ◀ 免费干货丨测试狗透射电子显微镜(TEM)资料汇总「TEM全家福」,从入门 到精通! ◀ SCI论文写作大全来啦!助你早 ...

  3. 新手必备pr 2021快速入门教程「十二」PR导出高清视频

    PR2021快速入门教程,学完之后,制作抖音视频,vlog,电影混剪,日常记录等不在话下!零基础,欢迎入坑! 本节内容 视频剪辑成我们想要的样子后,接下来重要的一步,就是导出!但是"如何导出 ...

  4. Adobe Premiere Pro 2020 入门教程(十二)视频特效

    目录 一.视频效果 --- 扭曲 --- 偏移 1.效果 2.操作过程 二.视频效果 --- 扭曲 ---变形稳定器 1.效果 三.视频效果 --- 扭曲 --- 变换 1.效果 2.操作过程 四.视 ...

  5. tensorflow入门教程(三十四)疲劳检测之开眼闭眼识别

    # #作者:韦访 #博客:https://blog.csdn.net/rookie_wei #微信:1007895847 #添加微信的备注一下是CSDN的 #欢迎大家一起学习 # ------韦访 2 ...

  6. tensorflow入门教程(四十四)人体姿态检测(二)

    # #作者:韦访 #博客:https://blog.csdn.net/rookie_wei #微信:1007895847 #添加微信的备注一下是CSDN的 #欢迎大家一起学习 # ------韦访 2 ...

  7. Spring Boot入门教程(四十二):微信支付集成-H5支付

    分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 一:开发文档 场景介绍 H5支付是指商户在微信客户端外的移动端网页展示商品或服务,用户在前述页面确认使用微信支付时,商户发 ...

  8. ReportStudio入门教程(七十二) - 显示时间进度(进度条版)

    上一次, 我们只是使用一个文字显示出了进度,这回呢, 我们使用一个像进度条一样的图形来显示 像这样: 1. CSS样式 这是为了控制样式 <style type="text/css&q ...

  9. 微信小程序开发入门教程(十二)

    背景 上一篇文章我们讲述了微信小程序的三个基础组件icon.text和progress.这些基础组件主要用途是进行信息展示,微信小程序除了信息展示还需要与用户交互,而表单是应用中获取用户输入的重要手段 ...

最新文章

  1. 计算机书籍-人工智能时代的设计师生存手册
  2. GOOGLE突破图书馆入口IP限制之技巧
  3. eclipse的下载JDK的安装与配置
  4. 现代制造工程课堂笔记07——应力应变分析(考点应力莫尔圆)
  5. 【Ubuntu】ubuntu系统下python3和python2环境自由切换
  6. JAR包、WAR包及EAR包的区别
  7. 【零知ESP8266教程】快速入门28 六轴传感器模块的使用
  8. 基于三星ARM9(S3C2410)的交通违章抓拍系统的开发
  9. CocosCreator H5 微信内置浏览器调起微信支付
  10. 已经选择IDE模式安装完操作系统后开启AHCI的方法
  11. AT91SAM9261开发板SBC6000X(转)
  12. java 判断是不是图片_java判断是否是图片
  13. 地理信息服务器架设,基于arcgis+server构建地理信息服务.pdf
  14. 初中化学人教版教案四-Leo老师
  15. ex2 ex3_他甩了我后,我Ex了我的前任
  16. 誓死要将Notepad++拉下马,大佬推出了一款国产开源编辑器.....
  17. 哈哈,原来老酷比我的更新还要慢~~ 嘿嘿
  18. 孜然导航系统V2.5.5_站长导航系统源码_网址导航源码
  19. 微信小程序实现动态获取输入框的字符长度
  20. 推荐:chrome 浏览器的 Json 格式插件

热门文章

  1. 智能车学习(二十三)——浅谈心得体会
  2. 谷歌浏览器打开播放链接,视频无法播放并且视频黑屏,没声音
  3. VIM-状态栏插件(powerline)
  4. 中国量子计算机“婴儿”诞生
  5. 【云服务器】基于docker容器部署Halo项目的个人博客搭建并部署到云服务器
  6. python 小说下载_通过python自动获取小说并下载
  7. mysql webhook_webhook功能概述
  8. Deep Learning of Binary Hash Codes for Fast Image Retrieval
  9. 华为鸿蒙系统公测时间,华为鸿蒙系统下月公测,这37款华为/荣耀手机有望首升!...
  10. 手把手叫你如何集成高德地图,实现地图显示、定位蓝点、大头针显示、获取周围地点信息等