目标检测 dcn v2_使用Detectron2分6步进行目标检测
目標檢測 dcn v2
Have you ever tried training an object detection model using a custom dataset of your own choice from scratch?
您是否曾經嘗試使用自己選擇的自定義數據集來訓練對象檢測模型?
If yes, you’d know how tedious the process would be. We need to start with building a model using a Feature Pyramid Network combined with a Region Proposal Network if we opt for region proposal based methods such as Faster R-CNN or we can also use one-shot detector algorithms like SSD and YOLO.
如果是,您將知道該過程將是多么乏味。 如果我們選擇基于區(qū)域提議的方法(例如Faster R-CNN),或者使用像SSD和YOLO這樣的單次檢測器算法,則需要從使用特征金字塔網絡與區(qū)域提議網絡相結合來構建模型開始。
Either of them is a bit complicated to work with if we want to implement it from scratch. We need a framework where we can use state-of-the-art models such as Fast, Faster, and Mask R-CNNs with ease. Nevertheless, it is important to try building a model at least once from scratch to understand the math behind it.
如果我們想從頭開始實現,那么使用它們中的任一個都有些復雜。 我們需要一個框架,在這里我們可以輕松使用最新模型,例如Fast,Faster和Mask R-CNN。 盡管如此,重要的是嘗試至少從頭開始構建模型一次以了解其背后的數學原理。
Detectron 2 comes to the rescue if we want to train an object detection model in a snap with a custom dataset. All the models present in the model zoo of the Detectron 2 library are pre-trained on COCO Dataset. We just need to fine-tune our custom dataset on the pre-trained model.
如果我們想使用自定義數據集快速訓練對象檢測模型,則可以使用Detectron 2進行救援。 Detectron 2庫的模型庫中存在的所有模型都在COCO Dataset上進行了預訓練。 我們只需要在預先訓練的模型上微調我們的自定義數據集即可。
Detectron 2 is a complete rewrite of the first Detectron which was released in the year 2018. The predecessor was written on Caffe2, a deep learning framework that is also backed by Facebook. Both the Caffe2 and Detectron are now deprecated. Caffe2 is now a part of PyTorch and the successor, Detectron 2 is completely written on PyTorch.
Detectron 2是對2018年發(fā)布的第一個Detectron的完全重寫。其前身是在Caffe2上編寫的,Caffe2是一個深度學習框架,也得到Facebook的支持。 Caffe2和Detectron現在都已棄用。 Caffe2現在是PyTorch的一部分,其后繼者Detectron 2完全用PyTorch編寫。
Detectron2 is meant to advance machine learning by offering speedy training and addressing the issues companies face when making the step from research to production.
Detectron2旨在通過提供快速的培訓并解決公司從研究到生產的過程中面臨的問題,來促進機器學習。
These are the various types of Object Detection models that the Detectron 2 offers.
這些是Detectron 2提供的各種類型的對象檢測模型。
https://research.fb.com/wp-content/uploads/2019/12/4.-detectron2.pdfhttps://research.fb.com/wp-content/uploads/2019/12/4.-detectron2.pdfLet’s dive into Instance Detection directly.
讓我們直接研究實例檢測 。
Instance Detection refers to the classification and localization of an object with a bounding box around it. In this article, We are going to deal with identifying the language of text from images using the Faster RCNN model from the Detectron 2’s model zoo.
實例檢測是指對象的分類和本地化,并帶有包圍框。 在本文中, 我們將使用Detectron 2模型動物園中的Faster RCNN模型來識別圖像中的文本語言。
Note that we are going to limit our languages by 2.
請注意,我們將語言限制為2種。
We identify Hindi and English Text and we include a class labeled Others for other languages.
我們識別北印度語和英語文本,并為其他語言提供了一個名為“ 其他”的類。
Final Results from ColabColab的最終結果We’ll implement a model where the output would be in this way.
我們將實現一個以這種方式輸出的模型。
Let’s get started!
讓我們開始吧!
Using Detectron 2, Object Detection can be performed on any custom dataset using seven steps. All the steps are readily available in this Google Colab Notebook and you can run it straight away!
使用Detectron 2,可以使用七個步驟對任何自定義數據集執(zhí)行對象檢測。 所有這些步驟都可以在此Google Colab筆記本中輕松找到,您可以立即運行!
Using Google Colab for this would be an easy task as we can use a GPU for faster training.
使用Google Colab進行這項工作很容易,因為我們可以使用GPU進行更快的培訓。
步驟1:安裝Detectron 2 (Step 1: Installing Detectron 2)
Kickstart with installing a few dependencies such as Torch Vision and COCO API and check whether CUDA is available. CUDA helps in keeping track of the currently selected GPU. And then install Detectron2.
首先安裝一些依賴項,例如Torch Vision和COCO API,然后檢查CUDA是否可用。 CUDA有助于跟蹤當前選擇的GPU 。 然后安裝Detectron2。
# install dependencies:!pip install -U torch==1.5 torchvision==0.6 -f https://download.pytorch.org/whl/cu101/torch_stable.html
!pip install cython pyyaml==5.1
!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
!gcc --version# install detectron2:
!pip install detectron2==0.1.3 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.5/index.html
步驟2:準備和注冊數據集 (Step 2: Prepare and Register the Dataset)
Import a few necessary packages.
導入一些必要的程序包。
Datasets that have builtin support in detectron2 are listed in builtin datasets. If you want to use a custom dataset while also reusing detectron2’s data loaders, you will need to Register your dataset (i.e., tell detectron2 how to obtain your dataset).
內置數據集中列出了detectron2具有內置支持的數據集 。 如果要使用自定義數據集,同時還要重用detectron2的數據加載器,則需要注冊您的數據集(即,告訴detectron2如何獲取數據集)。
We use our Text Detection Dataset which has three classes:
我們使用具有三個類別的文本檢測數據集:
We’ll train a text detection model from an existing model pre-trained on the COCO dataset, available in detectron2’s model zoo.
我們將從在COCO數據集上預先訓練的現有模型中訓練文本檢測模型,該模型可在detectron2的模型動物園中使用。
If you’re interested to know the conversion from the raw format of the dataset to the format that Detectron 2 accepts, checkout this Colab Notebook.
如果您想知道從數據集的原始格式到Detectron 2接受的格式的轉換,請簽出此Colab Notebook 。
There are certain formats in how data can be fed to a model such as a YOLO format, PASCAL VOC format, COCO format, etc. Detectron2 accepts the COCO Format of the dataset. COCO format of the dataset consists of a JSON file which includes all the details of an image such as size, annotations (i.e., bounding box coordinates), labels corresponding to it’s bounding box, etc. For example,
如何將數據饋送到模型中有某些格式,例如YOLO格式,PASCAL VOC格式,COCO格式等。Detectron2接受數據集的COCO格式。 數據集的COCO格式由JSON文件組成,該JSON文件包含圖像的所有詳細信息,例如大小,注釋(即邊界框坐標),與該邊界框相對應的標簽等。例如,
Detectron 2’s format of DatasetDetectron 2的數據集格式This is how the JSON looks like for one image. There are different types of formats for the bounding box representation. It must be a member of structures.BoxMode for Detectron2. There are 5 such formats. But currently, it supports BoxMode.XYXY_ABS, BoxMode.XYWH_ABS. We use the second format. The (X, Y) represents one coordinate of the bounding box, and W, H represents Width and Height of that box. The category_id refers to the class the box belongs to.
這就是一張圖片的JSON外觀。 邊界框表示形式有不同類型。 它必須是Detectron2的structure.BoxMode的成員。 有5種此類格式。 但目前,它支持BoxMode.XYXY_ABS,BoxMode.XYWH_ABS 。 我們使用第二種格式。 (X,Y)表示邊界框的一個坐標,而W,H表示邊界框的Width和Height。 category_id是指盒子所屬的類。
Then, we need to register our dataset.
然后,我們需要注冊我們的數據集。
To verify the data loading is correct, let’s visualize the annotations of randomly selected samples in the training set.
為了驗證數據加載是否正確,讓我們可視化訓練集中隨機選擇的樣本的注釋。
步驟3:可視化訓練集 (Step 3: Visualize the Training Set)
We’ll randomly pick 3 pictures from the train folder of our dataset and see how the bounding boxes look like.
我們將從數據集的train文件夾中隨機選擇3張圖片,并查看邊界框的外觀。
The output looks in this way,
輸出看起來是這樣的,
Results from Colab來自Colab的結果步驟4:訓練模型 (Step 4: Training the Model)
The big step. This is the step where we give configurations and set the model ready to get trained. Technically, we just fine-tune our model on the dataset as the model is already pre-trained on COCO Dataset.
邁出了一大步 。 這是我們進行配置并設置模型以進行訓練的步驟。 從技術上講,我們只是在數據集上微調我們的模型,因為該模型已經在COCO數據集上進行了預訓練。
There are a ton of models available for object detection in the Detectron2’s Model Zoo. Here, we use the faster_rcnn_R_50_FPN_3x model which looks in this way on a high level.
Detectron2的Model Zoo中有大量模型可用于對象檢測。 在這里,我們使用faster_rcnn_R_50_FPN_3x模型,它以這種方式看起來很高級。
Source資源There’d be a Backbone Network (Resnet in this case) which is used to extract features from the image followed by a Region Proposal Network for proposing region proposals and a Box Head for tightening the bounding box.
有一個骨干網(本例中為Resnet),用于從圖像中提取特征,然后是用于提議區(qū)域提議的區(qū)域提議網絡和用于收緊邊界框的箱形頭。
You can read more about how Faster R-CNN works in my previous article.
您可以在上一篇文章中閱讀有關Faster R-CNN如何工作的更多信息。
Let’s set the configuration for training.
讓我們設置訓練的配置。
I wouldn’t say this is the best configuration. Surely, the accuracy may get improved for other configs as well. After all, it depends on choosing the right hyperparameters.
我不會說這是最好的配置。 當然,其他配置的準確性也會有所提高。 畢竟,這取決于選擇正確的超參數。
Training Process (Results from Colab)培訓過程(Colab的結果)Note that, here we also calculate the accuracy for every 500 iterations on the Validation Set.
請注意,這里我們還計算驗證集上每500次迭代的準確性。
步驟5:使用訓練后的模型進行推斷 (Step 5: Inference using the Trained Model)
It’s time to infer the results by testing the model on the Validation Set.
現在該通過在Validation Set上測試模型來推斷結果了。
An output folder gets saved in the local storage after successful completion of training in which the final weights are stored. You can save this folder for inferencing from this model in the future.
成功完成培訓后,輸出文件夾將保存在本地存儲中,其中存儲了最終權重。 您可以保存此文件夾以便將來從該模型進行推斷。
Results:
結果:
步驟6:評估訓練后的模型 (Step 6: Evaluation of the Trained Model)
Usually, the model is evaluated following the COCO Standards of evaluation. Mean Average Precision (mAP) is used to evaluate the performance of the model. Here’s an article that precisely gives an idea on the mAP.
通常,模型是按照COCO評估標準進行評估的。 平均平均精度(mAP)用于評估模型的性能。 這是一篇關于mAP的文章。
Evaluation Metrics (Results from Colab)評估指標(來自Colab的結果)We get an accuracy of around 79.4% for an IoU of 0.5 which is not that bad. It can be increased certainly by tweaking the parameters a bit and increasing the number of iterations. But keep an eye on training extensively as the model might overfit the training set.
對于0.5的IoU,我們獲得約79.4%的精度,這還不錯。 可以通過稍微調整參數并增加迭代次數來肯定地增加它。 但請密切注意培訓,因為該模型可能會超出培訓范圍。
Go through this Colab Notebook if you need inference from a saved model.
如果您需要從保存的模型中進行推斷,請瀏覽此Colab筆記本 。
結論 (Conclusion)
In this article, I emphasized on the procedure of objection detection using a custom dataset using detectron 2 rather than focusing on gaining higher accuracy.
在本文中,我重點介紹了使用使用Detectron 2的自定義數據集進行異物檢測的過程,而不是著重于獲得更高的準確性。
Although this seems to be a pretty straight forward process, there’s a lot to explore in Detectron 2’s library. We have a good amount of optimization parameters that can be further tweaked to attain higher accuracy, which completely depends on one’s custom dataset.
盡管這似乎是一個非常簡單的過程,但在Detectron 2的庫中還有很多值得探索的地方。 我們有大量的優(yōu)化參數,可以進一步調整以獲得更高的準確性,這完全取決于一個人的自定義數據集。
Hope you’ve learned something new today.
希望您今天學到了一些新知識。
You can download the notebook from my Github repository and try running it on Google Colab or Jupyter Notebooks.
您可以從我的Github存儲庫下載筆記本,然后嘗試在Google Colab或Jupyter Notebooks上運行它。
You could read more about Object Detection and the legacy R-CNN's in my previous article.
在我之前的文章中,您可以閱讀有關對象檢測和傳統(tǒng)R-CNN的更多信息。
If you’d like to get in touch, connect with me on LinkedIn.
如果您想取得聯(lián)系,請通過LinkedIn與我聯(lián)系。
翻譯自: https://towardsdatascience.com/object-detection-in-6-steps-using-detectron2-705b92575578
目標檢測 dcn v2
總結
以上是生活随笔為你收集整理的目标检测 dcn v2_使用Detectron2分6步进行目标检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无惧跌落混凝土地面!三星S23将采用全新
- 下一篇: 生成高分辨率pdf_用于高分辨率图像合成