Sunday, March 31, 2019

自定義 Object_detection yolov3 待續...

自定義 Object_detection yolov3


這大概是我裝過算有點難裝的一套 darknet github 版本的 yolo v3

(You only look once)

演算法更快 更穩 ,當然安裝更難裝
首先呢,
先安裝 vs2017
再來就是下載 cuda 9.1 嗯嗯別問我為什麼 我在 cuda 9.0 包 這邊 走跟 github 不同的方式
因為會有衝突,visual studio 2017 請裝 community 因為可以新增套件,等等
所以詳細步驟則是


  • install vs2017
  • install opencv3.1
  • install cuda 9.1 
  • Cuda v9.1 setting
  • visual studio 2017 setting
  • build
  • runing object data

系統配置


windwos10
cuda V9.1
opencv 3.1
darknet gpu 版
Install Visual studio 2017 




這邊安裝完後的時候請勾選
vc++ 2015v140 工具組
然後 darknet 專案檔案為
C:\Users\x2132\Desktop\darknet-master\build\darknet


Install opencv3.1


在nuget 裡面搜尋 opencv3.1
Install cuda9.1 



這邊官網會說 請把 2017 移除再重裝cuda 這邊的話會花費太多時間,而且還不一定會對
在darknet 編譯的過程中會需要裡面資料夾的一些設定檔
D:\download\cuda_9.1.85_win10\CUDAVisualStudioIntegration\extras\visual_studio_integration\MSBuildExtensions

複製到
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v140\BuildCustomizations

Cuda v9.1 setting


在這邊的話 為了讓visual studio 2017 可以抓到 ,恩..大致原因就是 cuda 跟不太上 vs 速度 哈哈
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include\crt
更改為 1920 或更高版號


visual studio 2017 setting


這邊呢,假設是要用自定義的 cuda 這邊選用為 cuda 9.1設定檔則

更改為 cuda 9.1 設定檔

然後喔這邊的組態設定為 所有組態為x64,平台工具組設為 v140
請設置為你目前電腦版本的cuda! 這邊設定為v9.1
這邊聽官方說的只留這個compute_30,sm_30;

build!


當然不可能給你這麼好過

加個括號
終於編譯成功 , 半天時間也消失了@@


runing object data


可能明天才會做 數據標籤的教學,然後則
https://pjreddie.com/media/files/yolov3.weights
這個是 國外大大訓練好的數據,可以下來再來測試
下載完則放置於
開始下指令囉,
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
這邊 chrome 分頁開太多qq
關掉一些





自定義 Object_detection tensorflow 複習


自定義 Object_detection 


朋友台中面試題目呢嗯嗯嗯,上一次電腦硬碟掛掉掛電今天來重寫一份,如何自定義 訓練
Object_detectio,那麼首先又是複雜的環境設定 今天目標  RUN起這份 soruce code

前置作業


額 由於呢,這份 source code 年代有點久遠,那麼大概會分成這幾個步驟

  1. Download anconda
  2. git clone  https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10.git
  3. Download tensorflow model
  4. Download Faster-rcnn-inception-v2-coco model
  5. folder  setting
  6. Environment setting
  7. Labelimg
  8. Configure training
  9. Traing
  10. python  Object_detection_webcam.py 
Download Anconda 



自己去官網下載!

Download tensorflow model


有裝過 git 的話 直接git clone 就可以了,沒有的話則
Download tensorflow model


作者的 github 的預設連結是連到 最新 model 版本 ,這邊我用 作者的方法run 不起來所以我去翻找 一個更久以前的model
https://github.com/Rmuli/models,這邊作者有說到,在新的版本的model的話train.py的這個文件可以在legacy ,被找到 不過,ㄏㄏ除了這個我還有其他函數會被衝突因為,不知是否是我使用的環境問題,還是pyhton 版本,先download

Download Faster-rcnn-inception-v2-coco model


這邊的話,是該次主要訓練的 model設定,詳細看github

Foldder setting 


這邊我覺得我有點看不懂再說什麼,可能我英文真的濫倒爆炸,
上述下載的檔案有三個
分別是



那麼我來放置一下官網的設定
我們將在
C:\tensorflow1\models
這邊就是放置
  • tensorflow model
然後research裡面! 的object_detection
完成!
Anconda Environment setting


在這邊的話就照官網

2d. Set up new Anaconda virtual environment

Next, we'll work on setting up a virtual environment in Anaconda for tensorflow-gpu. From the Start menu in Windows, search for the Anaconda Prompt utility, right click on it, and click “Run as Administrator”. If Windows asks you if you would like to allow it to make changes to your computer, click Yes.
In the command terminal that pops up, create a new virtual environment called “tensorflow1” by issuing the following command:
這邊意思大概就是開一個環境裡面的pyhton 版本是3.5
C:\> conda create -n tensorflow1 pip python=3.5
Then, activate the environment by issuing:


C:\> activate tensorflow1

啟動環境的一個概念,docker?


Install tensorflow-gpu in this environment by issuing:
這邊安裝 tensorflow 的步驟省略看下一章安裝
(tensorflow1) C:\> pip install --ignore-installed --upgrade tensorflow-gpu

Install the other necessary packages by issuing the following commands:

