一. 概述
本文将介绍一套非常好用的一个免费资源,由 Google 所提供的 Colab 这项云端服务。
延续上一章节的理念,此系列的目的为工具书导向,这里将介绍如何利用 Colab 建立一套物件辨识应用,读者仅须要依照步骤,按部就班即可完成实作!! 时不宜迟,赶紧动手操作吧!! 如下图文章架构图所示,此架构图隶属于 i.MX8M Plus 的方案博文中,并属于 Third Party 软体资源的 Google Colab 密技大公开 之部分,目前章节介绍 “利用 Colab 打造 AI 学习库”。
若新读者欲理解更多人工智能、机器学习以及深度学习的资讯,可点选查阅下方博文
大大通精彩博文 【ATU Book-i.MX8系列】博文索引
Colab 系列博文-文章架构示意图
二. 利用 Colab 打造 AI 学习库
如下图所示,本文章会先由 Google 提供的 Colab 之 GPU 资源训练出一套关于物件识别模组,接着将训练完成的模组移植至 NXP i.MX8M Plus 开发板中运行。
(1) 透过 Colab 训练出物件识别模组 :
开启 Colab 笔记本一步一步运行代码,即可完成实作 !!
结合迁移学习方法与 TF-Slim资料库实现 TOTORO 物件检测器(Object Detector)
PS : 灰底为程式储存格的代码,复制贴上至 Colab 即可使用 !!
第一步 : 开启 Colab 设定环境
%tensorflow_version 1.x
!python -c 'import matplotlib as tf; print(tf.__version__)' # Check the version of the tensorflow
第二步 : TensorFlow Model Garden 下载与安装
%cd root
!git clone https://github.com/tensorflow/models.git
%cd root/models/research/
!protoc object_detection/protos/*.proto --python_out=. # gernate *.proto
!python setup.py build # 建置 TensorFlow Model Garden 档案
第三步 : TensorFlow Slim 下载与安装
import os
os.environ['PYTHONPATH'] += ':/root/models/research/:/root/models/research/slim/:/root/models/research/object_detection/utils/:/root/models/research/object_detection'
!pip install tf_slim # 安装 TensorFlow Slim
!python object_detection/builders/model_builder_test.py # TensorFlow Slim 模组建立是否成功测试
第四步 : 下载资料库
常见的物件识别的资料库为 COCO DataSets
读者也可以更换资料库就可以训练出不同的识别应用。
%cd /root/models/
!git clone https://github.com/fllay/totoro.git #Download TOTORO
第五步 : 数据特征处理
解析纪录物件之位置特征与分类的 XML 档案,如代码所示。
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
# 将 xml 档资料转换成 DataFrame 形式
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
# 将 xml 资料转换成 train_labels.csv 与 test_labels.csv 两个档案
def main():
image_path = os.path.join(os.getcwd(), 'totoro/images/train')
xml_df = xml_to_csv(image_path)
xml_df.to_csv('totoro/data/train_labels.csv', index=None)
image_path = os.path.join(os.getcwd(), 'totoro/images/test')
xml_df = xml_to_csv(image_path)
xml_df.to_csv('totoro/data/test_labels.csv',index=None)
main()
第六步 : 制作 TensorFlow Record
欲了解 TensorFlow Record 介绍,可参考 TensorFlow 官方网站资讯,或是 github 作者所撰写的 generate_tfrecord.py 用法。
%cd /root/models/totoro/tfrecord
!python generate_tfrecord.py --csv_input=/root/models/totoro/data/train_labels.csv \
--output_path=train.record --image_dir=/root/models/totoro/images/train
!python generate_tfrecord.py --csv_input=/root/models/totoro/data/test_labels.csv\
--output_path=test.record --image_dir=/root/models/totoro/images/test
PS : 执行后,将于 models/ totoro/tfrecord 资料夹内产出 train.record & test.record' 档案
第七步 : 下载训练过的 MobileNet 模组
此步骤利用之前训练过的模组资源重新训练,即 迁移学习(Transfer Learning) 的技术,未来将有系列博文专门介绍,敬请期待 !!
%cd ~/models
import shutil
import tarfile
from requests import get
MODEL = 'ssd_mobilenet_v1_coco_2017_11_17'
MODEL_FILE = MODEL + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
DEST_DIR = 'pretrained_model'
# 下载mobilenet 模组
if not (os.path.exists(MODEL_FILE)):
with open(MODEL_FILE, "wb") as file:
response = get(DOWNLOAD_BASE + MODEL_FILE)
file.write(response.content)
# 解压缩 mobilenet 模组
tar = tarfile.open(MODEL_FILE)
tar.extractall()
tar.close()
os.remove(MODEL_FILE)
if (os.path.exists(DEST_DIR)):
shutil.rmtree(DEST_DIR)
os.rename(MODEL, DEST_DIR)
# 移动 mobilenet.config" 资讯
shutil.move( "/root/models/research/object_detection/samples/configs/ssd_mobilenet_v1_coco.config", "/root/models" )
第八步 : 修改 Config 档案
%cd /root/models/research/
# 编辑Pipeline 资讯
import tensorflow as tf
from google.protobuf import text_format
from object_detection.protos import pipeline_pb2
pipeline = pipeline_pb2.TrainEvalPipelineConfig()
config_path = '/root/models/ssd_mobilenet_v1_coco.config'
with tf.gfile.GFile( config_path, "r") as f:
proto_str = f.read()
text_format.Merge(proto_str, pipeline)
pipeline.train_input_reader.tf_record_input_reader.input_path[:] = ['/root/models/totoro/tfrecord/train.record'] # train data
pipeline.train_input_reader.label_map_path = '/root/models/totoro/data/object-detection.pbtxt'
pipeline.eval_input_reader[0].tf_record_input_reader.input_path[:] = ['/root/models/totoro/tfrecord/test.record'] # test data
pipeline.eval_input_reader[0].label_map_path = '/root/models/totoro/data/object-detection.pbtxt' # network
pipeline.train_config.fine_tune_checkpoint = '/root/models/pretrained_model/model.ckpt' # weight
pipeline.train_config.num_steps = 500 # training step
pipeline.model.ssd.num_classes = 2 # classes num
pipeline.eval_config.num_examples = 5 # test image number
config_text = text_format.MessageToString(pipeline)
with tf.gfile.Open( config_path, "wb") as f:
f.write(config_text)
第九步 : 进行训练
!python /root/models/research/object_detection/legacy/train.py \
--logtostderr \
--train_dir=/root/models/trained \
--pipeline_config_path=/root/models/ssd_mobilenet_v1_coco.config
PS : 训练完成后,将于 models/trained/ 资料夹内产出 model.ckpt-500 档案
第十步 : 产生 Frozen Graph
此步骤可以调整模组输出大小,比如说将原本输入大小 224x224 改成 96x96 。
PS : 训练完成后,将于 models/fine_tuned_model / 资料夹内产出 tflite_graph.pb 档案
!python /root/models/research/object_detection/export_tflite_ssd_graph.py \
--pipeline_config_path=/root/models/ssd_mobilenet_v1_coco.config \
--output_directory=/root/models/fine_tuned_model \
--trained_checkpoint_prefix=/root/models/trained/model.ckpt-500
第十一步 : TensorFlow Lite 转换
PS : 训练完成后,将于 models/fine_tuned_model / 资料夹内产出 mobilenetssd_totoro_uint8.tflite 档案
# 此处以指令方式进行转换,亦可使用上述文章所介绍代码方式。
! tflite_convert \
--output_file=/root/models/fine_tuned_model/ mobilenetssd_totoro_uint8.tflite \
--graph_def_file=/root/models/fine_tuned_model/tflite_graph.pb \
--inference_type=QUANTIZED_UINT8 \
--input_arrays=normalized_input_image_tensor \
--input_shapes=1,300,300,3 \
--output_arrays= 'TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3’ \
--default_ranges_min=0 \
--default_ranges_max=6 \
--mean_values=128 \
--std_dev_values=127 \
--allow_custom_ops
第十二步 : Object Detection 范例实现
建立 app.py 开始撰写 python 代码,如下:
import cv2
import numpy as np
from tflite_runtime.interpreter import Interpreter
# 解析 tensorflow lite 档案
interpreter = Interpreter(model_path='mobilenetssd_totoro_uint8.tflite') # 记得将模组移动至 i.MX8 平台
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
width = input_details[0]['shape'][2]
height = input_details[0]['shape'][1]
# 读取测试资料,并设置于解译器中
frame = cv2.imread(' test.jpg’)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb, (width, height))
input_data = np.expand_dims(frame_resized, axis=0)
interpreter.set_tensor(input_details[0]['index'], input_data)
# 进行推理
interpreter.invoke()
# 取得输出资料
detection_boxes = interpreter.get_tensor(output_details[0]['index']) # 输出位置资讯
detection_classes = interpreter.get_tensor(output_details[1]['index']) # 输出类别资讯
detection_scores = interpreter.get_tensor(output_details[2]['index']) # 输出分数资讯
num_boxes = interpreter.get_tensor(output_details[3]['index'])
# 标示物件
for i in range(10):
if detection_scores[0, i] > .5: # 预测值大于 0.5则显示
x = detection_boxes[0, i, [1, 3]] * frame_rgb.shape[1]
y = detection_boxes[0, i, [0, 2]] * frame_rgb.shape[0]
class_id = detection_classes[0, i]
cv2.rectangle(frame_rgb, (x[0], y[0]), (x[1], y[1]), (0, 255, 0), 2)
cv2.imshow('TOTORO',frame_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
(2) 于 NXP i.MX8M Plus 实作 Object Detection 范例实现 :
开启 NXP i.MX8M Plus 开发板后,即可将 模组(mobilenetssd_totoro_uint8.tflite)、测试图档(test.jpg)、程式码(app.py) 传送至开发板中。
# 利用网路将档案传至开发板中
$ scp mobilenetssd_totoro_uint8.tflite root@xx.xx.xx.xx:~
$ scp test.jpg root@xx.xx.xx.xx:~
$ scp app.py root@xx.xx.xx.xx:~
接上萤幕,运行物件识别 DEMO。
$ python3 app.py
如下图所示,成功检测出豆豆龙(物件) !! 在 i.MX8M Plus 的 NPU 处理器,推理时间(Inference Time) 约 9 ms。
三. 结语
本文主要目的是推广 Colab 的实用性为主,其用意是希望读者可以将此系列博文当作一套工具书来查阅,来达到快速应用之目的。本文介绍了如何利用 Colab 与迁移学习的方式来客制化属于自己的 AI 模型,那本篇是以豆豆龙作呈现,后续读者若是想创造不同的物件识别目的时,仅须要更换训练的资料库来源即可,像是识别人脸或是手部皆可以这种方式实现 !! 笔者这么推崇 Colab 作为机器学的入门必学项目,其理由是 Colab 不须花费自身电脑资源之外,更看中的是结合 Google Drive 的这项应用,每当建立一套笔记本或是新的应用时,就能自动储存至该使用者帐户中,实为方便 !! 利用这项特点,不断累积新的技术就能打造自身的 AI 学习库了!!
四. 参考文件
[1] 官方文件 - Colaboratory 官网
[2] 第三方文件 -鸟哥的首页
如有任何相关 Colab 技术问题,欢迎至博文底下留言提问 !!
接下来还会分享更多 Colab 的技术文章 !!敬请期待 【ATU Book-i.MX8 系列 - Colab】 !!
评论