日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > 目标检测 >内容正文

目标检测

目标检测 dcn v2_使用Detectron2分6步进行目标检测

發(fā)布時(shí)間:2023/12/15 目标检测 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 目标检测 dcn v2_使用Detectron2分6步进行目标检测 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目標(biāo)檢測(cè) dcn v2

Have you ever tried training an object detection model using a custom dataset of your own choice from scratch?

您是否曾經(jīng)嘗試使用自己選擇的自定義數(shù)據(jù)集來(lái)訓(xùn)練對(duì)象檢測(cè)模型?

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.

如果是,您將知道該過(guò)程將是多么乏味。 如果我們選擇基于區(qū)域提議的方法(例如Faster R-CNN),或者使用像SSD和YOLO這樣的單次檢測(cè)器算法,則需要從使用特征金字塔網(wǎng)絡(luò)與區(qū)域提議網(wǎng)絡(luò)相結(jié)合來(lái)構(gòu)建模型開(kāi)始。

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.

如果我們想從頭開(kāi)始實(shí)現(xiàn),那么使用它們中的任一個(gè)都有些復(fù)雜。 我們需要一個(gè)框架,在這里我們可以輕松使用最新模型,例如Fast,Faster和Mask R-CNN。 盡管如此,重要的是嘗試至少?gòu)念^開(kāi)始構(gòu)建模型一次以了解其背后的數(shù)學(xué)原理。

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.

如果我們想使用自定義數(shù)據(jù)集快速訓(xùn)練對(duì)象檢測(cè)模型,則可以使用Detectron 2進(jìn)行救援。 Detectron 2庫(kù)的模型庫(kù)中存在的所有模型都在COCO Dataset上進(jìn)行了預(yù)訓(xùn)練。 我們只需要在預(yù)先訓(xùn)練的模型上微調(diào)我們的自定義數(shù)據(jù)集即可。

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是對(duì)2018年發(fā)布的第一個(gè)Detectron的完全重寫(xiě)。其前身是在Caffe2上編寫(xiě)的,Caffe2是一個(gè)深度學(xué)習(xí)框架,也得到Facebook的支持。 Caffe2和Detectron現(xiàn)在都已棄用。 Caffe2現(xiàn)在是PyTorch的一部分,其后繼者Detectron 2完全用PyTorch編寫(xiě)。

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旨在通過(guò)提供快速的培訓(xùn)并解決公司從研究到生產(chǎn)的過(guò)程中面臨的問(wèn)題,來(lái)促進(jìn)機(jī)器學(xué)習(xí)。

These are the various types of Object Detection models that the Detectron 2 offers.

這些是Detectron 2提供的各種類(lèi)型的對(duì)象檢測(cè)模型。

https://research.fb.com/wp-content/uploads/2019/12/4.-detectron2.pdfhttps://research.fb.com/wp-content/uploads/2019/12/4.-detectron2.pdf

Let’s dive into Instance Detection directly.

讓我們直接研究實(shí)例檢測(cè)

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.

實(shí)例檢測(cè)是指對(duì)象的分類(lèi)和本地化,并帶有包圍框。 在本文中, 我們將使用Detectron 2模型動(dòng)物園中的Faster RCNN模型來(lái)識(shí)別圖像中的文本語(yǔ)言。

Note that we are going to limit our languages by 2.

請(qǐng)注意,我們將語(yǔ)言限制為2種。

We identify Hindi and English Text and we include a class labeled Others for other languages.

我們識(shí)別北印度語(yǔ)英語(yǔ)文本,并為其他語(yǔ)言提供了一個(gè)名為“ 其他”的類(lèi)。

Final Results from ColabColab的最終結(jié)果

We’ll implement a model where the output would be in this way.

我們將實(shí)現(xiàn)一個(gè)以這種方式輸出的模型。

Let’s get started!

讓我們開(kāi)始吧!

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,可以使用七個(gè)步驟對(duì)任何自定義數(shù)據(jù)集執(zhí)行對(duì)象檢測(cè)。 所有這些步驟都可以在此Google Colab筆記本中輕松找到,您可以立即運(yùn)行!

Using Google Colab for this would be an easy task as we can use a GPU for faster training.

