PacktPublishing / TinyML-Cookbook_2E

TinyML Cookbook, 2E_Published by Packt
MIT License
45 stars 19 forks source link

chapter 5. model save problem. #6

Open zoldaten opened 4 months ago

zoldaten commented 4 months ago

i worked with jupyter notebook using tensorflow-2.16.1 and got stuck with model save. found out that tensorflow uses keras 3.0. i fixed the problem with https://keras.io/guides/migrating_to_keras_3/ but could not quantize model. have to downgrade tensorflow to 2.8.0 as it uses keras 2.0.

Moreover i got a quite huge model 3.6Мб ! dont know whats wrong with it as tflite model is less 1Мб (see attached) ? *i didnot make photoes of bathrooms just took cats, dogs and horses as classes.

models.zip

zoldaten commented 4 months ago

ok. got it fixed. as i using windows i have to convert tfmodel to C array with this code:

import numpy as np
import os

def convert_tflite_to_header(tflite_path, output_header_path):

    with open(tflite_path, 'rb') as tflite_file:
        tflite_content = tflite_file.read()

    hex_lines = [', '.join([f'0x{byte:02x}' for byte in tflite_content[i:i+12]]) for i in range(0, len(tflite_content), 12)]

    hex_array = ',\n  '.join(hex_lines)

    with open(output_header_path, 'w') as header_file:

        header_file.write('const unsigned char model[] = {\n  ')
        header_file.write(f'{hex_array}\n')
        header_file.write('};\n\n')

if __name__ == "__main__":
    tflite_path = 'model.tflite'
    output_header_path = 'model.h'

    convert_tflite_to_header(tflite_path, output_header_path)
    convert_tflite_to_header(tflite_path, output_header_path)

compare with xxd -c 60 -i model.tflite > indoor_scene_recognition.h on linux !

after that i couldnt catch why i cant compile. the problem was with tflu_model = tflite::GetModel(model); where model name should be taken from model.h: const unsigned char model[] = { - i.e. model

moreover i have to include debug_log.cpp from tflibrary hello_world. and lib i useed Arduino_TensorFlowLite-2.4.0-ALPHA-precompiled not from this Book.

files.zip

so... it works! 2024-03-14_14h57_23