-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnnn.py
More file actions
194 lines (174 loc) · 7.97 KB
/
nnn.py
File metadata and controls
194 lines (174 loc) · 7.97 KB
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
182
183
184
185
186
187
188
189
190
191
192
import sys
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Input, Reshape, Conv2D, MaxPooling2D, Dense, Flatten, Dropout, Activation
class Model(object):
def __init__(self, input_shape=None, model_path=None):
self.input = None
self.tensor = None
if type(input_shape) is str and model_path is None:
input_shape, model_path = None, input_shape
if input_shape is not None:
self.input= Input(shape=input_shape, name='input')
self.tensor = self.input
elif model_path is not None:
self.load(model_path)
def __call__(self, input):
assert self.model is not None, 'Model is not built yet'
self.__used = True
if isinstance(self.model, tf.keras.Model):
return self.model.predict(input, verbose=0)
else:
input_index = self.model.get_input_details()[0]['index']
output_index = self.model.get_output_details()[0]['index']
self.model.set_tensor(input_index, input)
self.model.invoke()
return self.model.get_tensor(output_index)[0][0]
def reshape(self, shape, **kwargs):
return self.add(Reshape(shape, **kwargs))
def conv2d(self, out_channels, kernel, **kwargs):
return self.add(Conv2D(out_channels, kernel, **kwargs))
def maxpool(self, pool_size, strides=None, padding='valid', **kwargs):
return self.add(MaxPooling2D(pool_size, strides=strides, padding=padding, **kwargs))
def dense(self, units, **kwargs):
return self.add(Dense(units, **kwargs))
def flatten(self, **kwargs):
return self.add(Flatten(**kwargs))
def dropout(self, rate, **kwargs):
return self.add(Dropout(rate, **kwargs))
def activation(self, activation, **kwargs):
return self.add(Activation(activation, **kwargs))
def add(self, func, **kwargs):
assert self.tensor is not None, 'Model is empty!'
node = self.__class__()
node.input = self.input
node.tensor = func(self.tensor, **kwargs)
return node
def summary(self):
self.__build()
self.model.summary()
def train(self, x, y=None, batch_size=None, loss='mse', optimizer='adam', epochs=1, validation_data=None):
assert self.tensor is not None, 'Model is empty!'
self.__build()
self.model.compile(loss=loss, optimizer=optimizer)
# disable my logger since it doesn't compatible with fit's output
logger = sys.stdout
sys.stdout = sys.stdout.stdout
self.model.fit(x, y, epochs=epochs, batch_size=batch_size, validation_data=validation_data)
sys.stdout = logger
def get_weights(self):
if hasattr(self, 'weights'):
return self.weights
elif hasattr(self, 'model'):
weights = []
if(isinstance(self.model, tf.keras.Model)):
for layer in self.model.layers:
if len(layer.weights) > 0:
weights.append(layer.weights)
else:
if not hasattr(self, '__used'):
self.__call__(np.ones(tuple(self.model.get_input_details()[0]['shape']), dtype=np.float32))
tensor_details = self.model.get_tensor_details()
dq_weights = []
for d in tensor_details:
if d['name'].endswith('_dequantize'):
dq_weights.append(d['name'])
for d in tensor_details:
index = d['index']
name = d['name']
if name == 'input':
continue
if len(dq_weights) == 0 or name + '_dequantize' in dq_weights:
scale = d['quantization_parameters']['scales']
zp = d['quantization_parameters']['zero_points']
tensor = self.model.tensor(index)()
weights.append({'name': name, 'shape': tensor.shape, 'scale': scale, 'zero-points': zp, 'numpy': tensor})
return weights
else:
return []
def load(self, model_path):
print('[+] loading ' + model_path)
if model_path.endswith('.tflite'):
tflite_model = open(model_path, 'rb').read()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
self.model = interpreter
else:
self.model = tf.keras.models.load_model(model_path)
self.weights = list(self.model.__dict__['_serialized_attributes']['variables'])
def save(self, model_path, type=None, quantization=None):
print('[+] save model: ' + model_path)
assert self.model, 'This does not contain a model to save'
if type is None:
if model_path.endswith('.tflite') or quantization is None:
type = 'tflite'
if quantization is not None and not model_path.endswith('.tflite'):
model_path = model_path + quantization + '.tflite'
print('[-] model_path was set to: ' + model_path)
else:
type = 'savedModel'
assert type == 'savedModel' or type == 'tflite'
if quantization is not None:
self.__quantize(target=quantization)
if isinstance(self.model, tf.keras.Model):
if type == 'savedModel':
self.model.save(model_path)
elif type == 'tflite':
if not hasattr(self, 'converter'):
self.converter = tf.lite.TFLiteConverter.from_keras_model(self.model)
converted = self.converter.convert()
open(model_path, 'wb').write(converted)
else:
raise Exception('Unknown type: ' + type)
else:
raise Exception('Not implemented yet')
def __quantize(self, target='float16', x_train=None):
self.converter = tf.lite.TFLiteConverter.from_keras_model(self.model)
if target == 'float16':
print('[+] quantizing to ' + target)
self.converter.target_spec.supported_types = [tf.float16]
self.converter.optimizations = [tf.lite.Optimize.DEFAULT]
elif target == 'int8':
print('[+] quantizing to ' + target)
assert dataset is not None, 'x_train must be supplied to quantize to int8'
self.converter.optimizations = [tf.lite.Optimize.DEFAULT]
self.converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
self.converter.inference_input_type = tf.int8
self.converter.inference_output_type = tf.int8
else:
raise Exception('Unknown target: ' + target)
def __build(self):
if not hasattr(self, 'model') or self.model is None:
self.model = tf.keras.Model(inputs=self.input, outputs=self.tensor)
def set_logger(filename=None):
class Logger(object):
def __init__(self, filename):
self.str = ''
self.stdout = sys.stdout if not isinstance(sys.stdout, Logger) else sys.stdout.stdout
self.file = open(filename, 'w') if filename else None
def write(self, str):
if str == '\n':
if self.str:
return
str += '\033[0m'
if str.startswith('\r'):
sys.stderr.write('\b' * len(self.str) + str + '\033[0m\033[K')
sys.stderr.flush()
self.str = str
return
if self.str:
sys.stderr.write('\n')
if self.file:
self.file.write(str + '\n')
self.file.flush()
if str.startswith('[+]'):
str = '\033[36m' + str
elif str.startswith('[-]'):
str = '\033[31m' + str
self.stdout.write(str)
self.str = ''
def flush(self):
#self.write('')
self.stdout.flush()
sys.stdout = Logger(filename)
set_logger(None)