py-faster-rcnn代碼roidb.py的解讀

roidb是比較復雜的數據結構,存放了數據集的roi信息。原始的roidb來自數據集,在trian.py的get_training_roidb(imdb)函數進行了水平翻轉擴充數量,然后prepare_roidb(imdb)【定義在roidb.py】為roidb添加了一些說明性的屬性。

在這里暫時記錄下roidb的結構信息,后面繼續看的時候可能會有些修正:

roidb是由字典組成的list,roidb[img_index]包含了該圖片索引所包含到roi信息,下面以roidb[img_index]為例說明:

roidb[img_index]包含的key, value
boxes box位置信息,box_num*4的np array
gt_overlaps 所有box在不同類別的得分,box_num*class_num矩陣
gt_classes 所有box的真實類別,box_num長度的list
flipped 是否翻轉
?image 該圖片的路徑,字符串
width 圖片的寬
height? 圖片的高
max_overlaps 每個box的在所有類別的得分最大值,box_num長度
max_classes 每個box的得分最高所對應的類,box_num長度
bbox_targets 每個box的類別,以及與最接近的gt-box的4個方位偏移
(共5列)

def add_bbox_regression_targets(roidb):"""Add information needed to train bounding-box regressors."""assert len(roidb) > 0assert 'max_classes' in roidb[0], 'Did you call prepare_roidb first?'num_images = len(roidb)# Infer number of classes from the number of columns in gt_overlaps# 類別數,roidb[0]對應第0號圖片上的roi,shape[1]多少列表示roi屬于不同類上的概率num_classes = roidb[0]['gt_overlaps'].shape[1]for im_i in xrange(num_images):rois = roidb[im_i]['boxes']max_overlaps = roidb[im_i]['max_overlaps']max_classes = roidb[im_i]['max_classes']# bbox_targets:每個box的類別,以及與最接近的gt-box的4個方位偏移roidb[im_i]['bbox_targets'] = \_compute_targets(rois, max_overlaps, max_classes)# 這里config是falseif cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:# Use fixed / precomputed "means" and "stds" instead of empirical values# 使用固定的均值和方差代替經驗值means = np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS), (num_classes, 1))stds = np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS), (num_classes, 1))else:# Compute values needed for means and stds# 計算所需的均值和方差# var(x) = E(x^2) - E(x)^2# 計數各個類別出現box的數量class_counts = np.zeros((num_classes, 1)) + cfg.EPS  #加上cfg.EPS防止除0出錯# 21類*4個位置,如果出現box的類別與其中某一類相同,將該box的4個target加入4個列元素中sums = np.zeros((num_classes, 4)) # 21類*4個位置,如果出現box的類別與其中某一類相同,將該box的4個target的平方加入4個列元素中squared_sums = np.zeros((num_classes, 4))for im_i in xrange(num_images):targets = roidb[im_i]['bbox_targets']for cls in xrange(1, num_classes):cls_inds = np.where(targets[:, 0] == cls)[0]# box的類別與該類匹配,計入if cls_inds.size > 0:class_counts[cls] += cls_inds.sizesums[cls, :] += targets[cls_inds, 1:].sum(axis=0)squared_sums[cls, :] += \(targets[cls_inds, 1:] ** 2).sum(axis=0)means = sums / class_counts # 均值stds = np.sqrt(squared_sums / class_counts - means ** 2) #標準差print 'bbox target means:'print meansprint means[1:, :].mean(axis=0) # ignore bg classprint 'bbox target stdevs:'print stdsprint stds[1:, :].mean(axis=0) # ignore bg class# Normalize targets# 對每一box歸一化targetif cfg.TRAIN.BBOX_NORMALIZE_TARGETS:print "Normalizing targets"for im_i in xrange(num_images):targets = roidb[im_i]['bbox_targets']for cls in xrange(1, num_classes):cls_inds = np.where(targets[:, 0] == cls)[0]roidb[im_i]['bbox_targets'][cls_inds, 1:] -= means[cls, :]roidb[im_i]['bbox_targets'][cls_inds, 1:] /= stds[cls, :]else:print "NOT normalizing targets"# 均值和方差也用于預測# These values will be needed for making predictions# (the predicts will need to be unnormalized and uncentered)return means.ravel(), stds.ravel()  # ravel()排序拉成一維def _compute_targets(rois, overlaps, labels):  # 參數rois只含有當前圖片的box信息"""Compute bounding-box regression targets for an image."""# Indices目錄 of ground-truth ROIs# ground-truth ROIsgt_inds = np.where(overlaps == 1)[0]if len(gt_inds) == 0:# Bail if the image has no ground-truth ROIs# 不存在gt ROI,返回空數組return np.zeros((rois.shape[0], 5), dtype=np.float32)# Indices of examples for which we try to make predictions# BBOX閾值,只有ROI與gt的重疊度大于閾值,這樣的ROI才能用作bb回歸的訓練樣本ex_inds = np.where(overlaps >= cfg.TRAIN.BBOX_THRESH)[0]# Get IoU overlap between each ex ROI and gt ROI# 計算ex ROI and gt ROI的IoUex_gt_overlaps = bbox_overlaps(# 變數據格式為floatnp.ascontiguousarray(rois[ex_inds, :], dtype=np.float),np.ascontiguousarray(rois[gt_inds, :], dtype=np.float))# Find which gt ROI each ex ROI has max overlap with:# this will be the ex ROI's gt target# 這里每一行代表一個ex_roi,列代表gt_roi,元素數值代表兩者的IoUgt_assignment = ex_gt_overlaps.argmax(axis=1) #按行求最大,返回索引.gt_rois = rois[gt_inds[gt_assignment], :]  #每個ex_roi對應的gt_rois,與下面ex_roi數量相同ex_rois = rois[ex_inds, :]targets = np.zeros((rois.shape[0], 5), dtype=np.float32)targets[ex_inds, 0] = labels[ex_inds]  #第一個元素是labeltargets[ex_inds, 1:] = bbox_transform(ex_rois, gt_rois)  #后4個元素是ex_box與gt_box的4個方位的偏移return targets

