segmodels-keras 0.1.0.dev10__tar.gz → 0.1.0.dev12__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/PKG-INFO +1 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/pyproject.toml +1 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/__init__.py +5 -2
- segmodels_keras-0.1.0.dev12/segmodels_keras/_version.py +2 -0
- segmodels_keras-0.1.0.dev12/segmodels_keras/backbones/__init__.py +1 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/_common_blocks.py +6 -3
- segmodels_keras-0.1.0.dev12/segmodels_keras/backbones/backbones_factory.py +408 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/inception_resnet_v2.py +9 -3
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/inception_v3.py +9 -2
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/resnet_18_34.py +3 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/base/__init__.py +2 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/base/functional.py +35 -10
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/base/objects.py +31 -18
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/losses.py +24 -22
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/metrics.py +22 -19
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/_common_blocks.py +1 -2
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/_utils.py +1 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/fpn.py +26 -22
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/linknet.py +27 -23
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/pspnet.py +23 -22
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/unet.py +38 -35
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/utils.py +16 -13
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras.egg-info/PKG-INFO +1 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/setup.py +2 -0
- segmodels_keras-0.1.0.dev12/tests/test_backbones.py +57 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/tests/test_helper.py +2 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/tests/test_metrics.py +2 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/tests/test_models.py +43 -1
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/tests/test_utils.py +2 -0
- segmodels_keras-0.1.0.dev10/segmodels_keras/_version.py +0 -2
- segmodels_keras-0.1.0.dev10/segmodels_keras/backbones/__init__.py +0 -0
- segmodels_keras-0.1.0.dev10/segmodels_keras/backbones/backbones_factory.py +0 -293
- segmodels_keras-0.1.0.dev10/tests/test_backbones.py +0 -21
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/LICENSE +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/MANIFEST.in +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/README.rst +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/_compat.py +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/_weights.py +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/backbones/resnet_common.py +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/__init__.py +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/models/model_factory.py +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras/py.typed +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras.egg-info/SOURCES.txt +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras.egg-info/dependency_links.txt +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras.egg-info/requires.txt +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/segmodels_keras.egg-info/top_level.txt +0 -0
- {segmodels_keras-0.1.0.dev10 → segmodels_keras-0.1.0.dev12}/setup.cfg +0 -0
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"""Segmentation Models for Keras."""
|
|
2
|
+
|
|
1
3
|
import functools # noqa: I001
|
|
2
4
|
import os
|
|
3
5
|
from typing import Any
|
|
@@ -43,12 +45,12 @@ def filter_kwargs(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
|
43
45
|
|
|
44
46
|
|
|
45
47
|
def framework() -> str | None:
|
|
46
|
-
"""Return name of Segmentation Models framework"""
|
|
48
|
+
"""Return name of Segmentation Models framework."""
|
|
47
49
|
return _KERAS_FRAMEWORK
|
|
48
50
|
|
|
49
51
|
|
|
50
52
|
def set_framework(name: str) -> None:
|
|
51
|
-
"""Set framework for Segmentation Models
|
|
53
|
+
"""Set framework for Segmentation Models.
|
|
52
54
|
|
|
53
55
|
Args:
|
|
54
56
|
name (str): one of ``keras``, ``tf.keras``, case insensitive.
|
|
@@ -118,6 +120,7 @@ get_available_backbone_names = Backbones.models_names
|
|
|
118
120
|
|
|
119
121
|
|
|
120
122
|
def get_preprocessing(name: str) -> Callable[[Any], Any]:
|
|
123
|
+
"""Get the preprocessing function for a backbone by name."""
|
|
121
124
|
return Backbones.get_preprocessing(name)
|
|
122
125
|
|
|
123
126
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Backbones package."""
|
|
@@ -44,6 +44,7 @@ def GroupConv2D(
|
|
|
44
44
|
If you don't specify anything, no activation is applied
|
|
45
45
|
(ie. "linear" activation: a(x) = x).
|
|
46
46
|
padding: one of "valid" or "same" (case-insensitive).
|
|
47
|
+
kwargs: additional keyword arguments (unused).
|
|
47
48
|
|
|
48
49
|
Input shape:
|
|
49
50
|
4D tensor with shape: (batch, rows, cols, channels) if data_format is
|
|
@@ -97,12 +98,14 @@ def expand_dims(x, channels_axis):
|
|
|
97
98
|
|
|
98
99
|
|
|
99
100
|
def ChannelSE(reduction=16, **kwargs): # noqa: ARG001
|
|
100
|
-
"""
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
"""Squeeze and Excitation block.
|
|
102
|
+
|
|
103
|
+
Reimplementation inspired by
|
|
104
|
+
https://github.com/Cadene/pretrained-models.pytorch/blob/master/pretrainedmodels/models/senet.py.
|
|
103
105
|
|
|
104
106
|
Args:
|
|
105
107
|
reduction: channels squeeze factor
|
|
108
|
+
kwargs: additional keyword arguments (unused).
|
|
106
109
|
|
|
107
110
|
"""
|
|
108
111
|
channels_axis = 3 if backend.image_data_format() == "channels_last" else 1
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"""Backbones factory module."""
|
|
2
|
+
|
|
3
|
+
import keras
|
|
4
|
+
import keras.applications as ka
|
|
5
|
+
|
|
6
|
+
from . import inception_resnet_v2 as irv2
|
|
7
|
+
from . import inception_v3 as iv3
|
|
8
|
+
from . import resnet_18_34
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BackbonesFactory:
|
|
12
|
+
"""Dict with all supported backbones.
|
|
13
|
+
|
|
14
|
+
Each backbone is represented as a tuple of 3 elements:
|
|
15
|
+
1. Backbone class
|
|
16
|
+
2. Preprocessing function
|
|
17
|
+
3. List of layers to take features from backbone in the following order:
|
|
18
|
+
(x16, x8, x4, x2, x1) - `x4` mean that features has 4 times less spatial
|
|
19
|
+
resolution (Height x Width) than input image.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
_models = { # noqa: RUF012
|
|
23
|
+
# Feature sizes in comments are HxWxC for a 512x512 input.
|
|
24
|
+
# ResNets < 50 layers are NOT available via keras.applications, so use a
|
|
25
|
+
# torchvision-compatible post-activation BasicBlock implementation.
|
|
26
|
+
# Layer names follow the keras.applications ResNet50+ naming convention.
|
|
27
|
+
"resnet18": (
|
|
28
|
+
resnet_18_34.ResNet18,
|
|
29
|
+
resnet_18_34.preprocess_input,
|
|
30
|
+
# resnet18: layer1=2, layer2=2, layer3=2, layer4=2 blocks
|
|
31
|
+
# backbone output = conv5_block2_out (512ch); skips are the 4 stages before
|
|
32
|
+
(
|
|
33
|
+
"conv4_block2_out", # 32x32x256
|
|
34
|
+
"conv3_block2_out", # 64x64x128
|
|
35
|
+
"conv2_block2_out", # 128x128x64
|
|
36
|
+
"conv1_relu", # 256x256x64
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
"resnet34": (
|
|
40
|
+
resnet_18_34.ResNet34,
|
|
41
|
+
resnet_18_34.preprocess_input,
|
|
42
|
+
# resnet34: layer1=3, layer2=4, layer3=6, layer4=3 blocks
|
|
43
|
+
# backbone output = conv5_block3_out (512ch); skips are the 4 stages before
|
|
44
|
+
(
|
|
45
|
+
"conv4_block6_out", # 32x32x256
|
|
46
|
+
"conv3_block4_out", # 64x64x128
|
|
47
|
+
"conv2_block3_out", # 128x128x64
|
|
48
|
+
"conv1_relu", # 256x256x64
|
|
49
|
+
),
|
|
50
|
+
),
|
|
51
|
+
# ResNets > 50 layers are available via keras.applications, so use those.
|
|
52
|
+
# Skip layers (inverted) from https://github.com/yingkaisha/keras-unet-collection
|
|
53
|
+
"resnet50": (
|
|
54
|
+
ka.ResNet50,
|
|
55
|
+
ka.resnet.preprocess_input,
|
|
56
|
+
(
|
|
57
|
+
"conv4_block6_out", # 32x32x1024
|
|
58
|
+
"conv3_block4_out", # 64x64x512
|
|
59
|
+
"conv2_block3_out", # 128x128x256
|
|
60
|
+
"conv1_relu", # 256x256x64
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
"resnet101": (
|
|
64
|
+
ka.ResNet101,
|
|
65
|
+
ka.resnet.preprocess_input,
|
|
66
|
+
(
|
|
67
|
+
"conv4_block23_out", # 32x32x1024
|
|
68
|
+
"conv3_block4_out", # 64x64x512
|
|
69
|
+
"conv2_block3_out", # 128x128x256
|
|
70
|
+
"conv1_relu", # 256x256x64
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
"resnet152": (
|
|
74
|
+
ka.ResNet152,
|
|
75
|
+
ka.resnet.preprocess_input,
|
|
76
|
+
(
|
|
77
|
+
"conv4_block36_out", # 32x32x1024
|
|
78
|
+
"conv3_block8_out", # 64x64x512
|
|
79
|
+
"conv2_block3_out", # 128x128x256
|
|
80
|
+
"conv1_relu", # 256x256x64
|
|
81
|
+
),
|
|
82
|
+
),
|
|
83
|
+
# ResNetV2
|
|
84
|
+
# Skip layers (inverted) from https://github.com/yingkaisha/keras-unet-collection
|
|
85
|
+
"resnet50v2": (
|
|
86
|
+
ka.ResNet50V2,
|
|
87
|
+
ka.resnet_v2.preprocess_input,
|
|
88
|
+
(
|
|
89
|
+
"conv4_block6_1_relu", # 32x32x256
|
|
90
|
+
"conv3_block4_1_relu", # 64x64x128
|
|
91
|
+
"conv2_block3_1_relu", # 128x128x64
|
|
92
|
+
"conv1_conv", # 256x256x64
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
"resnet101v2": (
|
|
96
|
+
ka.ResNet101V2,
|
|
97
|
+
ka.resnet_v2.preprocess_input,
|
|
98
|
+
(
|
|
99
|
+
"conv4_block23_1_relu", # 32x32x256
|
|
100
|
+
"conv3_block4_1_relu", # 64x64x128
|
|
101
|
+
"conv2_block3_1_relu", # 128x128x64
|
|
102
|
+
"conv1_conv", # 256x256x64
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
"resnet152v2": (
|
|
106
|
+
ka.ResNet152V2,
|
|
107
|
+
ka.resnet_v2.preprocess_input,
|
|
108
|
+
(
|
|
109
|
+
"conv4_block36_1_relu", # 32x32x256
|
|
110
|
+
"conv3_block8_1_relu", # 64x64x128
|
|
111
|
+
"conv2_block3_1_relu", # 128x128x64
|
|
112
|
+
"conv1_conv", # 256x256x64
|
|
113
|
+
),
|
|
114
|
+
),
|
|
115
|
+
# VGG
|
|
116
|
+
# Skip layers from segmentation_models
|
|
117
|
+
"vgg16": (
|
|
118
|
+
ka.vgg16.VGG16,
|
|
119
|
+
ka.vgg16.preprocess_input,
|
|
120
|
+
(
|
|
121
|
+
"block5_conv3", # 32x32x512
|
|
122
|
+
"block4_conv3", # 64x64x512
|
|
123
|
+
"block3_conv3", # 128x128x256
|
|
124
|
+
"block2_conv2", # 256x256x128
|
|
125
|
+
"block1_conv2", # 512x512x64
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
"vgg19": (
|
|
129
|
+
ka.vgg19.VGG19,
|
|
130
|
+
ka.vgg19.preprocess_input,
|
|
131
|
+
(
|
|
132
|
+
"block5_conv4", # 32x32x512
|
|
133
|
+
"block4_conv4", # 64x64x512
|
|
134
|
+
"block3_conv4", # 128x128x256
|
|
135
|
+
"block2_conv2", # 256x256x128
|
|
136
|
+
"block1_conv2", # 512x512x64
|
|
137
|
+
),
|
|
138
|
+
),
|
|
139
|
+
# DenseNet
|
|
140
|
+
# Skip layers (inverted) from https://github.com/yingkaisha/keras-unet-collection
|
|
141
|
+
"densenet121": (
|
|
142
|
+
ka.densenet.DenseNet121,
|
|
143
|
+
ka.densenet.preprocess_input,
|
|
144
|
+
(
|
|
145
|
+
"pool4_conv", # 32x32x512
|
|
146
|
+
"pool3_conv", # 64x64x256
|
|
147
|
+
"pool2_conv", # 128x128x128
|
|
148
|
+
"conv1/relu", # 256x256x64
|
|
149
|
+
),
|
|
150
|
+
),
|
|
151
|
+
"densenet169": (
|
|
152
|
+
ka.densenet.DenseNet169,
|
|
153
|
+
ka.densenet.preprocess_input,
|
|
154
|
+
(
|
|
155
|
+
"pool4_conv", # 32x32x640
|
|
156
|
+
"pool3_conv", # 64x64x256
|
|
157
|
+
"pool2_conv", # 128x128x128
|
|
158
|
+
"conv1/relu", # 256x256x64
|
|
159
|
+
),
|
|
160
|
+
),
|
|
161
|
+
"densenet201": (
|
|
162
|
+
ka.densenet.DenseNet201,
|
|
163
|
+
ka.densenet.preprocess_input,
|
|
164
|
+
(
|
|
165
|
+
"pool4_conv", # 32x32x896
|
|
166
|
+
"pool3_conv", # 64x64x256
|
|
167
|
+
"pool2_conv", # 128x128x128
|
|
168
|
+
"conv1/relu", # 256x256x64
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
# Inception
|
|
172
|
+
# Skip layers from segmentation_models
|
|
173
|
+
"inceptionresnetv2": (
|
|
174
|
+
irv2.InceptionResNetV2,
|
|
175
|
+
irv2.preprocess_input,
|
|
176
|
+
# Use the layer indexes instead of names because otherwise loading weights
|
|
177
|
+
# saved with keras 2 cannot be loaded with keras 3.
|
|
178
|
+
# (
|
|
179
|
+
# "activation_161",
|
|
180
|
+
# "activation_74",
|
|
181
|
+
# "activation_3",
|
|
182
|
+
# "activation",
|
|
183
|
+
# # "input_1",
|
|
184
|
+
# ),
|
|
185
|
+
(
|
|
186
|
+
594, # 32x32x1088
|
|
187
|
+
260, # 64x64x320
|
|
188
|
+
16, # 128x128x192
|
|
189
|
+
9, # 256x256x64
|
|
190
|
+
),
|
|
191
|
+
),
|
|
192
|
+
"inceptionv3": (
|
|
193
|
+
iv3.InceptionV3,
|
|
194
|
+
iv3.preprocess_input,
|
|
195
|
+
(
|
|
196
|
+
228, # 32x32x768
|
|
197
|
+
86, # 64x64x288
|
|
198
|
+
16, # 128x128x192
|
|
199
|
+
9, # 256x256x64
|
|
200
|
+
),
|
|
201
|
+
),
|
|
202
|
+
# MobileNet
|
|
203
|
+
# Skip layers from segmentation_models
|
|
204
|
+
"mobilenet": (
|
|
205
|
+
ka.mobilenet.MobileNet,
|
|
206
|
+
ka.mobilenet.preprocess_input,
|
|
207
|
+
(
|
|
208
|
+
"conv_pw_11_relu", # 32x32x512
|
|
209
|
+
"conv_pw_5_relu", # 64x64x256
|
|
210
|
+
"conv_pw_3_relu", # 128x128x128
|
|
211
|
+
"conv_pw_1_relu", # 256x256x64
|
|
212
|
+
),
|
|
213
|
+
),
|
|
214
|
+
"mobilenetv2": (
|
|
215
|
+
ka.mobilenet_v2.MobileNetV2,
|
|
216
|
+
ka.mobilenet_v2.preprocess_input,
|
|
217
|
+
(
|
|
218
|
+
"block_13_expand_relu", # 32x32x576
|
|
219
|
+
"block_6_expand_relu", # 64x64x192
|
|
220
|
+
"block_3_expand_relu", # 128x128x144
|
|
221
|
+
"block_1_expand_relu", # 256x256x96
|
|
222
|
+
),
|
|
223
|
+
),
|
|
224
|
+
# EfficientNet
|
|
225
|
+
# Skip layers from segmentation_models
|
|
226
|
+
"efficientnetb0": [
|
|
227
|
+
ka.EfficientNetB0,
|
|
228
|
+
ka.efficientnet.preprocess_input,
|
|
229
|
+
(
|
|
230
|
+
"block6a_expand_activation", # 32x32x672
|
|
231
|
+
"block4a_expand_activation", # 64x64x240
|
|
232
|
+
"block3a_expand_activation", # 128x128x144
|
|
233
|
+
"block2a_expand_activation", # 256x256x96
|
|
234
|
+
),
|
|
235
|
+
],
|
|
236
|
+
"efficientnetb1": [
|
|
237
|
+
ka.EfficientNetB1,
|
|
238
|
+
ka.efficientnet.preprocess_input,
|
|
239
|
+
(
|
|
240
|
+
"block6a_expand_activation", # 32x32x672
|
|
241
|
+
"block4a_expand_activation", # 64x64x240
|
|
242
|
+
"block3a_expand_activation", # 128x128x144
|
|
243
|
+
"block2a_expand_activation", # 256x256x96
|
|
244
|
+
),
|
|
245
|
+
],
|
|
246
|
+
"efficientnetb2": [
|
|
247
|
+
ka.EfficientNetB2,
|
|
248
|
+
ka.efficientnet.preprocess_input,
|
|
249
|
+
(
|
|
250
|
+
"block6a_expand_activation", # 32x32x720
|
|
251
|
+
"block4a_expand_activation", # 64x64x288
|
|
252
|
+
"block3a_expand_activation", # 128x128x144
|
|
253
|
+
"block2a_expand_activation", # 256x256x96
|
|
254
|
+
),
|
|
255
|
+
],
|
|
256
|
+
"efficientnetb3": [
|
|
257
|
+
ka.EfficientNetB3,
|
|
258
|
+
ka.efficientnet.preprocess_input,
|
|
259
|
+
(
|
|
260
|
+
"block6a_expand_activation", # 32x32x816
|
|
261
|
+
"block4a_expand_activation", # 64x64x288
|
|
262
|
+
"block3a_expand_activation", # 128x128x192
|
|
263
|
+
"block2a_expand_activation", # 256x256x144
|
|
264
|
+
),
|
|
265
|
+
],
|
|
266
|
+
"efficientnetb4": [
|
|
267
|
+
ka.EfficientNetB4,
|
|
268
|
+
ka.efficientnet.preprocess_input,
|
|
269
|
+
(
|
|
270
|
+
"block6a_expand_activation", # 32x32x960
|
|
271
|
+
"block4a_expand_activation", # 64x64x336
|
|
272
|
+
"block3a_expand_activation", # 128x128x192
|
|
273
|
+
"block2a_expand_activation", # 256x256x144
|
|
274
|
+
),
|
|
275
|
+
],
|
|
276
|
+
"efficientnetb5": [
|
|
277
|
+
ka.EfficientNetB5,
|
|
278
|
+
ka.efficientnet.preprocess_input,
|
|
279
|
+
(
|
|
280
|
+
"block6a_expand_activation", # 32x32x1056
|
|
281
|
+
"block4a_expand_activation", # 64x64x384
|
|
282
|
+
"block3a_expand_activation", # 128x128x240
|
|
283
|
+
"block2a_expand_activation", # 256x256x144
|
|
284
|
+
),
|
|
285
|
+
],
|
|
286
|
+
"efficientnetb6": [
|
|
287
|
+
ka.EfficientNetB6,
|
|
288
|
+
ka.efficientnet.preprocess_input,
|
|
289
|
+
(
|
|
290
|
+
"block6a_expand_activation", # 32x32x1200
|
|
291
|
+
"block4a_expand_activation", # 64x64x432
|
|
292
|
+
"block3a_expand_activation", # 128x128x240
|
|
293
|
+
"block2a_expand_activation", # 256x256x192
|
|
294
|
+
),
|
|
295
|
+
],
|
|
296
|
+
"efficientnetb7": [
|
|
297
|
+
ka.EfficientNetB7,
|
|
298
|
+
ka.efficientnet.preprocess_input,
|
|
299
|
+
(
|
|
300
|
+
"block6a_expand_activation", # 32x32x1344
|
|
301
|
+
"block4a_expand_activation", # 64x64x480
|
|
302
|
+
"block3a_expand_activation", # 128x128x288
|
|
303
|
+
"block2a_expand_activation", # 256x256x192
|
|
304
|
+
),
|
|
305
|
+
],
|
|
306
|
+
# EfficientNetV2
|
|
307
|
+
# Stage layout differs from EfficientNet-Bx.
|
|
308
|
+
# Skip layers selected to match decoder resolutions:
|
|
309
|
+
# x16 -> x8 -> x4 -> x2
|
|
310
|
+
"efficientnetv2s": (
|
|
311
|
+
ka.EfficientNetV2S,
|
|
312
|
+
ka.efficientnet_v2.preprocess_input,
|
|
313
|
+
(
|
|
314
|
+
"block6a_expand_conv", # 32x32x960
|
|
315
|
+
"block4a_expand_conv", # 64x64x256
|
|
316
|
+
"block2a_expand_activation", # 128x128x96
|
|
317
|
+
"stem_activation", # 256x256x24
|
|
318
|
+
),
|
|
319
|
+
),
|
|
320
|
+
"efficientnetv2s-ss": (
|
|
321
|
+
ka.EfficientNetV2S,
|
|
322
|
+
ka.efficientnet_v2.preprocess_input,
|
|
323
|
+
(
|
|
324
|
+
"block5i_add", # 32x32x160
|
|
325
|
+
"block3d_add", # 64x64x64
|
|
326
|
+
"block2d_add", # 128x128x48
|
|
327
|
+
"stem_activation", # 256x256x24
|
|
328
|
+
),
|
|
329
|
+
),
|
|
330
|
+
"efficientnetv2m": (
|
|
331
|
+
ka.EfficientNetV2M,
|
|
332
|
+
ka.efficientnet_v2.preprocess_input,
|
|
333
|
+
(
|
|
334
|
+
"block6a_expand_conv", # 32x32x1056
|
|
335
|
+
"block4a_expand_conv", # 64x64x320
|
|
336
|
+
"block2a_expand_activation", # 128x128x96
|
|
337
|
+
"stem_activation", # 256x256x24
|
|
338
|
+
),
|
|
339
|
+
),
|
|
340
|
+
"efficientnetv2m-ss": (
|
|
341
|
+
ka.EfficientNetV2M,
|
|
342
|
+
ka.efficientnet_v2.preprocess_input,
|
|
343
|
+
(
|
|
344
|
+
"block5n_add", # 32x32x176
|
|
345
|
+
"block3e_add", # 64x64x80
|
|
346
|
+
"block2e_add", # 128x128x48
|
|
347
|
+
"stem_activation", # 256x256x24
|
|
348
|
+
),
|
|
349
|
+
),
|
|
350
|
+
"efficientnetv2l": (
|
|
351
|
+
ka.EfficientNetV2L,
|
|
352
|
+
ka.efficientnet_v2.preprocess_input,
|
|
353
|
+
(
|
|
354
|
+
"block6a_expand_conv", # 32x32x1344
|
|
355
|
+
"block4a_expand_conv", # 64x64x384
|
|
356
|
+
"block2a_expand_activation", # 128x128x128
|
|
357
|
+
"stem_activation", # 256x256x32
|
|
358
|
+
),
|
|
359
|
+
),
|
|
360
|
+
"efficientnetv2l-ss": (
|
|
361
|
+
ka.EfficientNetV2L,
|
|
362
|
+
ka.efficientnet_v2.preprocess_input,
|
|
363
|
+
(
|
|
364
|
+
"block5s_add", # 32x32x224
|
|
365
|
+
"block3g_add", # 64x64x96
|
|
366
|
+
"block2g_add", # 128x128x64
|
|
367
|
+
"stem_activation", # 256x256x32
|
|
368
|
+
),
|
|
369
|
+
),
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
@property
|
|
373
|
+
def models(self):
|
|
374
|
+
"""Get the dictionary of available backbones."""
|
|
375
|
+
return self._models
|
|
376
|
+
|
|
377
|
+
def models_names(self):
|
|
378
|
+
"""Get the list of available backbone names."""
|
|
379
|
+
return list(self.models.keys())
|
|
380
|
+
|
|
381
|
+
"""Get a backbone model by name."""
|
|
382
|
+
|
|
383
|
+
def get_backbone(self, name, *args, **kwargs) -> keras.Model:
|
|
384
|
+
"""Get a backbone model by name."""
|
|
385
|
+
backbone = self._models.get(name)
|
|
386
|
+
if backbone is None:
|
|
387
|
+
raise ValueError(f"Backbone with name '{name}' is not supported.")
|
|
388
|
+
|
|
389
|
+
model_fn, _, _ = backbone
|
|
390
|
+
model = model_fn(*args, **kwargs)
|
|
391
|
+
return model
|
|
392
|
+
|
|
393
|
+
def get_feature_layers(self, name, n=5):
|
|
394
|
+
"""Get the list of skip layers for a backbone by name."""
|
|
395
|
+
return self._models.get(name)[2][:n]
|
|
396
|
+
|
|
397
|
+
def get_preprocessing(self, name):
|
|
398
|
+
"""Get the preprocessing function for a backbone by name."""
|
|
399
|
+
return self._models.get(name)[1]
|
|
400
|
+
|
|
401
|
+
def get_custom_objects(self, name):
|
|
402
|
+
"""Get the custom objects for a backbone by name."""
|
|
403
|
+
if name == "inceptionresnetv2":
|
|
404
|
+
return irv2.get_custom_objects()
|
|
405
|
+
return {}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
Backbones = BackbonesFactory()
|
|
@@ -314,16 +314,18 @@ def conv2d_bn(
|
|
|
314
314
|
|
|
315
315
|
|
|
316
316
|
class CustomScaleLayer(layers.Layer):
|
|
317
|
-
|
|
317
|
+
"""Custom layer to scale the residuals before adding them to the shortcut branch."""
|
|
318
|
+
|
|
319
|
+
def __init__(self, scale, **kwargs): # noqa: D107
|
|
318
320
|
super().__init__(**kwargs)
|
|
319
321
|
self.scale = scale
|
|
320
322
|
|
|
321
|
-
def get_config(self):
|
|
323
|
+
def get_config(self): # noqa: D102
|
|
322
324
|
config = super().get_config()
|
|
323
325
|
config.update({"scale": self.scale})
|
|
324
326
|
return config
|
|
325
327
|
|
|
326
|
-
def call(self, inputs):
|
|
328
|
+
def call(self, inputs): # noqa: D102
|
|
327
329
|
return inputs[0] + inputs[1] * self.scale
|
|
328
330
|
|
|
329
331
|
|
|
@@ -416,6 +418,9 @@ def preprocess_input(x, data_format=None):
|
|
|
416
418
|
|
|
417
419
|
Args:
|
|
418
420
|
x: a 4D numpy array consists of RGB values within [0, 255].
|
|
421
|
+
data_format: image data format, either "channels_first" or
|
|
422
|
+
"channels_last". Defaults to the value found in
|
|
423
|
+
`keras.backend.image_data_format()`.
|
|
419
424
|
|
|
420
425
|
Returns:
|
|
421
426
|
Preprocessed array.
|
|
@@ -443,4 +448,5 @@ def decode_predictions(preds, top=5):
|
|
|
443
448
|
|
|
444
449
|
|
|
445
450
|
def get_custom_objects():
|
|
451
|
+
"""Returns a dictionary containing the custom objects used by the model."""
|
|
446
452
|
return {"CustomScaleLayer": CustomScaleLayer}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"""Inception V3 model for Keras.
|
|
2
|
+
|
|
2
3
|
Note that the input image format for this model is different than for
|
|
3
4
|
the VGG16 and ResNet models (299x299 instead of 224x224),
|
|
4
5
|
and that the input preprocessing function is also different (same as Xception).
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
References:
|
|
8
|
+
----------
|
|
6
9
|
- [Rethinking the Inception Architecture for Computer Vision](
|
|
7
|
-
http://arxiv.org/abs/1512.00567) (CVPR 2016)
|
|
10
|
+
http://arxiv.org/abs/1512.00567) (CVPR 2016).
|
|
8
11
|
"""
|
|
9
12
|
|
|
10
13
|
import os
|
|
@@ -30,6 +33,7 @@ WEIGHTS_PATH_NO_TOP = (
|
|
|
30
33
|
|
|
31
34
|
def conv2d_bn(x, filters, num_row, num_col, padding="same", strides=(1, 1), name=None):
|
|
32
35
|
"""Utility function to apply conv + BN.
|
|
36
|
+
|
|
33
37
|
# Arguments
|
|
34
38
|
x: input tensor.
|
|
35
39
|
filters: filters in `Conv2D`.
|
|
@@ -76,9 +80,11 @@ def InceptionV3(
|
|
|
76
80
|
**kwargs, # noqa: ARG001
|
|
77
81
|
):
|
|
78
82
|
"""Instantiates the Inception v3 architecture.
|
|
83
|
+
|
|
79
84
|
Optionally loads weights pre-trained on ImageNet.
|
|
80
85
|
Note that the data format convention used by the model is
|
|
81
86
|
the one specified in your Keras config at `~/.keras/keras.json`.
|
|
87
|
+
|
|
82
88
|
# Arguments
|
|
83
89
|
include_top: whether to include the fully-connected
|
|
84
90
|
layer at the top of the network.
|
|
@@ -372,6 +378,7 @@ def InceptionV3(
|
|
|
372
378
|
|
|
373
379
|
def preprocess_input(x, **kwargs):
|
|
374
380
|
"""Preprocesses a numpy array encoding a batch of images.
|
|
381
|
+
|
|
375
382
|
# Arguments
|
|
376
383
|
x: a 4D numpy array consists of RGB values within [0, 255].
|
|
377
384
|
# Returns
|
|
@@ -109,7 +109,6 @@ def ResNet(
|
|
|
109
109
|
Returns:
|
|
110
110
|
A ``keras.Model`` instance.
|
|
111
111
|
"""
|
|
112
|
-
|
|
113
112
|
# Determine proper input shape
|
|
114
113
|
input_shape = _obtain_input_shape(
|
|
115
114
|
input_shape,
|
|
@@ -358,6 +357,9 @@ def preprocess_input(x, data_format=None):
|
|
|
358
357
|
|
|
359
358
|
Args:
|
|
360
359
|
x: a 4D numpy array consists of RGB values within [0, 255].
|
|
360
|
+
data_format: image data format, either "channels_first" or
|
|
361
|
+
"channels_last". Defaults to the value found in
|
|
362
|
+
`keras.backend.image_data_format()`.
|
|
361
363
|
|
|
362
364
|
Returns:
|
|
363
365
|
Preprocessed array.
|