YOLO!如果你對機器學習感興趣,這個術(shù)語一定不陌生。確實,You Only Look Once已經(jīng)成為過去幾年中目標檢測的默認方法之一。受到卷積神經(jīng)網(wǎng)絡(luò)取得的進展推動,許多版本的目標檢測方法已經(jīng)被創(chuàng)建。然而,近年來,一個競爭對手出現(xiàn)在了視野中——那就是在計算機視覺中使用基于Transformer的模型。更具體地說,是使用Transformer進行目標檢測。
在今天的教程中,你將了解到這種類型的Transformer模型。你還將學會使用Python、一個默認的Transformer模型和HuggingFace Transformers庫創(chuàng)建自己的目標檢測流程。本文將按照下列步驟講解:
環(huán)顧四周,很可能你會看到很多東西——可能是一臺電腦顯示器、一個鍵盤和鼠標,或者當你在移動瀏覽器中瀏覽時,是一部智能手機。這些都是物體,是特定類別的實例。例如,在下面的圖像中,我們看到一個人類類別的實例。我們還看到了許多瓶子類別的實例。雖然類別是藍圖,但物體是真實存在的,具有許多獨特的特征,同時因為共享的特征而屬于類別的成員。
在圖片和視頻中,我們看到了許多這樣的物體。例如,當你拍攝交通視頻時,很可能會看到許多行人、汽車、自行車等實例。知道它們在圖像中存在是非常有益的。為什么呢?因為你可以計數(shù)它們,舉一個例子。這可以讓你對社區(qū)的擁擠程度有所了解。另一個例子是在繁忙地區(qū)檢測到一個停車位,讓你可以停車。
然后,我們將數(shù)據(jù)分配給一些變量,并遍歷每個結(jié)果,繪制邊界框。
# Open the imagewith Image.open("street.jpg") as im: # Perform object detection bounding_boxes = object_detector(im) # Iteration elements num_boxes = len(bounding_boxes) index = 0 # Draw bounding box for each result for bounding_box in bounding_boxes: # Get actual box box = bounding_box["box"] # Draw the bounding box im = draw_bounding_box(im, bounding_box["score"], bounding_box["label"],/ box["xmin"], box["ymin"], box["xmax"], box["ymax"], index, num_boxes) # Increase index by one index += 1 # Save image im.save("street_bboxes.jpg") # Done print("Done!")
如果你創(chuàng)建了自己的模型,或者想要使用不同的模型,那么很容易使用它來代替基于ResNet-50的DeTr Transformer。這將需要你添加以下導(dǎo)入:
from transformers import DetrFeatureExtractor, DetrForObjectDetection
然后,你可以初始化特征提取器和模型,并使用它們初始化object_detector,而不是默認的一個。例如,如果你想將ResNet-101用作你的骨干,那么你可以這樣做:
# Initialize another model and feature extractorfeature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-101')model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-101')# Initialize the object detection pipelineobject_detector = pipeline("object-detection", model = model, feature_extractor = feature_extractor)
以下是我們在輸入圖像上運行目標檢測流程后得到的結(jié)果:
當放大時:
本文鏈接:http://www.tebozhan.com/showinfo-26-98871-0.html基于 Python 和 HuggingFace Transformers 的目標檢測
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com