下面解讀一下這兩個函數。

1.?_compute_targets(rois, overlaps, labels)

這個函數用來計算roi的偏移量。基本的步驟就是先確認是否含有ground-truth rois,主要通過?ground-truth ROIs的overlaps=1來確認。

然后找到重疊度大于一定閾值的box,再進行計算。


對于滿足條件的box,會調用程序bbox_overlaps重新計算box對應于ground-truth box的重疊度,根據最大的重疊度找對應的ground truth box.

這樣就可以利用?fast_rcnn.bbox_transform 的bbox_transform計算4個偏移(分別是中心點的x,y坐標,w,d長度偏移)。

輸出的是一個二維數組,橫坐標是盒子的序號,縱坐標是5維,第一維是類別,第二維到第五維為偏移。

bbox_overlaps的代碼如下:

def bbox_overlaps(np.ndarray[DTYPE_t, ndim=2] boxes,np.ndarray[DTYPE_t, ndim=2] query_boxes):"""Parameters----------boxes: (N, 4) ndarray of floatquery_boxes: (K, 4) ndarray of floatReturns-------overlaps: (N, K) ndarray of overlap between boxes and query_boxes"""cdef unsigned int N = boxes.shape[0]cdef unsigned int K = query_boxes.shape[0]cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)cdef DTYPE_t iw, ih, box_areacdef DTYPE_t uacdef unsigned int k, nfor k in range(K):box_area = ((query_boxes[k, 2] - query_boxes[k, 0] + 1) *(query_boxes[k, 3] - query_boxes[k, 1] + 1))for n in range(N):iw = (min(boxes[n, 2], query_boxes[k, 2]) -max(boxes[n, 0], query_boxes[k, 0]) + 1)if iw > 0:ih = (min(boxes[n, 3], query_boxes[k, 3]) -max(boxes[n, 1], query_boxes[k, 1]) + 1)if ih > 0:ua = float((boxes[n, 2] - boxes[n, 0] + 1) *(boxes[n, 3] - boxes[n, 1] + 1) +box_area - iw * ih)overlaps[n, k] = iw * ih / uareturn overlaps


