OHEM 代码详解

article/2025/11/11 5:52:47

目录

1.网络结构

2.OHEM 前向传播

3 .reference


1.网络结构

##########################
## Readonly RoI Network ##
######### Start ##########
layer {name: "roi_pool5_readonly"type: "ROIPooling"bottom: "conv5_3"bottom: "rois"top: "pool5_readonly"propagate_down: falsepropagate_down: falseroi_pooling_param {pooled_w: 7pooled_h: 7spatial_scale: 0.0625 # 1/16}
}
layer {name: "fc6_readonly"type: "InnerProduct"bottom: "pool5_readonly"top: "fc6_readonly"propagate_down: falseparam {name: "fc6_w"}param {name: "fc6_b"}inner_product_param {num_output: 4096}
}
layer {name: "relu6_readonly"type: "ReLU"bottom: "fc6_readonly"top: "fc6_readonly"propagate_down: false
}
layer {name: "drop6_readonly"type: "Dropout"bottom: "fc6_readonly"top: "fc6_readonly"propagate_down: falsedropout_param {dropout_ratio: 0.5}
}
layer {name: "fc7_readonly"type: "InnerProduct"bottom: "fc6_readonly"top: "fc7_readonly"propagate_down: falseparam {name: "fc7_w"}param {name: "fc7_b"}inner_product_param {num_output: 4096}
}
layer {name: "relu7_readonly"type: "ReLU"bottom: "fc7_readonly"top: "fc7_readonly"propagate_down: false
}
layer {name: "drop7_readonly"type: "Dropout"bottom: "fc7_readonly"top: "fc7_readonly"propagate_down: falsedropout_param {dropout_ratio: 0.5}
}
layer {name: "cls_score_readonly"type: "InnerProduct"bottom: "fc7_readonly"top: "cls_score_readonly"propagate_down: falseparam {name: "cls_score_w"}param {name: "cls_score_b"}inner_product_param {num_output: 21weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}}
}
layer {name: "bbox_pred_readonly"type: "InnerProduct"bottom: "fc7_readonly"top: "bbox_pred_readonly"propagate_down: falseparam {name: "bbox_pred_w"}param {name: "bbox_pred_b"}inner_product_param {num_output: 84weight_filler {type: "gaussian"std: 0.001}bias_filler {type: "constant"value: 0}}
}
layer {name: "cls_prob_readonly"type: "Softmax"bottom: "cls_score_readonly"top: "cls_prob_readonly"propagate_down: false
}
layer {name: "hard_roi_mining"type: "Python"bottom: "cls_prob_readonly"bottom: "bbox_pred_readonly"bottom: "rois"bottom: "labels"bottom: "bbox_targets"bottom: "bbox_inside_weights"bottom: "bbox_outside_weights"top: "rois_hard"top: "labels_hard"top: "bbox_targets_hard"top: "bbox_inside_weights_hard"top: "bbox_outside_weights_hard"propagate_down: falsepropagate_down: falsepropagate_down: falsepropagate_down: falsepropagate_down: falsepropagate_down: falsepropagate_down: falsepython_param {module: "roi_data_layer.layer"layer: "OHEMDataLayer"param_str: "'num_classes': 21"}
}
########## End ###########
## Readonly RoI Network ##
##########################
layer {name: "roi_pool5"type: "ROIPooling"bottom: "conv5_3"bottom: "rois_hard"top: "pool5"propagate_down: truepropagate_down: falseroi_pooling_param {pooled_w: 7pooled_h: 7spatial_scale: 0.0625 # 1/16}
}
layer {name: "fc6"type: "InnerProduct"bottom: "pool5"top: "fc6"param {name: "fc6_w"lr_mult: 1decay_mult: 1}param {name: "fc6_b"lr_mult: 2decay_mult: 0}inner_product_param {num_output: 4096}
}
layer {name: "relu6"type: "ReLU"bottom: "fc6"top: "fc6"
}
layer {name: "drop6"type: "Dropout"bottom: "fc6"top: "fc6"dropout_param {dropout_ratio: 0.5}
}
layer {name: "fc7"type: "InnerProduct"bottom: "fc6"top: "fc7"param {name: "fc7_w"lr_mult: 1decay_mult: 1}param {name: "fc7_b"lr_mult: 2decay_mult: 0}inner_product_param {num_output: 4096}
}
layer {name: "relu7"type: "ReLU"bottom: "fc7"top: "fc7"
}
layer {name: "drop7"type: "Dropout"bottom: "fc7"top: "fc7"dropout_param {dropout_ratio: 0.5}
}
layer {name: "cls_score"type: "InnerProduct"bottom: "fc7"top: "cls_score"param {name: "cls_score_w"lr_mult: 1decay_mult: 1}param {name: "cls_score_b"lr_mult: 2decay_mult: 0}inner_product_param {num_output: 21weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}}
}
layer {name: "bbox_pred"type: "InnerProduct"bottom: "fc7"top: "bbox_pred"param {name: "bbox_pred_w"lr_mult: 1decay_mult: 1}param {name: "bbox_pred_b"lr_mult: 2decay_mult: 0}inner_product_param {num_output: 84weight_filler {type: "gaussian"std: 0.001}bias_filler {type: "constant"value: 0}}
}
layer {name: "loss_cls"type: "SoftmaxWithLoss"bottom: "cls_score"bottom: "labels_hard"top: "loss_cls"propagate_down: truepropagate_down: falseloss_weight: 1
}
layer {name: "loss_bbox"type: "SmoothL1Loss"bottom: "bbox_pred"bottom: "bbox_targets_hard"bottom: "bbox_inside_weights_hard"bottom: "bbox_outside_weights_hard"top: "loss_bbox"propagate_down: truepropagate_down: falsepropagate_down: falsepropagate_down: falseloss_weight: 1

可视化结果如下:

  

    特征从relu5_3出来后分成2两路,一路是roi_pooling5_readonly,另外一路是roi_pooling5. 其中roi_pooling5_readonly 只进行前向传播和计算损失,并不反向传播梯度(在prototxt 中 propagate_down: false 也可以看得出来). 比较两路的prototxt, 可以看到fc6,fc7,cls_score, bbox_pred 中指定了相同的参数名,如:

                        param {name: "fc6_w"}

caffe中如果某两层参数名字是一样的,说明这两层共享权值. 

roi_pooling5_readonly 这一层的主要功能就是计算样本的损失(包含分类损失和边框回归损失),然后把损失降序排列(损失越大说明是困难样本), 然后采样或者直接取排列里前batch个样本送入到roi_pooling5层计算困难样本的损失并反传梯度.

2.OHEM 前向传播

    def forward(self, bottom, top):"""Compute loss, select RoIs using OHEM. Use RoIs to get blobs and copy them into this layer's top blob vector."""cls_prob = bottom[0].data # 分类预测值bbox_pred = bottom[1].data # 回归预测值rois = bottom[2].datalabels = bottom[3].dataif cfg.TRAIN.BBOX_REG:bbox_target = bottom[4].databbox_inside_weights = bottom[5].databbox_outside_weights = bottom[6].dataelse:bbox_target = Nonebbox_inside_weights = Nonebbox_outside_weights = Noneflt_min = np.finfo(float).eps# classification loss 分类损失 softmaxwithlossloss = [ -1 * np.log(max(x, flt_min)) \for x in [cls_prob[i,label] for i, label in enumerate(labels)]]if cfg.TRAIN.BBOX_REG: # 回归损失, smoothL1 loss# bounding-box regression loss# d := w * (b0 - b1)# smoothL1(x) = 0.5 * x^2    if |x| < 1#               |x| - 0.5    otherwisedef smoothL1(x):if abs(x) < 1:return 0.5 * x * xelse:return abs(x) - 0.5bbox_loss = np.zeros(labels.shape[0])for i in np.where(labels > 0 )[0]:indices = np.where(bbox_inside_weights[i,:] != 0)[0]bbox_loss[i] = sum(bbox_outside_weights[i,indices] * [smoothL1(x) \for x in bbox_inside_weights[i,indices] * (bbox_pred[i,indices] - bbox_target[i,indices])])loss += bbox_loss # 选择OHEM的时候的损失是分类和回归损失的和# 获取一个blob的困难样本blobs = get_ohem_minibatch(loss, rois, labels, bbox_target, \bbox_inside_weights, bbox_outside_weights)# 前向传播困难样本.for blob_name, blob in blobs.iteritems():top_ind = self._name_to_top_map[blob_name]# Reshape net's input blobstop[top_ind].reshape(*(blob.shape))# Copy data into net's input blobstop[top_ind].data[...] = blob.astype(np.float32, copy=False)
def get_ohem_minibatch(loss, rois, labels, bbox_targets=None,bbox_inside_weights=None, bbox_outside_weights=None):"""Given rois and their loss, construct a minibatch using OHEM."""loss = np.array(loss)if cfg.TRAIN.OHEM_USE_NMS:# 非极大值抑制去重复, 作者使用了一张图片中所有的正样本和负样本,没有使用原来fast-rcnn# 的采样策略,这样可以增加样本的复杂性# 选择策略在minibatch.py中的 _all_rois()中可以看到# Do NMS using loss for de-dup and diversitykeep_inds = []nms_thresh = cfg.TRAIN.OHEM_NMS_THRESH # OHEM_NMS_THRESH=0.7source_img_ids = [roi[0] for roi in rois]for img_id in np.unique(source_img_ids):for label in np.unique(labels):sel_indx = np.where(np.logical_and(labels == label, \source_img_ids == img_id))[0]if not len(sel_indx):continueboxes = np.concatenate((rois[sel_indx, 1:],loss[sel_indx][:,np.newaxis]), axis=1).astype(np.float32)keep_inds.extend(sel_indx[nms(boxes, nms_thresh)])# NMS 之后选择困难样本hard_keep_inds = select_hard_examples(loss[keep_inds])hard_inds = np.array(keep_inds)[hard_keep_inds]else:hard_inds = select_hard_examples(loss)blobs = {'rois_hard': rois[hard_inds, :].copy(),'labels_hard': labels[hard_inds].copy()}if bbox_targets is not None:assert cfg.TRAIN.BBOX_REGblobs['bbox_targets_hard'] = bbox_targets[hard_inds, :].copy()blobs['bbox_inside_weights_hard'] = bbox_inside_weights[hard_inds, :].copy()blobs['bbox_outside_weights_hard'] = bbox_outside_weights[hard_inds, :].copy()return blobs
def select_hard_examples(loss):"""Select hard rois."""# Sort and select top hard examples.# 对样本进行降序排列sorted_indices = np.argsort(loss)[::-1]# np.minimum(len(loss), cfg.TRAIN.BATCH_SIZE)至多选择TRAIN.BATCH_SIZE=128个困难样本.hard_keep_inds = sorted_indices[0:np.minimum(len(loss), cfg.TRAIN.BATCH_SIZE)]# (explore more ways of selecting examples in this function; e.g., sampling)return hard_keep_inds

3 .reference

1.https://github.com/abhi2610/ohem


http://chatgpt.dhexx.cn/article/awT6MBYc.shtml

相关文章

检测模型改进—OHEM与Focal-Loss算法总结

1. 概述 论文名称:Training Region-based Object Detectors with Online Hard Example Mining 代码地址:OHEM OHEM(online hard example miniing)算法的核心思想是: 根据输入样本的损失进行筛选,筛选出hard example,表示对分类和检测影响较大的样本,然后将筛选得到的这…

【训练方法】OHEM

目录 概述细节困难负样本挖掘&#xff08;HEM&#xff09;在线困难负样本挖掘&#xff08;OHEM&#xff09; 概述 目标检测任务重存在严重的正负样本不均衡问题&#xff0c;以往的解决方法是困难负样本挖掘方法&#xff08;HEM&#xff09;&#xff0c;但是它不太适合端到端的…

OHEM(Online Hard Example Mining)在线难例挖掘(在线困难样例挖掘) Pytorch实现 HNM (目标检测)

Hard Negatie Mining与Online Hard Example Mining&#xff08;OHEM&#xff09;都属于难例挖掘&#xff0c;它是解决目标检测老大难问题的常用办法&#xff0c;运用于R-CNN&#xff0c;fast R-CNN&#xff0c;faster rcnn等two-stage模型与SSD等&#xff08;有anchor的&#x…

OHEM介绍

目标检测之OHEM介绍 论文地址&#xff1a;https://arxiv.org/pdf/1604.03540.pdf 在two-stage检测算法中&#xff0c;RPN阶段会生成大量的检测框&#xff0c;由于很多时候一张图片可能只会有少量几个目标&#xff0c;也就是说绝大部分框是没有目标的&#xff0c;为了减少计算就…

OHEM

Training Region-based Object Detectors with Online Hard Example Mining 00 Astract 摘要主要讲了四点&#xff1a; (1) 训练过程需要进行参数的空间搜索 (2) 简单样本与难分辨样本之间的类别不平衡是亟需解决的问题 (3) 自动地选择难分辨样本来进行训练不仅效率高而且性能好…

样本均衡之OHEM

论文地址&#xff1a;Training Region-based Object Detectors with Online Hard Example Mining OHEM 主要解决神经网络中困难负样本的问题。 困难负样本是神经网络难以区分的负样本。我们希望模型在训练的时候&#xff0c;负样本的confidence接近0&#xff0c;但是如果不加…

OHEM 详解

OHEM 详解 一、Hard Negative Mining Method&#xff08;一&#xff09;Hard Negative Mining Method 思想&#xff08;二&#xff09;Hard Negative Mining Method 使用&#xff08;三&#xff09;Hard Negative Mining Method 缺点 二、OHEM&#xff08;一&#xff09;训练方…

OHEM(Online Hard Example Mining )算法

OHEM算法提出于论文 Training Region-based Object Detectors with Online Hard Example Mining&#xff0c;链接&#xff1a;https://arxiv.org/abs/1604.03540。在hard example&#xff08;损失较大的样本&#xff09;反向传播时&#xff0c;可以减少运算量。 OHEM主要思想是…

虚拟机VMware官网下载教程,中文详细步骤(图文)

目录 一、找到官网 二、注册&#xff0c;登录&#xff08;之前有账号的直接登录&#xff09; 三、下载 四、VMware安装 五、Ubuntu下载&#xff0c;安装 一、找到官网 中文官网&#xff1a;VMware 中国 - 交付面向企业的数字化基础 | CNhttps://www.vmware.com/cn.html …

虚拟机VMware Workstation安装使用教程

1.官网下载&#xff0c;百度一个序列号进行注册。 2.打开VM&#xff0c;创建虚拟机 2.初次使用的话&#xff0c;选择典型就可以 3.如果有已有的iso文件&#xff0c;可以选择“安装程序光盘印象文件”&#xff0c;但是这是简易安装&#xff0c;可能后期会出现bug&#xff0c;不…

VMware安装虚拟机详细教程

VMware安装虚拟机目录 一、VMware添加虚拟机二、虚拟机启动及配置三、IP地址、子网掩码、网关和域名 一、VMware添加虚拟机 在我们安装完VWware&#xff0c;下载好安装的操作系统的镜像文件后&#xff08;这里我们是&#xff08;centos7版本&#xff09;&#xff0c;可以开始安…

VMware下载与安装教程(详细)

虚拟机VMware下载与安装教程 1. VMware最新版下载Vmware历史版本下载 2. VMware的安装 1. VMware最新版下载 Vmware官网 官网界面如下 点击产品,找到 Workstation Pro 之后会跳转到如下界面&#xff0c;点击下载试用版 根据自己的操作系统是Linux还是Windows选择对应的版…

虚拟机 VMware 16安装教程

虚拟机 VMware 16下载 见评论区 软件简介 VMware&#xff08;虚拟机&#xff09;是指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统&#xff0c;它能在Windows系统上虚拟出多个计算机&#xff0c;每个虚拟计算机可以独立运行&#xff0c…

虚拟化技术之 VMware Workstation教程(一)

目录 第一章 虚拟化技术 1.1虚拟化技术简介 1.2主流的虚拟化厂商及产品 第二章 虚拟机的安装 2.1安装VMwareWorkstation 12 2.2物理机所需硬件 2.3在虚拟机中安装Windows 7操作系统 第三章 虚拟机的网络设置 3.1网络接入模式 3.2设置虚拟机上网 3.3设置两台虚拟机之…

VMware Workstation Pro详细安装教程

一&#xff0c;VMware Workstation Pro介绍 VMware Workstation&#xff08;中文名“威睿工作站”&#xff09;是一款功能强大的桌面虚拟计算机软件&#xff0c;提供用户可在单一的桌面上同时运行不同的操作系统&#xff0c;和进行开发、测试 、部署新的应用程序的最佳解决方案…

VMware虚拟机下载与安装教程(详细的很,一看就懂)

CSDN话题挑战赛第2期 参赛话题&#xff1a;学习笔记 &#x1f536; 个人主页&#xff1a; 神仙阿姨的博客 &#x1f534; 分享网站&#xff1a; 《Python自学网》&#x1f449;&#x1f449;适合新手入门到精通 | python全栈体系课程 | WEB开发 | 爬虫 | 自动化运维 …

VMware安装虚拟机操作步骤[史上最详细]

一、下载并安装VMware虚拟化软件 百度搜索关键词 安装步骤&#xff1a;傻瓜式安装&#xff08;鼠标点点点&#xff0c;这里不做截屏演示&#xff09; 二、安装秘钥 1、百度搜索关键词VMware秘钥&#xff0c;秘钥不一定都有效&#xff0c;一个一个试总有一个可以的 ​​​​​…

虚拟机VMware的安装及使用

一 虚拟机VMware的安装 1.准备工作 1)需要软件VMware安装包 VMware下载地址&#xff1a; http://www.uzzf.com/soft/51188.html 2)需要一个系统镜像 windows系统&#xff1a; http://www.xitongcheng.com/win7/ ubuntu系统镜像下载&#xff1a; http://www.ubuntu.org.cn/do…

VMware虚拟机安装教程

虚拟机(Virtual Machine)是通过软件模拟的完整计算机系统。在实体计算机中能够完成的工作在虚拟机中都能够实现。在计算机中创建虚拟机时&#xff0c;需要将实体机的部分硬盘和内存容量作为虚拟机的硬盘和内存容量。每个虚拟机都有独立的CMOS、硬盘和操作系统&#xff0c;可以像…

vmware虚拟机的基础使用

1.vmware VMWare虚拟机软件来测试软件、测试安装操作系统&#xff08;如linux&#xff09;、测试病毒木马等。 VMWare是真正“同时”运行&#xff0c;多个操作系统在主系统的平台上&#xff0c;就象标准Windows应用程序那样切换。而且每个操作系统你都可以进行虚拟的分区、配置…