raspberrypi / pico-tflmicro

Pico TensorFlow Lite Port
635 stars 97 forks source link

Quantized model not working #15

Closed Perugius closed 8 months ago

Perugius commented 8 months ago

Hello,

I am trying to run a quantized model on the pico w. However when I try to flash it with a quantized model, the pico w never even turns on? The device becomes unrecognized and I can't get an output from it. I changed the hello_world example for the purpose of this model.

However running the same model but unquantized works fine, the device gets recognized and I can get the output normally.

The quantized model works on the Arduino Nano 33 BLE and the coral micro, so I doubt the model itself is the problem.

/* Copyright 2022 The TensorFlow Authors. 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 "constants.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "hello_world_float_model_data.h"
#include "main_functions.h"
#include "output_handler.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_log.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
//#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Globals, used for compatibility with Arduino-style sketches.
namespace {
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
TfLiteTensor* input = nullptr;
TfLiteTensor* output = nullptr;
int inference_count = 0;

constexpr int kTensorArenaSize = 20000;
uint8_t tensor_arena[kTensorArenaSize];
}  // namespace

absolute_time_t endTime;
absolute_time_t startTime;
absolute_time_t invokeDuration;
const int8_t test_data[] = { 
9,9,6,9,8,6,8,2,9,1,3,6,0,5,1,4,1,2,6,3,8,7,2,0,0,5,8,0,6,8,8,4,8,7,3,4,0,7,9,5,2,0,5,2,6,1,5,0,3,1,1,8,7,3,5,1,9,6,6,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7,6,8,9,7,8,0,3,4,5,2,6,0,3,9,2,8,0,6,9,5,8,2,8,8,7,0,8,5,4,8,0,6,9,4,4,0,6,5,6,0,0,0,1,7,6,5,6,9,9,6,3,4,3,4,8,0,9,0,2,7,2,0,1,0,5,9,9,6,7,1,5,0,7,4,7,3,5,1,1,4,7,7,1,1,6,7,3,6,2,1,7,3,7,3,3,2,4,7,9,9,9,0,3,9,2,8,7,1,7,0,7,6,0,2,3,3,9,1,0,8,3,2,7,7,4,9,1,5,2,4,5,6,5,8,1,7,9,7,9,7,2,5,5,7,7,2,0,9,7,7,4,1,3,6,2,4,2,2,0,9,6,7,5,9,8,6,2,6,0,8,};

// The name of this function is important for Arduino compatibility.
void setup() {
  stdio_init_all();
  tflite::InitializeTarget();

  // Map the model into a usable data structure. This doesn't involve any
  // copying or parsing, it's a very lightweight operation.
  model = tflite::GetModel(g_hello_world_float_model_data);
  if (model->version() != TFLITE_SCHEMA_VERSION) {
    MicroPrintf(
        "Model provided is schema version %d not equal "
        "to supported version %d.",
        model->version(), TFLITE_SCHEMA_VERSION);
    return;
  }

  // This pulls in all the operation implementations we need.
  // NOLINTNEXTLINE(runtime-global-variables)
  static tflite::MicroMutableOpResolver<3> resolver;
  resolver.AddFullyConnected();
  resolver.AddSoftmax();
  resolver.AddReshape();
  resolver.AddQuantize();
  resolver.AddDequantize();
  //static tflite::AllOpsResolver resolver;
  // if (resolve_status != kTfLiteOk) {
  //   MicroPrintf("Op resolution failed");
  //   return;
  // }

  // Build an interpreter to run the model with.
  static tflite::MicroInterpreter static_interpreter(
      model, resolver, tensor_arena, kTensorArenaSize);
  interpreter = &static_interpreter;

  // Allocate memory from the tensor_arena for the model's tensors.
  TfLiteStatus allocate_status = interpreter->AllocateTensors();
  if (allocate_status != kTfLiteOk) {
    MicroPrintf("AllocateTensors() failed");
    return;
  }

  // Obtain pointers to the model's input and output tensors.
  input = interpreter->input(0);
  output = interpreter->output(0);

  // Keep track of how many inferences we have performed.
  inference_count = 0;
}

// The name of this function is important for Arduino compatibility.
void loop() {

  for (int i = 0; i < 240; i++) {
    MicroPrintf("filling tensor");
    MicroPrintf("%d", i);
    input->data.int8[i] = test_data[i];
  }
  // Run inference, and report any error
  startTime = get_absolute_time();
  TfLiteStatus invoke_status = interpreter->Invoke();
  if (invoke_status != kTfLiteOk) {
    MicroPrintf("Invoke failed");
    return;
  }
  endTime = get_absolute_time();
  invokeDuration = endTime - startTime;

  MicroPrintf("time elapsed %llu \n", invokeDuration);
  sleep_ms(1000);
  // 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;
}
Perugius commented 8 months ago

Never mind the op resolver was wrong, had to change it from 3 to 5....