Can 'csv' format data be used in Caffe Training Proecess?

238 views
Skip to first unread message

刘治来

unread,
Jul 6, 2017, 8:23:29 AM7/6/17
to Caffe Users

Everyone: 
 I have a prob when I want to train caffe in a Facial Emotion Recognition database.The database listed as follow:

The task is to categorize each face based on the emotion shown in the facial expression(1st Col)  in to one of seven categories (0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).

The face image can be descibed in the 2nd col as a vec ,a space-separated pixel values in row major order.


My prob is :How can I convert this format data to lmdb file ?   Matlab or Python code will be appreciated.......THX A LOT!!!


Jason lee

unread,
Jul 7, 2017, 3:23:12 AM7/7/17
to Caffe Users
you can convert the csv to hd5 by following code:

import os
import numpy as np
from pandas.io.parsers import read_csv
from sklearn.utils import shuffle
import h5py

TRAIN_CSV = 'path/train.csv'

def csv_to_hd5():
    dataframe = read_csv(os.path.expanduser(TRAIN_CSV))
    dataframe['Image'] = dataframe['Image'].apply(lambda  img:np.fromstring(img,sep='   '))
    dataframe = dataframe.dropna()
    data = np.vstack(dataframe['Image'].values)/255

    label = dataframe[dataframe.columns[:-1]].values
    label = (label-48)/48
    data, label = shuffle(data, label, random_state = 0)

    return data, label

if __name__ == '__main__':
    data,label = csv_to_hd5()
    data = data.reshape(-1,1,96,96)
    data_train = data[:-100,:,:,:]
    data_val = data[-100:,:,:,:]

    label = label.reshape(-1,1,1,30)
    label_train =label[:-100,:,:,:]
    label_val = label[-100:,:,:,:]

    fhandle = h5py.File('train.hd5','w')
    fhandle.create_dataset('data',data = data_val,compression='gzip',compression_opts =4)
    fhandle.create_dataset('label', data=label_val, compression='gzip', compression_opts=4)
    fhandle.close()


2017년 7월 6일 목요일 오후 8시 23분 29초 UTC+8, 刘治来 님의 말:
Reply all
Reply to author
Forward
0 new messages