Caffe-多类别和多标签图像分类

时间:2018-10-21 05:44:09

标签: deep-learning caffe multilabel-classification

我正在尝试在caffe中创建一个单一的多类多标签网络配置。

比方说狗的分类: 狗是大还是小? (类) 它是什么颜色? (类) 它有衣领吗? (标签)

使用咖啡可以做这件事吗? 这样做的正确方法是什么? 构建lmdb文件的正确方法是什么?

所有有关多标签分类的出版物都来自2015年左右,此主题此后发生了变化?

谢谢。

2 个答案:

答案 0 :(得分:0)

Caffe的LMDB接口的问题在于它仅允许使用single int label per image
如果您希望每个图像有多个标签,则必须使用不同的输入层。
我建议使用"HDF5Data"层:
这样可以提供更大的灵活性来设置输入数据,您可能希望为该层设置任意数量的"top"。您可能会对每个输入图像使用多个标签,并且对网络进行训练有很多损失。

有关如何为caffe创建hdf5数据,请参见this post

答案 1 :(得分:0)

感谢Shai

只是想了解实践方法。 创建包含文本的所有标签的2个.text文件(一个用于训练,一个用于验证)后,例如:

/train/img/1.png 0 4 18
/train/img/2.png 1 7 17 33
/train/img/3.png 0 4 17

运行py脚本:

import h5py, os
import caffe
import numpy as np

SIZE = 227 # fixed size to all images
with open( 'train.txt', 'r' ) as T :
    lines = T.readlines()
# If you do not have enough memory split data into
# multiple batches and generate multiple separate h5 files
X = np.zeros( (len(lines), 3, SIZE, SIZE), dtype='f4' ) 
y = np.zeros( (len(lines),1), dtype='f4' )
for i,l in enumerate(lines):
    sp = l.split(' ')
    img = caffe.io.load_image( sp[0] )
    img = caffe.io.resize( img, (SIZE, SIZE, 3) ) # resize to fixed size
    # you may apply other input transformations here...
    # Note that the transformation should take img from size-by-size-by-3 and transpose it to 3-by-size-by-size
    # for example
    transposed_img = img.transpose((2,0,1))[::-1,:,:] # RGB->BGR
    X[i] = transposed_img
    y[i] = float(sp[1])
with h5py.File('train.h5','w') as H:
    H.create_dataset( 'X', data=X ) # note the name X given to the dataset!
    H.create_dataset( 'y', data=y ) # note the name y given to the dataset!
with open('train_h5_list.txt','w') as L:
    L.write( 'train.h5' ) # list all h5 files you are going to use

并创建train.h5和val.h5(X数据集包含图像,Y包含标签吗?)

从以下位置替换我的网络输入层:

layers { 
 name: "data" 
 type: DATA 
 top:  "data" 
 top:  "label" 
 data_param { 
   source: "/home/gal/digits/digits/jobs/20181010-191058-21ab/train_db" 
   backend: LMDB 
   batch_size: 64 
 } 
 transform_param { 
    crop_size: 227 
    mean_file: "/home/gal/digits/digits/jobs/20181010-191058-21ab/mean.binaryproto" 
    mirror: true 
  } 
  include: { phase: TRAIN } 
} 
layers { 
 name: "data" 
 type: DATA 
 top:  "data" 
 top:  "label" 
 data_param { 
   source: "/home/gal/digits/digits/jobs/20181010-191058-21ab/val_db"  
   backend: LMDB 
   batch_size: 64
 } 
 transform_param { 
    crop_size: 227 
    mean_file: "/home/gal/digits/digits/jobs/20181010-191058-21ab/mean.binaryproto" 
    mirror: true 
  } 
  include: { phase: TEST } 
} 

layer {
  type: "HDF5Data"
  top: "X" # same name as given in create_dataset!
  top: "y"
  hdf5_data_param {
    source: "train_h5_list.txt" # do not give the h5 files directly, but the list.
    batch_size: 32
  }
  include { phase:TRAIN }
}

layer {
  type: "HDF5Data"
  top: "X" # same name as given in create_dataset!
  top: "y"
  hdf5_data_param {
    source: "val_h5_list.txt" # do not give the h5 files directly, but the list.
    batch_size: 32
  }
  include { phase:TEST }
}

我猜HDF5不需要mean.binaryproto吗?

接下来,如何改变输出层以输出多个标签概率? 我想我需要交叉熵层而不是softmax吗? 这是当前的输出层:

layers {
  bottom: "prob"
  bottom: "label"
  top: "loss"
  name: "loss"
  type: SOFTMAX_LOSS
  loss_weight: 1
}
layers {
  name: "accuracy"
  type: ACCURACY
  bottom: "prob"
  bottom: "label"
  top: "accuracy"
  include: { phase: TEST }
}