京东金融大数据竞赛猪脸识别(2)- 图像特征提取之一

图像识别进入深度学习时代后,特征提取这个词的使用频率明显下降了。因为深度网络已经完成了从图像输入到分类结果输出的全过程,似乎不需要再关心特征的好坏和特征提取对于识别结果的影响。不过,不管从算法研究还是工程实现角度看,将特征提取独立出来应该更有利。这样我们可以对各种特征提取方法和各种识别算法进行组合,找出效果最好的方案。
我们先考虑非神经网络提取的特征,在深度神经网络大热以前,局部特征、空间金字塔、稀疏编码的结合合应该是达到最高识别准确率的方案。它在一些应用场合能够以更低的代价带来不次于深度网络的识别效果,可应用于实现方案。这里的代码是在工具箱reco_toolbox基础上修改的:

%exam1.m extract features from every image
clc,close all, clear all,drawnow
database_name        = {‘JDTest‘};
database_ext         = {‘jpg‘ , ‘jpg‘ , ‘png‘};
descriptors_name     = {‘mlhoee_spyr‘ , ‘mlhmslsd_spyr‘ , ‘mlhmslbp_spyr‘ , ‘mlhmsldp_spyr‘};

choice_database      = [1];
choice_descriptors   = [3]; %mlhoee_spyr=1/mlhmslsd_spyr=2/mlhmslbp_spyr=3/mlhmsldp_spyr=4
do_extract_feature   = 1;   %no=0/yes=1

data_name            = database_name{choice_database(1)};
im_ext               = database_ext{choice_database(1)};
rootbase_dir         = pwd;
images_dir           = fullfile(pwd , ‘images‘ , data_name);
core_dir             = fullfile(pwd , ‘core‘);
feat_dir             = fullfile(pwd , ‘features‘);
des_dir              = fullfile(pwd , ‘descriptors‘);
addpath(core_dir);
%执行描述符配置脚本文件
eval([data_name , ‘_config_descriptors‘]);
descriptors_param    = cell(1 , length(descriptors_name));
descriptors_param{1} = mlhoee;
descriptors_param{2} = mlhmslsd;
descriptors_param{3} = mlhmslbp;
descriptors_param{4} = mlhmsldp;

descriptors_size     = cell(1 , length(descriptors_name));
descriptors_size{1}  = descriptors_param{1}{1}.size;
descriptors_size{2}  = descriptors_param{2}{1}.size;
descriptors_size{3}  = descriptors_param{3}{1}.size;
descriptors_size{4}  = descriptors_param{4}{1}.size;
nb_descriptors       = length(choice_descriptors);

if(do_extract_feature)
    for j  = 1 : nb_descriptors
        current_path  = fullfile(pwd , ‘images‘ , data_name );
        current_dir     = dir(fullfile(current_path , [‘*.‘ , im_ext]));
        m                    = length(current_dir);
        current_descriptor = choice_descriptors(j);
        current_size           = descriptors_size{current_descriptor};
        base_descriptor     = descriptors_name{current_descriptor};
        eval([‘descriptors   = @‘ base_descriptor ‘;‘]);
        %初始化特征存储变量
        X                            = zeros(descriptors_size{current_descriptor} , m);
        for i = 1 : m
            I                  = imread(fullfile(current_path , current_dir(i).name));
            [h,w]           = size(I);
                        %将图像缩小一半
            J                 = imresize(I,[h/2,w/2]);
            X(:,i)            = descriptors(J , descriptors_param{current_descriptor}{:});
            fprintf(‘descriptor = %s, image = %s (%d/%d)\n‘ , base_descriptor ,  current_dir(i).name , i , m)
            drawnow
        end
                %将图像特征存储为文件
        save(fullfile(feat_dir , [data_name , ‘_‘ , base_descriptor]) , ‘X‘  )
        clear X ;
    end
    fprintf(‘Feature extraction  finished!‘);
end

运行该程序就可以对image文件夹下JDTest文件夹内各子文件夹内的图像提取特征并存为文件。

