| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/c/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/tensor_ctypes.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_log.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_utils.h" |
|
|
| namespace tflite { |
| namespace { |
|
|
| constexpr int kInputTensor = 0; |
| constexpr int kAxisTensor = 1; |
| constexpr int kOutputTensor = 0; |
|
|
| TfLiteStatus GetAxisValueFromTensor(TfLiteContext* context, |
| const TfLiteTensor* axis, |
| int32_t* axis_value) { |
| const int axis_dims = (tflite::GetTensorShape(axis)).DimensionsCount(); |
| if (axis_dims > 1) { |
| MicroPrintf("Axis has only one element for Expand_Dims.", axis_dims); |
| return kTfLiteError; |
| } |
|
|
| if (kTfLiteInt32 == (axis->type)) { |
| const int32_t* axis_ptr = tflite::GetTensorData<int32_t>(axis); |
| *axis_value = axis_ptr[0]; |
| return kTfLiteOk; |
| } else { |
| MicroPrintf("Axis type %s (%d) not supported by Expand_Dims.", |
| TfLiteTypeGetName(axis->type), axis->type); |
| return kTfLiteError; |
| } |
| } |
|
|
| |
| |
| |
| TfLiteStatus VerifyTensorDim(TfLiteContext* context, const TfLiteTensor* input, |
| const TfLiteTensor* axis_tensor, |
| const TfLiteTensor* output) { |
| int32_t axis_value = 0; |
| TF_LITE_ENSURE_OK(context, |
| GetAxisValueFromTensor(context, axis_tensor, &axis_value)); |
|
|
| tflite::RuntimeShape input_shape = tflite::GetTensorShape(input); |
| if (axis_value < 0) { |
| axis_value = input_shape.DimensionsCount() + 1 + axis_value; |
| } |
| TF_LITE_ENSURE(context, axis_value <= input_shape.DimensionsCount()); |
|
|
| |
| |
| |
| tflite::RuntimeShape output_shape = tflite::GetTensorShape(output); |
|
|
| TF_LITE_ENSURE(context, output_shape.DimensionsCount() == |
| input_shape.DimensionsCount() + 1); |
| for (int i = 0; i < output_shape.DimensionsCount(); ++i) { |
| if (i < axis_value) { |
| TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i)); |
| } else if (i == axis_value) { |
| TF_LITE_ENSURE(context, output_shape.Dims(i) == 1); |
| } else { |
| TF_LITE_ENSURE(context, output_shape.Dims(i) == input_shape.Dims(i - 1)); |
| } |
| } |
| return kTfLiteOk; |
| } |
|
|
| TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
| MicroContext* micro_context = GetMicroContext(context); |
|
|
| TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); |
| TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); |
| TfLiteTensor* input = |
| micro_context->AllocateTempInputTensor(node, kInputTensor); |
| TF_LITE_ENSURE(context, input != nullptr); |
| TfLiteTensor* axis = |
| micro_context->AllocateTempInputTensor(node, kAxisTensor); |
| TF_LITE_ENSURE(context, axis != nullptr); |
| TfLiteTensor* output = |
| micro_context->AllocateTempOutputTensor(node, kOutputTensor); |
| TF_LITE_ENSURE(context, output != nullptr); |
| output->type = input->type; |
| if (IsDynamicTensor(axis)) { |
| MicroPrintf("DynamicTensor is not yet supported by Expand_Dims."); |
| return kTfLiteError; |
| } |
| TF_LITE_ENSURE_OK(context, VerifyTensorDim(context, input, axis, output)); |
|
|
| micro_context->DeallocateTempTfLiteTensor(input); |
| micro_context->DeallocateTempTfLiteTensor(axis); |
| micro_context->DeallocateTempTfLiteTensor(output); |
| return kTfLiteOk; |
| } |
|
|
| template <typename T> |
| void memCopyN(T* out, const T* in, const int num_elements) { |
| for (int i = 0; i < num_elements; ++i) { |
| out[i] = in[i]; |
| } |
| } |
|
|
| TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
| const TfLiteEvalTensor* input = |
| tflite::micro::GetEvalInput(context, node, kInputTensor); |
| TfLiteEvalTensor* output = |
| tflite::micro::GetEvalOutput(context, node, kOutputTensor); |
| const int flat_size = ElementCount(*input->dims); |
|
|
| switch (input->type) { |
| case kTfLiteFloat32: { |
| memCopyN(tflite::micro::GetTensorData<float>(output), |
| tflite::micro::GetTensorData<float>(input), flat_size); |
| } break; |
| case kTfLiteInt8: { |
| memCopyN(tflite::micro::GetTensorData<int8_t>(output), |
| tflite::micro::GetTensorData<int8_t>(input), flat_size); |
| } break; |
| default: |
| MicroPrintf( |
| "Expand_Dims only currently supports int8 and float32, got %d.", |
| input->type); |
| return kTfLiteError; |
| } |
| return kTfLiteOk; |
| } |
| } |
|
|
| TfLiteRegistration Register_EXPAND_DIMS() { |
| return tflite::micro::RegisterOp(nullptr, Prepare, Eval); |
| } |
|
|
| } |
|
|