1 #!/usr/bin/python3
2""" Generate inputs for NNAPI benchamrks using a image
3
4Usage:
5./gen_mobilenet_input.py image_file.jpg
6"""
7
8
9from PIL import Image
10import numpy as np
11import sys
12import os
13
14def gen_input_files(filename, prefix, size):
15  basename = os.path.basename(filename) + "_" + prefix
16  img_f64 = np.array(Image.open(filename).resize(size)).astype(np.float) / 128 - 1
17  with open(basename + "_f32.bin", "wb") as f:
18    f.write(img_f64.astype('float32').tobytes())
19    print("Saving: " + basename + "_f32.bin")
20  with open(basename + "_u8.bin", "wb") as f:
21    f.write(((img_f64 + 1) * 255).astype('uint8').tobytes())
22    print("Saving: " + basename + "_u8.bin")
23
24if __name__ == '__main__':
25  if len(sys.argv) < 1:
26    print("Usage:\n ./gen_mobilenet_input.py image_file.jpg\n")
27    sys.exit(1)
28
29  gen_input_files(sys.argv[1], "mobilenet", (224, 224))
30  gen_input_files(sys.argv[1], "inceptionv3", (299, 299))
31  gen_input_files(sys.argv[1], "hdrnet", (256, 256))
32