2019年5月21日 星期二

訓練自己的偵測物件--YOLO


  (YOLO的訓練模型框架)

git clone https://github.com/thtrieu/darkflow.git

python setup.py build_ext --inplace

須要安裝MS visual studio才能編譯安裝

------------------------------------------------d

Flowing the graph using flow

python flow --help



Usage: 使用1顆GPU來run darkflow


python flow --model cfg/yolo.cfg --load bin/yolo.weights --gpu 1.0

All input images from default folder sample_img/ are flowed through the net and predictions are put in sample_img/out/
python flow --imgdir sample_img/ --model cfg/yolo.cfg --load bin/yolo.weights --gpu 1.0

--json
json output can be generated with descriptions of the pixel location of each bounding box and the pixel location.




Training new model

flow --train will train the model . To point to training set and annotations, use option --dataset and --annotation.

你會使用fine tune方式, 用既有的yolo network model (.cfg) 與weight (.weights)作為你訓練新模型的基本架構, 然後修改成你想要偵測物件的類別

1. ) 既有的yolo network model (.cfg) ==> 所以你會從cfg/ copy 一份新的cfg並稍微修改一下檔名.  新的cfg檔, 要改2個地方

     
...

[convolutional]  
size=1
stride=1
pad=1
filters=40     #在倒數第2個 [convolutional], 修改filters 數量
activation=linear

[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=3   #偵測的物件有3類
coords=4
num=5  # anchors  box的數量 (一個anchors會有confidence、座標、在每個類別的機率)
softmax=1


...
       
filters =num*(classes+5) , 若classes=3 , num(自訂)=5, filter=40


2.)  ---load xxxx. weights. ==> 會載入xxxx.weights時,會去 cfg/找到對應的xxxx.cfg ,如此才能載入權重, 如--load tiny-yolo-voc.weights 會去找 cfg/tiny-yolo-voc.cfg


When darkflow sees you are loading tiny-yolo-voc.weights it will look for tiny-yolo-voc.cfg in your cfg/ folder and compare that configuration file to the new one you have set with --model cfg/tiny-yolo-voc-3c.cfg. In this case, every layer will have the same exact number of weights except for the last two, so it will load the weights into all layers up to the last two because they now contain different number of weights.

3.) 修改 darkflow/labels.txt,  行數要和class數量一樣
  
aeroplane
bicycle
bird


例如. 若使用既有的tiny-yolo network model與weight

python flow --model cfg/tiny-yolo-joseph.cfg --load bin/tiny-yolo.weights --train --annotation train/annotations --dataset train/images --gpu 1.0


LabelImg 用來產生label 及框選Object



References:

https://github.com/thtrieu/darkflow

2019年5月6日 星期一

OpenVINO教學-OpenVINO model optimizer錯誤排解


www.ittraining.com.tw狀況一、OpenVINO model optimizer 發生內部錯誤

當使用Intel OpenVINO model optimizer 轉換tensorflow pb modelIR (Intermediate Representation) model時出現…… Exception occurred during running replacer "REPLACEMENT_ID (<……>)": list index out of range ……等訊息。


    









        出現此現象表示輸入之pb model可能未進行Freeze動作或Freeze動作未完全,故可參考
https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb將整個tensorflow session完全freeze,再行轉換即可成功。

Reference:

    
狀況二、使用OpenVINO model optimizer 出現 __new__() got an unexpected keyword argument 'serialized_options' 錯誤

      當使用Intel OpenVINO model optimizer 轉換tensorflow pb model至IR (Intermediate Representation) model時出現以下錯誤訊息:

[ ERROR ]  Error happened while importing tensorflow module. It may happen due to unsatisfied requirements of that module. Please run requirements installation script once more.
Details on module importing failure: __new__() got an unexpected keyword argument 'serialized_options'

[ ERROR ]

Detected not satisfied dependencies:
        tensorflow: package error, required: 1.10.0

表示系統所安裝之protoc(Protocol Buffer) binary與python protobuf package版本不一致所致。可使用以下3組指令確認系統目前所安裝之版本。

1.系統Global protoc binary version
protoc --version

2.使用pip所安裝之版本
pip list|grep protobuf

3.python3 interpreter實際抓到使用的版本
python3 -c "from google import protobuf;print(protobuf.__version__);print(protobuf.__file__)"

















指令1更版方式:
#Make sure you grab the latest version
wget 
https://github.com/google/protobuf/releases/download/v3.7.1/protoc-3.7.1-linux-x86_64.zip

# Unzip
unzip protoc-3.7.1-linux-x86_64.zip -d protoc3.7.1

# Detemine Directory, if exist delete it
if [ -d " protoc3.7.1" ]; then
    # Directory protoc3.7.1 exists
    echo "Directory protoc3.7.1 exists. Remove it."
    sudo rm -r protoc3.7.1
fi

# Move protoc to /usr/local/bin/
sudo mv protoc3.7.1/bin/* /usr/local/bin/

# Detemine Directory, if exist delete it
if [ -d "/usr/local/include/google" ]; then
    # Directory /usr/local/include/google exists
    echo "Directory /usr/local/include/google exists. Remove it."
    sudo rm -r /usr/local/include/google
fi

# Move protoc3/include to /usr/local/include/
sudo mv protoc3.7.1/include/* /usr/local/include/

# Optional: change owner
sudo chown $USER /usr/local/bin/protoc
sudo chown -R $USER /usr/local/include/google

# Delete protoc3.7.1 directory
rm -r protoc3.7.1

指令2更版方式:
Pip install -U -force-reinstall protobuf==3.7.1

指令3更版方式:
若dist-package有使用easy-install (EGG)安裝之套件,則依該套件的方法移除後重新執行指令2的更版方式即可修復

~強烈建議在Linux環境下使用virtualenv、Windows環境下使用anaconda做python環境控管~