cupy系列(二) 實現roi pooling

2021-09-24 04:55:42 字數 3270 閱讀 4931

從chainer中copy出來的。官方有cpu和gpu的分別實現

尊重原創,請看原始碼chainer_roipooling

import cupy as cp

import numpy as np

bottom_data = cp.random.randn(1,

3,40,

40, dtype=np.float32)

# 特徵feature

batch, channels, height, width = bottom_data.shape

spatial_scale =

1.0# 原始特徵和feature的比例

rois = cp.array([[

0,2,

2,10,

10],[

0,3,

3,20,

20]], dtype=np.float32)

# rois

pooled_weight =

7# 池化之後的寬度

pooled_height =

7# 池化之後的高度

top_data = cp.zeros((2

,3, pooled_height, pooled_weight)

, dtype=np.float32)

# 輸出的feature map

argmax_data = cp.zeros(top_data.shape, np.int32)

# 最大值對應的索引

## 定義核函式

roi_pooling_2d_fwd = cp.elementwisekernel(

''' raw t bottom_data, t spatial_scale, int32 channels,

int32 height, int32 width, int32 pooled_height, int32 pooled_width,

raw t bottom_rois

''','t top_data, int32 argmax_data'

,'''

// pos in output filter

int pw = i % pooled_width;

int ph = (i / pooled_width) % pooled_height;

int c = (i / pooled_width / pooled_height) % channels;

int num = i / pooled_width / pooled_height / channels;

int roi_batch_ind = bottom_rois[num * 5 + 0];

int roi_start_w = round(bottom_rois[num * 5 + 1] * spatial_scale);

int roi_start_h = round(bottom_rois[num * 5 + 2] * spatial_scale);

int roi_end_w = round(bottom_rois[num * 5 + 3] * spatial_scale);

int roi_end_h = round(bottom_rois[num * 5 + 4] * spatial_scale);

// force malformed rois to be 1x1

int roi_width = max(roi_end_w - roi_start_w + 1, 1);

int roi_height = max(roi_end_h - roi_start_h + 1, 1);

float bin_size_h = static_cast(roi_height)

/ static_cast(pooled_height);

float bin_size_w = static_cast(roi_width)

/ static_cast(pooled_width);

int hstart = static_cast(floor(static_cast(ph)

* bin_size_h));

int wstart = static_cast(floor(static_cast(pw)

* bin_size_w));

int hend = static_cast(ceil(static_cast(ph + 1)

* bin_size_h));

int wend = static_cast(ceil(static_cast(pw + 1)

* bin_size_w));

// add roi offsets and clip to input boundaries

hstart = min(max(hstart + roi_start_h, 0), height);

hend = min(max(hend + roi_start_h, 0), height);

wstart = min(max(wstart + roi_start_w, 0), width);

wend = min(max(wend + roi_start_w, 0), width);

bool is_empty = (hend <= hstart) || (wend <= wstart);

// define an empty pooling region to be zero

float maxval = is_empty ? 0 : -1e+37;

// if nothing is pooled, argmax=-1 causes nothing to be backprop'd

int maxidx = -1;

int data_offset = (roi_batch_ind * channels + c) * height * width;

for (int h = hstart; h < hend; ++h) }}

top_data = maxval;

argmax_data = maxidx;

''','roi_pooling_2d_fwd'

)roi_pooling_2d_fwd(bottom_data, spatial_scale, channels, height, width,

pooled_height, pooled_weight, rois, top_data, argmax_data)

top_data.shape

>>

>(2

,3,7

,7)

SRS系列二 初步實現HLS直播

srs實現hls直播,必然會實現rtmp直播,故srs部署已經初期的配置請參考我的另一篇部落格 srs系列一 實現rtmp直播 1 編寫srs配置檔案 此處srs部署已完成 cd trunk conf vim srs.conf 如圖所示,新增以下配置資訊 2 啟動srs 如已啟動過,需殺程序重啟 c...

python實現劍指offer系列 重建二叉樹

題目 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列和中序遍歷序列,則重建二叉樹並返回。class node def init self,data,left,right self.data data self.le...

爬蟲系列二

6.3.re庫的match物件 6.4.貪婪匹配和最小匹配 七 練習 一切為了資料探勘的準備 在中國大學mooc 上學習的北京理工大學嵩天老師的免費爬蟲課程課件,簡單易懂,感興趣的戳 嵩天老師爬蟲課程。侵刪 六 正規表示式 編譯 將符合正規表示式語法的字串轉化為正規表示式特徵,只有在compile之...