這邊可以看到 conda 很方便的是它可以幫你把你的套件,自動匹配相對應的lib 不用再為 包安裝的很辛苦!
(tensorflow1) C:\> conda install -c anaconda protobuf
(tensorflow1) C:\> pip install pillow
(tensorflow1) C:\> pip install lxml
(tensorflow1) C:\> pip install Cython
(tensorflow1) C:\> pip install jupyter
(tensorflow1) C:\> pip install matplotlib
(tensorflow1) C:\> pip install pandas
(tensorflow1) C:\> pip install opencv-python
(Note: The ‘pandas’ and ‘opencv-python’ packages are not needed by TensorFlow, but they are used in the Python scripts to generate TFRecords and to work with images, videos, and webcam feeds.)

2e. Configure PYTHONPATH environment variable

這邊可能是 tensorflow 需要抓到 models 的系統環境變數 ,每次 都要重新設定
A PYTHONPATH variable must be created that points to the \models, \models\research, and \models\research\slim directories. Do this by issuing the following commands (from any directory):
(tensorflow1) C:\> set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim
(Note: Every time the "tensorflow1" virtual environment is exited, the PYTHONPATH variable is reset and needs to be set up again.)

2f. Compile Protobufs and run setup.py

Next, compile the Protobuf files, which are used by TensorFlow to configure model and training parameters. Unfortunately, the short protoc compilation command posted on TensorFlow’s Object Detection API installation page does not work on Windows. Every .proto file in the \object_detection\protos directory must be called out individually by the command.
In the Anaconda Command Prompt, change directories to the \models\research directory and copy and paste the following command into the command line and press Enter:
嗯嗯嗯  等級不夠以後再來研究
protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto
This creates a name_pb2.py file from every name.proto file in the \object_detection\protos folder.
(Note: TensorFlow occassionally adds new .proto files to the \protos folder. If you get an error saying ImportError: cannot import name 'something_something_pb2' , you may need to update the protoc command to include the new .proto files.)
Finally, run the following commands from the C:\tensorflow1\models\research directory:
(tensorflow1) C:\tensorflow1\models\research> python setup.py build
(tensorflow1) C:\tensorflow1\models\research> python setup.py install
Anconda install tensorflow-gpu


額 由於呢,這份 source code 年代有點久遠,tensorflow 照他的流程 我會一一走下去
不知道為什麼我按照他的指令下會更新到很舊的版本,反正我們強制指定

pip install tensorflow-gpu==1.5 

這邊呢跟以前我發過的教學一樣,

選 windows 10  
1.5版(含以上)請參考:


CUDA 9.0
在CUDA 9.0 安裝過程中 有衝突的話 在安裝前 把推薦 選 自定義,把失敗的給勾掉就ok了!

cuDNN 7.0 是需要加入nvdia 社群的 所以嗯嗯自己去加


下載玩呢先把 CUDA9.0給安裝完,再把 cudnn 解壓縮的東西 丟到

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0


完整的丟過去,嗯嗯 打開Anaconda Prompt
切去tensorflow1環境
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

labelimg 





大大門常常說的標記資料,就是這麼一回事,像google v2的驗證碼?標記完資料呢

python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record

這兩行沒記錯的話應該是產生 像對應 資料集,並打亂?



後面的話 label map 則是 , 根據你所標記的  資料集也就是

返回的結果 ,進行 話框框 並 標註 類似


那麼開始進行 traing 
Configure trainging


這邊跟官網比較不同的話,我可能是環境比較特殊,大家可以先做官網的教學不能再,可能情況像我一樣就學我修改
下面兩個配置 倒是沒什麼問題

我 <=====有問題的是這邊 ,請注意反斜線 和訓練 rate

新的model ,或者中間有更新 ,好像不支援 三個schedule 這樣子,這應該是 預計要訓練到 什麼時候
就是裡面的loss ,應該是每隔 幾萬步 學習 rate 就會再下降一點點,這邊的話可能要去找 周末凡老師 上一下課,反正就是,用下山來說,你每次都跨很大步,你以為是最佳路徑,而忽略鄉間小道可以更快下山,或者是 每次都走很小步,其實你很快就可以下山,這樣比喻會不會太籠統ㄏㄏ,是這樣嗎,這要視情況而定,各有利弊,這邊攸關訓練的時間哈哈,在其他種狀況,可能訓練跨太大步會梯度爆炸,攸關 你的 訓練模型,要自己寫的話還要考慮到,激活函數等等,嗯嗯以後再來整理一下
這邊照他說的,比較快一點的作法就是,先下載他的訓練包 也就是 Faster-rcnn-inception-v2-coco model  和作者 github 專案放置相對應位置  ,做到這邊的話, 那就大功告成囉
準備來訓練

Trainging!


python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
沒意外的話 大概兩個小時
要看實際狀態可以用,不過我的 tensorboard 壞掉了,可以觀看學習曲線

tensorboard --logdir=training

兩個小時 到了, 恩恩這邊的話 ,不爽等可以直接 ctrl - c
訓練完呢

接下來要產生 辨識模型

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph
這邊我有遇到一個 bug
C:\tensorflow1\models\research\object_detection\utils\learning_schedules.py
必須設定為
 [i for i in range(num_boundaries)]

裡面的 23419 目前中斷的地方為 23419 嗯嗯這樣的話會在 上一層的產生 辨識模型
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph
好囉!  終於產生完模型

python object_detection webcam.py 


對的,到了最後一步 ,先來運行一下, 兩小時而已,有點久,晚一點來發 yolo 物體辨識模型!