%exam2.m extract bag-of-features for every image
clc,close all, clear ,drawnow
database_name        = {‘JDPig‘ };
database_ext         = {‘jpg‘ , ‘jpg‘ , ‘png‘};
descriptors_name     = {‘denseSIFT‘ , ‘denseCOLOR‘ , ‘densePATCH‘ , ‘denseMBLBP‘ , ‘denseMBLDP‘};

encoding_name        = {‘yael_kmeans‘ , ‘mexTrainDL‘};
features_name          = {‘mlhbow_spyr‘ , ‘dl_spyr‘ , ‘mlhlcc_spyr‘};
choice_database      = [1];
choice_descriptors   = [1];
choice_encoding      = [2]; %Kmeans=1/Sparse Learning =2
choice_feature          = [2]; %SP-Histogram = 1/Sparse Pooling = 2/LCC = 3

do_extract_patches     = 1;   %no=0/yes=1
do_encoding                = 1;   %no=0/yes=1
do_compute_features  = 1;   %no=0/yes=1
dicoshared                   = 0;   %no=0/yes=1

data_name                   = database_name{choice_database(1)};
im_ext                           = database_ext{choice_database(1)};

rootbase_dir         = pwd;
images_dir            = fullfile(pwd , ‘images‘ , data_name);
core_dir                 = fullfile(pwd , ‘core‘);
feat_dir                  = fullfile(pwd , ‘features‘);
dico_dir                  = fullfile(pwd , ‘dico‘);
des_dir                   = fullfile(pwd , ‘descriptors‘);
addpath(core_dir)
dirim                       = dir(images_dir);
nb_topic                 = length(dirim) - 2;
classe_name          = cellstr(char(dirim(3:nb_topic+2).name))‘;

%执行相关配置文件
eval([data_name , ‘_config_descriptors‘]);
eval([data_name , ‘_config_encoding‘]);
eval([data_name , ‘_config_features‘]);

nbimagespertopic     = zeros(1 , nb_topic);
for i = 1:nb_topic
    dir_name            = dir(fullfile(pwd  , ‘images‘ , data_name , dirim(i+2).name , [‘*.‘ , im_ext]));
    nbimagespertopic(i) = length(dir_name);
end
N                    = sum(nbimagespertopic);

descriptors_param    = cell(1 , length(descriptors_name));
descriptors_param{1} = sift;
descriptors_param{2} = color;
descriptors_param{3} = patch;
descriptors_param{4} = mblbp;
descriptors_param{5} = mbldp;

descriptors_size     = cell(1 , length(descriptors_name));
descriptors_size{1}  = descriptors_param{1}{1}.size;
descriptors_size{2}  = descriptors_param{2}{1}.size;
descriptors_size{3}  = descriptors_param{3}{1}.size;
descriptors_size{4}  = descriptors_param{4}{1}.size;
descriptors_size{5}  = descriptors_param{5}{1}.size;

encoding_param       = cell(1 , length(encoding_name));
encoding_param{1}    = yael;
encoding_param{2}    = spams;

features_param        = cell(1 , length(features_name));
features_param{1}    = mlhbow_feat;
features_param{2}    = dl_feat;
features_param{3}    = mlhlcc_feat;

nb_descriptors       = length(choice_descriptors);
nb_encoding          = length(choice_encoding);
nb_features          = length(choice_encoding);

current_descriptor = choice_descriptors;
base_descriptor    = descriptors_name{current_descriptor};
featfile = fullfile(feat_dir , [data_name , ‘_‘ , base_descriptor]);
featfile = [featfile,‘.mat‘];
if (~exist(featfile))
    do_extract_patches = 1;
else
    do_extract_patches = 0;
