-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFModels.py
218 lines (202 loc) · 6.46 KB
/
TFModels.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# (4/12/22) bug with TF 2.8 requires us to do this load trick
import typing
from typing import List
import tensorflow as tf
import numpy as np
from tensorflow import keras
if typing.TYPE_CHECKING:
from keras.api._v2 import keras
from keras import datasets, layers, models
# ///
class MNISTModel(models.Model):
def __init__(self):
super(MNISTModel, self).__init__()
self.r_layers = []
# conv layers
self.r_layers += [
layers.Conv2D(32, 3, activation='relu')
]
# dense layers
self.r_layers += [
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10)
]
def call(self, x):
for layer in self.r_layers:
x = layer(x)
return x
class SolarFCNModel(models.Model):
def __init__(self, dropout_rate=0.2):
super(MNISTModel, self).__init__()
self.r_blocks: List[layers.Layer] = [[
# variable initial input
layers.Input(shape=(None,None,3))
]]
# conv blocks
# block 1
self.r_blocks += [[
# > ingestion conv
layers.Conv2D(64, 7, 2),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > max pool
layers.MaxPooling2D()
]]
# block 2
self.r_blocks += [[
# > 1
layers.Conv2D(64, 3, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(64, 3, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu)
]]
# block 3
self.r_blocks += [[
# > 1
layers.Conv2D(128, 3, 2),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(128, 3, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# --> average pool at end of last conv block
layers.AveragePooling2D()
]]
# fully connected (FC) block [idx 4]
# > 1
self.r_blocks += [[
layers.Conv2D(64, 1, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(64, 1, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu)
]]
# output FC block [idx 5]
# solar panel roof ? 1 : 0 (length = 2)
self.r_blocks += [[
layers.Conv2D(1, 1, 1),
layers.Dropout(dropout_rate),
layers.BatchNormalization(),
layers.Activation(tf.nn.sigmoid)
]]
def call(self, x: np.ndarray):
# let's do some fancy shortcuts, like ResNet
# input block + conv1
for i in range(2):
for j in range(len(self.r_blocks[i])):
x = self.r_blocks[i][j](x)
x_int = x.copy()
# do conv2, shortcut-add prev
for j in range(len(self.r_blocks[2])):
x = self.r_blocks[2][j](x)
x = layers.Add()([x, x_int])
x_int = x.copy()
# do conv3, shortcut-add prev
for j in range(len(self.r_blocks[3])):
x = self.r_blocks[3][j](x)
x = layers.Add()([x, x_int])
x_int = None
# do FC to finish
for i in range(4,len(self.r_blocks)):
for j in range(len(self.r_blocks[i])):
x = self.r_blocks[i][j](x)
return x
def Gen_SolarFCNModel(dropout_rate=0.2, output_bias=None,**kwargs) -> models.Model:
if output_bias is not None:
output_bias = keras.initializers.Constant(output_bias)
model = models.Sequential(**kwargs)
# this doesn't use subclassing, so it can't do shortcuts :<
# variable initial input
_blocks = [[layers.Input(shape=(None,None,3))]]
# conv blocks
# block 1
_blocks += [[
# > ingestion conv
layers.Conv2D(64, 7, 2),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > max pool
layers.MaxPooling2D(),
layers.Dropout(dropout_rate)
]]
# block 2
_blocks += [[
# > 1
layers.Conv2D(64, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(64, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 3
layers.Conv2D(64, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 4
layers.Conv2D(64, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu)
]]
# block 3
_blocks += [[
# > 1
layers.Conv2D(128, 3, 2),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(128, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 3
layers.Conv2D(128, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 4
layers.Conv2D(128, 3, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# --> average pool at end of last conv block
layers.AveragePooling2D(),
layers.Dropout(dropout_rate)
]]
# fully connected (FC) block [idx 4]
# > 1
_blocks += [[
layers.Conv2D(64, 1, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu),
# > 2
layers.Conv2D(64, 1, 1),
layers.BatchNormalization(),
layers.Activation(tf.nn.relu)
]]
# output FC block [idx 5]
# solar panel roof ? 1 : 0 (length = 2 classes)
_blocks += [[
layers.Conv2D(2, 1, 1, bias_initializer=output_bias),
layers.BatchNormalization(),
# one more pooling prior to activation to get down to (x,y)
layers.GlobalMaxPooling2D(),
layers.Activation(tf.nn.softmax)
]]
for block in _blocks:
for layer in block:
# print(layer)
model.add(layer)
model.summary()
return model