当前位置:首页 > 百科 > 正文

基础人工智能3(人工智能的发展)

ImageAI是一套Python的电脑视觉编程库,主要提供三大功能:Image prediction、Object detection、Video object Detection and tracking。

基础人工智能3(人工智能的发展)  第1张

作者是两个住在奈及利亚拉哥斯的黑人兄弟Moses Olafenwa和John Olafenwa,强调编程设计皆是自学而来,除了ImageAI,他们还另外开发了一套基于PyTorch的开源Deep learning framework,这些套件展现了他们非凡的编程功力和AI学养。

功能说明

Image prediction

针对Image prediction有四种深度学习模型可选择:

– SqueezeNet

Size为4.82 mb,预测速度最快,精确度中等。

SqueezeNet由UC Berkeley和Stanford所合作开发,与MobileNet、ShuffleNet、Xception并称为四种目前最知名的轻量级影像识别模型,它们皆针对模型中CNN卷积的方法作了改善,以减少深度并加快其效率。

– ResNet50

由Microsoft Research 所研发的模型,Size为98 mb,预测速度快,精确度高。

ResNet全名是Residual Neural Network,由微软研究院所开发,中文多半翻译为残差网路。它的特点是神经网路不一定要一层层接续往下执行,可以是跳跃方式略过下一层而直达下下层。因为它改善了深度持续增加时所出现的Degradation问题,因此ResNet可以更深,如ResNet50。

– InceptionV3

Inception是由Google Brain team所研发,Size为91.6 mb,预测速度慢,但精确度较高。

Inception是一种network in network的架构,针对输入资料同时并行不同filter尺寸的卷积处理和max pooling,最后再进行级联,这让系统在同一层便能取得不同level的特征。Inception是GoogLeNet的核心架构,一般所称的Inception的各系列版本,即是指GoogLeNet。

– DenseNet121

这是由Facebook AI Research所开发的模型,Size为31.6 mb,预测速度最慢,但精确度最高。

透过大量Dense block(直接与下层layer建立连接,以避免梯度消失)使用,并减少每层的复杂度,DenseNet号称使用大约一半的参数就能够达到与ResNet相同的表现,但这是指相同的层数下,故DenseNet121预测速度仍较ResNet50慢。

速度:SqueezeNet>ResNet50>InceptionV3>DenseNet121

精度:DenseNet121>InceptionV3>ResNet50>SqueezeNet

Object detection

针对Object detection功能,目前仅RetinaNet可选择,但官方说明会持续提供其它的模型选择。

RetinaNet

目前主流的Object detection技术主要有两种:one-stage及two-stage。

two-stage需先透过传统Image pyramid、或后来改进的Object Box Proposals等等方式来输出候选视窗,再去评估各区域的物件机率,此技术以Faster RCNN为代表。缺点是消耗的资源多,效率较低,但输出结果的准确度高。

one-stage则以YOLO、SSD为最知名,可直接在影像各区域上计算物件机率、或直接判断物件。这一类方法速度相当快,但准确度就没有two-stage高。

Video object Detection and tracking

影片物件侦测功能其实是进行多张图片进行连续快速的detection,因此与Object detection相同,目前仅支援RetinaNet。

安装

目前ImageAI仅支援Python3.5.1以上的版本,整个安装过程在半小时左右便可顺利结束。

安装必要的模组:

pip3 install –upgrade tensorflow

pip3 install numpy

pip3 install scipy

pip3 install opencv-python

pip3 install pillow

pip3 install matplotlib

pip3 install h5py

pip3 install keras

主编程:

pip3 install

使用说明

Image prediction

ImageAI针对影像辨识提供了前述的四种模型SqueezeNet, ResNet, InceptionV3及DenseNet,并提供 ImageNet-1000 影像资料库所训练好的参数让我们直接使用。(ImageNet-1000有120万张相片,1000种物件类型。)

编程:

from imageai.Prediction import ImagePrediction

import os

execution_path = os.getcwd()

prediction = ImagePrediction()

prediction.setModelTypeAsResNet()

prediction.setModelPath(os.path.join(execution_path, “prediction/resnet50_weights_tf_dim_ordering_tf_kernels.h5″))

prediction.loadModel()

predictions, probabilities = prediction.predictImage(os.path.join(execution_path, “predict1.jpg"), result_count=5 )

for eachPrediction, eachProbability in zip(predictions, probabilities):

print(eachPrediction + " : " + eachProbability)

不到十行编程便可完成相片预测功能了!

上述编程中所定义的预训练模型参数档 .h5可从github下载,共有四种。下面为我们要预测的图片predict1.jpg。

基础人工智能3(人工智能的发展)  第2张

B) OBJECT DETECTION

Object detection的功能透过ImageAI framework也是相当简单。

from imageai.Detection import ObjectDetection

import os

execution_path = os.getcwd()

detector = ObjectDetection()

detector.setModelTypeAsRetinaNet()

detector.setModelPath( os.path.join(execution_path , “detection/resnet50_coco_best_v2.0.1.h5″))

detector.loadModel()

detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , “2.jpg"), output_image_path=os.path.join(execution_path , “2new.jpg"))

for eachObject in detections:

print(eachObject[“name"] + " : " + eachObject[“percentage_probability"] )

print(“——————————–“)

RetinaNet的预训练权重档请从github下载。

我们以这张相片2.jpf为例。

基础人工智能3(人工智能的发展)  第3张

其预测结果如下:

基础人工智能3(人工智能的发展)  第4张

