caffe多标签训练

最近刚接触caffe弄了一个caffe多标签遇到各种蛋疼的问题跟大家分享分享。

 

一  准备数据这里用的验证码0-9+26个字母字母生成4位数的验证码

二  修改caffe源码涉及到修改的文件有 

      caffe.proto ,

      convert_imageset.cpp,

      data_layer.cpp,

      io.cpp,

      data_layer.hpp,

      io.hpp 

      具体修改就不介绍了去下面地址下载修改后的文件然后替换掉原有caffe中的

      文件下载地址:https://pan.baidu.com/s/1eSP1RUi  

      这里我们理解一下,caffe原本不支持多标签分类任务,这里的主要修改是为了是的caffe支持多标签分类,我们知道caffe的label原版指定的是整数型且只有1个,看caffe.proto里面写的:Datum就是我们的数据层,那么数据层的label是int32,这就限制了数据label的输入必须是一个整数,那么修改他的起点就是从proto里面开始,加一个labels,数组类型

      

修改完了然后接着修改caffe使用Datum部分代码,实现对labels的支持

最后修改convert_imageset.cpp,让他实现对例如如下:

imgs/abc.jpg 1 2 3 4 5

这种类型的多标签做支持

最终完成这次修改,再编译一遍就好了

三   制作数据标签

       图片路径 + 对应的标签   samples/MYL1.bmp 22 34 21 1

