-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
121 lines (109 loc) · 4.23 KB
/
models.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
from tensorflow.keras.models import Model
from tensorflow.keras.layers import (
Activation,
AveragePooling2D,
Conv2D,
Dense,
Dropout,
Flatten,
Input,
Reshape,
UpSampling2D,
)
from qkeras import QActivation, QConv2D, QDense, QDenseBatchnorm, quantized_bits
class TeacherAutoencoder:
def __init__(self, input_shape: tuple):
self.input_shape = input_shape
def get_model(self):
inputs = Input(shape=self.input_shape, name="teacher_inputs_")
x = Reshape((18, 14, 1), name="teacher_reshape")(inputs)
x = Conv2D(20, (3, 3), strides=1, padding="same", name="teacher_conv2d_1")(x)
x = Activation("relu", name="teacher_relu_1")(x)
x = AveragePooling2D((2, 2), name="teacher_pool_1")(x)
x = Conv2D(30, (3, 3), strides=1, padding="same", name="teacher_conv2d_2")(x)
x = Activation("relu", name="teacher_relu_2")(x)
x = Flatten(name="teacher_flatten")(x)
x = Dense(80, activation="relu", name="teacher_latent")(x)
x = Dense(9 * 7 * 30, name="teacher_dense")(x)
x = Reshape((9, 7, 30), name="teacher_reshape2")(x)
x = Activation("relu", name="teacher_relu_3")(x)
x = Conv2D(30, (3, 3), strides=1, padding="same", name="teacher_conv2d_3")(x)
x = Activation("relu", name="teacher_relu_4")(x)
x = UpSampling2D((2, 2), name="teacher_upsampling")(x)
x = Conv2D(20, (3, 3), strides=1, padding="same", name="teacher_conv2d_4")(x)
x = Activation("relu", name="teacher_relu_5")(x)
outputs = Conv2D(
1,
(3, 3),
activation="relu",
strides=1,
padding="same",
name="teacher_outputs",
)(x)
return Model(inputs, outputs, name="teacher")
class CicadaV1:
def __init__(self, input_shape: tuple):
self.input_shape = input_shape
def get_model(self):
inputs = Input(shape=self.input_shape, name="inputs_")
x = QDenseBatchnorm(
16,
kernel_quantizer=quantized_bits(8, 1, 1, alpha=1.0),
bias_quantizer=quantized_bits(8, 3, 1, alpha=1.0),
name="dense1",
)(inputs)
x = QActivation("quantized_relu(10, 6)", name="relu1")(x)
x = Dropout(1 / 8)(x)
x = QDense(
1,
kernel_quantizer=quantized_bits(12, 3, 1, alpha=1.0),
use_bias=False,
name="dense2",
)(x)
outputs = QActivation("quantized_relu(16, 8)", name="outputs")(x)
return Model(inputs, outputs, name="cicada-v1")
class CicadaV2:
def __init__(self, input_shape: tuple):
self.input_shape = input_shape
def get_model(self):
inputs = Input(shape=self.input_shape, name="inputs_")
x = Reshape((18, 14, 1), name="reshape")(inputs)
x = QConv2D(
4,
(2, 2),
strides=2,
padding="valid",
use_bias=False,
kernel_quantizer=quantized_bits(12, 3, 1, alpha=1.0),
name="conv",
)(x)
x = QActivation("quantized_relu(10, 6)", name="relu0")(x)
x = Flatten(name="flatten")(x)
x = Dropout(1 / 9)(x)
x = QDenseBatchnorm(
16,
kernel_quantizer=quantized_bits(8, 1, 1, alpha=1.0),
bias_quantizer=quantized_bits(8, 3, 1, alpha=1.0),
name="dense1",
)(x)
x = QActivation("quantized_relu(10, 6)", name="relu1")(x)
x = Dropout(1 / 8)(x)
x = QDense(
1,
kernel_quantizer=quantized_bits(12, 3, 1, alpha=1.0),
use_bias=False,
name="dense2",
)(x)
outputs = QActivation("quantized_relu(16, 8)", name="outputs")(x)
return Model(inputs, outputs, name="cicada-v2")
class SupervisedModel:
def __init__(self, input_shape: tuple, name: str = "supervised"):
self.input_shape = input_shape
self.name = name
def get_model(self):
inputs = Input(shape=self.input_shape, name="inputs_")
x = Conv2D(3, (3, 3), strides=2, activation="relu")(inputs)
x = Flatten()(x)
x = Dense(units=20, activation="relu")(x)
outputs = Dense(units=2, activation="softmax")(x)
return Model(inputs, outputs, name=self.name)