成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深度學(xué)習(xí)入門篇——手把手教你用TensorFlow訓(xùn)練模型

人工智能 深度學(xué)習(xí)
Tensorflow在更新1.0版本之后多了很多新功能,其中放出了很多用tf框架寫(xiě)的深度網(wǎng)絡(luò)結(jié)構(gòu)(https://github.com/tensorflow/models ),大大降低了開(kāi)發(fā)難度,利用現(xiàn)成的網(wǎng)絡(luò)結(jié)構(gòu),無(wú)論fine-tuning還是重新訓(xùn)練方便了不少。

[[206688]]

導(dǎo)語(yǔ)

Tensorflow在更新1.0版本之后多了很多新功能,其中放出了很多用tf框架寫(xiě)的深度網(wǎng)絡(luò)結(jié)構(gòu)(https://github.com/tensorflow/models ),大大降低了開(kāi)發(fā)難度,利用現(xiàn)成的網(wǎng)絡(luò)結(jié)構(gòu),無(wú)論fine-tuning還是重新訓(xùn)練方便了不少。最近筆者終于跑通TensorFlow Object Detection API的ssd_mobilenet_v1模型,這里記錄下如何完整跑通數(shù)據(jù)準(zhǔn)備到模型使用的整個(gè)過(guò)程,相信對(duì)自己和一些同學(xué)能有所幫助。

Object Detection API提供了5種網(wǎng)絡(luò)結(jié)構(gòu)的預(yù)訓(xùn)練的權(quán)重,全部是用COCO數(shù)據(jù)集進(jìn)行訓(xùn)練,這五種模型分別是SSD+mobilenet、SSD+inception_v2、R-FCN+resnet101、faster RCNN+resnet101、faster RCNN+inception+resnet101。各個(gè)模型的精度和計(jì)算所需時(shí)間如下。下面及介紹下如何使用Object Detection去訓(xùn)練自己的模型。

 

這里TensorFlow的安裝就不再說(shuō)明了,網(wǎng)上的教程一大把,大家可以找到很詳盡的安裝TensorFlow的文檔。

訓(xùn)練前準(zhǔn)備:

使用protobuf來(lái)配置模型和訓(xùn)練參數(shù),所以API正常使用必須先編譯protobuf庫(kù),這里可以下載直接編譯好的pb庫(kù)(https://github.com/google/protobuf/releases ),解壓壓縮包后,把protoc加入到環(huán)境變量中:

  1. $ cd tensorflow/models 
  2.  
  3. $ protoc object_detection/protos/*.proto --python_out=.  

(我是把protoc加到環(huán)境變量中,遇到找不到*.proto文件的報(bào)錯(cuò),后來(lái)把protoc.exe放到models/object_detection目錄下,重新執(zhí)行才可以)

然后將models和slim(tf高級(jí)框架)加入python環(huán)境變量:

  1. PYTHONPATH=$PYTHONPATH:/your/path/to/tensorflow/models:/your/path/to/tensorflow/models/slim 

數(shù)據(jù)準(zhǔn)備:

數(shù)據(jù)集需要轉(zhuǎn)化成PASCAL VOC結(jié)構(gòu),API提供了create_pascal_tf_record.py,把VOC結(jié)構(gòu)數(shù)據(jù)集轉(zhuǎn)換成.record格式。不過(guò)我們發(fā)現(xiàn)更簡(jiǎn)單的方式,Datitran提供一種更簡(jiǎn)單生產(chǎn).record格式的方法。

首先需要先要標(biāo)注圖像相應(yīng)標(biāo)簽,這里可以使用labelImg工具。每標(biāo)注一張樣本,即生成一個(gè)xml的標(biāo)注文件。然后,把這些標(biāo)注的xml文件,按訓(xùn)練集與驗(yàn)證集分別放置到兩個(gè)目錄下,在Datitran提供了xml_to_csv.py腳本。這里只要指定標(biāo)注的目錄名即可。接下來(lái),然后需要我們把對(duì)應(yīng)的csv格式轉(zhuǎn)換成.record格式。

 

  1. def main(): 
  2.     # image_path = os.path.join(os.getcwd(), 'annotations'
  3.     image_path = r'D:\training-sets\object-detection\sunglasses\label\test' 
  4.     xml_df = xml_to_csv(image_path) 
  5.     xml_df.to_csv('sunglasses_test_labels.csv'index=None) 
  6.     print('Successfully converted xml to csv.' 

調(diào)用generate_tfrecord.py,注意要指定–csv_input與–output_path這兩個(gè)參數(shù)。執(zhí)行下面命令:

  1. python generate_tfrecord.py --csv_input=sunglasses_test_labels.csv --output_path=sunglass_test.record 

這樣就生成了訓(xùn)練及驗(yàn)證用的train.record與test.record。接下來(lái)指定標(biāo)簽名稱,仿照models/ object_detection/data/ pet_label_map.pbtxt,重新創(chuàng)建一個(gè)文件,指定標(biāo)簽名。

  1. item { 
  2.   id: 1 
  3.   name'sunglasses' 
  4.  

訓(xùn)練:

根據(jù)自己的需要,選擇一款用coco數(shù)據(jù)集預(yù)訓(xùn)練的模型,把前綴model.ckpt放置在待訓(xùn)練的目錄,這里meta文件保存了graph和metadata,ckpt保存了網(wǎng)絡(luò)的weights,這幾個(gè)文件表示預(yù)訓(xùn)練模型的初始狀態(tài)。

打開(kāi)ssd_mobilenet_v1_pets.config文件,并做如下修改:

num_classes:修改為自己的classes num

 

將所有PATH_TO_BE_CONFIGURED的地方修改為自己之前設(shè)置的路徑(共5處)

 

其他參數(shù)均保持默認(rèn)參數(shù)。

準(zhǔn)備好上述文件后就可以直接調(diào)用train文件進(jìn)行訓(xùn)練。

  1. python object_detection/train.py \ 
  2. --logtostderr \ 
  3. --pipeline_config_path= D:/training-sets /data-translate/training/ssd_mobilenet_v1_pets.config \ 
  4. --train_dir=D:/training-sets/data-translate/training  

TensorBoard監(jiān)控:

通過(guò)tensorboard工具,可以監(jiān)控訓(xùn)練過(guò)程,輸入西面指令后,在瀏覽器輸入localhost:6006(默認(rèn))即可。

  1. tensorboard --logdir= D:/training-sets/data-translate/training  

 

 

 

這里面有很多指標(biāo)曲線,甚至有模型網(wǎng)絡(luò)架構(gòu),筆者對(duì)于這里面很多指標(biāo)含義還沒(méi)有弄明白,不過(guò)感覺(jué)出TensorBoard這個(gè)工具應(yīng)該是極其強(qiáng)大。不過(guò)我們可以通過(guò)Total_Loss來(lái)看整體訓(xùn)練的情況。

 

從整體上看,loss曲線確實(shí)是收斂的,整體的訓(xùn)練效果還是滿意的。另外,TensorFlow還提供了訓(xùn)練過(guò)程中利用驗(yàn)證集驗(yàn)證準(zhǔn)確性的能力,但是筆者在調(diào)用時(shí),仍有些問(wèn)題,這里暫時(shí)就不詳細(xì)說(shuō)明了。

Freeze Model模型導(dǎo)出:

查看模型實(shí)際的效果前,我們需要把訓(xùn)練的過(guò)程文件導(dǎo)出,生產(chǎn).pb的模型文件。本來(lái),tensorflow/python/tools/freeze_graph.py提供了freeze model的api,但是需要提供輸出的final node names(一般是softmax之類的***一層的激活函數(shù)命名),而object detection api提供提供了預(yù)訓(xùn)練好的網(wǎng)絡(luò),final node name并不好找,所以object_detection目錄下還提供了export_inference_graph.py。

  1. python export_inference_graph.py \ 
  2. --input_type image_tensor 
  3. --pipeline_config_path D:/training-sets /data-translate/training/ssd_mobilenet_v1_pets.config \ 
  4. --trained_checkpoint_prefix D:/training-sets /data-translate/training/ssd_mobilenet_v1_pets.config /model.ckpt-* \ 
  5. --output_directory D:/training-sets /data-translate/training/result  

導(dǎo)出完成后,在output_directory下,會(huì)生成frozen_inference_graph.pb、model.ckpt.data-00000-of-00001、model.ckpt.meta、model.ckpt.data文件。

調(diào)用生成模型:

目錄下本身有一個(gè)調(diào)用的例子,稍微改造如下:

  1. import cv2 
  2. import numpy as np 
  3. import tensorflow as tf 
  4. from object_detection.utils import label_map_util 
  5. from object_detection.utils import visualization_utils as vis_util 
  6.  
  7.  
  8. class TOD(object): 
  9.     def __init__(self): 
  10.         self.PATH_TO_CKPT = r'D:\lib\tf-model\models-master\object_detection\training\frozen_inference_graph.pb' 
  11.         self.PATH_TO_LABELS = r'D:\lib\tf-model\models-master\object_detection\training\sunglasses_label_map.pbtxt' 
  12.         self.NUM_CLASSES = 1 
  13.         self.detection_graph = self._load_model() 
  14.         self.category_index = self._load_label_map() 
  15.  
  16.     def _load_model(self): 
  17.         detection_graph = tf.Graph() 
  18.         with detection_graph.as_default(): 
  19.             od_graph_def = tf.GraphDef() 
  20.             with tf.gfile.GFile(self.PATH_TO_CKPT, 'rb'as fid: 
  21.                 serialized_graph = fid.read() 
  22.                 od_graph_def.ParseFromString(serialized_graph) 
  23.                 tf.import_graph_def(od_graph_def, name=''
  24.         return detection_graph 
  25.  
  26.     def _load_label_map(self): 
  27.         label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS) 
  28.         categories = label_map_util.convert_label_map_to_categories(label_map, 
  29.                                                                     max_num_classes=self.NUM_CLASSES, 
  30.                                                                     use_display_name=True
  31.         category_index = label_map_util.create_category_index(categories) 
  32.         return category_index 
  33.  
  34.     def detect(self, image): 
  35.         with self.detection_graph.as_default(): 
  36.             with tf.Session(graph=self.detection_graph) as sess: 
  37.                 # Expand dimensions since the model expects images to have shape: [1, None, None, 3] 
  38.                 image_np_expanded = np.expand_dims(image, axis=0) 
  39.                 image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0'
  40.                 boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0'
  41.                 scores = self.detection_graph.get_tensor_by_name('detection_scores:0'
  42.                 classes = self.detection_graph.get_tensor_by_name('detection_classes:0'
  43.                 num_detections = self.detection_graph.get_tensor_by_name('num_detections:0'
  44.                 # Actual detection. 
  45.                 (boxes, scores, classes, num_detections) = sess.run( 
  46.                     [boxes, scores, classes, num_detections], 
  47.                     feed_dict={image_tensor: image_np_expanded}) 
  48.                 # Visualization of the results of a detection. 
  49.                 vis_util.visualize_boxes_and_labels_on_image_array( 
  50.                     image, 
  51.                     np.squeeze(boxes), 
  52.                     np.squeeze(classes).astype(np.int32), 
  53.                     np.squeeze(scores), 
  54.                     self.category_index, 
  55.                     use_normalized_coordinates=True
  56.                     line_thickness=8) 
  57.  
  58.         cv2.namedWindow("detection", cv2.WINDOW_NORMAL) 
  59.         cv2.imshow("detection", image) 
  60.         cv2.waitKey(0) 
  61.  
  62. if __name__ == '__main__'
  63.     image = cv2.imread('image.jpg'
  64.     detecotr = TOD() 
  65.     detecotr.detect(image)  

下面是一些圖片的識(shí)別效果:

 

 

 

End. 

責(zé)任編輯:龐桂玉 來(lái)源: 36大數(shù)據(jù)
相關(guān)推薦

2022-10-19 14:30:59

2021-08-09 13:31:25

PythonExcel代碼

2022-08-04 10:39:23

Jenkins集成CD

2011-03-28 16:14:38

jQuery

2021-02-04 09:00:57

SQLDjango原生

2021-02-06 14:55:05

大數(shù)據(jù)pandas數(shù)據(jù)分析

2009-04-22 09:17:19

LINQSQL基礎(chǔ)

2012-01-11 13:40:35

移動(dòng)應(yīng)用云服務(wù)

2021-08-02 23:15:20

Pandas數(shù)據(jù)采集

2020-03-08 22:06:16

Python數(shù)據(jù)IP

2021-02-02 13:31:35

Pycharm系統(tǒng)技巧Python

2021-12-11 20:20:19

Python算法線性

2021-05-10 06:48:11

Python騰訊招聘

2021-01-21 09:10:29

ECharts柱狀圖大數(shù)據(jù)

2021-01-08 10:32:24

Charts折線圖數(shù)據(jù)可視化

2021-01-30 10:37:18

ScrapyGerapy網(wǎng)絡(luò)爬蟲(chóng)

2009-08-27 18:10:58

PHP繪制3D圖形

2017-10-29 21:43:25

人臉識(shí)別

2021-06-23 07:16:06

buildroot Linux內(nèi)核根文件系統(tǒng)

2021-01-27 21:55:13

代碼參數(shù)值ECharts
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 成人午夜电影网 | 美女视频h | 一区二区三区在线免费观看视频 | 欧美一区中文字幕 | 日本精品一区二区 | www.亚洲| 毛片一级片 | a成人| 国产一区二区三区视频在线观看 | 超碰超碰 | av中文字幕在线 | 91久久精 | 一区二区三区免费在线观看 | 欧美一二三 | 欧美aⅴ在线观看 | 久久久入口 | 国产激情免费视频 | 亚洲成人一区二区 | 国产情侣一区 | 99在线资源| 成人免费视频一区二区 | 久久精品中文字幕 | 91在线播 | 九九久久精品 | 欧美中文视频 | 日韩精品一区二区三区中文在线 | 日韩成人在线看 | 日本黄色的视频 | 亚洲 日本 欧美 中文幕 | 日韩精品免费视频 | 伊人手机在线视频 | 蜜桃免费一区二区三区 | 伊人一区 | 9191在线播放 | 久久久九九九九 | 成av人电影在线 | 性做久久久久久免费观看欧美 | 日日骚网 | 黄色大片免费观看 | 蜜臀久久| 中文字幕乱码一区二区三区 |