開題
經(jīng)歷了近一周的摸索,Tensorflow這個(gè)深度學(xué)習(xí)框架读规,也算是有一個(gè)了結(jié)抓督,總結(jié)一下吧!
這個(gè)流程一下子看上去涉及的東西的確是很大的束亏,所以細(xì)分為以下步驟:
一铃在、 Python 、Tensorflow 安裝及環(huán)境配置
二碍遍、 Object Detection API配置
三、 LabelImage對訓(xùn)練樣本標(biāo)注處理
四揣炕、 標(biāo)注后訓(xùn)練樣本驗(yàn)證樣本格式轉(zhuǎn)換tfrecord
五东跪、 訓(xùn)練模型選取及參數(shù)配置
六畸陡、 定位在Object Detection文件下train.py開始訓(xùn)練
七、 上一步訓(xùn)練結(jié)果固化成pb模型
八虽填、 視頻流中調(diào)用模型預(yù)測
下面的文章我也將對以上步驟從個(gè)人開發(fā)角度闡述下:
一、python 牲览、tensorflow的安裝問題此處不做說明(tensorflow我用的cpu版本、IDE:PyCharm)
二桑驱、關(guān)于 Object Detection API的配置
1竭恬、請前往官方地址下載 https://github.com/tensorflow/models
發(fā)現(xiàn)現(xiàn)在通過git和直接download下載很慢甚至下載一半就掛掉了,所以百度網(wǎng)盤提供目前我訓(xùn)練用的models文件(非最新版)獲取連接如下:
鏈接:https://pan.baidu.com/s/14lKw1nos0quZ4P7LetOVvg
提取碼:4i6d
下面操作都是在models/research下
2、在 https://github.com/google/protobuf/releases 網(wǎng)站中選擇windows 版本(最下面)押框,解壓后將bin文件夾中的protoc.exe放到C:\Windows下或?qū)in加入環(huán)境變量如:D:\protoc-3.4.0-win32\bin加入path中
進(jìn)入models\research\目錄下 按shift + 右鍵 打開命令行窗口理逊,輸入:
protoc object_detection/protos/*.proto --python_out=.
對proto文件進(jìn)行編譯盒揉,完成后會生成對應(yīng)名稱的py腳本文件
3兑徘、將models\reserach 和models\research\slim 加入環(huán)境變量
4、驗(yàn)證環(huán)境:
在models\research下 按shift + 右鍵 開啟命令行輸入
python object_detection/builders/model_builder_test.py
等待一秒左右藕漱,若下方出現(xiàn) ok 字樣 不報(bào)錯崭闲,說明環(huán)境配置成功
5、跑通官方示例:
在PyCharm下打開終端定位到models\research\object_detection下 輸入
jupyter-notebook
將自動打開瀏覽器如下
點(diǎn)擊上方標(biāo)記的object_detection_tutorial.ipynb 進(jìn)入可編輯腳本處理頁
說明腳本仍在執(zhí)行中如孝,耐心等待沙漏消失暑竟,運(yùn)行完畢后最下方可見【狗】和【滑翔傘】圖片識別結(jié)果
此處便是關(guān)于模型調(diào)用的官方樣例,以后關(guān)于視頻流的實(shí)時(shí)監(jiān)測也將在此基礎(chǔ)上優(yōu)化罗岖,這也是后話了腹躁,后面在介紹吧!
三纺非、自定義對象處理
1、訓(xùn)練樣品及驗(yàn)證樣品圖片標(biāo)注
官方下載地址LabelImage 下載地址https://github.com/tzutalin/labelImg/releases
解壓放在非中文路徑下弱左,新建文件夾分別保存訓(xùn)練和驗(yàn)證圖片炕淮、標(biāo)注后訓(xùn)練和驗(yàn)證的xml
雙擊labelImage.exe
(綠色的背景剛好影響了標(biāo)注框,不太明顯)
- Open Dir 選擇要標(biāo)記的圖片源
- Change Save Dir 選擇標(biāo)記后要保存xml的路徑
- 下一張币叹、上一張模狭、保存
- Create RextBox 點(diǎn)擊開始標(biāo)記
- raccoon 為標(biāo)簽,即要識別的物體名稱(如:dog cat person car...)
注:對圖片標(biāo)注不僅僅是體力活贩汉,其實(shí)也是技術(shù)活反砌,打標(biāo)區(qū)域不僅需要涵蓋物體可見區(qū)域,還要盡量減少無關(guān)背景在存在
2策菜、xml標(biāo)注文件轉(zhuǎn)tfrecord
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
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
def main():
# xml文件的路徑
image_path = os.path.join(os.getcwd(), 'data/train_xml')
xml_df = xml_to_csv(image_path)
# 輸出路徑
xml_df.to_csv('data/train.csv', index=None)
print('Successfully converted annotations xml to csv.')
image_path = os.path.join(os.getcwd(), data/'test_xml')
xml_df = xml_to_csv(image_path)
# 輸出路徑
xml_df.to_csv('data/text.csv', index=None)
print('Successfully converted textxml xml to csv.')
main()
將上一步訓(xùn)練和驗(yàn)證csv文件轉(zhuǎn)換tfrecord
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
flags = tf.app.flags
# 幾個(gè)輸入的參數(shù)又憨,
# python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=test/data/train.record --image_dir=images
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', '', 'Path to images')
FLAGS = flags.FLAGS
# TO-DO replace this with label map
# 改成你的類別 有幾個(gè)分類就往下依次進(jìn)行(return 1 return 2 ........ else return 0)
def class_text_to_int(row_label):
if row_label == 'raccoon': # raccoon
return 1
else:
return 0
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
path = os.path.join(FLAGS.image_dir)
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
'''
cd 到generate_tfrecord.py該文件目錄下
執(zhí)行命令:python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=test/data/train.record --image_dir=images
解釋:
python generate_tfrecord.py --csv_input=[train_labels.csv的路徑] --output_path=[輸出train.record的路徑] --image_dir=[輸入圖片的路徑]
最終得到:
train.record 和 test.record
注:在xml 轉(zhuǎn)csv 和csv轉(zhuǎn)tfrecord過程中 若出現(xiàn)錯誤,請檢查
1. 圖片和xml 是否一 一 對 應(yīng)
2. 路徑問題零如,最為保險(xiǎn)的是絕對路徑和雙斜杠 如:D:\PyCharm\raccoon_dataset_sample\testimg
r 、t祸憋、n等開頭的直接雙斜杠處理省得有歧義
3. 有幾個(gè)分類標(biāo)簽就依次對應(yīng)下來
結(jié)語:
由于篇幅限制這篇就到這里吧肖卧,下一篇在繼續(xù)吧!