def generate_c_array(self, name, array, datatype="q7_t", const="const "): if not os.path.exists(self.headers_dir): os.makedirs(self.headers_dir) w = None if type(array) is list: w = array size = len(array) else: w = array.numpy() w = w.ravel() size = tf.size(array) filename = name + "_data.h" filepath = self.headers_dir + filename self.generated_header_files.append(filename) print("Generating C header {}...".format(filepath)) with open(filepath, "w+") as f: f.write("{}\n\n".format(LICENSE)) f.write("#pragma once\n") f.write("// Generated by {}\n".format(os.path.basename(__file__))) f.write("#include <stdint.h>\n\n") f.write(const + datatype + " " + self.testdataset + '_' + name + "[%d] =\n{\n" % size) for i in range(size - 1): f.write(" %d,\n" % w[i]) f.write(" %d\n" % w[size - 1]) f.write("};\n")
def convert_tensor_np(self, tensor_in, converter): w = tensor_in.numpy() shape = w.shape w = w.ravel() fw = converter(w) fw.shape = shape return tf.convert_to_tensor(fw)
def convert_tensor(self, tensor_in, converter): w = tensor_in.numpy() shape = w.shape w = w.ravel() normal = np.array(w) float_normal = [] for i in normal: float_normal.append(converter(i)) np_float_array = np.asarray(float_normal) np_float_array.shape = shape return tf.convert_to_tensor(np_float_array)