Files
stm32-cnn/MY/my.c
2024-11-01 22:38:48 +08:00

402 lines
13 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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<EFBFBD>ļ<EFBFBD>[%s]<5D><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>\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<EFBFBD>ļ<EFBFBD>[%s]<5D><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>\r\n",_path);
return 200;
}
DEBUG_PRINT("\r\n<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>%s\r\n",_path);
if(_model -> array == NULL){
DEBUG_PRINT("\r\n<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD>ȡģ<EFBFBD>Ͳ<EFBFBD><EFBFBD><EFBFBD>[%s]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",_model -> name);
return 201;
}
DEBUG_PRINT("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>%s\r\n",_model -> name);
printf("\r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>%s<><73><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD>......\r\n",_model -> name);
while(_larr < _model -> maxlength){
res=f_read(file,fatbuf,11,&br);
if(res){
DEBUG_PRINT("<EFBFBD><EFBFBD>ȡ[%s]<5D>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>:%d\r\n",_path,res);
return 202;
}
else{
for(u32 i=0;i<br;i++){
if(fatbuf[i]==0x0d){
float value = atof(_tmp);
memset(_tmp, 0, sizeof(_tmp));
_model -> array[_larr++] = value;
//DEBUG_PRINT("<22>س<EFBFBD>[%d] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[string]: %s\r\n",i,_tmp);
//DEBUG_PRINT("<22>س<EFBFBD>[%d] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[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<72><72><EFBFBD><EFBFBD>ֵΪģ<CEAA><C4A3><EFBFBD><EFBFBD><EFBFBD>󳤶<EFBFBD>
DEBUG_PRINT("\r\n[%s]<5D><>ģ<EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>! ģ<>ͳ<EFBFBD><CDB3><EFBFBD>Ϊ %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<EFBFBD>ļ<EFBFBD>[%s]<5D><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>\r\n",_path);
return 200;
}
DEBUG_PRINT("\r\n<EFBFBD>µ<EFBFBD>data<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>%s\r\n",_path);
model_write(data_name);
printf("<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD> %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("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return 7;
}
sprintf(_path, "0:/%s.txt",model_name);
f_open(file,(const TCHAR*)_path,1);//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
while(_larr <= length){
res=f_read(file,fatbuf,11,&br);
if(res)DEBUG_PRINT("\r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:%d\r\n",res);
else{
for(u32 i=0;i<br;i++){
if(fatbuf[i]==0x0d){
float value = atof(_tmp);
memset(_tmp, 0, sizeof(_tmp));
floatArray[_larr++] = value;
DEBUG_PRINT("<EFBFBD>س<EFBFBD>[%d] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[string]: %s\r\n",i,_tmp);
DEBUG_PRINT("<EFBFBD>س<EFBFBD>[%d] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[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]);
}
}
}
}
for (int i=0;i<((length/gap)+(length%gap ? 2 : 1));i++)
printf("\r\nfloatArray[%d]: %f",i*gap<=length ? i*gap : length,i*gap<=length ? floatArray[i*gap] : floatArray[length]);
printf("\r\n");
return res;
}
void* model(char* model_name){
if(strcmp(model_name, "conv1_bias") == 0)return &conv1_bias;
else if(strcmp(model_name, "conv1_weight") == 0)return &conv1_weight;
else if(strcmp(model_name, "conv2_bias") == 0)return &conv2_bias;
else if(strcmp(model_name, "conv2_weight") == 0)return &conv2_weight;
else if(strcmp(model_name, "conv3_bias") == 0)return &conv3_bias;
else if(strcmp(model_name, "conv3_weight") == 0)return &conv3_weight;
else if(strcmp(model_name, "fc1_bias") == 0)return &fc1_bias;
else if(strcmp(model_name, "fc1_weight") == 0)return &fc1_weight;
else if(strcmp(model_name, "fc2_bias") == 0)return &fc2_bias;
else if(strcmp(model_name, "fc2_weight") == 0)return &fc2_weight;
else if(strcmp(model_name, "data") == 0)return &data;
return NULL;
}
void model_info(char* model_name){
if(strcmp(model_name, "all") == 0){
model_info("conv1_bias");
model_info("conv1_weight");
model_info("conv2_bias");
model_info("conv2_weight");
model_info("conv3_bias");
model_info("conv3_weight");
model_info("fc1_bias");
model_info("fc1_weight");
model_info("fc2_bias");
model_info("fc2_weight");
model_info("data");
}else {
Model *_model = model(model_name);
printf("\r\nmodel.name is: %s\r\n",_model -> 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;
}