使用HDF5的Caffe两类多标签分类 [英] Caffe two class multi label classification with hdf5

查看:88
本文介绍了使用HDF5的Caffe两类多标签分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.txt文件中的结构如下:

I have the following structure in a .txt file:

/path/to/image x y
/path/to/image x y

其中x和y是整数.

我现在要做的是:创建一个在Caffe('train.prototxt')中使用的hdf5文件

What I want to do now is: Create a hdf5 file to use in Caffe ('train.prototxt')

我的Python代码如下:

My Python code looks like this:

import h5py, os
import caffe
import numpy as np

SIZE = 256
with open( 'train.txt', 'r' ) as T :
    lines = T.readlines()


count_files = 0
split_after = 1000
count = -1

# If you do not have enough memory split data into
# multiple batches and generate multiple separate h5 files
X = np.zeros( (split_after, 3, SIZE, SIZE), dtype='f4' )
y1 = np.zeros( (split_after, 1), dtype='f4' )
y2 = np.zeros( (split_after, 1), dtype='f4' )

for i,l in enumerate(lines):
    count += 1
    sp = l.split(' ')
    img = caffe.io.load_image( sp[0] )
    img = caffe.io.resize( img, (3, SIZE, SIZE) )

    X[count] = img
    y1[count] = float(sp[1])
    y2[count] = float(sp[2])

    if (count+1) == split_after:
        with h5py.File('train_' + str(count_files) +  '.h5','w') as H:
            H.create_dataset( 'X', data=X ) # note the name X given to the dataset!
            H.create_dataset( 'y1', data=y1 )
            H.create_dataset( 'y2', data=y2 )

            X = np.zeros( (split_after, 3, SIZE, SIZE), dtype='f4' )
            y1 = np.zeros( (split_after, 1), dtype='f4' )
            y2 = np.zeros( (split_after, 1), dtype='f4' )
        with open('train_h5_list.txt','a') as L:
            L.write( 'train_' + str(count_files) + '.h5') # list all h5 files you are going to use
        count_files += 1
        count = 0

实际上,我想估算角度.这意味着我有两类,一类用于垂直角度,一类用于水平角度.第一类的范围是0-10度,第二类的范围是10-20,依此类推(对于水平和垂直角度).

In fact I want to estimate angles. That means I have two classes one for vertical angles one for horizontal angles. The first class ranges from 0-10 degrees the second from 10-20 and so on (for both horizontal and vertical angles).

.prototxt的外观如何?这是我的最后一层

How would the .prototxt look like? Here are my last layers

layer {
  name: "fc8"
  type: "InnerProduct"
  bottom: "fc7"
  top: "fc8"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 36
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc8"
  bottom: "y"
  top: "loss"
}

推荐答案

您还需要修改输入层:现在您有三个top:

You also need to modify the input layer: now you have three tops:

layer {
  type: "HDF5Data"
  name: "data"
  top: "X"
  top: "y1"
  top: "y2"
  # ... params and phase
}

现在,您的fc7top充当数据的高级描述符",您希望从中预测y1y2.因此,在fc7层之后,您应该具有:

Now, the top of your fc7 serves as a "high level descriptor" of your data, from which you wish to predict y1 and y2. Thus, after layer fc7 you should have:

layer {
  type: "InnerProduct"
  name: "class_y1" 
  bottom: "fc7"
  top: "class_y1"
  #... params num_output: 36 
}
layer {
  type: "SoftmaxWithLoss" # to be replaced with "Softmax" in deploy
  name: "loss_y1"
  bottom: "class_y1"
  bottom: "y1"
  top: "loss_y1"
  # optionally, loss_weight
}

并且:

layer {
  type: "InnerProduct"
  name: "class_y2" 
  bottom: "fc7"
  top: "class_y2"
  #... params num_output: 36 
}
layer {
  type: "SoftmaxWithLoss" # to be replaced with "Softmax" in deploy
  name: "loss_y2"
  bottom: "class_y2"
  bottom: "y2"
  top: "loss_y2"
  # optionally, loss_weight
}

这篇关于使用HDF5的Caffe两类多标签分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