-
Notifications
You must be signed in to change notification settings - Fork 3
/
keras_model.py
147 lines (125 loc) · 4.88 KB
/
keras_model.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
"""
@file keras_model.py
@brief Script for keras model definition
@author Toshiki Nakamura, Yuki Nikaido, and Yohei Kawaguchi (Hitachi Ltd.)
Copyright (C) 2020 Hitachi, Ltd. All right reserved.
"""
########################################################################
# import python-library
########################################################################
# from import
import tensorflow.keras as keras
import tensorflow.keras.models
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Activation
from tensorflow.keras.regularizers import l1
from qkeras.qlayers import QDense, QActivation
from qkeras.quantizers import quantized_bits, quantized_relu
import logging
def get_model(name,inputDim,**kwargs):
if name=='keras_model':
return get_keras_model(inputDim,**kwargs)
elif name=='qkeras_model':
return get_qkeras_model(inputDim,**kwargs)
else:
print('ERROR')
return None
########################################################################
# keras model
########################################################################
def get_keras_model(inputDim,hiddenDim=128,latentDim=8,
encodeDepth=4, encodeIn=128, decodeDepth=4,
decodeOut=128, batchNorm=True, l1reg=0, **kwargs):
"""
define the keras model
the model based on the simple dense auto encoder
(128*128*128*128*8*128*128*128*128)
"""
# Declare encode network
inputLayer = Input(shape=(inputDim,))
kwargs = {'kernel_regularizer': l1(l1reg)}
for i in range(encodeDepth):
if i==0:
h = Dense(encodeIn,**kwargs)(inputLayer)
else:
h = Dense(hiddenDim,**kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = Activation('relu')(h)
#Declare latent layer
if decodeDepth==0:
h = Dense(latentDim,**kwargs)(inputLayer)
else:
h = Dense(latentDim,**kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = Activation('relu')(h)
# Declare decoder network
for i in range(decodeDepth):
if i==decodeDepth-1:
h = Dense(decodeOut,**kwargs)(h)
else:
h = Dense(hiddenDim,**kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = Activation('relu')(h)
h = Dense(inputDim,**kwargs)(h)
return Model(inputs=inputLayer, outputs=h)
########################################################################
# qkeras model
########################################################################
def get_qkeras_model(inputDim,hiddenDim=128,latentDim=8,
encodeDepth=4, encodeIn=128, decodeDepth=4,
decodeOut=128,bits=7,intBits=0,
reluBits=7,reluIntBits=3,lastBits=7,
lastIntBits=7,l1reg=0,batchNorm=True, **kwargs):
"""
define the keras model
the model based on the simple dense autoencoder
(128*128*128*128*8*128*128*128*128)
"""
inputLayer = Input(shape=(inputDim,))
kwargs = {'kernel_quantizer': quantized_bits(bits,intBits,alpha=1),
'bias_quantizer': quantized_bits(bits,intBits,alpha=1),
'kernel_initializer': 'lecun_uniform',
'kernel_regularizer': l1(l1reg)
}
# Declare encoder network
for i in range(encodeDepth):
if i==0:
h = QDense(encodeIn, **kwargs)(inputLayer)
else:
h = QDense(hiddenDim, **kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = QActivation(activation=quantized_relu(reluBits,reluIntBits))(h)
# Declare latent space
if encodeDepth==0:
h = QDense(latentDim, **kwargs)(inputLayer)
else:
h = QDense(latentDim, **kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = QActivation(activation=quantized_relu(reluBits,reluIntBits))(h)
# Declare decoder network
for i in range(decodeDepth):
if i ==decodeDepth-1:
h = QDense(decodeOut, **kwargs)(h)
else:
h = QDense(hiddenDim, **kwargs)(h)
if batchNorm:
h = BatchNormalization()(h)
h = QActivation(activation=quantized_relu(reluBits,reluIntBits))(h)
kwargslast = {'kernel_quantizer': quantized_bits(lastBits,lastIntBits,alpha=1),
'bias_quantizer': quantized_bits(lastBits,lastIntBits,alpha=1),
'kernel_initializer': 'lecun_uniform',
'kernel_regularizer': l1(l1reg)
}
h = QDense(inputDim, **kwargslast)(h)
return Model(inputs=inputLayer, outputs=h)
#########################################################################
from qkeras.utils import _add_supported_quantized_objects
co = {}
_add_supported_quantized_objects(co)
def load_model(file_path):
return keras.models.load_model(file_path, custom_objects=co)