|
| 1 | +/* The Clear BSD License |
| 2 | + * |
| 3 | + * Copyright (c) 2025 EdgeImpulse Inc. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted (subject to the limitations in the disclaimer |
| 8 | + * below) provided that the following conditions are met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * * Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * * Neither the name of the copyright holder nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived from this |
| 19 | + * software without specific prior written permission. |
| 20 | + * |
| 21 | + * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
| 22 | + * THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 23 | + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 25 | + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 26 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 29 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 30 | + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | + |
| 35 | +#ifndef _FREEFORM_OUTPUT_HELPER_H_ |
| 36 | +#define _FREEFORM_OUTPUT_HELPER_H_ |
| 37 | + |
| 38 | +#include <vector> |
| 39 | +#include <stdio.h> |
| 40 | +#include "edge-impulse-sdk/classifier/ei_model_types.h" |
| 41 | +#include "edge-impulse-sdk/classifier/ei_run_classifier.h" |
| 42 | + |
| 43 | +#if EI_CLASSIFIER_FREEFORM_OUTPUT |
| 44 | + |
| 45 | +static std::map<ei_impulse_handle_t *, std::vector<matrix_t>> freeform_output_map; |
| 46 | + |
| 47 | +/** |
| 48 | + * For "freeform" outputs, the application needs to allocate the memory (one matrix_t per output tensor). |
| 49 | + * Here we'll use a global map (one per impulse handle) to allocate this memory |
| 50 | + * (on the heap, using a matrix_t wrapper). In your own application you can just use something like this: |
| 51 | + * |
| 52 | + * std::vector<matrix_t> freeform_outputs; |
| 53 | + * freeform_outputs.reserve(ei_default_impulse.impulse->freeform_outputs_size); |
| 54 | + * for (size_t ix = 0; ix < ei_default_impulse.impulse->freeform_outputs_size; ++ix) { |
| 55 | + * freeform_outputs.emplace_back(ei_default_impulse.impulse->freeform_outputs[ix], 1); |
| 56 | + * } |
| 57 | + * EI_IMPULSE_ERROR set_freeform_res = ei_set_freeform_output(freeform_outputs.data(), freeform_outputs.size()); |
| 58 | + * if (set_freeform_res != EI_IMPULSE_OK) { |
| 59 | + * printf("ei_set_freeform_output failed with %d\n", set_freeform_res); |
| 60 | + * exit(1); |
| 61 | + * } |
| 62 | + * |
| 63 | + * Where the memory is fully owned by your application (when freeform_outputs goes out of scope -> |
| 64 | + * memory is freed). |
| 65 | + */ |
| 66 | +void freeform_outputs_init(ei_impulse_handle_t *impulse_handle) { |
| 67 | + // make new object in the map based on handle (so this works with multiple impulses) |
| 68 | + auto& freeform_outputs = freeform_output_map[impulse_handle]; |
| 69 | + |
| 70 | + // reserve memory using the matrix_t (allocs on the heap) |
| 71 | + freeform_outputs.reserve(impulse_handle->impulse->freeform_outputs_size); |
| 72 | + for (size_t ix = 0; ix < impulse_handle->impulse->freeform_outputs_size; ++ix) { |
| 73 | + freeform_outputs.emplace_back(impulse_handle->impulse->freeform_outputs[ix], 1); |
| 74 | + } |
| 75 | + // and set the freeform output |
| 76 | + EI_IMPULSE_ERROR set_freeform_res = ei_set_freeform_output(freeform_outputs.data(), freeform_outputs.size()); |
| 77 | + if (set_freeform_res != EI_IMPULSE_OK) { |
| 78 | + printf("ei_set_freeform_output failed with %d\n", set_freeform_res); |
| 79 | + exit(1); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#else |
| 84 | + |
| 85 | +void freeform_outputs_init(ei_impulse_handle_t *impulse_handle) { } |
| 86 | + |
| 87 | +#endif // EI_CLASSIFIER_FREEFORM_OUTPUT |
| 88 | + |
| 89 | +#endif // _FREEFORM_OUTPUT_HELPER_H_ |
0 commit comments