2.?add_bbox_regression_targets

? ? 主要兩個兩件事: 1. 確定roidb每個圖片的box的回歸偏移量bbox_targets

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2. 對于所有的類別,計算偏移量的均值和方差,這樣輸出的矩陣是二維,行坐標是種類(這里是21類),縱坐標是偏移量(這里是4).

并且在需要正則化目標項(即cfg.TRAIN.BBOX_NORMALIZE_TARGETS=true)時,使每個偏移都減去均值除以標準差。并返回均值和方差的折疊一維向量,

用于預測(即逆操作,去正則化,則中心化)。


參考:

  • py-faster-rcnn代碼閱讀3-roidb.py
  • Faster RCNN roidb.py


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258656.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258656.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258656.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python 概率分布_python實現概率分布

伯努利分布from scipy import statsimport numpy as npimport matplotlib.pyplot as pltxnp.arange(0,2,1)xarray([0, 1])# 求對應分布的概率:概率質量函數 (PMF)p0.5# 硬幣朝上的概率dfstats.bernoulli.pmf(x,p)dfarray([0.5, 0.5])#繪圖vlines用于繪制豎直線(vert…

CodeForces 7D Palindrome Degree 字符串hash

題目鏈接&#xff1a;點擊打開鏈接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib…

程序清單8-9 回送所有命令行參數和所有環境字符串

1 /*2 3 Name : test.c4 Author : blank5 Version :6 Copyright : Your copyright notice7 Description : 程序清單8-9 回送所有命令行參數和所有環境字符串8 9 */ 10 11 #include "ourhdr.h" 12 13 int main(int argc, char *argv[]) 14…

SQL快速入門

關系化數據庫保存關系模式數據的容器關系模式是對業務對象實體&#xff0c;屬性以及關系的抽象&#xff0c;提煉需求的名詞是建立實體關系模型常用的方法。要了解E-R實體關系圖的繪制。常用關系數據庫Microsoft SQL Server&#xff1b;微軟公司產品&#xff0c;中等規模數據庫&…

Faster RCNN minibatch.py解讀

minibatch.py 的功能是&#xff1a; Compute minibatch blobs for training a Fast R-CNN network. 與roidb不同的是&#xff0c; minibatch中存儲的并不是完整的整張圖像圖像&#xff0c;而是從圖像經過轉換后得到的四維blob以及從圖像中截取的proposals&#xff0c;以及與之對…

oracle精簡版_使用Entity Framework Core訪問數據庫(Oracle篇)

前言哇。。看看時間 真的很久很久沒寫博客了 將近一年了。最近一直在忙各種家中事務和公司的新框架 終于抽出時間來更新一波了。本篇主要講一下關于Entity Framework Core訪問oracle數據庫的采坑。。強調一下&#xff0c;本篇文章發布之前 關于Entity Framework Core訪問oracl…

interrupt、interrupted 、isInterrupted 區別

interrupt&#xff1a;調用方法&#xff0c;是線程處于中斷狀態&#xff0c;但是這個方法只是讓線程設置為中斷狀態&#xff0c;并不會真正的停止線程。支持線程中斷的方法就是在堅持線程中斷狀態&#xff0c;一旦線程中斷狀態被設置為中斷&#xff0c;就會拋出異常。interrupt…

java String部分源碼解析

