R-CNN
简介
利用Selective Search方法在输入图像上提取候选框(每张图片约2k个),之后将这些候选框resize到一定尺寸后,传入预训练好的模型进行特征提取,最后利用SVM进行图像分类, 并且,单独训练一个BBox回归器用于回归Bounding Box
性能
RCNN算法在VOC-07数据集上取得了非常显著的效果,平均精度由33.7%(DPM-V5, 传统检测的SOTA算法)提升到58.5%。相比于传统检测算法,基于深度学习的检测算法在精度上取得了质的飞跃。
核心代码复现
- 用 Selective Search 提取图像ROI
安装opencv-contrib-python方法,利用其中的cv2.ximgproc.segmentation.createSelectiveSearchSegmentation 方法完成选择性搜索
def cal_pro_region(self, img_path):
'''
计算每张图片的proposal region
Returns:
np.array: proposal region的坐标, 大小为num*4, 4列分别[xmin, ymin, xmax, ymax]
'''
# 选择性搜索类
try:
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
except AttributeError:
raise Exception('需要安装opencv-contrib-python, 安装前请先删除原有的opencv-python')
ss.setBaseImage(cv2.imread(img_path))
ss.switchToSelectiveSearchFast()
rects = ss.process()
rects[:, 2] += rects[:, 0]
rects[:, 3] += rects[:, 1]
return rects
- 用预训练好的模型在提取的候选框图像上微调
def train(train_dataloader,val_dataloader,net,epochs,lr,loss_f