caffe多任务、多标签

解决的目标问题:多分类问题,比如车辆的外形和颜色,苹果的大小和颜色;多任务:车牌角点的定位和车牌的颜色。定位在技术上属于回归,车牌颜色判断则属于分类。

技术点

caffe默认是单输入任务单标签的,也就是一个样本,其任务只有一个,标签只有一个,比如图片是什么颜色,图片是什么物体。

# ${caffe_src_root}/tools/convert_imageset.cpp 第121行
status = ReadImageToDatum(root_folder + lines[line_id].first,
        lines[line_id].second, resize_height, resize_width, is_color,
        enc, &datum);

## 其中 ReadImageToDatum的定义如下 ${caffe_src_root}/include/caffe/util/io.hpp

bool ReadImageToDatum(const string& filename, const int label,
    const int height, const int width, const bool is_color,
    const std::string & encoding, Datum* datum);

##  ${caffe_src_root}/src/caffe/util/io.cpp 中的该函数实现,涉及到Datum的定义,需要把Datum定义修改成也要支持多标签

bool ReadImageToDatum(const string& filename, const int label,
    const int height, const int width, const bool is_color,
    const std::string & encoding, Datum* datum) {
  cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color);
  if (cv_img.data) {
    if (encoding.size()) {
      if ( (cv_img.channels() == 3) == is_color && !height && !width &&
          matchExt(filename, encoding) )
        return ReadFileToDatum(filename, label, datum);
      std::vector<uchar> buf;
      cv::imencode("."+encoding, cv_img, buf);
      datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]),
                      buf.size()));
      datum->set_label(label);
      datum->set_encoded(true);
      return true;
    }
    CVMatToDatum(cv_img, datum);
    datum->set_label(label);
    return true;
  } else {
    return false;
  }
}

为了支持多任务,多标签,首先要解决输入问题。比如一个样本 定义如下:

vehicle/1.jpg  0 1

 

修改源码支持多标签

其中第一个属性是车辆外形,0代表sedian,第二个属性是车身颜色,1代表白色。假如图片是60x60的RGB图像,    如果是单任务多属性输入,一个简单的更改方案是把ReadImageToDatum函数修改成如下定义,并修改相关的实现函数和convert_imageset.cpp

bool ReadImageToDatum(const string& filename, const vector<int> & labels,
    const int height, const int width, const bool is_color,
    const std::string & encoding, Datum* datum);

faster rcnn采用自定义的python输入层作用训练输入,输入有多个labels,检测目标的roi,其中bbox_targets, bbox_inside_weights, bbox_outside_weights是作为SmoothL1Loss损失函数的输入。自定义python输入层的源码参考 py-faster-rcnn/lib/roi_data_layer

name: "VGG_ILSVRC_16_layers"
layer {
  name: 'data'
  type: 'Python'
  top: 'data'
  top: 'rois'
  top: 'labels'
  top: 'bbox_targets'
  top: 'bbox_inside_weights'
  top: 'bbox_outside_weights'
  python_param {
    module: 'roi_data_layer.layer'
    layer: 'RoIDataLayer'
    param_str: "'num_classes': 21"
  }
}

 

从https://github.com/HolidayXue/CodeSnap/blob/master/convert_multilabel.cpp源码修改,保存到${caffe_root}/tools/convert_multi_label_imageset.cpp,重新编译caffe工程,在${caffe_root}目录下运行该工具,

.build_release/tools/convert_multi_label_imageset.bin -resize_width=256 -resize_height=256  ~/my\ workspace/bounding-box-tool/mlds/train.list /train-data/vehicle-type-color-dataset/

 

 

多数据源输入支持多标签

假设对于HxW的RGB图像,转换成caffe的blob定义上1x3xHxW,对于一个任务的有n个标签,则其blob定义是1xnx1x1,每个任务对应一个blob,???那么可以在在第二维度对两个blob进行拼接???

拼接之后再从第二维度对blob进行切分操作,切分出多个blob,作为每个属性训练任务的输入

 

拼接之后进行常规的卷积操作,只是在最后的每个任务的损失函数之前的fc层再切分,如下图

 训练

参考faster-rcnn的模型,可以看到损失函数是相互独立的,但多了一个weight参数,猜测是caffe在训练时,按下面的公式计算总的损失

Lt = w1*L1 + w2 * L2

faster-rcnn中经过一系列卷积层后,连接了一个ROIPooling层,再接上FC6、FC7层,从最后一个FC7层一分为2,分别接一个cls_score的FC层和名为loss_cls的SoftMaxWithLoss,接bbox_pred的FC层和名为loss_bbox的SmoothL1Loss的回归层

参考:

https://arxiv.org/abs/1604.02878v1

https://kpzhang93.github.io/MTCNN_face_detection_alignment/index.html?from=timeline&isappinstalled=1

https://kpzhang93.github.io/MTCNN_face_detection_alignment/paper/spl.pdf

https://github.com/happynear/MTCNN_face_detection_alignment

https://github.com/naritapandhe/Gender-Age-Classification-CNN

https://github.com/cunjian/multitask_CNN

https://zhuanlan.zhihu.com/p/22190532

https://github.com/rbgirshick/py-faster-rcnn/blob/master/models/pascal_voc/VGG16/fast_rcnn/train.prototxt

${caffe_source_root}/examples/pascal-multilabel-with-datalayer.ipynb

http://www.cnblogs.com/yymn/articles/7741741.html

https://yq.aliyun.com/ziliao/572047

https://blog.csdn.net/u013010889/article/details/53098346

caffe网络在线可视化工具: http://ethereon.github.io/netscope/#/editor

 

转载于:https://my.oschina.net/yishanhu/blog/3015920

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值