prepare hdf5 data for training

来源:互联网 发布:2016年7月淘宝新政策 编辑:程序博客网 时间:2024/06/03 14:14

Matlab:

% hdf5_file_2 clearvars;load train_data_2.mat;load mean_file.mat ; % load mean_filetrain_feat = [train_data{1};train_data{2};train_data{3};train_data{4}];test_feat = train_data{5} ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% imgName_temp1 = {train_feat.imgName};% imgName_temp2 = {test_feat.imgName};%% imgName2 = {imgName_temp1{:},imgName_temp2{:}};% % check all%% load datas.mat;%% imgName1 = {datas.imgName};%% result = setdiff(imgName1,imgName2); % 判断两个集合是否相等。% result0 = setdiff(imgName2,imgName1);%% test_imgName = {'AJ_Lamas_0001.jpg','nihao'};%% result2 = setdiff(test_imgName,imgName2);% result3 = setdiff(imgName2,test_imgName);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% in order to load data quickly ,and don't use 'shuffle' in prototxt%% we shuffle first,and pass data into netntrain = size(train_feat,1);ntest  = size(test_feat,1);if ~exist('rand_ind.mat','file')    train_rand_ind = randperm(ntrain);    test_rand_ind  = randperm(ntest);    save('rand_ind.mat','train_rand_ind','test_rand_ind');else    load rand_ind.mat; % load train_rand_ind ,test_rand_ind    disp('load rand_ind over ...');endtrain_feat = train_feat(train_rand_ind);test_feat = test_feat(test_rand_ind);% write to hdf5 file% follow demo.mimgSize = [227 227]; % 227 * 227 for alexnetn_train_file = 20;n_test_file = fix(n_train_file / 4);train_each_size = floor(length(train_feat)/n_train_file);test_each_size  = floor(length(test_feat)/n_test_file);train_filePath = './hdf5_file_2/train';test_filePath = './hdf5_file_2/test';train_split = cell(n_train_file,1);test_split = cell(n_test_file,1);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1: train 0: test %%%%%%%%%%%%%%if 0    for i = 1:n_train_file        start_ind = (i-1) * train_each_size + 1;        if i == n_train_file            end_ind = ntrain;        else            end_ind = i * train_each_size;        end        ind_range = start_ind : end_ind;        % trainFeature : width * height * c * nsamples        trainFeature = zeros(imgSize(1),imgSize(2),3,length(ind_range));        trainLabel   = zeros(2,length(ind_range)); % for headpose and smile        t = 1;        for j = start_ind : end_ind            imgPath = train_feat(j).imgPath;            imgPath = strrep(imgPath,'imgs','alex_imgs');            img = imread(imgPath); % rgb            img = permute(img,[2 1 3]);            img = img(:,:,[3 2 1]); % rgb 2 bgr            % 减去均值文件 mean_file            img = double(img) - mean_file ; % mean_file 文件满足:width * hegith * c (bgr)            trainFeature(:,:,:,t) = img;            trainLabel(1,t) = train_feat(j).headpose + 2; % for head pose label            trainLabel(2,t) = train_feat(j).smile;    % for smile label            t = t + 1;        end        fileName = ['train_part' num2str(i) '.h5']; % train_part1 train_part2 .....        %% save to hdf5        filePath = ['./hdf5_file_2/train/' fileName];        h5create(filePath,'/data',size(trainFeature),'Datatype','single');        h5create(filePath,'/label',size(trainLabel),'Datatype','single');        h5write(filePath,'/data',single(trainFeature));        h5write(filePath,'/label',single(trainLabel));    end    disp('save trainFeature/trainLabel to hdf5 over ...');else    for i = 1:n_test_file        start_ind = (i-1) * test_each_size + 1;        if i == n_test_file            end_ind = ntest;        else            end_ind = i * test_each_size;        end        ind_range = start_ind : end_ind;        % trainFeature : width * height * c * nsamples        testFeature = zeros(imgSize(1),imgSize(2),3,length(ind_range));        testLabel   = zeros(2,length(ind_range)); % for headpose and smile        t = 1;        for j = start_ind : end_ind            imgPath = test_feat(j).imgPath;            imgPath = strrep(imgPath,'imgs','alex_imgs');            img = imread(imgPath); % rgb            img = permute(img,[2 1 3]);            img = img(:,:,[3 2 1]); % rgb 2 bgr            % 减去均值文件 mean_file            img = double(img) - mean_file ; % mean_file 文件满足:width * hegith * c (bgr)            testFeature(:,:,:,t) = img;            testLabel(1,t) = test_feat(j).headpose + 2; % for head pose label            testLabel(2,t) = test_feat(j).smile;    % for smile            t = t + 1;        end        fileName = ['test_part' num2str(i) '.h5']; % train_part1 train_part2 .....        %% save to hdf5        filePath = ['./hdf5_file_2/test/' fileName];        h5create(filePath,'/data',size(testFeature),'Datatype','single');        h5create(filePath,'/label',size(testLabel),'Datatype','single');        h5write(filePath,'/data',single(testFeature));        h5write(filePath,'/label',single(testLabel));    end    disp('save testFeature/testLabel to hdf5 over ...');end

Python:

原创粉丝点击