|
a |
|
b/Segmentation/model/backbone.py |
|
|
1 |
import tensorflow as tf |
|
|
2 |
import tensorflow.keras.layers as tfkl |
|
|
3 |
from tensorflow.keras.applications import VGG16, VGG19 |
|
|
4 |
from tensorflow.keras.applications import ResNet50, ResNet50V2, ResNet101, ResNet101V2, ResNet152, ResNet152V2 |
|
|
5 |
from tensorflow.keras.models import Model |
|
|
6 |
|
|
|
7 |
class Encoder(object): |
|
|
8 |
def __init__(self, |
|
|
9 |
weights_init, |
|
|
10 |
model_architecture='vgg16'): |
|
|
11 |
|
|
|
12 |
self.weights_init = weights_init |
|
|
13 |
if model_architecture == 'vgg16': |
|
|
14 |
self.model = VGG16(weights=self.weights_init, include_top=False) |
|
|
15 |
self.bridge_list = [2, 5, 9, 13, 17] |
|
|
16 |
|
|
|
17 |
elif model_architecture == 'vgg19': |
|
|
18 |
self.model = VGG19(weights=self.weights_init, include_top=False) |
|
|
19 |
self.bridge_list = [2, 5, 10, 15, 20] |
|
|
20 |
|
|
|
21 |
elif model_architecture == 'resnet50': |
|
|
22 |
self.model = ResNet50(weights=self.weights_init, include_top=False) |
|
|
23 |
self.bridge_list = [4, 38, 80, 142, -1] |
|
|
24 |
|
|
|
25 |
elif model_architecture == 'resnet50v2': |
|
|
26 |
self.model = ResNet50V2(weights=self.weights_init, include_top=False) |
|
|
27 |
self.bridge_list = [2, 27, 62, 108, -1] |
|
|
28 |
|
|
|
29 |
elif model_architecture == 'resnet101': |
|
|
30 |
self.model = ResNet101(weghts=self.weights_init, include_top=False) |
|
|
31 |
self.bridge_list = [4, 38, 80, 312, -1] |
|
|
32 |
|
|
|
33 |
elif model_architecture == 'resnet101v2': |
|
|
34 |
self.model = ResNet101V2(weights=self.weights_init, include_top=False) |
|
|
35 |
self.bridge_list = [2, 27, 62, 328, -1] |
|
|
36 |
|
|
|
37 |
elif model_architecture == 'resnet152': |
|
|
38 |
self.model = ResNet152(weights=self.weights_init, include_top=False) |
|
|
39 |
self.bridge_list = [4, 38, 120, 482, -1] |
|
|
40 |
|
|
|
41 |
elif model_architecture == 'resnet152v2': |
|
|
42 |
self.model = ResNet152V2(weights=self.weights_init, include_top=False) |
|
|
43 |
self.bridge_list = [2, 27, 117, 515, -1] |
|
|
44 |
|
|
|
45 |
def construct_backbone(self): |
|
|
46 |
output_list = [] |
|
|
47 |
for _, layer_idx in enumerate(self.bridge_list): |
|
|
48 |
layer_output = self.model.layers[layer_idx].output |
|
|
49 |
output_list.append(layer_output) |
|
|
50 |
|
|
|
51 |
backbone = Model(inputs=self.model.input, outputs=output_list) |
|
|
52 |
|
|
|
53 |
return backbone |
|
|
54 |
|
|
|
55 |
def freeze_pretrained_layers(self): |
|
|
56 |
for layer in self.model.layers: |
|
|
57 |
layer.trainable = False |