end
if(do_extract_patches)
    for j  = 1 : nb_descriptors
        current_descriptor  = choice_descriptors(j);
        current_size           = descriptors_size{current_descriptor};
        base_descriptor     = descriptors_name{current_descriptor};
        nbpatches              = descriptors_param{current_descriptor}{1}.nbpatches;
        nbpatchetotal         = nbpatches*N;
        standardize            = descriptors_param{current_descriptor}{1}.standardize;
        whithning               = descriptors_param{current_descriptor}{1}.whithning;
        patchdim                = descriptors_param{current_descriptor}{1}.patchdim;

        eval([‘descriptors = @‘ base_descriptor ‘;‘]);
        fprintf(‘descriptor = %s \n\n‘ , base_descriptor)
        drawnow

        X                  = zeros(descriptors_size{current_descriptor} , N , ‘single‘);
        Z                  = zeros(nbpatches*6 , N , ‘single‘);
        S                  = zeros(2 , N , ‘uint16‘);
        Y                  = zeros(nbpatches , N , ‘single‘);
        y                  = zeros(1 , N );
        co                 = 1;

        %对每类图像进行处理
        for t = 1 : nb_topic
            current_path  = fullfile(pwd , ‘images‘ , data_name , dirim(t+2).name);
            current_dir   = dir(fullfile(current_path , [‘*.‘ , im_ext]));
            current_topic = char(classe_name(t));
            for i = 1 : length(current_dir)
                I                   = imread(fullfile(current_path , current_dir(i).name));
                fprintf(‘descriptor = %s, topic = %s (%d/%d), image = %s (%d/%d)\n‘ , base_descriptor , current_topic, t , nb_topic , current_dir(i).name , i , nbimagespertopic(t))
                drawnow
                [des , fea]         = descriptors(I , descriptors_param{current_descriptor}{:});
                X(: , co)            = reshape(single(des) , current_size , 1);
                Z(: , co)            = reshape(single(fea(1:6,:)) , nbpatches*6 , 1);
                Y(: , co)            = t*ones(nbpatches , 1);
                y(co)                 = t;
                co                     = co + 1;
            end
        end
        if(dicoshared)
            Z(3:4 , :)                                                                   = 1;
            descriptors_param{current_descriptor}{1}.scale     = 1;
            descriptors_param{current_descriptor}{1}.nbscale  = 1;
            descriptors_param{current_descriptor}{1}.dimcolor = 1;
            features_param{current_features}.scale                  = 1;
        end

        X                           = reshape(X , patchdim , nbpatchetotal);
        Z                           = reshape(Z , [6 , nbpatchetotal]);
        Y                           = reshape(Y , 1 , nbpatchetotal);

        if(standardize)
            fprintf(‘Standardize patches\n‘ )
            drawnow
            mX            = mean(X , 2);
            stdX          = std(X , 0 , 2);
            stdX(stdX==0) = 1;
            X             = (X - mX(: , ones(1 , size(X , 2))))./stdX(: , ones(1 , size(X , 2)));
            fprintf(‘End Standardize\n‘ )
            drawnow
        end
        if(whithning)
            fprintf(‘Whithning patches\n‘ )
            drawnow
            covX    = (1/(size(X,2)-1))*(X*X‘);
            [V,D]   = eig(covX);
            T       = (V*diag(sqrt(1 ./(diag(D) + 0.1))))*V‘;
            X       = T * X;
            fprintf(‘End whithning\n‘ )
            drawnow
        end
        fprintf(‘Saving patches descriptor  %s ...\n‘ , [data_name , ‘_‘ , base_descriptor]);
        drawnow
        save(fullfile(des_dir , [data_name , ‘_‘ , base_descriptor]) , ‘X‘ , ‘Z‘  , ‘Y‘ , ‘y‘ , ‘classe_name‘ , ‘patchdim‘ , ‘nbpatches‘ , ‘N‘  , ‘-v7.3‘)
        clear X Z Y y;
    end
end

current_descriptor = choice_descriptors;
base_descriptor    = descriptors_name{current_descriptor};
current_encoding = choice_encoding
base_encoding    = encoding_name{current_encoding};
dictfile = fullfile(dico_dir , [data_name , ‘_‘ , base_encoding , ‘_‘ , base_descriptor]);
dictfile = [dictfile,‘.mat‘];
if (~exist(featfile))
    do_encoding = 1;
else
    do_encoding = 0;
