prepare hdf5 data for training

33 篇文章 1 订阅
27 篇文章 0 订阅

Matlab:



% hdf5_file_2 

clearvars;

load train_data_2.mat;

load mean_file.mat ; % load mean_file

train_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 net

ntrain = 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 ...');
end
train_feat = train_feat(train_rand_ind);
test_feat = test_feat(test_rand_ind);

% write to hdf5 file
% follow demo.m
imgSize = [227 227]; % 227 * 227 for alexnet

n_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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值