短鏈接生成算法淄博seo網(wǎng)絡公司
Yolov5
Anchor
1.Anchor是啥?
anchor字面意思是錨,是個把船固定的東東(上圖),anchor在計算機視覺中有錨點或錨框,目標檢測中常出現(xiàn)的anchor box是錨框,表示固定的參考框。
目標檢測是"在哪里有什么"的任務,在這個任務中,目標的類別不確定、數(shù)量不確定、位置不確定、尺度不確定。傳統(tǒng)非深度學習方法和早期深度學習方法,都要金字塔多尺度+遍歷滑窗的方式,逐尺度逐位置判斷"這個尺度的這個位置處有沒有認識的目標",這種窮舉的方法非常低效。
最近SOTA的目標檢測方法幾乎都用了anchor技術(shù)。首先預設一組不同尺度不同位置的anchor,覆蓋幾乎所有位置和尺度,每個anchor負責檢測與其交并比大于閾值 (訓練預設值,常用0.5或0.7) 的目標,anchor技術(shù)將問題轉(zhuǎn)換為"這個固定參考框中有沒有認識的目標,目標框偏離參考框多遠",不再需要多尺度遍歷滑窗,真正實現(xiàn)了又好又快,如在Faster R-CNN和SSD兩大主流目標檢測框架及擴展算法中anchor都是重要部分。
目標檢測中的固定的參考框
yolov5s.yaml
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license# Parameters
nc: 80 # number of classes
# 控制模型的大小
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:- [10,13, 16,30, 33,23] # P3/8 目標框anchor是原圖 八分之一stride的大小- [30,61, 62,45, 59,119] # P4/16 十六分之一- [116,90, 156,198, 373,326] # P5/32
#感受野比較小,適合小目標檢測,淺層次的feature
#往下,感受野變大??適合檢測大物體
# YOLOv5 v6.0 backbone
backbone:# [from, number, module, args]
# number(layer重復的次數(shù) 實際上會乘上depth_multiple: 0.33,1*0.33)
# args(每層輸出的channel,實際上會乘上width_multiple: 0.50 ,64*0.50
# 向上取整
#每行是一個layer的類別,
# 第一列,輸入的feature從哪兒來的,-1表示輸入來自上一層的輸出
# [64, 6, 2, 2] 64是輸出的channel,輸入channel由上一層決定,6是卷積核大小,stride,padding[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
# 1-P2/4,第一層,p2 ,4 :featuremap的大小變成了原圖的 四分之一[-1, 3, C3, [128]],[-1, 1, Conv, [256, 3, 2]], # 3-P3/8[-1, 6, C3, [256]],[-1, 1, Conv, [512, 3, 2]], # 5-P4/16[-1, 9, C3, [512]],[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32[-1, 3, C3, [1024]],[-1, 1, SPPF, [1024, 5]], # 9# SPPF :不同尺度feature的融合]# YOLOv5 v6.0 head
head:[[-1, 1, Conv, [512, 1, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[[-1, 6], 1, Concat, [1]], # cat backbone P4[-1, 3, C3, [512, False]], # 13[-1, 1, Conv, [256, 1, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[[-1, 4], 1, Concat, [1]], # cat backbone P3[-1, 3, C3, [256, False]], # 17 (P3/8-small)[-1, 1, Conv, [256, 3, 2]],[[-1, 14], 1, Concat, [1]], # cat head P4[-1, 3, C3, [512, False]], # 20 (P4/16-medium)[-1, 1, Conv, [512, 3, 2]],[[-1, 10], 1, Concat, [1]], # cat head P5[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
#20*20*1024的feature map[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)]
bottleneck 和detect部分
.pt文件 訓練文件
路徑在這
detect.py
核心部分
# 通過命令行設置其中的一些參數(shù)
# 不同文件對應不同的模型(weights不同,目標檢測的置信度不同# --source,輸入是個文件夾,會對文件夾下所有的文件進行檢測
也可以指定一張圖片的相對路徑,
可以是視頻,會分成一幀一幀來處理
只支持YouTube視頻鏈接,別的視頻要下載下來
檢測不到的objections 是因為沒有進行標記
if __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument('--weights', nargs='+', type=str, default='yolov5l.pt', help='model.pt path(s)')parser.add_argument('--source', type=str, default='data/images', help='source') # file/folder, 0 for webcamparser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')置信度,檢測結(jié)果某個區(qū)域置信度大于0.25就當作是個物體實際運用過程中,對參數(shù)進行調(diào)整和確認的parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')default=0.45,iou的值大于閾值就當作一個objection,從兩個框中任選一個來表示這個物體,小于閾值就當作不同的物體設置為0,框和框之間不會有交集parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')-- device:使用CPU還是GPU,可以不同設置,默認是空parser.add_argument('--view-img', action='store_true', help='display results')在命令行中制定了這個參數(shù),就會parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')parser.add_argument('--nosave', action='store_true', help='do not save images/videos')parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')增強檢測結(jié)果的一些方式 parser.add_argument('--augment', action='store_true', help='augmented inference')parser.add_argument('--update', action='store_true', help='update all models')parser.add_argument('--project', default='runs/detect', help='save results to project/name')parser.add_argument('--name', default='exp', help='save results to project/name')parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')opt = parser.parse_args()print(opt)check_requirements(exclude=('pycocotools', 'thop'))
通過命令行設置其中的一些參數(shù)不同文件對應不同的模型(weights不同,目標檢測的置信度不同
# --source,輸入是個文件夾,會對文件夾下所有的文件進行檢測
也可以指定一張圖片的相對路徑,
可以是視頻,會分成一幀一幀來處理
只支持YouTube視頻鏈接,別的視頻要下載下來
檢測不到的objections 是因為沒有進行標記
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
對輸入的image進行 resize,再送入神經(jīng)網(wǎng)絡中
這些訓練模型,指定image size是640
訓練過程,網(wǎng)絡運算過程中對圖片大小進行縮放,會恢復,輸入輸出尺寸其實保持不變,
最好是和訓練模型指定的image size進行匹配,不匹配也沒關(guān)系
運行結(jié)果
進行實時檢測,rstp鏈接
通過手機電腦攝像頭檢測