回归caffe的测试标签,不允许浮动?

Dev*_*ven 12 regression neural-network deep-learning caffe

我正在使用caffe进行回归,而我test.txttrain.txt文件是这样的:

/home/foo/caffe/data/finetune/flickr/3860781056.jpg 2.0  
/home/foo/caffe/data/finetune/flickr/4559004485.jpg 3.6  
/home/foo/caffe/data/finetune/flickr/3208038920.jpg 3.2  
/home/foo/caffe/data/finetune/flickr/6170430622.jpg 4.0  
/home/foo/caffe/data/finetune/flickr/7508671542.jpg 2.7272
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我在阅读时使用浮动标签时,似乎caffe不允许像2.0这样的浮动标签,例如'test.txt'文件caffe只能识别

共1张图片

这是错的.

但是当我例如将文件中的2.0更改为2并且以下行相同时,caffe现在给出了

共2张图片

暗示浮动标签是造成问题的原因.

任何人都可以帮助我,解决这个问题,我肯定需要使用浮动标签进行回归,所以有人知道解决方案或解决方案吗?提前致谢.

编辑 对于任何面临类似问题的人来说,使用caffe来训练Lenet的CSV数据可能会有所帮助.感谢@Shai.

Sha*_*hai 23

使用图像数据集输入图层(带有后端lmdbleveldb后端)时,caffe 每个输入图像仅支持一个整数标签.

如果要进行回归并使用浮点标签,则应尝试使用HDF5数据层.例如,请参阅此问题.

在python中,您可以使用h5pypackage来创建hdf5文件.

import h5py, os
import caffe
import numpy as np

SIZE = 224 # 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
Run Code Online (Sandbox Code Playgroud)

一旦您拥有所有h5文件以及列出它们的相应测试文件,您就可以将HDF5输入层添加到train_val.prototxt:

 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 }
 }
Run Code Online (Sandbox Code Playgroud)

澄清:
当我说"caffe每个输入图像只支持一个整数标签"时,我并不是说leveldb/lmdb容器是有限的,我的意思是caffe的convert_imageset工具,特别是工具.
仔细观察,似乎caffe Datum在leveldb/lmdb中存储类型的数据,并且此类型的"label"属性被定义为整数(请参阅caffe.proto),因此当使用caffe接口到leveldb/lmdb时,您将被限制为单个每个图像的int32标签.