Network Architectures
Definition of building blocks for networks
get_keras_block(config, input_tensor=None, input_shape=None)
Create a block of connected keras layers based on the given configurations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config |
KerasModelConfig
|
configuration to create the layers. Consists of name of the model, its layers and params |
required |
input_tensor |
Optional[tf.Tensor]
|
a tensor of inputs to the model. If not provided, will be inferred from input_shape. If it isn't provided too, an error will be raised |
None
|
input_shape |
Optional[List[int]]
|
input shape of the block. Note that the shape doesn't include the batch dimension |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
tf.keras.models.Model
|
a functional model |
Source code in conftrainer/arch/blocks.py
get_preprocessor(config, input_shape)
Create a preprocessor with given configuration
Source code in conftrainer/arch/blocks.py
Functional models to use
BaseCNN
Bases: Model
Extends keras's native Model. Can be used to build a Functional model or to inherit and write custom loops
Methods
unfreeze_layers() : allows to unfreeze some layers of the network or its subnetworks. Usable for fine-tuning
Source code in conftrainer/arch/models.py
unfreeze_layers(freeze_layer_name, freeze_bn, unfreeze_all, submodel_name='backbone')
Unfreeze some layers of the network starting from layer_name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freeze_layer_name |
str
|
name of the layer to start unfreezing from |
required |
freeze_bn |
bool
|
whether to unfreeze all the BatchNormalization layers. If false, BatchNormalization layers will be treated as regular layers |
required |
unfreeze_all |
bool
|
whether to unfreeze the whole network |
required |
submodel_name |
str
|
submodel to unfreeze instead of the whole network. Is useful when the functional model consists of several other models as blocks |
'backbone'
|
Source code in conftrainer/arch/models.py
Classifier
Bases: BaseCNN
CNN classifier
Inherits from tf.keras.models.Model
Consists of 3 parts: - Augmenter - Backbone - Classification Head
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config |
ClassifierConfig
|
configuration with all necessary parameters. See ClassifierConfig for more details |
required |
Source code in conftrainer/arch/models.py
classification_head: Model
property
Classification head consisting of a prediction layer and optionally from hidden layers defined in config
preprocessor: Model
property
Preprocessor consisting of Normalize and Rescaling layers
create_classifier()
Create a classifier consisting of preprocessor, backbone and classification head
Source code in conftrainer/arch/models.py
call(inputs, training=False)
Run the model on given inputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs |
tf.Tensor
|
batch of inputs |
required |
training |
bool
|
whether to use training or inference mode. In inference mode Augmentation and Dropout layers are inactive, and BatchNormalization acts differently |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.Tensor
|
processed outputs |
Source code in conftrainer/arch/models.py
BYOL
Bases: BaseCNN
Implement a BYOL network
BYOL is a Siamese network consisting of: - Online network -> encoder + projection head + predictor - Target network -> encoder + projection head
BYOL creates 2 different augmentations of input image and runs them through online and target networks, then the online networks' weights are updated to minimize difference between its' and target networks predictions. Target network, on the other hand, is updated to have averaged weights of itself and the online network. This is handled by UpdateTarget callback
Parameters:
config : BYOLConfig configuration for creating a BYOL network. See BYOLConfig for more details
Methods:
call: augment the input and return their embeddings obtained from online and target network train_step: call the network and update the online network based on loss
Source code in conftrainer/arch/models.py
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
metrics: List[type(tf.keras.metrics.Metric)]
property
Override the metrics property of the base class
Define a metric tracker to use during training
Returns:
| Name | Type | Description |
|---|---|---|
out |
list
|
metric trackers of the network |
train_step(data)
Implementation of the training step for BYOL network
The method is called during fit method and represents a single training step, during which a batch of inputs is processed by the network and the parameters are updated
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
tf.Tensor
|
input of the network |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
dict
|
tracked metrics |
Source code in conftrainer/arch/models.py
test_step(inputs)
Overwrite the test step
Source code in conftrainer/arch/models.py
predict_step(data)
Infer on data using online encoder only. Overwrites predict_step method of the parent class
online_forward_pass(inputs, training=False)
Online network forward pass
Source code in conftrainer/arch/models.py
target_forward_pass(inputs, training=False)
Target network forward pass
Source code in conftrainer/arch/models.py
call(inputs, training=False)
Implement call method for the BYOL model
This function is used for fit, predict, evaluate methods
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs |
tf.Tensor
|
Inputs to the network. |
required |
training |
bool
|
Whether to use training (True) or inference (False) mode. During inference, augmentation layers are inactive |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
tuple of 2 tf.Tensors
|
Representations of the inputs obtained by online and target (teacher) networks respectively. |
Source code in conftrainer/arch/models.py
unfreeze_layers(freeze_layer_name, freeze_bn, unfreeze_all, **kwargs)
Unfreeze the same layers both in online and target networks
Source code in conftrainer/arch/models.py
fit(*args, **kwargs)
Add network specific callbacks before fitting the network
Source code in conftrainer/arch/models.py
multibranch_network(config)
Create a multibranch network based on given configuration
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config |
MultiBranchNetworkConfig
|
configuration for creating augmentor, preprocessor, backbone and output branches of the network |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
BaseCNN
|
keras functional with multiple outputs |