I am doing convolution operation for image. I have a problem in data stored in array pointer dynamically. The data in one of the array pointer variable is getting changed automatically after fopen
function.
I am getting array pointer from function called maxpool1
, then i am passing this array to conv2d2
convolution function.
Convolution function:
float* Conv2d2(float (*ptr)[1][96][27][27],int filters,int kernel,int strides,int prevFilters, int prevOutput_shape){
FILE *myFile;
FILE *Weight_file;
int i,j,k,l,m;
int output_shape;
int moveX = 0;
int moveY = 0;
output_shape = ceil(((prevOutput_shape-kernel)/strides+1) );//important is to select height
// load Weights Values from CSV Files
char buffer2[65536] ; //specifying it without pointer(like in conv1) produce error
char *record2,*line2;
int a=0,b=0,c=0,p=0;
Weight_file = fopen("SecondLayer.csv","r");
if(Weight_file == NULL)
{
printf("\n file opening failed ");
//return -1 ;
}
while((line2=fgets(buffer2,sizeof(buffer2),Weight_file))!=NULL)
{
record2 = strtok(line2,",");
while(record2 != NULL)
{
Weightsecond[a][b][p][c++] = atof(record2) ;
record2 = strtok(NULL,",");
}
p++;
c=0;
if(p==prevFilters){
p=0;
b++;
}
if(b==kernel){
b=0;
a++;
}
}
//calculating feature map by moving kernel window on the image and writing feature map to text file
myFile = fopen("featureMapSecond.txt","w");
float featureVector[1][filters][output_shape][output_shape] ;
for (m = 0;m < filters ;m++){ //loop for filters
for (k = 0;k < output_shape; k++){ //loop for Y-featuremap
for (l=0; l < output_shape; l++){ //loop for X-featuremap
float featureMapValue = 0;
for(p=0;p<prevFilters;p++){ //loop for filters in previous input
for (i = 0; i < kernel; i++){ //loop for Y-kernel
for (j = 0; j < kernel; j++){ //loop for X-kernel
featureMapValue = Weightsecond[i][j][p][m] * ptr[0][0][p][i+moveY][j+moveX] + featureMapValue;
}
}
if(p==72){
int temp=0;
}
}
fprintf(myFile,"%f\n", featureMapValue );
featureVector[0][m][k][l] = featureMapValue;
if (l != output_shape-1){
moveX = moveX + strides;}
else {moveX = 0 ;}
}
if (k != output_shape-1){
moveY = moveY + strides;}
else {moveY = 0 ;}
}
}
fclose(myFile);
float (*featureVectorSecond)[1][filters][output_shape][output_shape] = featureVector;
return featureVectorSecond;
}
Main function:
int main(){
Image image_maiz;
Image image_maiz_gray;
Image_load(&image_maiz, "testMaize.jpg");
ON_ERROR_EXIT(image_maiz.data == NULL, "Error in loading the image");
Image_to_gray(&image_maiz,&image_maiz_gray);
printf("width=%d ,height=%d, channel=%d",image_maiz_gray.width,image_maiz_gray.height,image_maiz_gray.channels);
Image_save(&image_maiz_gray, "gray.jpg");
float (*featureVector)[1][96][55][55] = Conv2dInput(&image_maiz_gray,96,11,4);
featureVector=ActivationRelu1(featureVector,96,55);
float (*featureVectorMaxPool1)[1][96][27][27]=MaxPooling1(featureVector,3,2,96,55);
//float *featureVectorMaxPool1=MaxPooling1(featureVector,3,2,96,55);
free(featureVector);
float (*featureVector2)[1][256][23][23] = NULL;
featureVector2=Conv2d2(featureVectorMaxPool1,256,5,1,96,27);
}
MaxPool1function:
float* MaxPooling1(float (*ptr)[1][96][55][55],int kernel,int strides,int prevFilters,int prevOutput_shape){
int height=prevOutput_shape;
int filters=prevFilters;
float max=0;
FILE *myFile;
int moveX=0,moveY=0;
int output_shape = ceil(((height-kernel)/strides+1) );
float featureVector[1][filters][output_shape][output_shape];
myFile = fopen("featureMapMaxpool1.txt","w");
for(int m=0;m<filters;m++){
for(int i =0;i<output_shape;i++){
for(int j=0;j<output_shape;j++){
for(int k=0;k<kernel;k++){
for(int l=0;l<kernel;l++){
if (*(*(*(*(*(ptr + 0)+0)+m)+k)+l)>max){
max=*(*(*(*(*(ptr + 0)+0)+m)+(k+moveY))+(l+moveX));
}
}
}
featureVector[0][m][i][j]=max;
fprintf(myFile,"%f\n", max );
max=0;
if (j != output_shape-1){
moveX = moveX + strides;}
else {moveX = 0 ;}
}
if (i != output_shape-1){
moveY = moveY + strides;}
else {moveY = 0 ;}
}
}
fclose(myFile);
float (*featureVectorMaxPool)[1][filters][output_shape][output_shape] = malloc(filters*output_shape*output_shape);
featureVectorMaxPool = featureVector;
return featureVectorMaxPool;
}
The last two lines of MaxPooling1 are
featureVectorMaxPool = featureVector; return featureVectorMaxPool;
. Earlier in that function, featureVector is defined as a local variable (allocated on the stack). When that function is returned and anther function is called, the stack memory used by MaxPooling1 for featureVector is used for the local variables of the other function. In this case that other function is Conv2d2 and its local variable Weight_file sits in stack memory that MaxPooling1 used for featureVector.