微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

fopen之后数组指针的值被更改

如何解决fopen之后数组指针的值被更改

我正在对图像进行卷积运算。我在动态存储在数组指针中的数据有问题。 fopen函数后,数组指针变量之一中的数据将自动更改。

我从名为maxpool1函数获取数组指针,然后将该数组传递给conv2d2卷积函数

previous content

changed content

卷积函数

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;
}

主要功能

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,55);
    float (*featureVectorMaxPool1)[1][96][27][27]=MaxPooling1(featureVector,3,2,55);
    //float *featureVectorMaxPool1=MaxPooling1(featureVector,55);
    free(featureVector);
    float (*featureVector2)[1][256][23][23] = NULL;
    featureVector2=Conv2d2(featureVectorMaxPool1,256,5,1,27);
}

MaxPool1功能

float* MaxPooling1(float (*ptr)[1][96][55][55],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,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;
}

解决方法

MaxPooling1的最后两行是featureVectorMaxPool = featureVector; return featureVectorMaxPool;。在该函数的前面,featureVector被定义为局部变量(分配在堆栈上)。返回该函数并调用另一个函数时,MaxPooling1用于featureVector的堆栈存储器将用于另一个函数的局部变量。在这种情况下,另一个函数是Conv2d2,其局部变量Weight_file位于MaxPooling1用于featureVector的堆栈内存中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。