1114580_ja-JP

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

1114580_ja-JP

1114580_ja-JP

TensorFlow Lite の「Hello World」<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ゴール
私たちの目標は、値 x を取り、その正弦 y を予測できるモデルをトレーニングすることです。実際のアプリケーションでは、x の正弦が必要な場合は、直接計算すればよいのです。しかし、結果を近似するようにモデルを学習させることで、機械学習の基本を示すことができます。
TensorFlow と Keras
TensorFlow は、機械学習モデルの構築、トレーニング、評価、デプロイのためのツールのセットです。もともと Google で開発された TensorFlow は、現在、世界中の何千人もの貢献者によって構築および保守されているオープンソース プロジェクトです。これは、機械学習で最も人気があり、広く使用されているフレームワークです。ほとんどの開発者は、Python ライブラリを介して TensorFlow と対話します。TensorFlow はさまざまなことを行います。この投稿では、ディープラーニングネットワークの構築とトレーニングを容易にするTensorFlowの高レベルAPIである Kerasを使用します。
モバイル デバイスや組み込みデバイスで TensorFlow を有効にするために、Google は TensorFlow Lite フレームワークを開発しました。これにより、これらの計算制限のあるデバイスは、TensorFlow Lite に変換された事前トレーニング済みの TensorFlow モデルで推論を実行できるようになります。これらの変換されたモデルは、これ以上トレーニングすることはできませんが、量子化やプルーニングなどの手法を使用して最適化できます。
モデルの構築
モデルを構築するには、以下の手順に従う必要があります。
  1. 単純なデータセットを取得します。
  2. ディープ ラーニング モデルをトレーニングします。
  3. モデルのパフォーマンスを評価します。
  4. デバイス上で実行するようにモデルを変換します。
このノートブックは、 TensorFlowモデルを作成し、それをTensorFlow Liteで使用するために変換するプロセスを示すために設計されています。
RT MCU へのモードのデプロイ
  • ハードウェアボード: MIMXRT1050 EVKボード

WeChat Image_20200203172635.jpg

図1 MIMXRT1050 EVKボード

  • テンプレート デモ コード: evkbimxrt1050_tensorflow_lite_cifar10
コード
/* 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 
#include 
#include 
#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 (;;) {}
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
テスト結果
MIMXRT1050 EVK Boardでは、入力データ( x_value )と推論された出力データ( y_value シリアルポート経由)を記録します。
2020-02-03_18-11-27.jpg
図2 受信データ
while ループ関数では、0 から 2π の範囲の x 値の進行に対して推論を実行し、繰り返します。実行されるたびに、新しい x 値が計算され、推論が実行され、データが出力されます。
2020-02-03_17-34-01.jpg
図3 テスト結果
さらに、次の図に示すように、Excelを使用して、受信したデータを実際の値に対して表示します。
2020-02-03_18-09-33.jpg
図4 ドットプロット
ほとんどの場合、予測値を表すドットは、実際の値の分布の中心に沿って滑らかな正弦曲線を形成していることがわかります。一般に、私たちのネットワークは正弦曲線を近似することを学習しました。

i.MXRT 105倍
タグ(1)
評価なし
バージョン履歴
最終更新日:
‎01-06-2026 02:10 AM
更新者: