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.