user824624
user824624

Reputation: 8068

how to feed the image data to HDF5 on caffe or existing examples?

I had hard time working on caffe with HDF5 on the image classification and regression tasks, for some reason, the training on HDF5 will always fail at the first beginning that the test and train loss could very soon drop to close to zero. after trying all the tricks such reducing the learning rate, adding RELU, dropout, nothing started to work, so I started to doubt that the HDF5 data I am feeding to caffe is wrong.

so currently I am working on the universal dataset (Oxford 102 category flower dataset and also it has public code ), firstly I started out by trying ImageData and LMDB layer for the classification, they all worked very well. at last i used HDF5 data layer for the finetuning, the training_prototxt doesn't change unless on the data layer which uses HDF5 instead. and again, at the start of the learning, the loss drops from 5 to 0.14 at iteration 60, 0.00146 at iteration 100, that seems to prove that HDF5 data is incorrect.

i have two image&label to HDF5 snippet on the github, all of them seem to generate the HDF5 dataset, but for some reason these dataset doesn't seem to be not working with caffe

I wonder anything wrong with this data, or anything that makes this example run in HDF5 or if you have some HDF5 examples for classification or regression, which can be helpful to me a lot.

one snippet is shown as

def generateHDF5FromText2(label_num):
    print '\nplease wait...'

    HDF5_FILE = ['hdf5_train.h5', 'hdf5_test1.h5']
        #store the training and testing data path and labels
    LIST_FILE = ['train.txt','test.txt']
    for kk, list_file in enumerate(LIST_FILE):

    #reading the training.txt or testing.txt to extract the all the image path and labels, store into the array
    path_list = []
    label_list = []
    with open(list_file, buffering=1) as hosts_file:
        for line in hosts_file:
            line = line.rstrip()
            array = line.split(' ')
            lab = int(array[1])
            label_list.append(lab)
            path_list.append(array[0])  

        print len(path_list), len(label_list)

                # init the temp data and labels storage for HDF5
        datas = np.zeros((len(path_list),3,227,227),dtype='f4') 
        labels = np.zeros((len(path_list), 1),dtype="f4")

        for ii, _file in enumerate(path_list):
                    # feed the image and label data to the TEMP data
            img = caffe.io.load_image( _file )
            img = caffe.io.resize( img, (227, 227, 3) ) # resize to fixed size
            img = np.transpose( img , (2,0,1))
            datas[ii] = img
            labels[ii] = int(label_list[ii])

            # store the temp data and label into the HDF5
        with h5py.File("/data2/"+HDF5_FILE[kk], 'w') as f:
            f['data'] = datas
            f['label'] = labels
            f.close()

Upvotes: 0

Views: 3898

Answers (1)

Shai
Shai

Reputation: 114796

One input transformation that seems to happen in the original net and is missing from your HDF5 creation in mean subtraction.
You should obtain mean file (looks like "imagenet_mean.binaryproto" in your example), read it into python and subtract it from each image.

BTW, the mean file can give you a clue as to the scale of the input image (if pixel values should be in [0..1] range or [0..255]).

You might find caffe.io useful converting binaryproto to numpy array.

Upvotes: 2

Related Questions