-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_hardware_accelerators_linux.py
181 lines (151 loc) · 7.52 KB
/
ai_hardware_accelerators_linux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
"""ai_hardware_accelerators.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/14OARrQT11K7yKwr8FxhxIa6nYKvxRMfD
"""
import numpy as np
import tensorflow as tf
import os, subprocess
current_dir = os.getcwd()
#BatchSize = 512
#512=2^9 -> 2^10
BatchSize = 1 # google coral will das so
#scale=4 # muss unter 8 sein, sonst coral Fehler
#picture_shape = (128*scale,128*scale,3)
#linear_data_shape = (128*scale)
picture_shape = (128,128,3)
linear_data_shape = (128)
pictures = tf.keras.Input(shape=picture_shape, batch_size=BatchSize)
linear_data = tf.keras.Input(shape=linear_data_shape, batch_size=BatchSize)
layers,models = ([],[])
openvino_installed=True
coral_installed=True
print("Create Simple Tensorflow Models")
# relu activation model
layers.append(tf.keras.layers.Activation('relu')(linear_data))
relu_act = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="relu_act")
models.append(relu_act)
# leaky relu activation model
layers.append(tf.keras.layers.LeakyReLU()(linear_data))
leaky_relu_act = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="leaky_relu_act")
models.append(leaky_relu_act)
# tanh activation model
layers.append(tf.keras.layers.Activation('tanh')(linear_data))
tanh_act = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="tanh_act")
models.append(tanh_act)
# sigmoid activation model
layers.append(tf.keras.layers.Activation('sigmoid')(linear_data))
sigmoid_act = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="sigmoid_act")
models.append(sigmoid_act)
# scalar_multiply model
layers.append(tf.keras.layers.Lambda(lambda x: x * 5.0)(linear_data))
scalar_mult = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="scalar_mult")
models.append(scalar_mult)
# small dense model
layers.append(tf.keras.layers.Dense(8)(linear_data))
small_dense = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="small_dense")
models.append(small_dense)
# big dense model
layers.append(tf.keras.layers.Dense(512)(linear_data))
big_dense = tf.keras.Model(inputs=linear_data,outputs=layers[-1],name="big_dense")
models.append(big_dense)
# simple conv2d model
layers.append(tf.keras.layers.Conv2D(12,3)(pictures))
simple_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="simple_conv2d")
models.append(simple_conv2d)
# dilated conv2d model
layers.append(tf.keras.layers.Conv2D(12,3,dilation_rate=2)(pictures))
dilated_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="dilated_conv2d")
models.append(dilated_conv2d)
# strided conv2d model
layers.append(tf.keras.layers.Conv2D(12,3,strides=5)(pictures))
strided_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="strided_conv2d")
models.append(strided_conv2d)
# big conv2d model
layers.append(tf.keras.layers.Conv2D(9,7)(pictures))
big_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="big_conv2d")
models.append(big_conv2d)
# small conv2d model
layers.append(tf.keras.layers.Conv2D(9,3)(pictures))
small_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="small_conv2d")
models.append(small_conv2d)
# many conv2d model
layers.append(tf.keras.layers.Conv2D(256,3)(pictures))
many_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="many_conv2d")
models.append(many_conv2d)
# few conv2d model
layers.append(tf.keras.layers.Conv2D(3,3)(pictures))
few_conv2d = tf.keras.Model(inputs=pictures,outputs=layers[-1],name="few_conv2d")
models.append(few_conv2d)
# convert and store models
if not os.path.isdir("TF_Lite-Models"): os.mkdir("TF_Lite-Models")
if not os.path.isdir("Tensor_Flow-Models"): os.mkdir("Tensor_Flow-Models")
for model in models:
model.summary()
print("\n")
model.save(("Tensor_Flow-Models/"+model.name)) # save tensorflow models
with open(("TF_Lite-Models/"+model.name+".tflite"), 'wb') as model_file:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
model_file.write(tflite_model)
with open(("TF_Lite-Models/"+model.name+"_int8.tflite"), 'wb') as model_file:
tmp_shape=model.get_layer(index=0).input_shape[0]
data_gen=lambda : (yield [tf.random.uniform(shape=tmp_shape,dtype=tf.dtypes.float32)])
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations=[tf.lite.Optimize.DEFAULT]
converter.inference_input_type=tf.int8
converter.inference_output_type=tf.int8
converter.target_spec.supported_ops=[tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.representative_dataset=data_gen
tflite_model = converter.convert()
model_file.write(tflite_model) # save quantizized tf-lite models for Edge-TPU
print("Compile Models for OpenVINO")
if not openvino_installed:
os.system('wget https://apt.repos.intel.com/openvino/2021/GPG-PUB-KEY-INTEL-OPENVINO-2021')
os.system('sudo apt-key add GPG-PUB-KEY-INTEL-OPENVINO-2021')
os.system('echo "deb https://apt.repos.intel.com/openvino/2021 all main" | sudo tee /etc/apt/sources.list.d/intel-openvino-2021.list')
os.system('sudo apt update > /dev/null $2>&1')
os.system('sudo apt install intel-openvino-dev-ubuntu20-2021.3.394 -y > /dev/null $2>&1')
os.system('bash /opt/intel/openvino_2021/bin/setupvars.sh')
os.system('python3 -m pip install openvino-dev')
if not os.path.isdir("OpenVINO-Models"): os.mkdir("OpenVINO-Models")
if not os.getlogin()=="martin": #aus irgendwelchen gruenden funktioniert der Befehl nicht bei mir
for model in models:
result = subprocess.run(['python3', '-m', 'mo', '--framework', 'tf',
'--progress', '--input_model_is_text',
'--batch', str(BatchSize), '--input_shape',
str(model.get_layer(index=0).input_shape),
'--data_type=FP16', '--model_name', model.name,
'--saved_model_dir',
current_dir+'/Tensor_Flow-Models/'+model.name,
'--output_dir', current_dir+'/OpenVINO-Models'],
stdout=subprocess.PIPE)
print(result.stdout.decode('ascii'))
else:
for model in models:
result = subprocess.run(['python3', '-m', 'mo_tf',
'--progress', '--input_model_is_text',
#'--batch', str(BatchSize),
'--input_shape',
str(model.get_layer(index=0).input_shape[0]),
'--data_type=FP16', '--model_name', model.name,
'--saved_model_dir',
current_dir+'/Tensor_Flow-Models/'+model.name,
'--output_dir', current_dir+'/OpenVINO-Models'],
stdout=subprocess.PIPE)
print(result.stdout.decode('ascii'))
print("Compile Models for Edge TPU")
if not coral_installed:
os.system('curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -')
os.system('echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list')
os.system('sudo apt-get update')
os.system('sudo apt-get install edgetpu-compiler')
if not os.path.isdir("Edge_TPU-Models"): os.mkdir("Edge_TPU-Models")
filenames = ""
for model in models:
filenames += " "+current_dir+"/TF_Lite-Models/"+model.name+"_int8.tflite"
result = subprocess.run(['edgetpu_compiler','-s','-o',
current_dir+'/Edge_TPU-Models',*filenames.split()],
stdout=subprocess.PIPE)
print(result.stdout.decode('ascii'))