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_www.jiahuayulpt.com weights'
top: 'bbox_outside_weights'
python_param {
module: 'roi_data_www.baohuayule.net/ 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_www.yongshi123.cn 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_www.078886.cn 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/ www.tiaotiaoylzc.com 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://www.yongshiyule178.com ethereon.github.io/netscope/#/editor
caffe多任务、多标签的更多相关文章
- Caffe实现多标签输入,添加数据层(data layer)
因为之前遇到了sequence learning问题(CRNN),里面涉及到一张图对应多个标签.Caffe源码本身是不支持多类标签数据的输入的. 如果之前习惯调用脚本create_imagenet.s ...
- caffe读取多标签的lmdb数据
问题描述: lmdb文件支持数据+标签的形式,但是却只能写入一个标签,引入多标签的解决方法有很多,这儿详细说一下我的办法:制作多个data数据,分别加入一个标签.我的方法只适用于标签数量较少的情况,标 ...
- caffe实现多任务学习
Github: https://github.com/Haiyang21/Caffe_MultiLabel_Classification Blogs 1. 采用多label的lmdb+Slice L ...
- 多标签caffe重新编译
说明: Caffe自带的图像转LMDB接口只支持单label,对于多label的任务,可以使用HDF5的格式,也可以通过修改caffe代码来实现.本篇文章介绍怎么通过修改DataLayer来实现带Mu ...
- MachineLN博客目录
MachineLN博客目录 https://blog.csdn.net/u014365862/article/details/78422372 本文为博主原创文章,未经博主允许不得转载.有问题可以加微 ...
- Caffe-SSD相关源码说明和调试记录
1 对Blob的理解及其操作: Blob是一个四维的数组.维度从高到低分别是: (num_,channels_,height_,width_) 对于图像数据来说就是:图片个数,彩色通道个数, ...
- 下载imagenet2012数据集,以及label说明
updated@2018-12-07 15:22:08 官方下载地址:http://www.image-net.org/challenges/LSVRC/2012/nonpub-downloads , ...
- caffe 根据txt生成多标签LMDB数据
1. 前提: 已经准备好train.txt, test.txt文件, 格式如下 此处有坑, 如果是windows下生成txt, 换行符为\r\n, 需要替换成 \n才能在linux运行. 可以使用se ...
- Multi label 多标签分类问题(Pytorch,TensorFlow,Caffe)
适用场景:一个输入对应多个label,或输入类别间不互斥 调用函数: 1. Pytorch使用torch.nn.BCEloss 2. Tensorflow使用tf.losses.sigmoid_cro ...
随机推荐
- DC-DC Controllers Use Average-Current-Mode Control for Infotainment Applications-3939
DC-DC Controllers Use Average-Current-Mode Control for Infotainment Applications Abstract: Auto info ...
- .NetCore Session.Redis (转载)
首先创建ASP.NET CORE Web项目,然后按如下顺序操作. 1.添加nuget程序包: Microsoft.AspNetCore.Session; Microsoft.AspNetCore.D ...
- Hadoop Version History and Feature
Versions and Features Hadoop has seen significant interest over the past few years. This has led to ...
- Python+Selenium爬取动态加载页面(2)
注: 上一篇<Python+Selenium爬取动态加载页面(1)>讲了基本地如何获取动态页面的数据,这里再讲一个稍微复杂一点的数据获取全国水雨情网.数据的获取过程跟人手动获取过程类似,所 ...
- 【转】基于Ubuntu Server16.04 安装Odoo11
使用 非 root 用户 进行下面的测试: 本文使用 有sudo 权限的 odoo 用户进行测试()如果是 阿里云,可以先创建 odoo 用户 sudo adduser odoo 2:给root 权限 ...
- 原创zynq文章整理(MiZ702教程+例程)
MiZ702教程+例程 网盘链接: http://pan.baidu.com/s/1sj23yxv 不时会跟新版本,增加勘误之类的,请关注--
- 【php增删改查实例】第八节 - 部门管理模块(编写PHP程序)
首先,在同级目录新建一个query.php文件: 接着,去刷新页面,打开F12,NetWork,看看当前的请求能不能走到对应的php文件? 这就说明datagrid确实能够访问到query.php 只 ...
- WordPress留言本插件推荐
WordPress不借助于任何插件也可以做个留言本,那就是建个 Page, 直接使用它的评论功能即可,而且给评论加上 Ajax 功能.WYSIWYG.引用.回复.留言分页等功能也可以做的很漂亮.但对于 ...
- 【干货】YUM安装PHP 7版本后,增加phalcon框架的报错解决
目录 1.yum安装php 7.x版本,此处部署7.3版本 2.安装phalcon框架 2.1.PHP版本依赖关系 2.2.编译phalcon扩展模块 2.3.增加扩展文件 3.部署phalcon遇到 ...
- JavaScript快速入门-ECMAScript本地对象(RexExp)
一.概述 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式 ...