Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeDecodeError #7

Open
ngz-sun opened this issue Oct 12, 2024 · 6 comments
Open

UnicodeDecodeError #7

ngz-sun opened this issue Oct 12, 2024 · 6 comments

Comments

@ngz-sun
Copy link

ngz-sun commented Oct 12, 2024

Hello, may I ask that this error occurs during the conversion? How should I solve it:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 25: invalid start byte?

@mlomb
Copy link
Owner

mlomb commented Oct 12, 2024

Hey!

  • What platfrom export are you using?
  • Can you check if the file is corrupted? (Looking at it and in an online UTF8 checker)

I just realized this is the onnx2code repo, not other of mine, sorry.

Can you share the model?

@ngz-sun
Copy link
Author

ngz-sun commented Oct 14, 2024

Thank you very much for your reply. This problem has been solved. However, I would like to ask, if I want to customize some operations, such as operations of modules like TCN and LSTM, then how should I do it? Also, I wonder if the author is still maintaining this project and will there be updates of other modules in the future? If I want to customize a model, how should I operate? Is there any relevant documentation for learning?

@mlomb
Copy link
Owner

mlomb commented Oct 14, 2024

Glad you fixed it.

This was a university project so it was very limited in scope. We implemented few operators and have no plans to continue further. As you can guess, there is no documentation apart from what you can find in the code. The closest you can get is the final writeup but it is in Spanish 😬.

If you want to implement your own operators, you can add them inside the ops folder, use as reference any of the existing ones. Ops are auto registered.

There are real world projects that compile directly to assembly (not C) that implement lots of operators. For example, glow.

@ngz-sun
Copy link
Author

ngz-sun commented Oct 14, 2024

Hi, I have a question for you. Test after generating C++ code, using g++ test, error reported AppData\Local\Temp\tmpfrj9poky\model-asm.o: No such file or directory. What is the reason for this

@ngz-sun
Copy link
Author

ngz-sun commented Oct 14, 2024

My test code
`#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "model.h"
float* readWeights(const char* filename, size_t size) {
FILE* file = fopen(filename, "rb");
if (!file) {
perror("Unable to open file");
return NULL;
}
float* weights = (float*)malloc(size * sizeof(float));
if (!weights) {
perror("Memory allocation failed");
fclose(file);
return NULL;
}
fread(weights, sizeof(float), size, file);
fclose(file);
return weights;
}
float* readInputData(const char* filename, int inputSize) {
FILE* file = fopen(filename, "r");
if (!file) {
perror("Unable to open file");
return NULL;
}
float* inputs = (float*)malloc(inputSize * sizeof(float));
if (!inputs) {
perror("Memory allocation failed");
fclose(file);
return NULL;
}
int count = 0;
while (count < inputSize && !feof(file)) {
if (fscanf(file, "%f", &inputs[count]) != 1) {
break;
}
count++;
}
fclose(file);
if (count < inputSize) {
printf("Warning: Only read %d lines, expected %d\n", count, inputSize);
}
return inputs;
}
int main() {
const int inputSize = 7800;
const int outputSize = 1 * 10;
const int weightsSize = 439818;
float* inputs = readInputData("mfcc_data.txt", inputSize);
float* outputs = (float*)malloc(outputSize * sizeof(float));
float* weights = readWeights("weights.bin", weightsSize);

printf("Input Data:\n");
for (int i = 0; i < 10; i++) {
    printf("%f\n", inputs[i]);
}
if (!inputs || !outputs || !weights) {
    printf("Memory allocation failed\n");
    return 1;
}
inference(weights, inputs, outputs);
printf("Output:\n");
float maxVal = outputs[0]; 
int maxIndex = 0;
for (int i = 0; i < outputSize; i++) {
    printf("%f\n", outputs[i]);
    if (outputs[i] > maxVal) {
        maxVal = outputs[i];
        maxIndex = i; 
    }
}
printf("Max value: %f, Index: %d\n", maxVal, maxIndex);
free(inputs);
free(outputs);
free(weights);
return 0;

}![screenshot-20241014-165207](https://github.com/user-attachments/assets/951137e5-1902-4fb4-8e98-e5ccd7bc0f4f) **My onnx export** import torch
import onnx
from model_train import Net
model = Net(num_classes=10)
weights_path = 'best_model_30.pth'
model.load_state_dict(torch.load(weights_path))
model.eval()
dummy_input = torch.randn(1, 1, 600, 13)
onnx_save_path = 'weights/best_model.onnx'
torch.onnx.export(model,
dummy_input,
onnx_save_path,
export_params=True,
opset_version=11,
)
model_onnx = onnx.load(onnx_save_path)
onnx.checker.check_model(model_onnx)
print(f"Model has been successfully exported to {onnx_save_path}")`
quesion
mfcc_data.txt stores 7800 rows with shape of (1, 1, 600, 13).flatten() flattened. However, my test result is different from the classification result in pytorch. I would like to ask, is the structure of my test C++ code correct?

@mlomb
Copy link
Owner

mlomb commented Oct 14, 2024

model-asm.o: No such file or directory

You should have an empty model.asm file in the output folder, check that. You should not compile/link that file, no operators were implemented using the .asm file (except for Identity, for testing).

I would like to ask, is the structure of my test C++ code correct

Looks ok, you can check service.c and debugger.c for reference.

my test result is different from the classification result in pytorch

If the model being exported does not report any errors after "Checking model with 3 random inputs", then the C++ inference code is correct. There might be something wrong with your input, check with the files suggested above.

mfcc_data.txt stores 7800 rows with shape of (1, 1, 600, 13).flatten() (...)

I'm not sure how are you saving the file. You can look at how the testing samples are saved:

inputs_np = np.concatenate([inp.reshape(-1) for inp in inputs.values()])
outputs_np = np.concatenate([o.reshape(-1) for o in out2])
inputs_np.tofile(temp_dir / "sample_inputs.bin")
outputs_np.tofile(temp_dir / "sample_outputs.bin")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants