【问题标题】:Caffe two class multi label classification with hdf5Caffe 2类多标签分类与hdf5
【发布时间】:2017-03-07 13:11:03
【问题描述】:

我在 .txt 文件中有以下结构:

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

其中 x 和 y 是整数。

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

我的 Python 代码如下所示:

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,依此类推(水平和垂直角度)。

.prototxt 会是什么样子?这是我的最后一层

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"
}

【问题讨论】:

  • 您正在编写trainval 文件吗?你在做什么有点令人困惑......
  • 在这种情况下,我正在尝试两者兼而有之。包含应该很容易吗?但我认为对于这两种情况,最后一层是相似的?
  • 我指的是第一部分:用于创建 h5 文件的 python 代码。
  • 哦,我都在做!我将只更改文件名并执行相同的操作!

标签: python neural-network deep-learning caffe multilabel-classification


【解决方案1】:

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

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

现在,top 987654324 @用作您的数据的“高级描述符”,您希望预测y1y2。因此,在fc7 层之后,您应该有:

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
}

【讨论】:

  • 好的,谢谢!你怎么称呼这样的分类任务?是多标签分类或多级分类? span>
  • @thigi 这有关系吗?更重要的是,你能做到吗?
  • 是的,我正在努力。问题是我的 hdf5 文件太大了。
  • @thigi。没问题。你可以有很多.h5 文件,在val_h5_list.txttrain_h5_list.txt 中列出它们的所有路径,caffe 会一个接一个地读取它们。
  • 问题是,我如何分割文件?如您所见,我只有一个大的 hdf5 文件。我知道可以列出其中的许多,但问题是如何拆分它们?
猜你喜欢
  • 2017-04-22
  • 2017-02-11
  • 2019-03-25
  • 2016-02-20
  • 2017-03-15
  • 2016-01-18
  • 2017-02-15
  • 2019-04-02
  • 2017-02-28
相关资源
最近更新 更多