keras faster R-CNN训练代码流程解读

最近学习faster RCNN 框架,把自己的理解记录下来,前提是已经知道大致原理和流程。

知乎有类似博客:https://zhuanlan.zhihu.com/p/31649026,写的挺好的。

Step1、生成基础模型的共享层

shared_layers = nn.nn_base(img_input, trainable=True)

Step2、在share_layer基础上,生成rpnclassify基础模型

rpn = nn.rpn(shared_layers, num_anchors)

classifier = nn.classifier(shared_layers, roi_input, cfg.num_rois,...)

Step3Rpnclassify模型编译,包括定义Loss function.

model_rpn.compile(loss=[rpn_loss_cls, rpn_loss_regr])

model_classifier.compile(loss=[class_loss_cls,class_loss_regr])

两个模型都包括分类与回归损失函数

Step4、读取数据RPN网络训练数据

X, Y, img_data = next(data_gen_train)

X,Y表示的是一张输入图片(6000*1240)生成的RPN网络,大概2万多张

#Y--是图像计算的rpn网络包括Y[0],Y[1]

#Y[0]--(1,width,height,num_anchor*2),width,height分别表示feature map的width,height;(1,width,height,0:num_anchor)表示y_is_box_valid,即anchor是否有效;(1,width,height,num_anchor:)表示(1,width,height,0:num_anchor),即窗口内是否有目标/重叠。

#X--(1,width,height,num_anchor*9),表示每一个窗口的梯度(平移和缩放因子)

Step5.迭代RPN网络,更新网络权值

loss_rpn = model_rpn.train_on_batch(X, Y)

step6.用此时的RPN网络预测

P_rpn = model_rpn.predict_on_batch(X)

Step7.窗口筛选

result = roi_helpers.rpn_to_roi(P_rpn[0], P_rpn[1], cfg, K.image_dim_ordering(), use_regr=True,overlap_thresh=0.7,max_boxes=300)

#将开始的2000多个窗,通过筛选300个,result存储的是框的featuremap上的坐标(x1,y1,x2,y2)

#得到了region proposals,接下来另一个重要的思想就是ROI,可将不同shape的特征图转化为固定shape,送到全连接层进行最终的预测。

#rpn_to_roi接收的是每张图片的预测输出

step8.classify网络准备数据

X2, Y1, Y2, IouS = roi_helpers.calc_iou(result, img_data, cfg, class_mapping)

#note: calc_iou converts from (x1,y1,x2,y2) to (x,y,w,h) format

#通过calc_iou()找出剩下的不多的region对应ground #truth里重合度最高的bbox,从而获得model_classifier的数据和标签。

#X2保留所有的背景和match bbox的框; Y1 是类别one-hot转码;

#Y2是对应类别的标签及回归要学习的坐标位置; IouS是debug用的。

step9.classify网络训练

loss_class = model_classifier.train_on_batch([X, X2[:, sel_samples, :]],[Y1[:, sel_samples, :], Y2[:, sel_samples, :]])

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值