Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RockPaperScissors/AppKit-E8_USB/algorithm/algorithm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define ALGORITHM_CONFIG_H_

#include "config_ml_model.h" // ML model configuration
#include "model_config.h"

// Input Data block size, in bytes
#ifndef ALGO_DATA_IN_BLOCK_SIZE
Expand All @@ -28,7 +29,7 @@

// Output Data block size, in bytes
#ifndef ALGO_DATA_OUT_BLOCK_SIZE
#define ALGO_DATA_OUT_BLOCK_SIZE (50U)
#define ALGO_DATA_OUT_BLOCK_SIZE (MODEL_NUM_CLASSES * sizeof(float))
#endif

#endif
33 changes: 30 additions & 3 deletions RockPaperScissors/AppKit-E8_USB/algorithm/arm_executor_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ bool classify_object = false;

output_label_t output_label;

float class_probs[MODEL_NUM_CLASSES] = {0.0};

constexpr int H = IMAGE_HEIGHT;

constexpr int W = IMAGE_WIDTH;
Expand Down Expand Up @@ -490,6 +492,31 @@ static void FormatShortLabel(const char* label, char* short_label,
short_label[copy_len] = '\0';
}

/**
* \brief Apply softmax function to convert logits to probabilities
* \param[in] logits Pointer to logit values array
* \param[out] probs Pointer to output probability array
* \param[in] n Number of elements
*/
void softmax(const float* logits, float* probs, int n) {
float max_val = logits[0];
for (int i = 1; i < n; i++) {
if (logits[i] > max_val) {
max_val = logits[i];
}
}

float sum = 0.0f;
for (int i = 0; i < n; i++) {
probs[i] = expf(logits[i] - max_val);
sum += probs[i];
}

for (int i = 0; i < n; i++) {
probs[i] /= sum;
}
}

/**
* \brief Prepare image input tensor for SqueezeNet model
* \param[in] image Pointer to input image data (HWC RGB format)
Expand Down Expand Up @@ -846,9 +873,9 @@ void print_outputs(RunnerContext& ctx)
printf("Number of classes: %d, expected: %d\n", numel, MODEL_NUM_CLASSES);
}

static float probs[MODEL_NUM_CLASSES];
float confidence = 0.0f;
uint16_t predicted_idx = -1;
softmax(logits, class_probs, numel);
status_t postprocess_result = postprocess((float*)logits, &result);

if(postprocess_result != STATUS_OK){
Expand Down Expand Up @@ -926,8 +953,8 @@ void postprocess(RunnerContext& ctx, uint8_t* img_buf,
print_outputs(ctx);

/* Copy shortened label plus confidence into caller's output buffer */
if (out_num >= sizeof(output_label_t)) {
memcpy(out_buf, &output_label, sizeof(output_label_t));
if (out_num >= sizeof(class_probs)) {
memcpy(out_buf, class_probs, sizeof(class_probs));
}

/* Only draw if label is valid */
Expand Down