end
if(do_encoding)
    for j  = 1 : nb_encoding
        current_encoding = choice_encoding(j);
        base_encoding    = encoding_name{current_encoding};
        eval([‘encoding = @‘ base_encoding ‘;‘]);

        for i  = 1 : nb_descriptors
            current_descriptor = choice_descriptors(i);
            base_descriptor    = descriptors_name{current_descriptor};
            nbpatches            = descriptors_param{current_descriptor}{1}.nbpatches;
            nbpatchetotal       = nbpatches*N;
            patchdim              = descriptors_param{current_descriptor}{1}.patchdim;
            nbscale               = descriptors_param{current_descriptor}{1}.nbscale;
            dimcolor              = descriptors_param{current_descriptor}{1}.dimcolor;
            K                         = encoding_param{current_encoding}{current_descriptor}.K;
            nbpatchesperclass  = encoding_param{current_encoding}{current_descriptor}.nbpatchesperclass;
            D                          = zeros(patchdim , K , nbscale , dimcolor , ‘single‘);
            fprintf(‘Loading patches descriptor %s ...\n‘ , [data_name , ‘_‘ , base_descriptor]);
            drawnow
            load(fullfile(des_dir , [data_name , ‘_‘ , base_descriptor]) , ‘X‘ , ‘Z‘ , ‘Y‘ , ‘y‘ , ‘classe_name‘)

            for c = 1 : dimcolor
                for s = 1 : nbscale
                    currentscale      = descriptors_param{current_descriptor}{1}.scale(s);
                    index                 = find( (Z(3 , :) == currentscale ) & (Z(4 , :) == c) );
                    Yindex               = Y(index);
                    lindex                 = length(index);
                    indexdico            = [];
                    for t = 1 : nb_topic
                        indt          = find(Yindex == t);
                        lindt         = length(indt);
                        idx           = randperm(lindt);
                        idxtemp   = idx(1:min(nbpatchesperclass ,lindt));
                        indexdico = [indexdico , index(indt(idxtemp))];
                    end

                    fprintf(‘Learning dictionary of K = %d words with encoder = %s, scale = %5.3f, dimcolor = %d from %d/%d patches of %s \n‘ , K , base_encoding , currentscale , c , length(indexdico) , lindex , base_descriptor)
                    drawnow
                    D(: , : , s , c)  = encoding(X(: , indexdico) , encoding_param{current_encoding}{current_descriptor});
                end
            end
            fprintf(‘Saving dictionary  %s ...\n‘ , [data_name , ‘_‘ , base_encoding , ‘_‘ , base_descriptor]);
            drawnow
            save(fullfile(dico_dir , [data_name , ‘_‘ , base_encoding , ‘_‘ , base_descriptor]) , ‘D‘);
            clear X Z Y y classe_name;
        end
    end
end

current_descriptor = choice_descriptors;
base_descriptor    = descriptors_name{current_descriptor};
current_features   = choice_feature;
base_features      = features_name{current_features};
dlfeatfile = fullfile(feat_dir , [data_name , ‘_‘ , base_descriptor , ‘_‘ , base_features]);
dlfeatfile = [dlfeatfile,‘.mat‘];
if (~exist(dlfeatfile))
    do_compute_features = 1;
else
    do_compute_features = 0;
end

