| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/reference/batch_to_space_nd.h" |
|
|
| #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 kBlockShapeTensor = 1; |
| constexpr int kCropsTensor = 2; |
| constexpr int kOutputTensor = 0; |
|
|
| |
| |
| |
| |
| const int kInputOutputMinDimensionNum = 3; |
| const int kInputOutputMaxDimensionNum = 4; |
|
|
| TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
| TF_LITE_ENSURE_EQ(context, NumInputs(node), 3); |
| TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); |
|
|
| MicroContext* micro_context = GetMicroContext(context); |
|
|
| TfLiteTensor* input = |
| micro_context->AllocateTempInputTensor(node, kInputTensor); |
| TfLiteTensor* output = |
| micro_context->AllocateTempOutputTensor(node, kOutputTensor); |
| TF_LITE_ENSURE(context, input != nullptr && output != nullptr); |
|
|
| TF_LITE_ENSURE(context, NumDimensions(input) >= kInputOutputMinDimensionNum); |
| TF_LITE_ENSURE(context, NumDimensions(output) >= kInputOutputMinDimensionNum); |
| TF_LITE_ENSURE(context, NumDimensions(input) <= kInputOutputMaxDimensionNum); |
| TF_LITE_ENSURE(context, NumDimensions(output) <= kInputOutputMaxDimensionNum); |
| TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type); |
|
|
| micro_context->DeallocateTempTfLiteTensor(input); |
| micro_context->DeallocateTempTfLiteTensor(output); |
|
|
| return kTfLiteOk; |
| } |
|
|
| TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
| const TfLiteEvalTensor* input = |
| tflite::micro::GetEvalInput(context, node, kInputTensor); |
| const TfLiteEvalTensor* block_shape = |
| tflite::micro::GetEvalInput(context, node, kBlockShapeTensor); |
| const TfLiteEvalTensor* crops = |
| tflite::micro::GetEvalInput(context, node, kCropsTensor); |
| TfLiteEvalTensor* output = |
| tflite::micro::GetEvalOutput(context, node, kOutputTensor); |
|
|
| switch (input->type) { |
| case kTfLiteFloat32: |
| reference_ops::BatchToSpaceND( |
| tflite::micro::GetTensorShape(input), |
| tflite::micro::GetTensorData<float>(input), |
| tflite::micro::GetTensorShape(block_shape), |
| tflite::micro::GetTensorData<int32_t>(block_shape), |
| tflite::micro::GetTensorShape(crops), |
| tflite::micro::GetTensorData<int32_t>(crops), |
| tflite::micro::GetTensorShape(output), |
| tflite::micro::GetTensorData<float>(output)); |
| break; |
| case kTfLiteInt8: |
| reference_ops::BatchToSpaceND( |
| tflite::micro::GetTensorShape(input), |
| tflite::micro::GetTensorData<int8_t>(input), |
| tflite::micro::GetTensorShape(block_shape), |
| tflite::micro::GetTensorData<int32_t>(block_shape), |
| tflite::micro::GetTensorShape(crops), |
| tflite::micro::GetTensorData<int32_t>(crops), |
| tflite::micro::GetTensorShape(output), |
| tflite::micro::GetTensorData<int8_t>(output)); |
| break; |
| default: |
| MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(input->type), |
| input->type); |
| return kTfLiteError; |
| } |
| return kTfLiteOk; |
| } |
|
|
| } |
|
|
| TfLiteRegistration Register_BATCH_TO_SPACE_ND() { |
| return tflite::micro::RegisterOp(nullptr, Prepare, Eval); |
| } |
|
|
| } |
|
|