|
| 1 | +#include <pxt.h> |
| 2 | +#include "modeltest.h" |
| 3 | +#include "mldataprocessor.h" |
| 4 | +//#include "testdata.h" |
| 5 | +//#include "testoutput.h" |
| 6 | + |
| 7 | +#define DBG_PRINT(...) uBit.serial.printf(__VA_ARGS__) |
| 8 | + |
| 9 | + |
| 10 | +static void runModelTest( |
| 11 | + const ml_actions_t *actions, ml_predictions_t *predictions, |
| 12 | + const float *test_filter, const float *test_model |
| 13 | +) { |
| 14 | + const unsigned int time_start = uBit.systemTime(); |
| 15 | + |
| 16 | + float *modelData = mlDataProcessor.getProcessedData(); |
| 17 | + if (modelData == NULL) { |
| 18 | + DBG_PRINT("Failed to processed data for the model\n"); |
| 19 | + uBit.panic(899); |
| 20 | + } |
| 21 | + const size_t processDataSize = mlDataProcessor.getProcessedDataSize(); |
| 22 | + |
| 23 | + // TODO: Uncomment to send the expected data to the model to check if |
| 24 | + // the model output is as expected |
| 25 | + // modelData = (float *)test_filter; |
| 26 | + |
| 27 | + // Print the processed data, compare it and print the expected values |
| 28 | + DBG_PRINT("Processed data:\t\t"); |
| 29 | + for (size_t i = 0; i < processDataSize; i++) { |
| 30 | + int modelDataValue = modelData[i] * 100000; |
| 31 | + int expectedDataValue = test_filter[i] * 100000; |
| 32 | + DBG_PRINT("%d", modelDataValue); |
| 33 | + if (abs(modelDataValue - expectedDataValue) > abs(expectedDataValue * 0.01)) { |
| 34 | + DBG_PRINT("!"); |
| 35 | + } else { |
| 36 | + DBG_PRINT(" "); |
| 37 | + } |
| 38 | + DBG_PRINT(", ", modelDataValue); |
| 39 | + if ((i +1) % 3 == 0) { |
| 40 | + DBG_PRINT("\t\t"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + DBG_PRINT("\nExpected data:\t\t"); |
| 45 | + for (size_t i = 0; i < processDataSize; i++) { |
| 46 | + int expectedDataValue = test_filter[i] * 100000; |
| 47 | + DBG_PRINT("%d , ", expectedDataValue); |
| 48 | + if ((i +1) % 3 == 0) { |
| 49 | + DBG_PRINT("\t\t"); |
| 50 | + } |
| 51 | + } |
| 52 | + DBG_PRINT("\n"); |
| 53 | + |
| 54 | + bool success = ml_predict(modelData, processDataSize, actions, predictions); |
| 55 | + if (!success) { |
| 56 | + DBG_PRINT("Failed to run model\n"); |
| 57 | + uBit.panic(897); |
| 58 | + } |
| 59 | + |
| 60 | + // Print the model output |
| 61 | + DBG_PRINT("Model output:\t\t"); |
| 62 | + for (size_t i = 0; i < actions->len; i++) { |
| 63 | + DBG_PRINT("%s[%d.%d] ", |
| 64 | + actions->action[i].label, |
| 65 | + (int)(predictions->prediction[i] * 100), |
| 66 | + (int)(predictions->prediction[i] * 1000) % 10); |
| 67 | + } |
| 68 | + DBG_PRINT("\n"); |
| 69 | + |
| 70 | + // Print the expected results and track the maximum difference |
| 71 | + DBG_PRINT("Expected Model:\t\t"); |
| 72 | + int maxModelDiff = 0; |
| 73 | + for (size_t i = 0; i < actions->len; i++) { |
| 74 | + int expectedResult = test_model[i] * 1000; |
| 75 | + int actualResult = predictions->prediction[i] * 1000; |
| 76 | + DBG_PRINT("%s[%d.%d] ", actions->action[i].label, expectedResult / 10, expectedResult % 10); |
| 77 | + maxModelDiff = max(abs(expectedResult - actualResult), maxModelDiff); |
| 78 | + } |
| 79 | + if (maxModelDiff > 0) { |
| 80 | + DBG_PRINT("\nModel output diff:\t%d.%d %%\n", maxModelDiff / 10, maxModelDiff % 10); |
| 81 | + } |
| 82 | + DBG_PRINT("\n\n"); |
| 83 | +} |
| 84 | + |
| 85 | +#pragma GCC push_options |
| 86 | +#pragma GCC optimize ("O0") |
| 87 | +void testModel(const ml_actions_t *actions, ml_predictions_t *predictions) { |
| 88 | + if (mlDataProcessor.getProcessedDataSize() != ML_TEST_FILTER_OUTPUT_SIZE) { |
| 89 | + DBG_PRINT("Invalid processed data size: %d\n", mlDataProcessor.getProcessedDataSize()); |
| 90 | + uBit.panic(890); |
| 91 | + } |
| 92 | + |
| 93 | + // Input the pre-recorded data into the filters and |
| 94 | + // run the model inference for each of the recordings |
| 95 | + for (size_t recordingIndex = 0; recordingIndex < ML_TEST_RECORDINGS; recordingIndex++) { |
| 96 | + DBG_PRINT("Recording %d\n", recordingIndex); |
| 97 | + for (size_t sample = 0; sample < ML_TEST_RECORDING_SIZE; sample++) { |
| 98 | + const float testData[3] = { |
| 99 | + test_data_x[recordingIndex][sample], |
| 100 | + test_data_y[recordingIndex][sample], |
| 101 | + test_data_z[recordingIndex][sample], |
| 102 | + }; |
| 103 | + MldpReturn_t recordDataResult = mlDataProcessor.recordData(testData, 3); |
| 104 | + if (recordDataResult != MLDP_SUCCESS) { |
| 105 | + DBG_PRINT("Failed to record test accelerometer data\n"); |
| 106 | + uBit.panic(892); |
| 107 | + } |
| 108 | + } |
| 109 | + runModelTest( |
| 110 | + actions, |
| 111 | + predictions, |
| 112 | + (const float *)&test_filter_output[recordingIndex][0], |
| 113 | + (const float *)&test_model_output[recordingIndex][0] |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | +#pragma GCC pop_options |
0 commit comments