1
YOLOv5網絡
YOLOv5 于2020年6月釋出!一經推出,便得到CV圈的矚目,目前在各大目标檢測競賽、落地實戰項目中得到廣泛應用。
YOLOv5在COCO上的性能表現:

YOLOv5代碼連結:https://github.com/ultralytics/yolov5
YOLOv5一共有4個版本,分别是Yolov5s、Yolov5m、Yolov5l、Yolov5x,其中性能依次增強。比如YOLOv5s模型參數量最小,速度最快,AP精度最低;YOLOv5x模型參數量最大,速度最慢,AP精度最高。
其中YOLOv5網絡結構如下:
圖源:https://zhuanlan.zhihu.com/p/172121380
2
OpenVINO™工具套件介紹
OpenVINO™工具套件是英特爾針對自家硬體平台開發的一套深度學習工具庫,包含推理庫,模型優化等等一系列與深度學習模型部署相關的功能。同時可以相容各種開源架構訓練好的模型,擁有算法模型上線部署的各種能力,隻要掌握了該工具,你可以輕松的将預訓練模型在英特爾的CPU、VPU等裝置上快速部署起來。
3
重新訓練YOLOv5
3.1 下載下傳YOLOv5代碼和權重檔案
大家可以直接clone YOLOv5官方github代碼:
git clone https://github.com/ultralytics/yolov5
複制
也可以在官方github的releases中下載下傳正式釋出的版本:
https://github.com/ultralytics/yolov5/releases
我們這裡下載下傳YOLOv5 v3.1版本的源代碼和yolov5s.pt權重檔案。值得注意,目前YOLOv5已更新至v5.0,但在實際轉換OpenVINO™工具套件推理應用中遇到不少問題,為了友善使用,這裡推薦較穩定的YOLOv5 v3.1版本。
3.2 資料集準備
資料集可以是自己标注的,也可以用網上開源的資料集。如果是标注自己的目标檢測資料集,一般使用labelImg工具(超好用!支援COCO等格式)。
這裡我們下載下傳使用roboflow開源的口罩檢測資料集(Mask Wearing Dataset),該資料集隻有149幅圖像,友善練手,而且格式直接支援YOLOv5!
https://public.roboflow.com/object-detection/mask-wearing
3.3 重新訓練YOLOv5
3.3.1 修改參數
訓練自定義資料集,一般需要修改兩個參數:
- nc:需要識别的類别
- anchors:YOLOv5預設自适應anchors計算,也可以自定義通過k-means算法計算
其中,nc是一定要修改的,比較每個資料集的類别會不一樣,而anchors可以不用修改,即預設自适應計算。
比如Mask Wearing資料集隻有兩種類别:mask和no-mask,是以nc = 2。可見data.yaml中的資訊:
這裡使用YOLOv5s進行訓練,是以需要同步修改yolov5/models/yolov5s.yaml 檔案中的nc數值,設定為2:
3.3.2 訓練YOLOv5
訓練指令如下:
python train.py --data 資料集路徑/data.yaml --cfg models/yolov5s.yaml --weights '' --batch-size 64 --epochs 100
複制
注:訓練指令行的參數含義可參考:https://docs.ultralytics.com,比如batch size、epochs可以根據訓練裝置自行調整
訓練完成後,權重檔案會自動儲存在runs檔案夾中,自動生成last.pt和best.pt,如下圖所示:
3.3.3 YOLOv5 Demo檢測
對測試集中的圖像進行檢測,執行指令如下:
python detect.py --weight runs/exp6/weights/best.pt --source 資料集路徑/test/images/1288126-10255706714jpg_jpg.rf.95f7324cbfd48e0386e0660b5e932223.jpg
複制
輸入圖像:
口罩檢測結果:
4
模型轉換
(YOLOv5—>OpenVINO™工具套件)
将YOLOv5的.pt訓練權重檔案轉換成OpenVINO™工具套件調用的檔案,主要的流程是:.pt 權重檔案 —> ONNX 權重檔案 —> IR 檔案(.bin和xml)。其中利用ONNX(Open Neural Network Exchange,開放神經網絡交換)進行檔案格式轉換。
使用版本說明:
- Ubuntu 18.04
- OpenVINO™工具套件 2021.03
4.1 .pt 權重檔案 —> ONNX 權重檔案
先安裝ONNX,然後運作腳本,實作轉換。
4.1.1 安裝ONNX
ONNX的安裝方法相對簡單,直接pip安裝即可:
pip install onnx
複制
4.1.2 ONNX轉換
YOLOv5官方提供了轉換成ONNX權重的腳本檔案,位于yolov5/models/export.py,使用說明詳見:https://github.com/ultralytics/yolov5/issues/251
注意,這裡需要将export.py腳本檔案中的opset_version修改為10:
torch.onnx.export(model, img, f, verbose=False, opset_version=10, input_names=['images'],
output_names=['classes', 'boxes'] if y is None else ['output'])
複制
然後再執行如下轉換指令:
python models/export.py --weights runs/exp6/weights/best.pt --img 640 --batch 1
複制
轉換成功後,就會在runs/exp6/weights檔案夾中生成best.onnx檔案。
注:這裡可以使用Netron打開yolov5s.onnx,進而可視化YOLOv5模型。
- Netron線上可視化:https://netron.app/
- Netron github:https://github.com/lutzroeder/netron
4.2 ONNX 權重檔案 —> IR 檔案(.bin和.xml)
先安裝、配置OpenVINO™工具套件,然後運作腳本,實作轉換。
4.2.1 安裝OpenVINO™工具套件
安裝OpenVINO™工具套件的方法有很多,詳見官網:
https://docs.openvinotoolkit.org/latest/index.html
這裡我是使用APT的方式,具體參考:https://docs.openvinotoolkit.org/latest/openvino_docs_install_guides_installing_openvino_apt.html
安裝指令如下:
wget https://apt.repos.intel.com/openvino/2021/GPG-PUB-KEY-INTEL-OPENVINO-2021
apt-key add GPG-PUB-KEY-INTEL-OPENVINO-2021
apt-key list
touch /etc/apt/sources.list.d/intel-openvino-2021.list
echo "deb https://apt.repos.intel.com/openvino/2021 all main" >> /etc/apt/sources.list.d/intel-openvino-2021.list
apt update
複制
執行完上述指令後,可出現:
然後搜尋可下載下傳的包,要注意系統版本:
sudo apt-cache search intel-openvino-dev-ubuntu18
複制
這裡安裝intel-openvino-dev-ubuntu18-2021.3.394版本
apt install intel-openvino-dev-ubuntu18-2021.3.394
複制
安裝成功後,輸出内容如下圖所示:
4.2.2 OpenVINO™工具套件轉換
安裝好OpenVINO™工具套件後,我們需要使用OpenVINO™工具套件的模型優化器(Model Optimizer)将ONNX檔案轉換成IR(Intermediate Representation)檔案。
首先設定 OpenVINO™工具套件的環境和變量:
source /opt/intel/openvino_2021/bin/setupvars.sh
複制
然後運作如下腳本,實作ONNX模型到IR檔案(.xml和.bin)的轉換:
python /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo.py --input_model runs/exp6/weights/best.onnx --model_name yolov5s_best -s 255 --reverse_input_channels --output Conv_487,Conv_471,Conv_455
複制
關于指令行的參數用法,更多細節可參考:https://docs.openvinotoolkit.org/cn/latest/openvino_docs_MO_DG_prepare_model_convert_model_Converting_Model_General.html
轉換成功後,即可得到yolov5s_best.xml 和 yolov5s_best.bin檔案。
5
使用OpenVINO™工具套件進行推理部署
5.1 安裝Python版的OpenVINO™工具套件
這裡使用Python進行推理測試。因為我上面采用apt的方式安裝OpenVINO™工具套件,這樣安裝後Python環境中并沒有OpenVINO™工具套件,是以我這裡需要用pip安裝一下OpenVINO™工具套件。
注:如果你是編譯源碼等方式進行安裝的,那麼可以跳過這步:
pip install openvino
複制
另外,安裝時要保持版本的一緻性:
5.2 OpenVINO™工具套件實測
OpenVINO™工具套件官方提供了YOLOv3版本的Python推理demo,可以參考:
https://github.com/openvinotoolkit/open_model_zoo/blob/master/demos/object_detection_demo/python/object_detection_demo.py
我們這裡參考這個已經适配好的YOLOv5版本:https://github.com/violet17/yolov5_demo/blob/main/yolov5_demo.py,該源代碼的輸入資料是camera或者video,是以我們可以将test資料集中的圖像轉換成視訊(test.mp4)作為輸入,或者可以自行修改成圖像處理的代碼。
其中YOLOv5版本相對于官方YOLOv3版本的主要修改點:
1. 自定義letterbox函數,預處理輸入圖像:
def letterbox(img, size=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232 shape = img.shape[:2] # current shape [height, width] w, h = size# Scale ratio (new / old) r = min(h / shape[0], w / shape[1])if not scaleup: # only scale down, do not scale up (for better test mAP) r = min(r, 1.0)# Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = w - new_unpad[0], h - new_unpad[1] # wh paddingif auto: # minimum rectangle dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh paddingelif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (w, h) ratio = w / shape[1], h / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2if shape[::-1] != new_unpad: # resize img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border top2, bottom2, left2, right2 = 0, 0, 0, 0if img.shape[0] != h: top2 = (h - img.shape[0])//2 bottom2 = top2 img = cv2.copyMakeBorder(img, top2, bottom2, left2, right2, cv2.BORDER_CONSTANT, value=color) # add borderelif img.shape[1] != w: left2 = (w - img.shape[1])//2 right2 = left2 img = cv2.copyMakeBorder(img, top2, bottom2, left2, right2, cv2.BORDER_CONSTANT, value=color) # add borderreturn img
複制
2. 自定義parse_yolo_region函數, 使用Sigmoid函數的YOLO Region層 :
def parse_yolo_region(blob, resized_image_shape, original_im_shape, params, threshold):
# ------------------------------------------ Validating output parameters ------------------------------------------
out_blob_n, out_blob_c, out_blob_h, out_blob_w = blob.shape
predictions = 1.0/(1.0+np.exp(-blob))
assert out_blob_w == out_blob_h, "Invalid size of output blob. It sould be in NCHW layout and height should " \
"be equal to width. Current height = {}, current width = {}" \
"".format(out_blob_h, out_blob_w)
# ------------------------------------------ Extracting layer parameters -------------------------------------------
orig_im_h, orig_im_w = original_im_shape
resized_image_h, resized_image_w = resized_image_shape
objects = list()
side_square = params.side * params.side
# ------------------------------------------- Parsing YOLO Region output -------------------------------------------
bbox_size = int(out_blob_c/params.num) #4+1+num_classes
for row, col, n in np.ndindex(params.side, params.side, params.num):
bbox = predictions[0, n*bbox_size:(n+1)*bbox_size, row, col]
x, y, width, height, object_probability = bbox[:5]
class_probabilities = bbox[5:]
if object_probability < threshold:
continue
x = (2*x - 0.5 + col)*(resized_image_w/out_blob_w)
y = (2*y - 0.5 + row)*(resized_image_h/out_blob_h)
if int(resized_image_w/out_blob_w) == 8 & int(resized_image_h/out_blob_h) == 8: #80x80,
idx = 0
elif int(resized_image_w/out_blob_w) == 16 & int(resized_image_h/out_blob_h) == 16: #40x40
idx = 1
elif int(resized_image_w/out_blob_w) == 32 & int(resized_image_h/out_blob_h) == 32: # 20x20
idx = 2
width = (2*width)**2* params.anchors[idx * 6 + 2 * n]
height = (2*height)**2 * params.anchors[idx * 6 + 2 * n + 1]
class_id = np.argmax(class_probabilities)
confidence = object_probability
objects.append(scale_bbox(x=x, y=y, height=height, width=width, class_id=class_id, confidence=confidence,
im_h=orig_im_h, im_w=orig_im_w, resized_im_h=resized_image_h, resized_im_w=resized_image_w))
return objects
複制
3. 自定義scale_bbox函數,進行邊界框後處理 :
def scale_bbox(x, y, height, width, class_id, confidence, im_h, im_w, resized_im_h=640, resized_im_w=640):
gain = min(resized_im_w / im_w, resized_im_h / im_h) # gain = old / new
pad = (resized_im_w - im_w * gain) / 2, (resized_im_h - im_h * gain) / 2 # wh padding
x = int((x - pad[0])/gain)
y = int((y - pad[1])/gain)
w = int(width/gain)
h = int(height/gain)
xmin = max(0, int(x - w / 2))
ymin = max(0, int(y - h / 2))
xmax = min(im_w, int(xmin + w))
ymax = min(im_h, int(ymin + h))
# Method item() used here to convert NumPy types to native types for compatibility with functions, which don't
# support Numpy types (e.g., cv2.rectangle doesn't support int64 in color parameter)
return dict(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, class_id=class_id.item(), confidence=confidence.item())
複制
但在實際測試中,會出現這個問題 :'openvino.inference_engine.ie_api.IENetwork' object has no attribute 'layers' :
[ INFO ] Creating Inference Engine... [ INFO ] Loading network files: yolov5/yolov5s_best.xml yolov5/yolov5s_best.bin yolov5_demo.py:233: DeprecationWarning: Reading network using constructor is deprecated. Please, use IECore.read_network() method instead net = IENetwork(model=model_xml, weights=model_bin) Traceback (most recent call last): File "yolov5_demo.py", line 414, in <module> sys.exit(main() or 0) File "yolov5_demo.py", line 238, in main not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers] AttributeError: 'openvino.inference_engine.ie_api.IENetwork' object has no attribute 'layers'
經過我調研後才得知,在OpenVINO™工具套件2021.02及以後版本, 'ie_api.IENetwork.layers' 就被官方删除了:
是以需要将第327、328行的内容:
out_blob = out_blob.reshape(net.layers[layer_name].out_data[0].shape) layer_params = YoloParams(net.layers[layer_name].params, out_blob.shape[2])
複制
修改為:
out_blob = out_blob.reshape(net.outputs[layer_name].shape)
params = [x._get_attributes() for x in function.get_ordered_ops() if x.get_friendly_name() == layer_name][0]
layer_params = YoloParams(params, out_blob.shape[2])
複制
并在第322行下面新添加一行代碼:
function = ng.function_from_cnn(net)
複制
最終在終端,輸入下面指令:
python yolov5_demo.py -m yolov5/yolov5s_best.xml test.mp4
複制
加上後處理,使用OpenVINO™工具套件的推理時間平均在220ms左右,測試平台為英特爾® 酷睿™ i5-7300HQ,而使用PyTorch CPU版本的推理時間平均在1.25s,可見OpenVINO™工具套件加速明顯!
最終檢測結果如下:
如果你想在CPU上實作模型的快速推理,可以試試OpenVINO™工具套件哦~