if(do_compute_features)
    for j  = 1 : nb_encoding
        current_features   = choice_feature(j);
        base_features      = features_name{current_features};
        current_encoding   = choice_encoding(j);
        base_encoding      = encoding_name{current_encoding};
        eval([‘features = @‘ base_features ‘;‘]);
        for i  = 1 : nb_descriptors
            current_descriptor                                          = choice_descriptors(i);
            base_descriptor                                             = descriptors_name{current_descriptor};
            nbpatches                                                      = descriptors_param{current_descriptor}{1}.nbpatches;
            nbpatchetotal                                               = nbpatches*N;
            patchdim                                                      = descriptors_param{current_descriptor}{1}.patchdim;
            nbscale                                                        = descriptors_param{current_descriptor}{1}.nbscale;
            dimcolor                                                       = descriptors_param{current_descriptor}{1}.dimcolor;
            features_param{current_features}{current_descriptor}.scale  = descriptors_param{current_descriptor}{1}.scale;
            features_param{current_features}{current_descriptor}.L      = patchdim;
            current_feature_param                                       = features_param{current_features}{current_descriptor};
            fprintf(‘Loading dictionnary %s ...\n‘ , [data_name , ‘_‘ , base_encoding , ‘_‘ , base_descriptor]);
            drawnow
            load(fullfile(dico_dir , [data_name , ‘_‘ , base_encoding , ‘_‘ , base_descriptor]) , ‘D‘);
            fprintf(‘Loading patches descriptor %s ...\n‘ , [data_name , ‘_‘ , base_descriptor]);
            drawnow
            load(fullfile(des_dir , [data_name , ‘_‘ , base_descriptor]) , ‘X‘ , ‘Z‘  , ‘y‘ ,  ‘classe_name‘);
            K                                                           = size(D , 2);
            nH                                                          = current_feature_param.nH;
            X                                                           = reshape(X , descriptors_size{current_descriptor} , N);
            Z                                                           = reshape(Z , 6*nbpatches , N);
            F                                                           = zeros(K*nH*nbscale*dimcolor , N);

            co                                                          = 1;
            for t = 1 : nb_topic
                current_path  = fullfile(pwd , ‘images‘ , data_name , dirim(t+2).name);
                current_dir   = dir(fullfile(current_path , [‘*.‘ , im_ext]));
                current_topic = char(classe_name(t));
                for i = 1 : length(current_dir)
                    fprintf(‘encoder = %s, topic = %s (%d/%d), patches = %s , image = %s (%d/%d)\n‘ , base_features , current_topic, t , nb_topic , base_descriptor , current_dir(i).name , i , nbimagespertopic(t))
                    drawnow
                    XX              = reshape(X(: , co) , patchdim , nbpatches);
                    ZZ              = reshape(Z(: , co) , 6 , nbpatches);
                    F(: , co)       = features(D , XX , ZZ  , current_feature_param);
                    co              = co + 1;
                end
            end
            X                  = F;
            clear F Z S;
            fprintf(‘Saving features  %s ...\n‘ , [data_name , ‘_‘ , base_descriptor , ‘_‘ , base_features]);
            drawnow
            dlfeatfile = fullfile(feat_dir , [data_name , ‘_‘ , base_descriptor , ‘_‘ , base_features]);
            save( dlfeatfile, ‘X‘ , ‘y‘ , ‘classe_name‘ , ‘-v7.3‘);
        end
    end
end
fprintf(‘Feature extraction  finished!!‘);

该程序提取提取后对特征聚类生成词袋,并依据词袋对图像特征进行编码,为每幅图像生成特征。详细内容可参看Matlab图像识别/检索系列(9)—开源工具介绍之图像识别reco_toolbox

原文地址:http://blog.51cto.com/8764888/2085964

时间: 2024-10-14 09:28:50

京东金融大数据竞赛猪脸识别(2)- 图像特征提取之一的相关文章

京东金融大数据竞赛猪脸识别(7)- 识别方法之四

除了softmax层构建的深度网络,Matlab还有一个简单的构建数据分类的函数,那就是patternnet,其用法类似.可以直接对图像特征数据处理,也可以对图像集处理.代码如下: %exam1.m 用训练图像特征构建深度网络并计算测试图像得分 clear; load('JDPig_mlhmslbp_spyr.mat'); m = numel(classe_name); n = length(y); label = [] for i=1:n label(:,i) = zeros(m,1); la

京东金融大数据竞赛猪脸识别(1)-从视频提取图像

2017年11月的京东金融大数据竞赛参与人数最多的是猪脸识别的算法比赛,参加整个大数据比赛的有四千多人,而猪脸识别算法组就有一千多人.可见,搞图像识别的的人很多啊.想要提升自己价值的小伙伴们,向语音.文本.机器人等领域进发吧,有了机器学习的基础,入门这些领域应该都不是太难.比赛给的数据是30头猪的视频,做识别的第一步还是从视频中去图像吧.本想用以前写过的视频取帧程序.看Matlab示例的时候发现用Matlab取帧更简便易行,那就用它吧.这样又省了很多时间.代码如下: %exam1.m extra

京东金融大数据竞赛猪脸识别(3)-方法选取

