gpt4 book ai didi

neural-network - 回归咖啡的测试标签,不允许 float ?

转载 作者:行者123 更新时间:2023-12-04 00:09:40 25 4
gpt4 key购买 nike

我正在使用 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

我的问题是,当我在阅读时使用 float 标签时,caffe 似乎不允许像 2.0 这样的 float 标签,例如 'test.txt'仅文件 caffe
承认

a total of 1 images



这是错误的。

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

a total of 2 images



暗示 float 标签是造成问题的原因。

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

编辑
对于任何面临类似问题的人 use caffe to train Lenet with CSV data可能会有帮助。感谢@Shai。

最佳答案

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

如果要进行回归并使用浮点标签,则应尝试使用 HDF5 数据层。参见示例 this question .

在 python 中,您可以使用 h5py包以创建 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

一旦你拥有所有 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 }
}

澄清 :
当我说“caffe 只支持每个输入图像一个整数标签”时,我并不是说 leveldb/lmdb 容器是有限的,我指的是 caffe 的工具,特别是 convert_imageset 工具。
仔细观察,似乎 caffe 存储了 Datum 类型的数据。在 leveldb/lmdb 中,这种类型的“标签”属性定义为整数(参见 caffe.proto),因此当使用 caffe 接口(interface)连接到 leveldb/lmdb 时,每个图像只能使用一个 int32 标签。

关于neural-network - 回归咖啡的测试标签,不允许 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774953/

25 4 0