从零开始机器学习-2 十分钟让你的AI睁开双眼

本文由 沈庆阳 所有,转载请与作者取得联系!
以人认识世界为例,当我们的眼睛捕捉到现实世界的图像的时候,图像会通过视觉神经传输到我们大脑的视觉中枢,形成视觉。数码相机也是一样的道理,现实世界反射的光进入到相机的感光芯片,然后感光芯片将信号传输到相机的处理芯片中形成图像。

人脑的神经网络

然而仅仅形成了视觉而理解不了视觉,这样就和植物没什么两样。图像经过我们大脑的处理,让我们认识了世界上的各种花花草草,认识了我们的宠物猫和各种物体。通过长时间的生活,人类的大脑中的神经网络形成了各种特殊的回路,使我们可以认识世界上的各种物体。
那么,如何才能让人工智能程序认识世界呢?

什么是目标检测(Object Detection)

目标检测或对象检测。目标检测是计算机视觉的一个分支,通常指的是在一幅图像中将物体框出来,进而识别该物体是什么。物体检测是分类和定位的综合。


物体检测的例子

在上述图像中,我们可以发现通过物体检测,人工智能的程序框出了图中的人和风筝,并且列出了这些物体的类别名称和可信度。
通过目标检测,我们可以让AI认识世界。可以实现照片的快速分类,可以使自动驾驶汽车认识路上遇到的人、车和道路,可以识别步入银行的重点客户等。目标检测为人工智能的各种应用提供了无限的可能。

TensorFlow的Object Detection API

TensorFlow的物体检测API提供了精确地机器学习模型,该模型能够定位和识别单张图片中的多种物体。物体的分类和定位在计算机视觉中一直都是一个难题,更是对机器学习的一个挑战。然而,Google的人工智能框架TensorFlow可以做到快速的实时的物体分类与定位。
在上一节中,我们在TensorFlow的Github页面下载了相关的模型(Models)。

https://github.com/tensorflow/models/

TensorFlow的Models仓库中有各种训练好的模型。
其中,Official Models为官方提供的模型,官方模型是对TensorFlow高层API的实现,由官方团队对其进行维护,拥有更好的稳定性。
其次,Research Models为各种人工智能学者们发表的论文中提到的各种模型,由研究者对其进行发布和维护。
而我们所需要的Object Detection则在Research Models中。
如果你在继续进行下去的时候提示缺少某些软件,请使用apt-get install 命令或pip命令进行安装
进入Object Detection的目录,并在当前目录中Jupyter Notebook。

cd <Models Path>/research/object_detection
jupyter notebook

此时会打开一个你当前目录下面的Web页面,在当前工作目录下找到object_detection_tutorial.ipynb,点击打开,会出现如下的界面。


物体检测的Notebook界面
关于Jupyter Notebook:
Jupyter Notebook(前身为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。
Jupyter Notebook可以编写漂亮的交互式文档,在文档中,我们可以修改代码,运行代码,观察代码输出的结果等。

点击最上方的Cell菜单,点击Run All,并静心等待结果。在运行过程中,我们把页面拉到最下方,不一会便会出现物体检测程序执行的结果。


物体检测程序运行结果

很好,该程序成功地认出了图片中的两只狗,并且框出了狗所在的位置和标注了准确度。但是,我们并不想仅仅识别教程图片中的狗狗,我们的目标是通过摄像头让人工智能程序认识我们身边的事物。

修改程序

回到我们的Jupyter Notebook中,点击左上角的菜单File->Download as->Python(.py)将笔记本到处为python的代码。


导出为Python的程序

修改厚的的Python代码如下

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image


import cv2

cap=cv2.VideoCapture(0)

if tf.__version__ < '1.4.0':
  raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')


# ## Env setup

# In[2]:


# This is needed to display the images.
#get_ipython().magic(u'matplotlib inline')

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")


# ## Object detection imports
# Here are the imports from the object detection module.

# In[3]:


from utils import label_map_util

from utils import visualization_utils as vis_util


# # Model preparation 

# ## Variables
# 
# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  
# 
# By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

# In[4]:


# What model to download.
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90


# ## Download Model

# In[5]:


opener = urllib.request.URLopener()
opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
  file_name = os.path.basename(file.name)
  if 'frozen_inference_graph.pb' in file_name:
    tar_file.extract(file, os.getcwd())


# ## Load a (frozen) Tensorflow model into memory.

# In[6]:


detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

# In[7]:


label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)


# ## Helper code

# In[8]:


def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)


# # Detection

PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)

# In[10]:


with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    while True:
      ret, image_np=cap.read()
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      image_np_expanded = np.expand_dims(image_np, axis=0)
      # Actual detection.
      (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8)
      cv2.imshow('object detection',cv2.resize(image_np,(800,600)))
      if cv2.waitKey(25) & 0xFF ==ord('q'):
          cv2.destroyAllWindows()
          break

在上述代码中,我们加入了python-opencv来从电脑的摄像头获取图像,然后传入原程序中进行处理。并在最后使用OpenCV的imshow函数来输出。整个程序执行的效果如下:


运行效果

控制台输出:

root@jack-QTJ5:~/Dev/tensorflow/models/research/object_detection# python object_detection_CONVERTED.py 
2018-01-12 16:09:56.439397: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-01-12 16:09:56.672274: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-01-12 16:09:56.672731: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 860M major: 5 minor: 0 memoryClockRate(GHz): 1.0195
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.74GiB
2018-01-12 16:09:56.672748: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0, compute capability: 5.0)

此时我们的程序已经开始执行了,并且通过摄像头获取到了图像,实时标出了常用物体模型内的物体名称和位置,其运行速度达到了实时的标准。在TensorFlow的Object Detection API的基础上,我们可以给的智能小车、智能家居和无人机等加上各种不可思议的功能。
在下一节中,我们将会开始学习怎样训练自己的模型,训练我们想要识别的物体的模型。

觉得写的不错的朋友可以点一个 喜欢♥ ~
谢谢你的支持!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335