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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

faster rcnn的源码理解(一)SmoothL1LossLayer论文与代码的结合理解

發布時間:2025/3/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 faster rcnn的源码理解(一)SmoothL1LossLayer论文与代码的结合理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自:faster rcnn的源碼理解(一)SmoothL1LossLayer論文與代碼的結合理解 - 野孩子的專欄 - 博客頻道 - CSDN.NET
http://blog.csdn.net/u010668907/article/details/51456928

源碼:

[python]?view plaincopy print?
  • //?------------------------------------------------------------------??
  • //?Fast?R-CNN??
  • //?Copyright?(c)?2015?Microsoft??
  • //?Licensed?under?The?MIT?License?[see?fast-rcnn/LICENSE?for?details]??
  • //?Written?by?Ross?Girshick??
  • //?------------------------------------------------------------------??
  • ??
  • #include?"caffe/fast_rcnn_layers.hpp"??
  • ??
  • namespace?caffe?{??
  • ??
  • template?<typename?Dtype>??
  • __global__?void?SmoothL1Forward(const?int?n,?const?Dtype*?in,?Dtype*?out,??
  • ????Dtype?sigma2)?{??
  • ??//?f(x)?=?0.5?*?(sigma?*?x)^2??????????if?|x|?<?1?/?sigma?/?sigma??
  • ??//????????|x|?-?0.5?/?sigma?/?sigma????otherwise??
  • ??CUDA_KERNEL_LOOP(index,?n)?{??
  • ????Dtype?val?=?in[index];??
  • ????Dtype?abs_val?=?abs(val);??
  • ????if?(abs_val?<?1.0?/?sigma2)?{??
  • ??????out[index]?=?0.5?*?val?*?val?*?sigma2;??
  • ????}?else?{??
  • ??????out[index]?=?abs_val?-?0.5?/?sigma2;??
  • ????}??
  • ??}??
  • }??
  • ??
  • template?<typename?Dtype>??
  • void?SmoothL1LossLayer<Dtype>::Forward_gpu(const?vector<Blob<Dtype>*>&?bottom,??
  • ????const?vector<Blob<Dtype>*>&?top)?{??
  • ??int?count?=?bottom[0]->count();??
  • ??caffe_gpu_sub(??
  • ??????count,??
  • ??????bottom[0]->gpu_data(),??
  • ??????bottom[1]->gpu_data(),??
  • ??????diff_.mutable_gpu_data());????//?d?:=?b0?-?b1??
  • ??if?(has_weights_)?{??
  • ????//?apply?"inside"?weights??
  • ????caffe_gpu_mul(??
  • ????????count,??
  • ????????bottom[2]->gpu_data(),??
  • ????????diff_.gpu_data(),??
  • ????????diff_.mutable_gpu_data());??//?d?:=?w_in?*?(b0?-?b1)??
  • ??}??
  • ??SmoothL1Forward<Dtype><<<CAFFE_GET_BLOCKS(count),?CAFFE_CUDA_NUM_THREADS>>>(??
  • ??????count,?diff_.gpu_data(),?errors_.mutable_gpu_data(),?sigma2_);??
  • ??CUDA_POST_KERNEL_CHECK;??
  • ??
  • ??if?(has_weights_)?{??
  • ????//?apply?"outside"?weights??
  • ????caffe_gpu_mul(??
  • ????????count,??
  • ????????bottom[3]->gpu_data(),??
  • ????????errors_.gpu_data(),??
  • ????????errors_.mutable_gpu_data());??//?d?:=?w_out?*?SmoothL1(w_in?*?(b0?-?b1))??
  • ??}??
  • ??
  • ??Dtype?loss;??
  • ??caffe_gpu_dot(count,?ones_.gpu_data(),?errors_.gpu_data(),?&loss);??
  • ??top[0]->mutable_cpu_data()[0]?=?loss?/?bottom[0]->num();??
  • }??
  • ??
  • template?<typename?Dtype>??
  • __global__?void?SmoothL1Backward(const?int?n,?const?Dtype*?in,?Dtype*?out,??
  • ????Dtype?sigma2)?{??
  • ??//?f'(x)?=?sigma?*?sigma?*?x?????????if?|x|?<?1?/?sigma?/?sigma??
  • ??//???????=?sign(x)???????????????????otherwise??
  • ??CUDA_KERNEL_LOOP(index,?n)?{??
  • ????Dtype?val?=?in[index];??
  • ????Dtype?abs_val?=?abs(val);??
  • ????if?(abs_val?<?1.0?/?sigma2)?{??
  • ??????out[index]?=?sigma2?*?val;??
  • ????}?else?{??
  • ??????out[index]?=?(Dtype(0)?<?val)?-?(val?<?Dtype(0));??
  • ????}??
  • ??}??
  • }??
  • ??
  • template?<typename?Dtype>??
  • void?SmoothL1LossLayer<Dtype>::Backward_gpu(const?vector<Blob<Dtype>*>&?top,??
  • ????const?vector<bool>&?propagate_down,?const?vector<Blob<Dtype>*>&?bottom)?{??
  • ??//?after?forwards,?diff_?holds?w_in?*?(b0?-?b1)??
  • ??int?count?=?diff_.count();??
  • ??SmoothL1Backward<Dtype><<<CAFFE_GET_BLOCKS(count),?CAFFE_CUDA_NUM_THREADS>>>(??
  • ??????count,?diff_.gpu_data(),?diff_.mutable_gpu_data(),?sigma2_);??
  • ??CUDA_POST_KERNEL_CHECK;??
  • ??for?(int?i?=?0;?i?<?2;?++i)?{??
  • ????if?(propagate_down[i])?{??
  • ??????const?Dtype?sign?=?(i?==?0)???1?:?-1;??
  • ??????const?Dtype?alpha?=?sign?*?top[0]->cpu_diff()[0]?/?bottom[i]->num();??
  • ??????caffe_gpu_axpby(??
  • ??????????count,???????????????????????????//?count??
  • ??????????alpha,???????????????????????????//?alpha??
  • ??????????diff_.gpu_data(),????????????????//?x??
  • ??????????Dtype(0),????????????????????????//?beta??
  • ??????????bottom[i]->mutable_gpu_diff());??//?y??
  • ??????if?(has_weights_)?{??
  • ????????//?Scale?by?"inside"?weight??
  • ????????caffe_gpu_mul(??
  • ????????????count,??
  • ????????????bottom[2]->gpu_data(),??
  • ????????????bottom[i]->gpu_diff(),??
  • ????????????bottom[i]->mutable_gpu_diff());??
  • ????????//?Scale?by?"outside"?weight??
  • ????????caffe_gpu_mul(??
  • ????????????count,??
  • ????????????bottom[3]->gpu_data(),??
  • ????????????bottom[i]->gpu_diff(),??
  • ????????????bottom[i]->mutable_gpu_diff());??
  • ??????}??
  • ????}??
  • ??}??
  • }??
  • ??
  • INSTANTIATE_LAYER_GPU_FUNCS(SmoothL1LossLayer);??
  • ??
  • }??//?namespace?caffe??

  • SmoothL1LossLayer?計算一張圖片的損失函數,對應于下圖的加號右邊部分

    ?

    imini-batchanchor的索引。

    Pi是目標的預測概率。

    有物體時pi*1,否則為0

    ti是一個向量,預測坐標

    ti*是一個向量,是gt包圍盒的坐標

    ?

    bottom[0]預測坐標,對應于下圖的ti

    bottom[1]target坐標,對應于下圖的ti*

    bottom[2]inside,有物體(fg)時為1,否則為0,對應于下圖的pi*

    bottom[3]outside,沒有前景(fg)也沒有后景(bg)的為0,其他為1/bg+fg),對應于加號右邊的系數部分(但其實這個地方我本人還是不懂,因為論文上說的系數都是一些固定的值,如入=10。初始代碼一直在更新,估計又換了別的方法。不論如何,在現在的代碼中outside是乘以了后面的結果)

    ?

    Lreg的公式就是下圖,另x=ti?-?ti*

    ?

    ?

    Pi*Leg(ti,?ti*)表明只有有fg20個物體類別)的才有回歸損失

    總結

    以上是生活随笔為你收集整理的faster rcnn的源码理解(一)SmoothL1LossLayer论文与代码的结合理解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。