Fig 1 MIMXRT1050 EVK Board
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Copyright 2018 NXP. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include <iostream>
#include <string>
#include <vector>
#include "timer.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"
#include "tensorflow/lite/string_util.h"
#include "Sine_mode.h"
int inference_count = 0;
// This is a small number so that it's easy to read the logs
const int kInferencesPerCycle = 30;
const float kXrange = 2.f * 3.14159265359f;
#define LOG(x) std::cout
void RunInference()
{
std::unique_ptr<tflite::FlatBufferModel> model;
std::unique_ptr<tflite::Interpreter> interpreter;
model = tflite::FlatBufferModel::BuildFromBuffer(sine_model_quantized_tflite, sine_model_quantized_tflite_len);
if (!model) {
LOG(FATAL) << "Failed to load model\r\n";
exit(-1);
}
model->error_reporter();
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
if (!interpreter) {
LOG(FATAL) << "Failed to construct interpreter\r\n";
exit(-1);
}
float input = interpreter->inputs()[0];
if (interpreter->AllocateTensors() != kTfLiteOk) {
LOG(FATAL) << "Failed to allocate tensors!\r\n";
}
while(true)
{
// Calculate an x value to feed into the model. We compare the current
// inference_count to the number of inferences per cycle to determine
// our position within the range of possible x values the model was
// trained on, and use this to calculate a value.
float position = static_cast<float>(inference_count) /
static_cast<float>(kInferencesPerCycle);
float x_val = position * kXrange;
float* input_tensor_data = interpreter->typed_tensor<float>(input);
*input_tensor_data = x_val;
Delay_time(1000);
// Run inference, and report any error
TfLiteStatus invoke_status = interpreter->Invoke();
if (invoke_status != kTfLiteOk)
{
LOG(FATAL) << "Failed to invoke tflite!\r\n";
return;
}
// Read the predicted y value from the model's output tensor
float* y_val = interpreter->typed_output_tensor<float>(0);
PRINTF("\r\n x_value: %f, y_value: %f \r\n", x_val, y_val[0]);
// Increment the inference_counter, and reset it if we have reached
// the total number per cycle
inference_count += 1;
if (inference_count >= kInferencesPerCycle) inference_count = 0;
}
}
/*
* @brief Application entry point.
*/
int main(void)
{
/* Init board hardware */
BOARD_ConfigMPU();
BOARD_InitPins();
BOARD_InitDEBUG_UARTPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
NVIC_SetPriorityGrouping(3);
InitTimer();
std::cout << "The hello_world demo of TensorFlow Lite model\r\n";
RunInference();
std::flush(std::cout);
for (;;) {}
}