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.shape
relabeled_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.shape
relabeled_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个物体,这纯粹是我一本正经的胡说八道,瞎猜的。
总结:
这种图像检测方式其实不太好用,只是用颜色标注,还识别出一堆东西,想应用就比较困难,有没有什么方法,能将识别到的物体框出来并标注呢?这就是我们下一节要讲的了。快凌晨一点半了,莫老师刚做完家务,真是辛苦了,休息了~