C) VIDEO OBJECT DETECTION AND TRACKING

ImageAI提供的Video object detection使用的模型为RetinaNet,可侦测80种物件类别如下:

person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop_sign, parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot dog, pizza, donot, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair dryer, toothbrush.

不过严格来说,从ImageAI提供的文件上看不出有Tracking的功能,仅能加入custom_objects参数来指定要侦测特定的物件类别。此外,Video object detection功能并非实时播放影片侦测,而是将输入影片进行侦测后再输出为新的影片,如果我们需要即时的处理影片串流,应该使用其object detection功能by frame来处理。

CUSTOM MODEL TRAINING & PREDICTION

值得一提的是,ImageAI提供很方便的方法,可让您训练自己的Image prediction模型,也是有四种模型可选:SqueezeNet、ResNet50、InceptionV3、DenseNet121,训练完之后,可得到.json(label定义)及.h5(模型权重档),我们可透过这两个档案来进行影像的预测。

Step1:Dataset准备

举例来说,如果我想训练一个数字0~5的手势模型,那么,我需要准备如格式下的image dataset,两层式的资料夹,第一层为dataset名称,第二层为各类别名称,然后在第二层资料夹放入相对应的图片。

基础人工智能3(人工智能的发展)  第5张

Step2:定义训练参数

下面五行

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()

model_trainer.setModelTypeAsInceptionV3()

model_trainer.setDataDirectory(“gesture")

model_trainer.trainModel(num_objects=6, num_experiments=10, enhance_data=True, batch_size=32, show_network_summary=True)

model_trainer.setModelTypeAsInceptionV3() → 指定训练模型为InceptionV3

model_trainer.setDataDirectory(“gesture") → 定义该dataset的第一层目录。

num_objects=6 → 要训练的物件类别数目,以本例来说,有0~5 六种手势。

num_experiments → 要训练的迭代次数,亦即epoch

enhance_data → 资料强化,亦即data augmentation,此选项为optional。

batch_size → 每一批次训练的image数目。

show_network_summary → 显示模型的层次架构,类似keras的model.summary。

Step3:开始训练

执接执行上方的编程,便会开始训练。建议一定要在GPU环境执行,否则原本2个小时的训练可能一整天也跑不完。执行时如下,一开始会显示模型的layer定义,然后开始训练。如果您有安装Nvidia Cuda的GPU环境,那么编程会自动使用GPU来训练。

训练了50 epochs,最终vlaidation成绩是0.9650

Step4:使用此模型

最后,让我们在编程中使用此模型吧,model_palm_mixed.h5以及model_palm_mixed.json是刚刚产生的档案。这个手势辨识dataset是我用webcamera拍摄自己手部摆出的0, 1, 2 ~ 5的数字手势。

import cv2

import matplotlib.pyplot as plt

%matplotlib inline

from imageai.Prediction.Custom import CustomImagePrediction

import os

execution_path = os.getcwd()

prediction = CustomImagePrediction()

prediction.setModelTypeAsInceptionV3()

prediction.setModelPath(os.path.join(execution_path, “model_palm_mixed.h5″))

prediction.setJsonPath(os.path.join(execution_path, “model_palm_mixed.json"))

prediction.loadModel(num_objects=7)

imgPath = “/home/chtseng/works/train-opencvCascade/keras/datasets/palm/train/3/aug_0_3215.jpg"

img = cv2.imread(imgPath)

plt.imshow(img[…,::-1])

#辨识command

predictions, probabilities = prediction.predictImage(imgPath, result_count=2 )

for eachPrediction, eachProbability in zip(predictions, probabilities):

print(eachPrediction + " : " + eachProbability)

下面我们从Test dataset(非train dataset)找出几个样本来测试。看起来效果相当不错,不过这些图片都是来自我自己的手势,样本变化太小,仅能作为测试性质。

基础人工智能3(人工智能的发展)  第6张

ImageAI这个套件相当的方便简单,适合刚入门想尝试一下计算机视觉的朋友们。

延伸阅读:谷歌的TPU是什么?真能吊打GPU?

谷歌的TPU预计将减少对大型数据中心的需求,否则需要更多的CPU和GPU来处理人工智能应用程序,从语音识别和分析,图像和视频处理,到提供搜索服务,再到那些小谷歌智能家庭系统。

TPU是针对TensorFlow上的机器学习工作负载量身定制的定制应用专用集成电路(ASIC)。 虽然第一代TPU仅用于推理,但Cloud TPU适用于推理和机器学习培训。 Cloud TPU采用四个定制ASIC构建,可提供强大的64 GB高带宽内存和180 TFLOPS性能。

去年,谷歌宣称它的TPU比现代GPU和推理CPU快15到30倍,并且TOPS / Watt测量值提高了30-80倍。在机器学习培训中,Cloud TPU的性能更强(180对120 TFLOPS),内存容量(64 GB对16 GB内存)比Nvidia最好的GPU Tesla V100大四倍。

基础人工智能3(人工智能的发展)  第7张

此外,谷歌还发布了Edge TPU,特定用途 ASIC 芯片,专为在设备上运行 TensorFlow Lite ML 模型而设计。Edge TPU 可以加速设备上的 ML 推理,或与 Google Cloud 配对,创建完整的云到边缘 ML 堆栈。针对Edge TPU的新设备是:AIY Edge TPU Dev 开发板和 AIY Edge TPU 加速器,二者皆采用 Google 的 Edge TPU 芯片。