#include "my.h" Model conv1_bias; Model conv1_weight; Model conv2_bias; Model conv2_weight; Model conv3_bias; Model conv3_weight; Model fc1_bias; Model fc1_weight; Model fc2_bias; Model fc2_weight; Model data; float* modelmym_init(char* model_name){ if(conv1_bias.array == NULL && strcmp(model_name, "conv1_bias") == 0) return conv1_bias.array = (float*)mymalloc(SRAMEX, CONV1_BIAS_ARRSIZE * sizeof(float)); else if(conv1_weight.array == NULL && strcmp(model_name, "conv1_weight") == 0) return conv1_weight.array = (float*)mymalloc(SRAMEX, CONV1_WEIGHT_ARRSIZE * sizeof(float)); else if(conv2_bias.array == NULL && strcmp(model_name, "conv2_bias") == 0) return conv2_bias.array = (float*)mymalloc(SRAMEX, CONV2_BIAS_ARRSIZE * sizeof(float)); else if(conv2_weight.array == NULL && strcmp(model_name, "conv2_weight") == 0) return conv2_weight.array = (float*)mymalloc(SRAMEX, CONV2_WEIGHT_ARRSIZE * sizeof(float)); else if(conv3_bias.array == NULL && strcmp(model_name, "conv3_bias") == 0) return conv3_bias.array = (float*)mymalloc(SRAMEX, CONV3_BIAS_ARRSIZE * sizeof(float)); else if(conv3_weight.array == NULL && strcmp(model_name, "conv3_weight") == 0) return conv3_weight.array = (float*)mymalloc(SRAMEX, CONV3_WEIGHT_ARRSIZE * sizeof(float)); else if(fc1_bias.array == NULL && strcmp(model_name, "fc1_bias") == 0) return fc1_bias.array = (float*)mymalloc(SRAMEX, FC1_BIAS_ARRSIZE * sizeof(float)); else if(fc1_weight.array == NULL && strcmp(model_name, "fc1_weight") == 0) return fc1_weight.array = (float*)mymalloc(SRAMEX, FC1_WEIGHT_ARRSIZE * sizeof(float)); else if(fc2_bias.array == NULL && strcmp(model_name, "fc2_bias") == 0) return fc2_bias.array = (float*)mymalloc(SRAMEX, FC2_BIAS_ARRSIZE * sizeof(float)); else if(fc2_weight.array == NULL && strcmp(model_name, "fc2_weight") == 0) return fc2_weight.array = (float*)mymalloc(SRAMEX, FC2_WEIGHT_ARRSIZE * sizeof(float)); else if(data.array == NULL && strcmp(model_name, "data") == 0) return data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); else if(strcmp(model_name, "all") == 0){ if(conv1_bias.array == NULL)conv1_bias.array = (float*)mymalloc(SRAMEX, CONV1_BIAS_ARRSIZE * sizeof(float)); if(conv1_weight.array == NULL)conv1_weight.array = (float*)mymalloc(SRAMEX, CONV1_WEIGHT_ARRSIZE * sizeof(float)); if(conv2_bias.array == NULL)conv2_bias.array = (float*)mymalloc(SRAMEX, CONV2_BIAS_ARRSIZE * sizeof(float)); if(conv2_weight.array == NULL)conv2_weight.array = (float*)mymalloc(SRAMEX, CONV2_WEIGHT_ARRSIZE * sizeof(float)); if(conv3_bias.array == NULL)conv3_bias.array = (float*)mymalloc(SRAMEX, CONV3_BIAS_ARRSIZE * sizeof(float)); if(conv3_weight.array == NULL)conv3_weight.array = (float*)mymalloc(SRAMEX, CONV3_WEIGHT_ARRSIZE * sizeof(float)); if(fc1_bias.array == NULL)fc1_bias.array = (float*)mymalloc(SRAMEX, FC1_BIAS_ARRSIZE * sizeof(float)); if(fc1_weight.array == NULL)fc1_weight.array = (float*)mymalloc(SRAMEX, FC1_WEIGHT_ARRSIZE * sizeof(float)); if(fc2_bias.array == NULL)fc2_bias.array = (float*)mymalloc(SRAMEX, FC2_BIAS_ARRSIZE * sizeof(float)); if(fc2_weight.array == NULL)fc2_weight.array = (float*)mymalloc(SRAMEX, FC2_WEIGHT_ARRSIZE * sizeof(float)); if(data.array == NULL)data.array = (float*)mymalloc(SRAMEX, DATA_ARRSIZE * sizeof(float)); } return NULL; } u8 modelmym_free(char* model_name){ if(conv1_bias.array != NULL && strcmp(model_name, "conv1_bias") == 0){ myfree(SRAMEX,conv1_bias.array); conv1_bias.array = NULL; conv1_bias.realength = NULL; return 1; } else if(conv1_weight.array != NULL && strcmp(model_name, "conv1_weight") == 0){ myfree(SRAMEX,conv1_weight.array); conv1_weight.array = NULL; conv1_weight.realength = NULL; return 1; } else if(conv2_bias.array != NULL && strcmp(model_name, "conv2_bias") == 0){ myfree(SRAMEX,conv2_bias.array); conv2_bias.array = NULL; conv2_bias.realength = NULL; return 1; } else if(conv2_weight.array != NULL && strcmp(model_name, "conv2_weight") == 0){ myfree(SRAMEX,conv2_weight.array); conv2_weight.array = NULL; conv2_weight.realength = NULL; return 1; } else if(conv3_bias.array != NULL && strcmp(model_name, "conv3_bias") == 0){ myfree(SRAMEX,conv3_bias.array); conv3_bias.array = NULL; conv3_bias.realength = NULL; return 1; } else if(conv3_weight.array != NULL && strcmp(model_name, "conv3_weight") == 0){ myfree(SRAMEX,conv3_weight.array); conv3_weight.array = NULL; conv3_weight.realength = NULL; return 1; } else if(fc1_bias.array != NULL && strcmp(model_name, "fc1_bias") == 0){ myfree(SRAMEX,fc1_bias.array); fc1_bias.array = NULL; fc1_bias.realength = NULL; return 1; } else if(fc1_weight.array != NULL && strcmp(model_name, "fc1_weight") == 0){ myfree(SRAMEX,fc1_weight.array); fc1_weight.array = NULL; fc1_weight.realength = NULL; return 1; } else if(fc2_bias.array != NULL && strcmp(model_name, "fc2_bias") == 0){ myfree(SRAMEX,fc2_bias.array); fc2_bias.array = NULL; fc2_bias.realength = NULL; return 1; } else if(fc2_weight.array != NULL && strcmp(model_name, "fc2_weight") == 0){ myfree(SRAMEX,fc2_weight.array); fc2_weight.array = NULL; fc2_weight.realength = NULL; return 1; } else if(data.array != NULL && strcmp(model_name, "fc2_weight") == 0){ myfree(SRAMEX,data.array); data.array = NULL; data.realength = NULL; return 1; } else if(strcmp(model_name, "all") == 0){ modelmym_free("conv1_bias"); modelmym_free("conv1_weight"); modelmym_free("conv2_bias"); modelmym_free("conv2_weight"); modelmym_free("conv3_bias"); modelmym_free("conv3_weight"); modelmym_free("fc1_bias"); modelmym_free("fc1_weight"); modelmym_free("fc2_bias"); modelmym_free("fc2_weight"); modelmym_free("data"); return 2; } return NULL; } u8 model_write(char* model_name) { if(strcmp(model_name, "all") == 0){ model_write("conv1_bias"); model_write("conv1_weight"); model_write("conv2_bias"); model_write("conv2_weight"); model_write("conv3_bias"); model_write("conv3_weight"); model_write("fc1_bias"); model_write("fc1_weight"); model_write("fc2_bias"); model_write("fc2_weight"); model_write("data"); model_info("all"); SDRAM_USED(); }else { u8 res=0; u32 _larr = 0; char _tmp[11] = ""; char _path[20] = {0}; int progress; Model *_model = model(model_name); if(_model == NULL){ if(f_open(file,(const TCHAR*) sprintf(_path, "0:/%s.txt",model_name),1)){ DEBUG_PRINT("\r\n文件[%s]打开失败\r\n",_path); return 200; } _model = model("data"); _model -> name = model_name; } if(_model -> array == NULL)modelmym_init(model_name); sprintf(_path, "0:/%s.txt",_model -> name); if(f_open(file,(const TCHAR*)_path,1)){ DEBUG_PRINT("\r\n文件[%s]打开失败\r\n",_path); return 200; } DEBUG_PRINT("\r\n文件名为:%s\r\n",_path); if(_model -> array == NULL){ DEBUG_PRINT("\r\n无法获取模型参数[%s]的数组",_model -> name); return 201; } DEBUG_PRINT("数组名为:%s\r\n",_model -> name); printf("\r\n正在写入%s,请稍后......\r\n",_model -> name); while(_larr < _model -> maxlength){ res=f_read(file,fatbuf,11,&br); if(res){ DEBUG_PRINT("读取[%s]文件的数据出错:%d\r\n",_path,res); return 202; } else{ for(u32 i=0;i array[_larr++] = value; //DEBUG_PRINT("回车[%d] 单行数据是[string]: %s\r\n",i,_tmp); //DEBUG_PRINT("回车[%d] 单行数据是[float]: %f\r\n",i+1,value); }else{ char buffer[2]; sprintf(buffer, "%c", fatbuf[i]); strcat(_tmp, buffer); //DEBUG_PRINT("[%d]: %c\r\n",i,fatbuf[i]); } } if(_model -> maxlength >= 73728 && (_larr % (_model -> maxlength/10) == 0 || _larr >= _model -> maxlength || _larr == 1)) { progress = _larr >= _model -> maxlength ? 100 : _larr == 1 ? 0 : progress + 10; DEBUG_PRINT("\r\n["); for(u16 j=0; j<50;j++){ if(j < progress/2) printf("="); else DEBUG_PRINT(" "); } printf("] %d%%", progress); } } } _model -> realength = _larr; //_larr最大值为模型最大长度 DEBUG_PRINT("\r\n[%s]的模型参数已写入到内存中! 模型长度为 %d\r\n",_model -> name,_model -> realength); return res; } return NULL; } void model_read(char* model_name, u32 gap){ if(strcmp(model_name, "all") == 0){ model_read("conv1_bias", gap); model_read("conv1_weight", gap); model_read("conv2_bias", gap); model_read("conv2_weight", gap); model_read("conv3_bias", gap); model_read("conv3_weight", gap); model_read("fc1_bias", gap); model_read("fc1_weight", gap); model_read("fc2_bias", gap); model_read("fc2_weight", gap); model_read("data", gap); }else { Model *_model = model(model_name); for (int i=0;i<((_model -> realength/gap)+(_model -> realength%gap ? 2 : 1));i++) printf("\r\n%s_floatArray[%d]: %f",_model -> name,i*gap<_model -> realength ? i*gap : _model -> realength-1,i*gap<_model -> realength ? _model -> array[i*gap] : _model -> array[_model -> realength-1]); printf("\r\n"); } } u8 switch_data(char* data_name){ char _path[20] = {0}; if(f_open(file,(const TCHAR*) sprintf(_path, "0:/%s.txt",data_name),1)){ DEBUG_PRINT("\r\n文件[%s]打开失败\r\n",_path); return 200; } DEBUG_PRINT("\r\n新的data文件名为:%s\r\n",_path); model_write(data_name); printf("已切换到 %s.txt",data_name); return 1; } u8 sd_read(u32 length, char* model_name, u32 gap) { u8 res=0; u32 _larr = 0; char _tmp[11] = ""; char _path[20] = {0}; float* floatArray = NULL; Model *_model = model(model_name); if(length >= _model -> maxlength)length = _model -> maxlength -1; if((floatArray = modelmym_init(model_name)) == NULL){ DEBUG_PRINT("初始化出错!!"); return 7; } sprintf(_path, "0:/%s.txt",model_name); f_open(file,(const TCHAR*)_path,1);//打开文件夹 while(_larr <= length){ res=f_read(file,fatbuf,11,&br); if(res)DEBUG_PRINT("\r\n读数据出错了:%d\r\n",res); else{ for(u32 i=0;i name); printf("model.array.address is: 0X%X\r\n",_model -> array); printf("model.maxlength is: %d\r\n",_model -> maxlength); printf("model.realength is: %d\r\n",_model -> realength); } } void model_init(){ conv1_bias.name = "conv1_bias"; conv1_bias.array = modelmym_init(conv1_bias.name); conv1_bias.maxlength = CONV1_BIAS_ARRSIZE; conv1_weight.name = "conv1_weight"; conv1_weight.array = modelmym_init(conv1_weight.name); conv1_weight.maxlength = CONV1_WEIGHT_ARRSIZE; conv2_bias.name = "conv2_bias"; conv2_bias.array = modelmym_init(conv2_bias.name); conv2_bias.maxlength = CONV2_BIAS_ARRSIZE; conv2_weight.name = "conv2_weight"; conv2_weight.array = modelmym_init(conv2_weight.name); conv2_weight.maxlength = CONV2_WEIGHT_ARRSIZE; conv3_bias.name = "conv3_bias"; conv3_bias.array = modelmym_init(conv3_bias.name); conv3_bias.maxlength = CONV3_BIAS_ARRSIZE; conv3_weight.name = "conv3_weight"; conv3_weight.array = modelmym_init(conv3_weight.name); conv3_weight.maxlength = CONV3_WEIGHT_ARRSIZE; fc1_bias.name = "fc1_bias"; fc1_bias.array = modelmym_init(fc1_bias.name); fc1_bias.maxlength = FC1_BIAS_ARRSIZE; fc1_weight.name = "fc1_weight"; fc1_weight.array = modelmym_init(fc1_weight.name); fc1_weight.maxlength = FC1_WEIGHT_ARRSIZE; fc2_bias.name = "fc2_bias"; fc2_bias.array = modelmym_init(fc2_bias.name); fc2_bias.maxlength = FC2_BIAS_ARRSIZE; fc2_weight.name = "fc2_weight"; fc2_weight.array = modelmym_init(fc2_weight.name); fc2_weight.maxlength = FC2_WEIGHT_ARRSIZE; data.name = "data"; data.array = modelmym_init(data.name); data.maxlength = DATA_ARRSIZE; }