ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
res2bin.py
Go to the documentation of this file.
1import numpy as np
2import h5py
3import struct
4import sys
5
6# This script takes hdf5 file and unfolds it in a vector of float values
7# which is then writen in binary format to a given file
8# This is used by infer_testcases.py
9
10
11def res2bin(infilename, outfilename):
12 # print("Input filename: ", infilename)
13 # print("Output filename: " , outfilename)
14
15 f = h5py.File(infilename)
16 dset = f[list(f.keys())[0]]
17
18 vals = np.zeros(np.shape(dset), dtype='float32')
19 for i in range(np.size(dset, 0)):
20 vals[i, :] = np.asarray(dset[i], dtype='float32')
21 vals = list(np.reshape(vals, (vals.size)))
22
23 with open(outfilename, 'wb') as outfile:
24 outfile.write(struct.pack('f' * len(vals), *vals))
25
26
27if __name__ == '__main__':
28 argc = len(sys.argv)
29 if (argc > 2):
30 res2bin(sys.argv[1], sys.argv[2])
31 else:
32 print("Not enough arguments, expected: hdf5 filename, output filename")
33 exit()