-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet_xception_model.py
79 lines (53 loc) · 2.78 KB
/
unet_xception_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
from tensorflow import keras
from tensorflow.keras import layers
def get_model(img_size, in_channels, classes):
UNET_model = Unet_xception(img_size=img_size, in_channels=in_channels, classes=classes)
return UNET_model.model
class Unet_xception():
def __init__(self, img_size, in_channels, classes):
self.img_size = img_size
self.in_channels = in_channels
self.classes = classes
inputs = keras.Input(shape=self.img_size + (self.in_channels,))
### First half of the network: downsampling inputs ###
# Entry Block
x = layers.Conv2D(32, kernel_size=3, strides=2, padding="same")(inputs)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
self.saved_residue = x
for filters in [64, 128, 256]:
x = self.sampling_down(filters, x)
### Second half of the network: upsampling inputs ###
for filters in [256, 128, 64, 32]:
x = self.sampling_up(filters, x)
# Adding final output (classification) layer
outputs = layers.Conv2D(self.classes, kernel_size=3, activation="softmax", padding="same")(x)
self.model = keras.Model(inputs=inputs, outputs=outputs)
return None
def sampling_down(self, f, x):
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(f, kernel_size=3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(f, kernel_size=3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(pool_size=3, strides=2, padding="same")(x)
residue = layers.Conv2D(f, kernel_size=1, strides=2, padding="same")(
self.saved_residue
)
x = layers.add([x, residue]) # Adding back residue
self.saved_residue = x # Setting aside next residue
return x
def sampling_up(self, f, x):
x = layers.Activation("relu")(x)
x = layers.Conv2DTranspose(f, kernel_size=3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2DTranspose(f, kernel_size=3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.UpSampling2D(size=2)(x)
residue = layers.UpSampling2D(size=2)(self.saved_residue)
residue = layers.Conv2D(f, kernel_size=1, padding="same")(residue)
x = layers.add([x, residue]) # Adding back residue
self.saved_residue = x # Setting aside next residue
return x