由于可用的特征提取和分类器构造方法较多,为了确定选取方法.现在在视频提取的图像上进行算法验证,取这些图像的一部分进行训练,剩余部分进行测试,并进行交叉验证.找到准确率最高的方法后再将其用于测试集.以reco_toolbox为例,下面给出方法选取的代码: clc,close all, clear ,drawnow database_name = {'JDPig' }; database_ext = {'jpg' , 'jpg' , 'png'}; descriptors_name = {'dens

京东金融大数据竞赛猪脸识别(9)- 识别方法之五

这里给出使用深度网络中间层输出结果作为图像特征,并构建分类模型和对训练数据进行识别的代码.相关内容可参看Matlab图像识别/检索系列(7)-10行代码完成深度学习网络之取中间层数据作为特征.代码如下: clear trainPath = fullfile(pwd,'image'); trainData = imageDatastore(trainPath,... 'IncludeSubfolders',true,'LabelSource','foldernames'); %对训练数据集进行划分

京东金融大数据竞赛猪脸识别(5)- 识别方法之二

该方法提取图像集的词袋(bag-of-features),然后根据词袋对各图像编码得出图像特征,再对测试图像在训练图像集上进行检索,最后根据检索出的图像类别判断测试图像所属类别.该方法直接对图像进行处理,不需要先提取特征,再将特征文件导入.不过该方法消耗内存很大,适用于小规模图像集.代码如下: clear; % 设置图象集路径 imgSetFolder = fullfile(pwd, 'image'); imgqueryFolder = fullfile(pwd, 'query'); %构造图像

京东金融大数据竞赛猪脸识别(3)- 图像特征提取之二

深度网络既然在图像识别方面有很高的准确率,那将某一层网络输出数据作为图像特征也应该是可行的.该程序给出了使用Alexnet第七层作为激活层提取图像特征的示例.代码如下: clear; trainPath = fullfile(pwd,'image'); trainData = imageDatastore(trainPath,... 'IncludeSubfolders',true,'LabelSource','foldernames'); [trainingImages,testImages]

大数据竞赛平台——Kaggle 入门

大数据竞赛平台--Kaggle 入门篇 这篇文章适合那些刚接触Kaggle.想尽快熟悉Kaggle并且独立完成一个竞赛项目的网友,对于已经在Kaggle上参赛过的网友来说,大可不必耗费时间阅读本文.本文分为两部分介绍Kaggle,第一部分简单介绍Kaggle,第二部分将展示解决一个竞赛项目的全过程.如有错误,请指正! 1.Kaggle简介 Kaggle是一个数据分析的竞赛平台,网址:https://www.kaggle.com/ 企业或者研究者可以将数据.问题描述.期望的指标发布到Kaggle上

阿里巴巴天池大数据竞赛黄金联赛全面开战,全球同步报名,只为寻找最聪明的你!

阿里巴巴天池大数据竞赛黄金联赛全面开战,全球同步报名,只为寻找最聪明的你!        天池大数据竞赛是由阿里巴巴集团主办,面向全球新生代力量的高端算法竞赛.通过开放海量数据和"天池"分布式计算平台,大赛让所有参与者有机会运用其设计的算法解决各类社会生活问题和商业世界中的实际问题.特别优秀的解决方案将有机会直接上线阿里巴巴旗下各电商网站(含淘宝.天猫等)或第三方合作伙伴平台,服务中国乃至世界数以亿计的用户.        2015年天池大数据竞赛将全面升级为黄金联赛,包含三个不同场景

Excel催化剂开源第42波-与金融大数据TuShare对接实现零门槛零代码获取数据

在金融大数据功能中,使用了TuShare的数据接口,其所有接口都采用WebAPI的方式提供,本来还在纠结着应该搬那些数据接口给用户使用,后来发现,所有数据接口都有其通用性,结合Excel灵活友好的输入方式,将其输入参数统一在Excel界面进行维护,最终实现了所有接口均可由用户自己去维护参数的方式发出查询获得所有的结果,非常完美. 此篇对应的Excel催化剂功能实现:第98波-零代码零距离轻松接触并拥有金融大数据 - 简书 https://www.jianshu.com/p/3cd41a48344