- 首页 > it技术 > >
python|python hdf5_for_caffe
import random
import os
import numpy as np
from PIL import Image
import h5py
LABELS = {"0":(1,0,0,0,0),"1":(0,1,0,0,0),"2":(0,0,1,0,0),"3":(0,0,0,1,0),"4":(0,0,0,0,1)}
#print LABELSIMAGE_DIR = ['image_train','image_test']
HDF5_FILE = ['hdf5_train.h5','hdf5_test.h5']
LIST_FILE = ['list_train.txt','list_test.txt']filename = "/home/XXXXX/my_file/lenet/traintestTxt/train11_abspath.txt"
LABEL_SIZE = 5#Multi-labels
MEAN_VALUE = https://www.it610.com/article/128setname ='train'with open(filename,'r') as f:
lines = f.readlines()np.random.shuffle(lines)sample_size = len(lines)datas = np.zeros((sample_size,3,56,56))
labels = np.zeros((sample_size,5))h5_filename = '{}.h5'.format(setname)#print datas.shape
#print h5_filenamefor i,line in enumerate(lines):
data_line = line.split(" ")[0]
labels_line = line.split(" ")[1]
temp_img = np.array(Image.open(data_line)).astype(np.float32)
temp_img = temp_img[:,:,::-1] #turn RGB to BGR
temp_img = temp_img.transpose((2,0,1))# turn 56,56,3 to 3,56,56
temp_img = temp_img.copy()
temp_img.resize(3,56,56)datas[i,:,:,:] = (temp_img-128)/256labels[i,:] = np.array(LABELS[labels_line.strip("\n")]).astype(np.int)
print('processed {} images!'.format(i))with h5py.File(h5_filename,'w') as h:
h['data'] = datas
h['label'] = labels
f.close()
with open('{}.txt'.format(setname),'w') as f:
f.write(os.path.abspath(h5_filename)+'\n')
f.close()
推荐阅读