-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransformer.py
More file actions
35 lines (26 loc) · 1.37 KB
/
Copy pathtransformer.py
File metadata and controls
35 lines (26 loc) · 1.37 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
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose, Add, BatchNormalization, Activation
class TransformerNetwork:
def __init__(self, input_shape=(224, 224, 3)):
self.input_shape = input_shape
self.model = self._build_transformer_network()
def _build_transformer_network(self):
inputs = Input(shape=self.input_shape)
x = Conv2D(32, (9, 9), strides=1, padding='same', activation='relu')(inputs)
x = Conv2D(64, (3, 3), strides=2, padding='same', activation='relu')(x)
x = Conv2D(128, (3, 3), strides=2, padding='same', activation='relu')(x)
for _ in range(5):
x = self._residual_block(x)
x = Conv2DTranspose(64, (3, 3), strides=2, padding='same', activation='relu')(x)
x = Conv2DTranspose(32, (3, 3), strides=2, padding='same', activation='relu')(x)
outputs = Conv2D(3, (9, 9), padding='same', activation='tanh')(x)
return Model(inputs, outputs, name="TransformerNetwork")
def _residual_block(self, x):
y = Conv2D(128, (3, 3), padding='same')(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(128, (3, 3), padding='same')(y)
y = BatchNormalization()(y)
return Add()([x, y])
def get_model(self):
return self.model