GFOR setup

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

GFOR setup

Postby GBenito » Fri Dec 13, 2013 6:50 pm

Hi,

I've been reading the GFOR tutorial without luck on this matter, so i decided to ask here...

I'm trying to use a GFOR loop, with for loops within (no interdependence between inner loops), so i write this:

Code: Select all
   af::gfor(array i, image_W){
           for (int j=0, j<image_H, j++){
                   for (int m=0, m<image_W, m++){
                           for (int n=0, n<image_H, n++){
                                   MatDist(m,n)=sqrt(pow((float)(m-i),2)+pow((float)(n-j),2));
                           }
                   }
           }
   }


but i always get this error message for "expecting an identifier" at the af::gfor(array i, image_W){ line (by the way, image_W is an integer)

do I need to explicitly define GFOR in my program? isn´t including af/gfor.h enough? what else is needed to set GFOR up correctly?
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Sat Dec 14, 2013 9:22 pm

Hi

Can you please provide the CPU code that you have for this part so that I can better understand the goal of this segment of the code.
As for defining gfor, you do not need to. You do not need to include any other files except for arrayfire.h. All files (except util.h) are included in arrayfire.h so you do not need to include them in your code.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Mon Dec 16, 2013 11:39 am

Hello,

Here´s the contextual code for that function:

Code: Select all

   af::array Avg = af::mean(MatRes,2);
        printf("\nDimensiones de Avg:\n");
   printf("%d,%d\n",Avg.dims(0),Avg.dims(1));

   af::array Maxi= af::constant(0,image_H,image_W);
   af::array MatAng= af::constant(0,image_H,image_W);
   max(Maxi, MatAng, MatRes,2);

   MatAng=MatAng.as(af::f32);
   printf("\nDimensiones de Maxi:\n");
   printf("%d,%d\n",Maxi.dims(0),Maxi.dims(1));

   printf("\nDimensiones de MatAng:\n");
   printf("%d,%d\n",MatAng.dims(0),MatAng.dims(1));

   float maxAvg;
   float maxMaxi;
   float maxMatAng;
   float minMatAng;
   int tempind;
   max(&maxAvg,&tempind,Avg);
   max(&maxMaxi,&tempind,Maxi);

//Calculo de Confianza/////////////////////////////////////
//   af::array Conf= af::constant(0,image_H,image_W);
//   af::array pase= af::anytrue(Maxi != 0,2);

//   printf("\nDimensiones de pase:\n");
//   printf("%d,%d\n",pase.dims(0),pase.dims(1));

//   print(pase);
//   Conf = Avg / Maxi;
///////////////////////////////////////////////////////////
//   af::array Vot= Conf > 0.3;

//   print(Avg(1,af::span));
//   print(Maxi(1,af::span));
   //print(Conf);//(1,af::span));
//////////////////////////////////////////////////////////
//RADIO DE VOTACION
   float diagonal = sqrt(pow((float)image_W,2)+pow((float)image_H,2));
   float radio=(float)0.35 * diagonal;

   printf("\n%f\n",diagonal);

   af::array MatVot=af::constant(0,image_H,image_W);
   af::array MatDist=af::constant(0,image_H,image_W);

   af::gfor(array i, image_W){
      for (int j=0, j<image_H, j++){
         for (int m=0, m<image_W, m++){
            for (int n=0, n<image_H, n++){
               MatDist(m,n)=sqrt(pow((float)(m-i),2)+pow((float)(n-j),2));
            }
         }
      
      }
   }


The purpose of that part is to create an mxn matrix for each element [i,j] with the distance of every other position to such element. This is beacause I have to do a voting system which takes into account the points under a certain range of a pixel.
I also have to create a matrix for each pixel with the relative angles of the other points. It's purpose is that I compare the relative angles with the texture angle (obtained with Gabor filtering) within a certain range to a pixel, so that i sum up the number of points "aiming" towards that pixel...

Don't know if this helps you enough to understand it's purpose...
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Mon Dec 16, 2013 12:09 pm

Hi

As I understand it, you are trying to create a permutation of every pixel with every other pixel (for some distance calculation). Is this correct? If I misunderstand, is this a common algorithm I can read about on the internet or do you have any documentation for this?
Looking at your code, the first thing I notice is that MatDist is only 2D and hence it will be overwritten for every i,j values. Hence only the last values of m,n will be stored.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Mon Dec 16, 2013 12:44 pm

Hi,

It's just as you said, a permutation of every pixel by every other, in order to get the distance between each other. I made that algorithm so there's no documentation on the internet.
What i can provide you with, is the Matlab function I made first and which serves as reference for this algorithm i'm writing...

It's also true that being a 2D array, MatDist would be overwriten on each iteration, but that would be ok since, actually, the voting process goes on within the for loop of "j", after calculating MatDist, and the result is only one number that goes into an element of a 2D matrix called MatVot, so there's no problem with overwriting MatDist (although, as i read in GFOR documentation, this could be inconvenient to use a GFOR loop)...

Here's the matlab algorithm:

Code: Select all
 function [MatVot,MatAng]=votacionIII(I,MatRes,k,OrientacionI)
    J=double(rgb2gray(I));
    Avg=mean(MatRes,3);
    [Maxi,MatAng]=max(MatRes,[],3);
    [Conf]=confianza(Maxi,Avg,J);

    Filtro_orientacion=zeros(size(OrientacionI,1),size(OrientacionI,2));
   
    %Esta función le asigna un 1 al pixel que posee suficiente confianza en su
    %orientación de textura, para que vote por un candidato a punto de fuga.
    %Asigna 0 a los que no posean confianza suficiente (> 0.3)
   
    Vot=(Conf>0.3);

    %construir matriz de votación
    MatVot=zeros(size(J,1),size(J,2));

    %radio de disco de votación a partir de diagonal de la imagen
    diagonal=sqrt(size(J,1)^2+size(J,2)^2);
    radio=.35*diagonal;

    %analizar punto por punto los votantes
    for x=1+k/2:size(J,1)-k/2
        for y=1+k/2:size(J,2)-k/2
            if Vot(x,y)==0
                %MatVot=MatVot;
            else
               
                %Esta función genera dos matrices por cada punto de la imagen ingresado,
                %una con las distancias de cada punto votante con posición inferior respecto
                %al candidato evaluado; y otra con los ángulos relativos de dichos votantes
                %respecto del candidato.
                MatDist=zeros(size(J,1),size(J, 2));
                for m=1:size(J,1)
                    for n=1:size(J,2)
                        MatDist(m,n)=sqrt((m-x)^2+(n-y)^2);
                    end
                end
                Filtro_orientacion(:,:)=circshift(OrientacionI(:,:),[nearest(-(size(J,1)-x)) nearest(-(size(J,2)-y))]);
                Filtro_orientacion(:,size(J,2)+1:size(Filtro_orientacion,2))=[];
                Filtro_orientacion(x:size(Filtro_orientacion,1), : ) = 0;
                               
                %matriz de filtro por distancia y por votantes confiables
                FiltDist=(MatDist<=radio);%
           
                %normalizacion de la matriz de distancias
                MatDist=MatDist/diagonal;
                MatDist3=(1+MatDist).^(-1);

                %FiltPoint=(MatDist~=0);

                %matriz de votos emitidos por el punto
           
                MatVot=MatVot + (FiltDist & Filtro_orientacion==MatAng(x,y)) .* MatDist3; %

                %Limpieza de variables propias de cada candidato
                clear MatDist AngRel epsylon FiltDist Filtro_orientacion;
           
            end
        end
    end
imshow(mat2gray(MatVot));

    %Esta función establece el grado de confianza que hay en la estimación de la orientación de textura de cada pixel de la imagen. Tomando la media de las respuestas de cada punto, dividiendolas por la norma y luego restando ese resultado a 1.
    function [Conf]=confianza(Maxi,Avg,J)
        Conf=zeros(size(J,1),size(J,2));
    for i=1:size(J,1)
        for j=1:size(J,2)
            if Maxi(i,j)==0
                Conf(i,j)=0;
            else
                Conf(i,j)=1-Avg(i,j)./Maxi(i,j);
            end
        end
    end
    %Limpieza de variables
    clear Avg i j;
   
    end   

 end
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby pavanky » Mon Dec 16, 2013 12:46 pm

Hi,

Can you use "[code]" and "[\code]" instead of "[b]" and "[\b]"
Pavan Yalamanchili,
ArrayFire
--
~ If it is not broken, you have not tried hard enough ~
User avatar
pavanky
Site Admin
 
Posts: 1123
Joined: Mon Mar 15, 2010 7:39 pm
Location: Atlanta, GA

Re: GFOR setup

Postby GBenito » Mon Dec 16, 2013 5:58 pm

Hi,

Of course! I'm sorry, hadn't seen it above....

Here's the Arrayfire code:

Code: Select all
   af::array Avg = af::mean(MatRes,2);
        printf("\nDimensiones de Avg:\n");
   printf("%d,%d\n",Avg.dims(0),Avg.dims(1));

   af::array Maxi= af::constant(0,image_H,image_W);
   af::array MatAng= af::constant(0,image_H,image_W);
   max(Maxi, MatAng, MatRes,2);

   MatAng=MatAng.as(af::f32);
   printf("\nDimensiones de Maxi:\n");
   printf("%d,%d\n",Maxi.dims(0),Maxi.dims(1));

   printf("\nDimensiones de MatAng:\n");
   printf("%d,%d\n",MatAng.dims(0),MatAng.dims(1));

   float maxAvg;
   float maxMaxi;
   float maxMatAng;
   float minMatAng;
   int tempind;
   max(&maxAvg,&tempind,Avg);
   max(&maxMaxi,&tempind,Maxi);

//Calculo de Confianza/////////////////////////////////////
//   af::array Conf= af::constant(0,image_H,image_W);
//   af::array pase= af::anytrue(Maxi != 0,2);

//   printf("\nDimensiones de pase:\n");
//   printf("%d,%d\n",pase.dims(0),pase.dims(1));

//   print(pase);
//   Conf = Avg / Maxi;
///////////////////////////////////////////////////////////
//   af::array Vot= Conf > 0.3;

//   print(Avg(1,af::span));
//   print(Maxi(1,af::span));
   //print(Conf);//(1,af::span));
//////////////////////////////////////////////////////////
//RADIO DE VOTACION
   float diagonal = sqrt(pow((float)image_W,2)+pow((float)image_H,2));
   float radio=(float)0.35 * diagonal;

   printf("\n%f\n",diagonal);

   af::array MatVot=af::constant(0,image_H,image_W);
   af::array MatDist=af::constant(0,image_H,image_W);

   af::gfor(array i, image_W){
      for (int j=0, j<image_H, j++){
         for (int m=0, m<image_W, m++){
            for (int n=0, n<image_H, n++){
               MatDist(m,n)=sqrt(pow((float)(m-i),2)+pow((float)(n-j),2));
            }
         }
     
      }
   }


And here's the Matlab code:

Code: Select all
 function [MatVot,MatAng]=votacionIII(I,MatRes,k,OrientacionI)
    J=double(rgb2gray(I));
    Avg=mean(MatRes,3);
    [Maxi,MatAng]=max(MatRes,[],3);
    [Conf]=confianza(Maxi,Avg,J);

    Filtro_orientacion=zeros(size(OrientacionI,1),size(OrientacionI,2));
   
    %Esta función le asigna un 1 al pixel que posee suficiente confianza en su
    %orientación de textura, para que vote por un candidato a punto de fuga.
    %Asigna 0 a los que no posean confianza suficiente (> 0.3)
   
    Vot=(Conf>0.3);

    %construir matriz de votación
    MatVot=zeros(size(J,1),size(J,2));

    %radio de disco de votación a partir de diagonal de la imagen
    diagonal=sqrt(size(J,1)^2+size(J,2)^2);
    radio=.35*diagonal;

    %analizar punto por punto los votantes
    for x=1+k/2:size(J,1)-k/2
        for y=1+k/2:size(J,2)-k/2
            if Vot(x,y)==0
                %MatVot=MatVot;
            else
               
                %Esta función genera dos matrices por cada punto de la imagen ingresado,
                %una con las distancias de cada punto votante con posición inferior respecto
                %al candidato evaluado; y otra con los ángulos relativos de dichos votantes
                %respecto del candidato.
                MatDist=zeros(size(J,1),size(J, 2));
                for m=1:size(J,1)
                    for n=1:size(J,2)
                        MatDist(m,n)=sqrt((m-x)^2+(n-y)^2);
                    end
                end
                Filtro_orientacion(:,:)=circshift(OrientacionI(:,:),[nearest(-(size(J,1)-x)) nearest(-(size(J,2)-y))]);
                Filtro_orientacion(:,size(J,2)+1:size(Filtro_orientacion,2))=[];
                Filtro_orientacion(x:size(Filtro_orientacion,1), : ) = 0;
                               
                %matriz de filtro por distancia y por votantes confiables
                FiltDist=(MatDist<=radio);%
           
                %normalizacion de la matriz de distancias
                MatDist=MatDist/diagonal;
                MatDist3=(1+MatDist).^(-1);

                %FiltPoint=(MatDist~=0);

                %matriz de votos emitidos por el punto
           
                MatVot=MatVot + (FiltDist & Filtro_orientacion==MatAng(x,y)) .* MatDist3; %

                %Limpieza de variables propias de cada candidato
                clear MatDist AngRel epsylon FiltDist Filtro_orientacion;
           
            end
        end
    end
imshow(mat2gray(MatVot));

    %Esta función establece el grado de confianza que hay en la estimación de la orientación de textura de cada pixel de la imagen. Tomando la media de las respuestas de cada punto, dividiendolas por la norma y luego restando ese resultado a 1.
    function [Conf]=confianza(Maxi,Avg,J)
        Conf=zeros(size(J,1),size(J,2));
    for i=1:size(J,1)
        for j=1:size(J,2)
            if Maxi(i,j)==0
                Conf(i,j)=0;
            else
                Conf(i,j)=1-Avg(i,j)./Maxi(i,j);
            end
        end
    end
    %Limpieza de variables
    clear Avg i j;
   
    end   

 end
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby GBenito » Wed Dec 18, 2013 11:22 am

Hi,

Any leads on what could be making my VS2010 tell me "Error: expected an identifier" every time I try to write a GFOR loop?

I've tried to write another GFOR loop for another part getting the same results....
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Wed Dec 18, 2013 4:45 pm

Hi

Can you please try the following code to replace the gfor segment:
Code: Select all
array rows = tile(seq(MatDist.dims(0)), 1, MatDist.dims(1));
array cols = tile(array(seq(MatDist.dims(1))).T(), MatDist.dims(0));

for (int i=0, i<image_W, i++){
            for (int j=0, j<image_j, j++){
                   MatDist = sqrt(pow2(rows - i) + pow2(cols - j));
       }
}


Note, the rows and columns arrays are probably going to stay constant throughout.

Let me know if this works out.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Thu Dec 19, 2013 6:11 pm

Hi,

Well!! now i can't even test your proposal...seems arrayfire is failing...

I'll tell i'm using the free version, and when i try to execute the code i've been using so far (no Gfor loop included, just the part that worked fine), the program stops and pops the message of Debug Error...

code R6010:
- abort() has been called

Any ideas on this??
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Thu Dec 19, 2013 6:17 pm

Are you using GFor around the double for loop I had sent?
Can you send the section of the code you are working with right now?
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Fri Dec 20, 2013 11:40 am

Hi,

No i'm not using gfor loops. Right at the moment i can't run even the previous working version of my program (the one prior to writing the for loops).

Can this be related to Arrayfire 2.0 release?
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby GBenito » Tue Jan 21, 2014 8:02 pm

Hello,

I have tried your suggestion, but the result does not seem to comply with what I want it to do...

I tried that formula for one element and displaying MatDist to see how it calculates distance, doing as follows:

Code: Select all
//   for (int i=0; i < image_W; i++){
//         for (int j=0; j < image_H; j++){
            MatDist = sqrt(pow2(rows - 48) + pow2(cols - 64));
//         }
//   }

   print(MatDist(1,af::span));


Note that the for loop iterations are commented so they do not execute...

but that didn't return the correct values...
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Wed Jan 22, 2014 11:14 am

Is it possible for you to send a sample input and output data the segment of code you are trying to port?
You can upload it as an attachment.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Thu Jan 23, 2014 6:28 pm

Hi,

I don't understand very well what you want me to send.

You told me to try replacing gfor loop with:

Code: Select all
   af::array rows = af::tile(af::seq(MatDist.dims(0)), 1, MatDist.dims(1));
   af::array cols = af::tile(af::array(af::seq(MatDist.dims(1))).T(), MatDist.dims(0));

   for (int i=0; i < image_W; i++){
         for (int j=0; j < image_H; j++){
            MatDist = sqrt(pow2(rows - 48) + pow2(cols - 64));
         }
   }


what i'm trying to generate in each iteration, is a matrix MatDist which holds the lineal distances of the rest of the elements in the image to the [i,j] element.

So, for example, if I execute the following line:

Code: Select all
MatDist = sqrt(pow2(rows - 48) + pow2(cols - 64));


I'm working on the [48, 64] element (considering "1-based indexing"), and if i tried to print on screen the last row of MatDist with:

Code: Select all
print(MatDist(1,af::span));


I should be getting a vector with:

[64 63 62 ..... 3 2 1 0]

but what i'm getting is as seen in the attachment

captura de pantalla.jpg
captura de pantalla.jpg (109.41 KiB) Viewed 35163 times
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: GFOR setup

Postby shehzan » Thu Jan 23, 2014 7:05 pm

Hi

I apologize for my mistake. I used pow2 instead of pow(x, n).

Try this code and see if it works for you. I have tested it for linear distances on my end.

Code: Select all
array MatDist = constant(0, 12, 16);
array rows = tile(seq(MatDist.dims(0)), 1, MatDist.dims(1));
array cols = tile(array(seq(MatDist.dims(1))).T(), MatDist.dims(0));
int i = 6, j = 10;
MatDist = sqrt(pow(rows - i, 2) + pow(cols - j, 2));
print(MatDist);


Here is the output for the code above:
Code: Select all
MatDist [12 16] =
       11.6619    10.8167    10.0000     9.2195     8.4853     7.8102     7.2111     6.7082     6.3246     6.0828     6.0000     6.0828     6.3246     6.7082     7.2111     7.8102
       11.1803    10.2956     9.4340     8.6023     7.8102     7.0711     6.4031     5.8310     5.3852     5.0990     5.0000     5.0990     5.3852     5.8310     6.4031     7.0711
       10.7703     9.8489     8.9443     8.0623     7.2111     6.4031     5.6569     5.0000     4.4721     4.1231     4.0000     4.1231     4.4721     5.0000     5.6569     6.4031
       10.4403     9.4868     8.5440     7.6158     6.7082     5.8310     5.0000     4.2426     3.6056     3.1623     3.0000     3.1623     3.6056     4.2426     5.0000     5.8310
       10.1980     9.2195     8.2462     7.2801     6.3246     5.3852     4.4721     3.6056     2.8284     2.2361     2.0000     2.2361     2.8284     3.6056     4.4721     5.3852
       10.0499     9.0554     8.0623     7.0711     6.0828     5.0990     4.1231     3.1623     2.2361     1.4142     1.0000     1.4142     2.2361     3.1623     4.1231     5.0990
       10.0000     9.0000     8.0000     7.0000     6.0000     5.0000     4.0000     3.0000     2.0000     1.0000     0.0000     1.0000     2.0000     3.0000     4.0000     5.0000
       10.0499     9.0554     8.0623     7.0711     6.0828     5.0990     4.1231     3.1623     2.2361     1.4142     1.0000     1.4142     2.2361     3.1623     4.1231     5.0990
       10.1980     9.2195     8.2462     7.2801     6.3246     5.3852     4.4721     3.6056     2.8284     2.2361     2.0000     2.2361     2.8284     3.6056     4.4721     5.3852
       10.4403     9.4868     8.5440     7.6158     6.7082     5.8310     5.0000     4.2426     3.6056     3.1623     3.0000     3.1623     3.6056     4.2426     5.0000     5.8310
       10.7703     9.8489     8.9443     8.0623     7.2111     6.4031     5.6569     5.0000     4.4721     4.1231     4.0000     4.1231     4.4721     5.0000     5.6569     6.4031
       11.1803    10.2956     9.4340     8.6023     7.8102     7.0711     6.4031     5.8310     5.3852     5.0990     5.0000     5.0990     5.3852     5.8310     6.4031     7.0711
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: GFOR setup

Postby GBenito » Fri Jan 24, 2014 5:53 pm

Hi,

Can't believe I didn't pay attention to that either!

Guess the saying "By searching the ant, you missed the elephant" applies quite right to me hehe...


Works perfectly now, thanks a lot for your time.
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm


Return to [archive-commercial] Programming & Development with ArrayFire

cron