Most of the devices TensorFlow Lite for Microcontrollers runs on don’t have file systems, so the model data is typically included by compiling a source file containing an array of bytes into the executable. I recently added a utility to help convert files into nicely-formatted source files and headers, as the convert_bytes_to_c_source() function, but I’ve also had requests to go the other way. If you have a .cc file (like one from the examples) how do you get back to a TensorFlow Lite file that you can feed into other tools (such as the Netron visualizer)?
My hacky answer to this is a Python script that does a rough job of parsing an input file, looks for large chunks of numerical data, converts the values into bytes, and writes them into an output file. The live Gist of this is at https://gist.github.com/petewarden/493294425ac522f00ff45342c71939d7 and will contain the most up to date version, but here’s the code inline:
import re output_data = bytearray() with open('tensorflow/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc', 'r') as file: for line in file: values_match = re.match(r"\W*(0x[0-9a-fA-F,x ]+).*", line) if values_match: list_text = values_match.group(1) values_text = filter(None, list_text.split(",")) values = [int(x, base=16) for x in values_text] output_data.extend(values) with open('converted.tfl', 'wb') as output_file: output_file.write(output_data)
You’ll need to replace the input file name with the path of the one you want to convert, but otherwise this should work on most of these embedded model data source files.
convert_bytes_to_c_source() function can yield incorrect .cpp file. Minimal-working example:
$ cat main.cpp
#include
#include “myarray.h”
int main() {
std::cout << myarray_len << "\n";
std::cout << "Done\n";
return EXIT_SUCCESS;
}
$ cat myarray.h
#ifndef MY_ARRAY_H_
#define MY_ARRAY_H_
extern const int myarray_len;
#endif // MYARRAY_H_
$ cat myarray.cpp
const int myarray_len = 13;
g++ main.cpp myarray.cpp
will yield undefined reference to `myarray_len'
Fix: add #include "myarray.h" to myarray.cpp
The same fix is needed in the cpp source file generated by convert_bytes_to_c_source() function