ONE - On-device Neural Engine
Loading...
Searching...
No Matches
jpeg2hdf5.py
Go to the documentation of this file.
1from PIL import Image
2import numpy as np
3import h5py
4import sys
5import glob
6import subprocess
7import struct
8import datetime
9
10# Generates hdf5 files (and optionally binary files) from JPEGs
11# -f - specifies framework to generate them for
12# -t - specifies testcases directory (see it's structure in readme)
13# -i - specifies input node name of the model that will use them (required by nnkit)
14# -r - if files already exist, rewrites them
15# -b - enable binary file generation
16# -p - allow some sort of parallelism by processing only a subset of files,
17# you need to specify number of processes and run as much of them
18# manually with diferent numbers
19#
20# Example:
21# python3 conv.py -f tfl -t inc_slim/testcases -i input -p 16 1
22#
23
24helpstr = 'Usage: -f (tfl | caf) ' + \
25 '-t <testcases_directory> ' + \
26 '[-i <input_layer_name>] ' + \
27 '[-r] [-b]' + \
28 '[-p <number_of_processes> <process number>]'
29
30supported_frameworks = ['tfl', 'caf']
31args = {}
32# Defaults
33args['-p'] = (1, 1)
34args['-r'] = False
35args['-b'] = False
36
37argc = len(sys.argv)
38for i in range(len(sys.argv)):
39 arg = sys.argv[i]
40 if arg == '-r' or arg == '-b':
41 args[arg] = True
42 elif arg == '-f' or arg == '-t' or arg == '-i':
43 if i + 1 >= argc or sys.argv[i + 1][0] == '-':
44 print(arg, " is missing it's value")
45 print(helpstr)
46 exit()
47 args[arg] = sys.argv[i + 1]
48 elif arg == '-p':
49 if i + 2 >= argc or sys.argv[i + 1][0] == '-' or sys.argv[i + 2][0] == '-':
50 print(arg, " is missing some of it's values")
51 print(helpstr)
52 exit()
53 args[arg] = (int(sys.argv[i + 1]), int(sys.argv[i + 2]))
54 elif arg[0] == '-':
55 print('Unsupported argument: ', arg)
56 exit()
57
58if not ('-f' in args and '-t' in args):
59 print('Some arguments are not provided')
60 print(helpstr)
61 exit()
62
63fw = args['-f']
64if not fw in supported_frameworks:
65 print('Unsupported framework: ', fw)
66 exit()
67
68indirname = args['-t']
69
70if not '-i' in args:
71 if fw == 'caf':
72 inputname = 'data'
73 elif fw == 'tfl':
74 inputname = 'input'
75else:
76 inputname = args['-i']
77
78nproc, proc_num = args['-p']
79remove_existing = args['-r']
80gen_binary = args['-b']
81
82print('started at', datetime.datetime.now())
83testcases = glob.glob(indirname + '/testcase*/')
84testcases.sort()
85testcases = testcases[proc_num - 1::nproc]
86
87number = 0
88for testcase in testcases:
89 try:
90 infilename = glob.glob(testcase + 'input/*.JPEG')
91 if len(infilename) > 0:
92 number += 1
93 infilename = infilename[0]
94 outfilename = testcase + 'input/' + infilename.split('/')[-1] + '.hdf5'
95 binoutfilename = testcase + 'input/' + infilename.split('/')[-1] + '.dat'
96 found_hdf = len(glob.glob(outfilename)) != 0
97 found_bin = len(glob.glob(binoutfilename)) != 0
98 if not found_hdf or (not found_bin and gen_binary) or remove_existing:
99 with Image.open(infilename) as im:
100 #TODO: check if order is correct here and in other places
101 h = im.size[0]
102 w = im.size[1]
103 s = im.split()
104 if len(s) == 3:
105 r, g, b = s
106 else:
107 r = s[0]
108 g = s[0]
109 b = s[0]
110 rf = r.convert('F')
111 gf = g.convert('F')
112 bf = b.convert('F')
113 rfb = rf.tobytes()
114 gfb = gf.tobytes()
115 bfb = bf.tobytes()
116
117 made_hdf = False
118 if not found_hdf or remove_existing:
119 if fw == 'tfl':
120 reds = np.fromstring(rfb, count=(h * w), dtype='float32')
121 greens = np.fromstring(gfb, count=(h * w), dtype='float32')
122 blues = np.fromstring(bfb, count=(h * w), dtype='float32')
123
124 dset_shape = (1, h, w, 3)
125 narr = np.ndarray(shape=(0))
126 mixed_ch = []
127 for i in range(h * w):
128 mixed_ch += [
129 reds[i] / 255.0, greens[i] / 255.0, blues[i] / 255.0
130 ]
131 narr = np.append(narr, mixed_ch)
132 elif fw == 'caf':
133 dset_shape = (1, 3, h, w)
134 narr = np.fromstring(rfb + gfb + bfb,
135 count=(3 * h * w),
136 dtype='float32')
137 for i in range(3 * h * w):
138 narr[i] /= 255.0
139 if remove_existing:
140 subprocess.call(['rm', '-f', outfilename])
141 with h5py.File(outfilename) as f:
142 # nnkit hdf5_import asserts to use IEEE_F32BE, which is >f4 in numpy
143 dset = f.require_dataset(inputname, dset_shape, dtype='>f4')
144 dset[0] = np.reshape(narr, dset_shape)
145 made_hdf = True
146
147 if gen_binary and (not found_bin or remove_existing):
148 if fw == 'tfl' and made_hdf:
149 l = narr.tolist()
150 else:
151 reds = np.fromstring(rfb, count=(h * w), dtype='float32')
152 greens = np.fromstring(gfb, count=(h * w), dtype='float32')
153 blues = np.fromstring(bfb, count=(h * w), dtype='float32')
154 l = np.ndarray(shape=(0))
155 mixed_ch = []
156 for i in range(h * w):
157 mixed_ch += [
158 reds[i] / 255.0, greens[i] / 255.0, blues[i] / 255.0
159 ]
160 l = np.append(l, mixed_ch)
161 l = l.tolist()
162 with open(binoutfilename, 'wb') as out:
163 out.write(struct.pack('f' * len(l), *l))
164 print(number, ': ' + testcase + ' Done')
165 else:
166 print(testcase, ' nothing to do')
167 else:
168 print(testcase, ' JPEG not found')
169 except:
170 print(testcase, " FAILED")
171print('started at', ended.datetime.now())