Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Out of Memory" Matlab

Tags:

matlab

Well I made 4 standalone executable from 4 different Matlab functions to build a face recognition system. I am calling those 4 executables using different batch codes and performing tasks on images. The total number of images I have is above 300k. 3 of these 4 executables is working good, but I am facing "out of memory" problem when I am trying to call the standalone executable of Fisherface function. It simply calculates unique features of each image using Fisher's linear discriminant analysis. The analysis is applied on the huge face matrix which consists of pixel values of over 150,000 images of size 60*60. Hence the size of the matrix is 150,000*3600.

Well what I understand is its happening due to shortage of contiguous memory in RAM. So as a way out, I chose to divide my large image set into number of subsets, each of which contains 3000 images. Now when an input face is provided, it searches for best matches of that input in each of those subset and finally sorts out the final list of 3 best matches with lowest distances (Euclidean). This resolved the out of memory error but the recognition rate became much lower. Because when the discriminant analysis is done in the original face matrix (which I have tested in smaller datasets containing 4000-5000 images), it gives good recognition rate.

I am seeking a way out of this problem. I want to perform all the operations on the large matrix. Is there a way to implement the function more efficiently, for example, allocating memory dynamically in Matlab? I hope I have been fairly specific in order to explain my problem. Below, I have provided the code segment of that particular executable.

    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
like image 479
RookieCoder Avatar asked Nov 01 '22 08:11

RookieCoder


1 Answers

It's not easy to help you, because we can't see the sizes of your matrices.

At least you could use the Matlab clear command after you don't use a variable anymore (e.g. A).

Maybe you could use the single() command when you allocate A variable instead of in every equation.

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

And then

L = A'*A;  

Also you could use the Matlab profiler with memory usage to see your memory demand.

Another option could be to use sparse matrices or reduce to even smaller datatypes like uint8, if appropriate for some data.

like image 146
JaBe Avatar answered Nov 15 '22 10:11

JaBe