四   写个多分类网络

     

     结构如下:

     

  1. name:"LeNet"
  2. layer{
  3.  name:"mnist"
  4.  type:"Data"
  5.  top:"data"
  6.  top:"label"
  7.  include{
  8.    phase: TRAIN
  9.  }
  10.  transform_param{
  11.    scale:0.003921568627451
  12.  }
  13.  data_param{
  14.    source:"train_lmdb"
  15.    batch_size:64
  16.    backend: LMDB
  17.  }
  18. }
  19. layer{
  20.  name:"mnist"
  21.  type:"Data"
  22.  top:"data"
  23.  top:"label"
  24.  include{
  25.    phase: TEST
  26.  }
  27.  transform_param{
  28.    scale:0.003921568627451
  29.  }
  30.  data_param{
  31.    source:"val_lmdb"
  32.    batch_size:64
  33.    backend: LMDB
  34.  }
  35. }
  36. layer{
  37.  name:"conv1"
  38.  type:"Convolution"
  39.  bottom:"data"
  40.  top:"conv1"
  41.  param{
  42.    lr_mult:1
  43.  }
  44.  param{
  45.    lr_mult:2
  46.  }
  47.  convolution_param{
  48.    num_output:128
  49.    kernel_size:7
  50.    stride:1
  51.    weight_filler{
  52.      type:"xavier"
  53.    }
  54.    bias_filler{
  55.      type:"constant"
  56.    }
  57.  }
  58. }
  59. layer{
  60.  name:"pool1"
  61.  type:"Pooling"
  62.  bottom:"conv1"
  63.  top:"pool1"
  64.  pooling_param{
  65.    pool: MAX
  66.    kernel_size:2
  67.    stride:2
  68.  }
  69. }
  70. layer{
  71.  name:"conv2"
  72.  type:"Convolution"
  73.  bottom:"pool1"
  74.  top:"conv2"
  75.  param{
  76.    lr_mult:1
  77.  }
  78.  param{
  79.    lr_mult:2
  80.  }
  81.  convolution_param{
  82.    num_output:128
  83.    kernel_size:5
  84.    stride:1
  85.    weight_filler{
  86.      type:"xavier"
  87.    }
  88.    bias_filler{
  89.      type:"constant"
  90.    }
  91.  }
  92. }
  93. layer{
  94.  name:"pool2"
  95.  type:"Pooling"
  96.  bottom:"conv2"
  97.  top:"pool2"
  98.  pooling_param{
  99.    pool: MAX
  100.    kernel_size:2
  101.    stride:1
  102.  }
  103. }
  104. layer{
  105.  name:"Relu"
  106.  type:"ReLU"
  107.  bottom:"pool2"
  108.  top:"pool2"
  109. }
  110. layer{
  111.  name:"conv3"
  112.  type:"Convolution"
  113.  bottom:"pool2"
  114.  top:"conv3"
  115.  param{
  116.    lr_mult:1
  117.  }
  118.  param{
  119.    lr_mult:2
  120.  }
  121.  convolution_param{
  122.    num_output:128
  123.    kernel_size:3
  124.    stride:1
  125.    weight_filler{
  126.      type:"xavier"
  127.    }
  128.    bias_filler{
  129.      type:"constant"
  130.    }
  131.  }
  132. }
  133. layer{
  134.  name:"Relu2"
  135.  type:"ReLU"
  136.  bottom:"conv3"
  137.  top:"conv3"
  138. }
  139. layer{
  140.  name:"conv4"
  141.  type:"Convolution"
  142.  bottom:"conv3"
  143.  top:"conv4"
  144.  param{
  145.    lr_mult:1
  146.  }
  147.  param{
  148.    lr_mult:2
  149.  }
  150.  convolution_param{
  151.    num_output:128
  152.    kernel_size:3
  153.    stride:1
  154.    weight_filler{
  155.      type:"xavier"
  156.    }
  157.    bias_filler{
  158.      type:"constant"
  159.    }
  160.  }
  161. }
  162. layer{
  163.  name:"Relu3"
  164.  type:"ReLU"
  165.  bottom:"conv4"
  166.  top:"conv4"
  167. }
  168. layer{
  169.  name:"conv5"
  170.  type:"Convolution"
  171.  bottom:"conv4"
  172.  top:"conv5"
  173.  param{
  174.    lr_mult:1
  175.  }
  176.  param{
  177.    lr_mult:2
  178.  }
  179.  convolution_param{
  180.    num_output:128
  181.    kernel_size:3
  182.    stride:1
  183.    weight_filler{
  184.      type:"xavier"
  185.    }
  186.    bias_filler{
  187.      type:"constant"
  188.    }
  189.  }
  190. }
  191. layer{
  192.  name:"fc81"
  193.  type:"InnerProduct"
  194.  bottom:"conv5"
  195.  top:"fc81"
  196.  param{
  197.    lr_mult:1
  198.  }
  199.  param{
  200.    lr_mult:2
  201.  }
  202.  inner_product_param{
  203.    num_output:36
  204.    weight_filler{
  205.      type:"xavier"
  206.    }
  207.    bias_filler{
  208.      type:"constant"
  209.    }
  210.  }
  211. }
  212. layer{
  213.  name:"fc82"
  214.  type:"InnerProduct"
  215.  bottom:"conv5"
  216.  top:"fc82"
  217.  param{
  218.    lr_mult:1
  219.  }
  220.  param{
  221.    lr_mult:2
  222.  }
  223.  inner_product_param{
  224.    num_output:36
  225.    weight_filler{
  226.      type:"xavier"
  227.    }
  228.    bias_filler{
  229.      type:"constant"
  230.    }
  231.  }
  232. }
  233. layer{
  234.  name:"fc83"
  235.  type:"InnerProduct"
  236.  bottom:"conv5"
  237.  top:"fc83"
  238.  param{
  239.    lr_mult:1
  240.  }
  241.  param{
  242.    lr_mult:2
  243.  }
  244.  inner_product_param{
  245.    num_output:36
  246.    weight_filler{
  247.      type:"xavier"
  248.    }
  249.    bias_filler{
  250.      type:"constant"
  251.    }
  252.  }
  253. }
  254. layer{
  255.  name:"fc84"
  256.  type:"InnerProduct"
  257.  bottom:"conv5"
  258.  top:"fc84"
  259.  param{
  260.    lr_mult:1
  261.  }
  262.  param{
  263.    lr_mult:2
  264.  }
  265.  inner_product_param{
  266.    num_output:36
  267.    weight_filler{
  268.      type:"xavier"
  269.    }
  270.    bias_filler{
  271.      type:"constant"
  272.    }
  273.  }
  274. }
  275. layer{
  276.  name:"slice2"
  277.  type:"Slice"
  278.  bottom:"label"
  279.  top:"l1"
  280.  top:"l2"
  281.  top:"l3"
  282.  top:"l4"
  283.  slice_param{
  284.    axis:1
  285.    slice_point:1
  286.    slice_point:2
  287.    slice_point:3
  288.  }
  289. }
  290. layer{
  291.  name:"accuracy1"
  292.  type:"Accuracy"
  293.  bottom:"fc81"
  294.  bottom:"l1"
  295.  top:"accuracy1"
  296.  include{
  297.    phase: TEST
  298.  }
  299. }
  300. layer{
  301.  name:"accuracy2"
  302.  type:"Accuracy"
  303.  bottom:"fc82"
  304.  bottom:"l2"
  305.  top:"accuracy2"
  306.  include{
  307.    phase: TEST
  308.  }
  309. }
  310. layer{
  311.  name:"accuracy3"
  312.  type:"Accuracy"
  313.  bottom:"fc83"
  314.  bottom:"l3"
  315.  top:"accuracy3"
  316.  include{
  317.    phase: TEST
  318.  }
  319. }
  320. layer{
  321.  name:"accuracy4"
  322.  type:"Accuracy"
  323.  bottom:"fc84"
  324.  bottom:"l4"
  325.  top:"accuracy4"
  326.  include{
  327.    phase: TEST
  328.  }
  329. }
  330. layer{
  331.  name:"loss1"
  332.  type:"SoftmaxWithLoss"
  333.  bottom:"fc81"
  334.  bottom:"l1"
  335.  top:"loss1"
  336.  loss_weight:0.1
  337. }
  338. layer{
  339.  name:"loss2"
  340.  type:"SoftmaxWithLoss"
  341.  bottom:"fc82"
  342.  bottom:"l2"
  343.  top:"loss2"
  344.  loss_weight:0.1
  345. }
  346. layer{
  347.  name:"loss3"
  348.  type:"SoftmaxWithLoss"
  349.  bottom:"fc83"
  350.  bottom:"l3"
  351.  top:"loss3"
  352.  loss_weight:0.1
  353. }
  354. layer{
  355.  name:"loss4"
  356.  type:"SoftmaxWithLoss"
  357.  bottom:"fc84"
  358.  bottom:"l4"
  359.  top:"loss4"
  360.  loss_weight:0.1
  361. }

      然后就可以开始训练了

      结果如下:

      

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以回答这个问题。标签平滑是一种用于降低模型过度自信的技术。它通过将标签分布从 0 或 1 调整到介于 0 和 1 之间的值来实现。这样可以使模型更加谦虚,从而提高泛化能力。 在 Python 中,可以使用 NumPy 库来实现标签平滑。以下是一个简单的示例代码: ```python import numpy as np def smooth_labels(labels, factor=0.1): """ 标签平滑函数 :param labels: 原始标签 :param factor: 平滑因子 :return: 平滑后的标签 """ smooth_labels = (1 - factor) * labels + factor / labels.shape[1] return smooth_labels ``` 在 Caffe 中,可以使用 SoftmaxWithLoss 层来实现标签平滑。以下是一个示例网络结构: ```protobuf layer { name: "data" type: "Data" top: "data" include { phase: TRAIN } transform_param { mirror: true crop_size: 227 mean_value: 104 mean_value: 117 mean_value: 123 } data_param { source: "train_lmdb" batch_size: 64 backend: LMDB } } layer { name: "label_smoothing" type: "Python" bottom: "label" top: "label_smooth" python_param { module: "python_layers" layer: "LabelSmoothingLayer" param_str: "'factor': 0.1" } } layer { name: "fc1" type: "InnerProduct" bottom: "data" bottom: "label_smooth" top: "fc1" inner_product_param { num_output: 1000 weight_filler { type: "xavier" } bias_filler { type: "constant" value: 0 } } } layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc1" bottom: "label_smooth" top: "loss" } ``` 在这个网络中,我们使用 Python 层来实现标签平滑。LabelSmoothingLayer 的实现类似于上面的 Python 示例。然后,我们将平滑后的标签传递给 InnerProduct 层和 SoftmaxWithLoss 层。这样,我们就可以使用标签平滑来训练我们的模型了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值