cuda-convnet 中数据组织格式

import os
import cPickle
import numpy as np
import Image

def MakeBatch(load_path_list, save_path, class_list):
    data = []
    filenames = []
    for load_path in load_path_list:    
        file_list = os.listdir(load_path)
        for item in file_list:
            if item.endswith(".jpg"):
                n = os.path.join(load_path, item)
                input = Image.open(n)
                arr = np.array(input, order='C')
                im = np.fliplr(np.rot90(arr, k=3))
                data.append(im.T.flatten('C'))
                filenames.append(item)
    data = np.array(data)
    out_file = open(save_path, 'w+')
    flipDat = np.flipud(data)
    rotDat = np.rot90(flipDat, k=3)
    dic = {'batch_label':'batch 1 of 1', 'data':rotDat, 'labels':class_list, 'filenames':filenames}
    cPickle.dump(dic, out_file, True)
    out_file.close()
    
def MakeMate(data_batch_list, label_name_list, save_path):
    
    aBatch = file(data_batch_list[0], 'rb')
    l = cPickle.load(aBatch)
    num_cases_per_batch = len(l['data'][0])
    
    label_name = label_name_list
    tmp = [0]*len(l['data'])
    data_mean = [[0]]*len(l['data'])
    
    for i in range(len(data_batch_list)):
         f = file(data_batch_list[i], 'rb')
         l = cPickle.load(f)
         for j in range(len(l['data'])): # a batch 196608
             for k in range(len(l['data'][j])): # a row 60
                 tmp[j] += l['data'][j][k]
                 
    for i in range(len(tmp)):
        data = 0.0
        data = round(float(tmp[i])/(len(data_batch_list)*num_cases_per_batch), 8)
        t = [data]
        data_mean[i] = t
    
    data_mean = np.array(data_mean, dtype='f')
    
    dic = {'num_cases_per_batch':num_cases_per_batch, 'label_names':label_name_list, 'data_mean':data_mean}
    out_file = open(save_path, 'w+')
    cPickle.dump(dic, out_file, True)
    out_file.close()
    
if __name__=='__main__':
    #class_list is a list that contain nums labels
    load_path_list = ['/home/tj/Pictures/MakeBatch/accordion', '/home/tj/Pictures/MakeBatch/butterfly']
    save_path = '/home/tj/Pictures/MakeBatch/OUTPUT/data_batch_1'
    class_list = [1]*30 + [2]*30
    MakeBatch(load_path_list, save_path, class_list)
    
   
    label_name_list = ['accordion', 'butterfly']
    data_batch_list = ['/home/tj/Pictures/MakeBatch/OUTPUT/data_batch_1']
    save_path_meta = '/home/tj/Pictures/MakeBatch/OUTPUT/batches.meta'
    MakeMate(data_batch_list, label_name_list, save_path_meta)
    
    print 'end'
    #f = file('/home/tj/Pictures/mybatch', 'rb')
    #l = cPickle.load(f)
    #print l['data']


你可能感兴趣的:(cuda-convnet 中数据组织格式)