"内存不足" MATLAB

时间:2015-03-20 10:13:21

标签: matlab

我从4个不同的Matlab函数中制作了4个独立的可执行文件来构建人脸识别系统。我使用不同的批处理代码调用这4个可执行文件并在图像上执行任务。我拥有的图像总数超过300k。这4个可执行文件中有3个工作正常,但我面临内存不足和#34;当我试图调用Fisherface函数的独立可执行文件时出现问题。它使用Fisher的线性判别分析简单地计算每个图像的独特特征。该分析应用于巨大的面部矩阵,该矩阵由超过150,000个尺寸为60 * 60的图像的像素值组成。因此矩阵的大小为150,000 * 3600。

我理解的是由于RAM中连续内存不足而发生的事情。因此,作为一种出路,我选择将我的大图像集划分为多个子集,每个子​​集包含3000个图像。现在,当提供输入面时,它在每个子集中搜索该输入的最佳匹配,并最终排序具有最低距离的3个最佳匹配的最终列表(欧几里德)。这解决了内存不足错误,但识别率变得更低。因为当在原始面部矩阵中进行判别分析时(我在包含4000-5000个图像的较小数据集中进行了测试),它可以提供良好的识别率。

我正在寻找解决这个问题的方法。我想在大矩阵上执行所有操作。有没有办法更有效地实现该功能,例如,在Matlab中动态分配内存?我希望我能够相当具体地解释我的问题。下面,我提供了该特定可执行文件的代码段。

    function FisherfaceCorenew(matname)
    load(matname);
    Class_number = size(T,2) ;
    Class_population = 1;
    P = Class_population * Class_number; % Total number of training images

    %%%%%%%%%%%%%%%%%%%%%%%% calculating the mean image 

    m_database = single(mean(T,2)); 

    %%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image
    A = T - repmat(m_database,1,P);

    L = single(A')*single(A);  
    [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.

    %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating small eigenvalues
    L_eig_vec = [];

    for i = 1 : P 
    L_eig_vec = [L_eig_vec V(:,i)];
    end

    %%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C'
    V_PCA = single(A) * single(L_eig_vec); 

    %%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors onto eigenspace

    ProjectedImages_PCA = [];
    for i = 1 : P
    temp = single(V_PCA')*single(A(:,i));
    ProjectedImages_PCA = [ProjectedImages_PCA temp]; 
    end

    %%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean of each class in eigenspace
    m_PCA = mean(ProjectedImages_PCA,2); % Total mean in eigenspace

    m = zeros(P,Class_number);
    Sw = zeros(P,P); %new
    Sb = zeros(P,P); %new

    for i = 1 : Class_number
    m(:,i) = mean( ( ProjectedImages_PCA(:,((i-1)*Class_population+1):i*Class_population) ), 2 )';    

    S  = zeros(P,P); %new
    for j = ( (i-1)*Class_population+1 ) : ( i*Class_population )
    S = S + (ProjectedImages_PCA(:,j)-m(:,i))*(ProjectedImages_PCA(:,j)-m(:,i))';
    end

    Sw = Sw + S; % Within Scatter Matrix
    Sb = Sb + (m(:,i)-m_PCA) * (m(:,i)-m_PCA)'; % Between Scatter Matrix
    end

    %%%%%%%%%%%%%%%%%%%%%%%% Calculating Fisher discriminant basis's
    % We want to maximise the Between Scatter Matrix, while minimising the
    % Within Scatter Matrix. Thus, a cost function J is defined, so that this condition is satisfied.
    [J_eig_vec, J_eig_val] = eig(Sb,Sw); 
    J_eig_vec = fliplr(J_eig_vec);

    %%%%%%%%%%%%%%%%%%%%%%%% Eliminating zero eigens and sorting in descend order
    for i = 1 : Class_number-1 
    V_Fisher(:,i) = J_eig_vec(:,i); 
    end

    %%%%%%%%%%%%%%%%%%%%%%%% Projecting images onto Fisher linear space

    for i = 1 : Class_number*Class_population
    ProjectedImages_Fisher(:,i) = V_Fisher' * ProjectedImages_PCA(:,i);
    end

    save fisherdata.mat m_database V_PCA V_Fisher ProjectedImages_Fisher; 

    end

1 个答案:

答案 0 :(得分:2)

帮助你并不容易,因为我们看不到你的矩阵的大小。

至少你可以在不再使用变量后使用Matlab clear命令(例如A)。

当您分配A变量而不是每个等式时,也许可以使用single()命令。

A = single(T - repmat(m_database,1,P));

然后

L = A'*A;  

您也可以使用Matlab profiler with memory usage来查看您的内存需求。

另一种选择可能是使用sparse矩阵或减少甚至更小的数据类型,如uint8,如果适用于某些数据。