Test labels for regression caffe, float not allowed?


狗头军师
2024-12-12 12:33:00 (2月前)

我正在使用caffe进行回归,而我test.txt和train.txt文件如下所示:


  1. /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

我的问题是,当我在阅读时使用浮动标签时,例如’test.txt’caffe 不允许使用2.0之类的浮动标签,例如caffe文件只能识别

共1张图片

这是错误的。

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

共2张图片

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

任何人都可以在这里帮助我,以解决此问题,我肯定需要使用float标签进行回归分析,所以有人知道解决此问题的方法或解决方案吗?提前致谢。

2 条回复
  1. 1# v-star*위위 | 2020-07-30 14-35

    当使用图像数据集输入层(带后端lmdb或带leveldb后端)时,caffe 每个输入图像仅支持一个整数标签。

    如果要进行回归并使用浮点标签,则应尝试使用HDF5数据层。例如参见这个问题。

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

    1. import h5py, os
    2. import caffe
    3. import numpy as np
    4. SIZE = 224 # fixed size to all images
    5. with open( 'train.txt', 'r' ) as T :
    6. lines = T.readlines()
    7. # If you do not have enough memory split data into
    8. # multiple batches and generate multiple separate h5 files
    9. X = np.zeros( (len(lines), 3, SIZE, SIZE), dtype='f4' )
    10. y = np.zeros( (len(lines),1), dtype='f4' )
    11. for i,l in enumerate(lines):
    12. sp = l.split(' ')
    13. img = caffe.io.load_image( sp[0] )
    14. img = caffe.io.resize( img, (SIZE, SIZE, 3) ) # resize to fixed size
    15. # you may apply other input transformations here...
    16. # Note that the transformation should take img from size-by-size-by-3 and transpose it to 3-by-size-by-size
    17. # for example
    18. # transposed_img = img.transpose((2,0,1))[::-1,:,:] # RGB->BGR
    19. X[i] = transposed_img
    20. y[i] = float(sp[1])
    21. with h5py.File('train.h5','w') as H:
    22. H.create_dataset( 'X', data=X ) # note the name X given to the dataset!
    23. H.create_dataset( 'y', data=y ) # note the name y given to the dataset!
    24. with open('train_h5_list.txt','w') as L:
    25. L.write( 'train.h5' ) # list all h5 files you are going to use

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

    1. layer {
    2. type: "HDF5Data"
    3. top: "X" # same name as given in create_dataset!
    4. top: "y"
    5. hdf5_data_param {
    6. source: "train_h5_list.txt" # do not give the h5 files directly, but the list.
    7. batch_size: 32
    8. }
    9. include { phase:TRAIN }
    10. }

    澄清:
    当我说“ caffe每个输入图像仅支持一个整数标签”时,我并不是说leveldb / lmdb容器是有限的,我指的是caffe的convert_imageset工具,特别是该工具。
    经过仔细检查,似乎caffe将类型Datum为typedb的数据存储在leveldb / lmdb中,并且此类型的“ label”属性定义为整数(请参阅caffe.proto),因此,在将caffe接口用于leveldb / lmdb时,您只能使用一个每个图像的int32标签。

登录 后才能参与评论