内存不足 Matlab

5
我用四个不同的 Matlab 函数创建了四个独立的可执行文件,以构建一个人脸识别系统。我使用不同的批处理代码调用这些 4 个可执行文件,并对图像执行任务。我拥有的图像总数超过 300,000 张。其中三个可执行文件工作得很好,但当我尝试调用 Fisherface 函数的独立可执行文件时,我遇到了“内存不足”的问题。它仅仅是使用 Fisher 线性判别分析计算每个图像的唯一特征。该分析应用于巨大的人脸矩阵上,该矩阵由大小为 60*60 的 150,000 张图像的像素值组成。因此,矩阵的大小为 150,000 * 3600。
我理解的是这是由于 RAM 中连续内存不足导致的。因此,作为一种解决方法,我选择将我的大型图像集分成多个子集,每个子集包含 3000 张图像。现在,当提供输入面孔时,它会在每个子集中搜索最佳匹配项,并最终排序出最低距离(欧几里得)的前三个最佳匹配项的最终列表。这解决了内存不足的错误,但识别率大大降低了。因为当在原始人脸矩阵上进行判别分析时(我已经在包含 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个回答

2

由于我们无法看到您矩阵的大小,因此帮助您并不容易。

至少在您不再使用变量(例如A)后,可以使用Matlab的clear命令清除它。

也许您可以在分配A变量时使用single()命令,而不是在每个方程中都使用它。

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

然后。
L = A'*A;  

你可以使用Matlab内存使用分析器来查看内存需求。另一个选择是使用稀疏矩阵或将数据类型缩小到uint8等更小的类型(如果适用)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接