使用Google Colab進(jìn)行這項(xiàng)工作很容易,因?yàn)槲覀兛梢允褂肎PU進(jìn)行更快的培訓(xùn)。

步驟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.

首先安裝一些依賴(lài)項(xiàng),例如Torch Vision和COCO API,然后檢查CUDA是否可用。 CUDA有助于跟蹤當(dāng)前選擇的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:準(zhǔn)備和注冊(cè)數(shù)據(jù)集 (Step 2: Prepare and Register the Dataset)

Import a few necessary packages.

導(dǎo)入一些必要的程序包。

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).

內(nèi)置數(shù)據(jù)集中列出了detectron2具有內(nèi)置支持的數(shù)據(jù)集 。 如果要使用自定義數(shù)據(jù)集,同時(shí)還要重用detectron2的數(shù)據(jù)加載器,則需要注冊(cè)您的數(shù)據(jù)集(即,告訴detectron2如何獲取數(shù)據(jù)集)。

We use our Text Detection Dataset which has three classes:

我們使用具有三個(gè)類(lèi)別的文本檢測(cè)數(shù)據(jù)集:

  • English

    英語(yǔ)
  • Hindi

    印地語(yǔ)
  • Others

    其他
  • We’ll train a text detection model from an existing model pre-trained on the COCO dataset, available in detectron2’s model zoo.

    我們將從在COCO數(shù)據(jù)集上預(yù)先訓(xùn)練的現(xiàn)有模型中訓(xùn)練文本檢測(cè)模型,該模型可在detectron2的模型動(dòng)物園中使用。

    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.

    如果您想知道從數(shù)據(jù)集的原始格式到Detectron 2接受的格式的轉(zhuǎn)換,請(qǐng)簽出此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,

    如何將數(shù)據(jù)饋送到模型中有某些格式,例如YOLO格式,PASCAL VOC格式,COCO格式等。Detectron2接受數(shù)據(jù)集的COCO格式。 數(shù)據(jù)集的COCO格式由JSON文件組成,該JSON文件包含圖像的所有詳細(xì)信息,例如大小,注釋(即邊界框坐標(biāo)),與該邊界框相對(duì)應(yīng)的標(biāo)簽等。例如,

    Detectron 2’s format of DatasetDetectron 2的數(shù)據(jù)集格式

    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外觀。 邊界框表示形式有不同類(lèi)型。 它必須是Detectron2的structure.BoxMode的成員。 有5種此類(lèi)格式。 但目前,它支持BoxMode.XYXY_ABS,BoxMode.XYWH_ABS 。 我們使用第二種格式。 (X,Y)表示邊界框的一個(gè)坐標(biāo),而W,H表示邊界框的Width和Height。 category_id是指盒子所屬的類(lèi)。

    Then, we need to register our dataset.

    然后,我們需要注冊(cè)我們的數(shù)據(jù)集。

    To verify the data loading is correct, let’s visualize the annotations of randomly selected samples in the training set.

    為了驗(yàn)證數(shù)據(jù)加載是否正確,讓我們可視化訓(xùn)練集中隨機(jī)選擇的樣本的注釋。

    步驟3:可視化訓(xùn)練集 (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.

    我們將從數(shù)據(jù)集的train文件夾中隨機(jī)選擇3張圖片,并查看邊界框的外觀。

    The output looks in this way,

    輸出看起來(lái)是這樣的,

    Results from Colab來(lái)自Colab的結(jié)果

    步驟4:訓(xùn)練模型 (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.

    邁出了一大步 。 這是我們進(jìn)行配置并設(shè)置模型以進(jìn)行訓(xùn)練的步驟。 從技術(shù)上講,我們只是在數(shù)據(jù)集上微調(diào)我們的模型,因?yàn)樵撃P鸵呀?jīng)在COCO數(shù)據(jù)集上進(jìn)行了預(yù)訓(xùn)練。

    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中有大量模型可用于對(duì)象檢測(cè)。 在這里,我們使用faster_rcnn_R_50_FPN_3x模型,它以這種方式看起來(lái)很高級(jí)。

    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.

    有一個(gè)骨干網(wǎng)(本例中為Resnet),用于從圖像中提取特征,然后是用于提議區(qū)域提議的區(qū)域提議網(wǎng)絡(luò)和用于收緊邊界框的箱形頭。

    You can read more about how Faster R-CNN works in my previous article.

    您可以在上一篇文章中閱讀有關(guān)Faster R-CNN如何工作的更多信息。

    Let’s set the configuration for training.

    讓我們?cè)O(shè)置訓(xùn)練的配置。

    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.

    我不會(huì)說(shuō)這是最好的配置。 當(dāng)然,其他配置的準(zhǔn)確性也會(huì)有所提高。 畢竟,這取決于選擇正確的超參數(shù)。

    Training Process (Results from Colab)培訓(xùn)過(guò)程(Colab的結(jié)果)

    Note that, here we also calculate the accuracy for every 500 iterations on the Validation Set.

    請(qǐng)注意,這里我們還計(jì)算驗(yàn)證集上每500次迭代的準(zhǔn)確性。

    步驟5:使用訓(xùn)練后的模型進(jìn)行推斷 (Step 5: Inference using the Trained Model)

    It’s time to infer the results by testing the model on the Validation Set.

    現(xiàn)在該通過(guò)在Validation Set上測(cè)試模型來(lái)推斷結(jié)果了。

    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.

    成功完成培訓(xùn)后,輸出文件夾將保存在本地存儲(chǔ)中,其中存儲(chǔ)了最終權(quán)重。 您可以保存此文件夾以便將來(lái)從該模型進(jìn)行推斷。

    Results:

    結(jié)果:

    步驟6:評(píng)估訓(xùn)練后的模型 (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評(píng)估標(biāo)準(zhǔn)進(jìn)行評(píng)估的。 平均平均精度(mAP)用于評(píng)估模型的性能。 這是一篇關(guān)于mAP的文章。

    Evaluation Metrics (Results from Colab)評(píng)估指標(biāo)(來(lái)自Colab的結(jié)果)

    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.

    對(duì)于0.5的IoU,我們獲得約79.4%的精度,這還不錯(cuò)。 可以通過(guò)稍微調(diào)整參數(shù)并增加迭代次數(shù)來(lái)肯定地增加它。 但請(qǐng)密切注意培訓(xùn),因?yàn)樵撃P涂赡軙?huì)超出培訓(xùn)范圍。

    Go through this Colab Notebook if you need inference from a saved model.

    如果您需要從保存的模型中進(jìn)行推斷,請(qǐng)瀏覽此Colab筆記本 。

    結(jié)論 (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.

    在本文中,我重點(diǎn)介紹了使用使用Detectron 2的自定義數(shù)據(jù)集進(jìn)行異物檢測(cè)的過(guò)程,而不是著重于獲得更高的準(zhǔn)確性。

    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.

    盡管這似乎是一個(gè)非常簡(jiǎn)單的過(guò)程,但在Detectron 2的庫(kù)中還有很多值得探索的地方。 我們有大量的優(yōu)化參數(shù),可以進(jìn)一步調(diào)整以獲得更高的準(zhǔn)確性,這完全取決于一個(gè)人的自定義數(shù)據(jù)集。

    Hope you’ve learned something new today.

    希望您今天學(xué)到了一些新知識(shí)。

    You can download the notebook from my Github repository and try running it on Google Colab or Jupyter Notebooks.

    您可以從我的Github存儲(chǔ)庫(kù)下載筆記本,然后嘗試在Google Colab或Jupyter Notebooks上運(yùn)行它。

    You could read more about Object Detection and the legacy R-CNN's in my previous article.

    在我之前的文章中,您可以閱讀有關(guān)對(duì)象檢測(cè)和傳統(tǒng)R-CNN的更多信息。

    If you’d like to get in touch, connect with me on LinkedIn.

    如果您想取得聯(lián)系,請(qǐng)通過(guò)LinkedIn與我聯(lián)系。

    翻譯自: https://towardsdatascience.com/object-detection-in-6-steps-using-detectron2-705b92575578

    目標(biāo)檢測(cè) dcn v2

    總結(jié)

    以上是生活随笔為你收集整理的目标检测 dcn v2_使用Detectron2分6步进行目标检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。