String類型的成員變量 /** String的屬性值 */ private final char value[];/** The offset is the first index of the storage that is used. *//**數組被使用的開始位置**/private final int offset;/** The count is the number of characters in the String. *//**String中…

python在材料模擬中的應用_基于Python的ABAQUS二次開發及在板料快速沖壓成形模擬中的應用...

2009doi:1013969/j1issn1100722012120091041013基于Python的ABAQUS二次開發及在板料快速沖壓成形模擬中的應用(北京航空航天大學飛行器制造工程系,北京100191)吳向東劉志剛萬敏王文平黃霖摘要:采用Python腳本語言對ABAQUS的前處理模塊進行二次開發,討論了Python腳本在ABAQUS二次…

Doxygen簡介

&#xff08;轉自&#xff1a;http://www.cnblogs.com/liuliunumberone/archive/2012/04/10/2441391.html&#xff09; 一&#xff0e;什么是Doxygen? Doxygen 是一個程序的文件產生工具&#xff0c;可將程序中的特定批注轉換成為說明文件。通常我們在寫程序時&#xff0c;或多…

javascript之閉包理解以及應用場景

1 function fn(){2 var a 0;3 return function (){4 return a;5 } 6 }如上所示&#xff0c;上面第一個return返回的就是一個閉包&#xff0c;那么本質上說閉包就是一個函數。那么返回這個函數有什么用呢&#xff1f;那是因為這個函數可以調用到它外部的a…

faster rcnn學習之rpn、fast rcnn數據準備說明

在上文《 faster-rcnn系列學習之準備數據》,我們已經介紹了imdb與roidb的一些情況&#xff0c;下面我們準備再繼續說一下rpn階段和fast rcnn階段的數據準備整個處理流程。 由于這兩個階段的數據準備有些重合&#xff0c;所以放在一起說明。 我們并行地從train_rpn與train_fas…

sql server規范

常見的字段類型選擇 1.字符類型建議采用varchar/nvarchar數據類型2.金額貨幣建議采用money數據類型3.科學計數建議采用numeric數據類型4.自增長標識建議采用bigint數據類型 (數據量一大&#xff0c;用int類型就裝不下&#xff0c;那以后改造就麻煩了)5.時間類型建議采用為dat…

關于標準庫中的ptr_fun/binary_function/bind1st/bind2nd

http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少&#xff0c;后來發現這兩個函數還挺好玩的&#xff0c;于是關心上了。在C Primer對于bind函數的描述如下&#xff1a;“綁定器binder通過把二元函數對象的一個實參綁定到…

CSS偽類

一、首字母的顏色字體寫法 p:first-letter 二、文本的特殊樣式設置 first-line css偽類可與css類配合使用 偽元素只能用于塊級元素 轉載于:https://www.cnblogs.com/boyblog/p/4623374.html

php 結構體_【開發規范】PHP編碼開發規范下篇:PSR-2編碼風格規范

之前的一篇文章是對PSR-1的基本介紹接下來是PSR-2 編碼風格規范&#xff0c;它是 PSR-1 基本代碼規范的繼承與擴展。PSR-1 和PSR-2是PHP開發中基本的編碼規范&#xff0c;大家其實都可以參考學習下&#xff0c;雖然說每個開發者都有自己熟悉的一套開發規范&#xff0c;但是我覺…

faster rcnn學習之rpn訓練全過程

上篇我們講解了rpn與fast rcnn的數據準備階段&#xff0c;接下來我們講解rpn的整個訓練過程。最后 講解rpn訓練完畢后rpn的生成。 我們順著stage1_rpn_train.pt的內容講解。 name: "VGG_CNN_M_1024" layer {name: input-datatype: Pythontop: datatop: im_infotop: …

BitMapData知識 轉

Bitmap和BitmapData 2010.5.25 smartblack整理 一、flash.display.Bitmap類及其兩個子類 1、繼承自DisplayObject&#xff0c;和InteractiveObject平級&#xff0c;所以無法調度鼠標事件&#xff0c;可以使用額外的包裝容器(Sprite)來實現偵聽。 2、只支持GIF、JPEG、PNG格式&a…

Android學習之高德地圖的通用功能開發步驟(二)

周一又來了&#xff0c;我就接著上次的開發步驟&#xff08;一&#xff09;來吧&#xff0c;繼續把高德地圖的相關簡單功能分享一下 上次寫到了第六步&#xff0c;接著寫第七步吧。 第七步&#xff1a;定位 地圖選點 路徑規劃 實時導航 以下是我的這個功能NaviMapActivity的…

Oracle中分區表中表空間屬性

Oracle中的分區表是Oracle中的一個很好的特性&#xff0c;可以把大表劃分成多個小表&#xff0c;從而提高對于該大表的SQL執行效率&#xff0c;而各個分區對應用又是透明的。分區表中的每個分區有獨立的存儲特性&#xff0c;包括表空間、PCT_FREE等。那分區表中的各分區表空間之…