myco.architectures¶
Deep learning model architectures and functions for validating their structure.
aug_layer()
¶
Add a layer to randomy flip and rotate input features
Source code in myco/architectures.py
191 192 193 194 195 196 197 198 199 200 201 |
|
channel_transform_layer(layer, dropout_rate=0.0, batch_normalize=False)
¶
Apply a 1x1 (aka network-in-netowrk) convolution.
stats.stackexchange.com/questions/194142/what-does-1x1-convolution-mean-in-a-neural-network
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
Layer
|
the input feature layer |
required |
dropout_rate |
float
|
the fraction of random neurons to be set to zero |
0.0
|
batch_normalize |
bool
|
if batch normalization should be applied or not |
False
|
Returns:
Type | Description |
---|---|
Layer
|
the transformed layer space |
Source code in myco/architectures.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
compute_padding_dim(kernel_size, in_lyr=None, out_lyr=None)
¶
Calculates the dimensions of tf.pad function to match the shape of tensors with the desired shape, in layers followed by updampling and in the last layer the in/out-lyr helps acquiring matching shapes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel_size |
Tuple[int, int]
|
conv kernel size |
required |
in_lyr |
Layer
|
input layer |
None
|
out_lyr |
Layer
|
expected layer dimensions |
None
|
Returns:
Type | Description |
---|---|
the dimensions of padding to attain the out_lyr dimensions |
Source code in myco/architectures.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
conv2d_layer(layer, n_filters, kernel_size, padding, activation, activation_parameters, batch_normalize, dropout_rate, kernel_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, regularization_type, regularization_lambda, padding_dim=None)
¶
Create a 2D convolution layer with optional batch normalization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
Layer
|
Input convolution layer. |
required |
n_filters |
int
|
Filter count for initial convolutions. |
required |
kernel_size |
Tuple[int, int]
|
Kernel size used for local neighborhood convolutions. |
required |
padding |
str
|
One of ["valid", "same", "zeros", "symmetric", "reflect"]. "valid" means no padding. |
required |
activation |
str
|
Activation function used between layers. See Keras docs for details. |
required |
activation_parameters |
dict
|
Keywords to pass to non-standard activation functions. |
required |
batch_normalize |
bool
|
Use batch normalization layers, which are typically applied after convolutions. |
required |
dropout_rate |
float
|
The 0-1 proportion of layer weights to randomly set to 0 in each training step. |
required |
kernel_initializer |
str
|
Method for initializing layer weights. |
required |
kernel_regularizer |
bool
|
Apply regularization on the weights of kernel. |
required |
bias_regularizer |
bool
|
Apply regularization on the bias values. |
required |
activity_regularizer |
bool
|
Apply regularization on the layer output. |
required |
regularization_type |
str
|
norm of regularizer ("l1", "l2" or "l1_l2") |
required |
regularization_lambda |
float
|
regularization parameter is a scaler that defines the amount of regularization that is applied to the kernel weights, bias, and output. lambda reduces the variance of the estiamted parameters. Increasing this value encourages weights to towards smaller values. Very large lambda will prevent model from learning. |
required |
padding_dim |
Tuple[int, int]
|
2D padding dimensions. Infers this value from kernel_size if unspecified. |
None
|
Returns:
Type | Description |
---|---|
Layer
|
Keras layer for the start of the network. |
Source code in myco/architectures.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
dense_2d_block(layer, opts, block_depth)
¶
Create a dense 2D convolution layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
Layer
|
Input layer to the convolution. |
required |
opts |
dict
|
All options to pass into the input convolution layer. |
required |
block_depth |
int
|
Layer count for the dense block. |
required |
Returns:
Type | Description |
---|---|
Layer
|
Keras layer for the start of the network. |
Source code in myco/architectures.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
dense_unet(input_shape, n_classes=dense_unet_config.n_classes, n_filters=dense_unet_config.n_filters, filter_growth=dense_unet_config.filter_growth, kernel_size=dense_unet_config.kernel_size, block_structure=dense_unet_config.block_structure, internal_activation=dense_unet_config.internal_activation, output_activation=dense_unet_config.output_activation, output_dtype=dense_unet_config.output_dtype, activation_parameters=unet_config.activation_parameters, padding=dense_unet_config.padding, pool_size=dense_unet_config.pool_size, batch_normalize=dense_unet_config.batch_normalize, dropout_rate=dense_unet_config.dropout_rate, kernel_initializer=dense_unet_config.kernel_initializer, augmentation_layer=dense_unet_config.augmentation_layer, feature_transform=dense_unet_config.feature_transform, kernel_regularizer=unet_config.kernel_regularizer, bias_regularizer=unet_config.bias_regularizer, activity_regularizer=unet_config.activity_regularizer, regularization_type=unet_config.regularization_type, regularization_lambda=unet_config.regularization_lambda)
¶
Create a Dense U-Net architecture based on custom feature dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shape |
Tuple[int, int, int]
|
Input feature dimensions, in [width, height, n_bands] order. |
required |
n_classes |
int
|
Number of unique input classes. Set to 1 for regression models. |
dense_unet_config.n_classes
|
n_filters |
int
|
Filter count for initial convolutions. Increases if |
dense_unet_config.n_filters
|
filter_growth |
bool
|
Doubles the number of convolution filters. |
dense_unet_config.filter_growth
|
kernel_size |
Tuple[int, int]
|
Kernel size used for local neighborhood convolutions. |
dense_unet_config.kernel_size
|
block_structure |
List[int]
|
Layer count in each network block. The tuple length is the number of
network blocks, and the value of each element is the number of layers in that block.
|
dense_unet_config.block_structure
|
internal_activation |
str
|
Activation function used between layers. See Keras docs for details. |
dense_unet_config.internal_activation
|
output_activation |
str
|
Activation function for the final output layer. |
dense_unet_config.output_activation
|
output_dtype |
str
|
Data type of the final activation function. |
dense_unet_config.output_dtype
|
activation_parameters |
dict
|
Parameters to pass to activation functions with non-standard keywords
(e.g. |
unet_config.activation_parameters
|
padding |
str
|
One of ["valid", "zeros", "symmetric", "reflect"]. valid means no padding. See www.tensorflow.org/api_docs/python/tf/pad for others. |
dense_unet_config.padding
|
pool_size |
Tuple[int, int]
|
Pooling and upsampling size during each downsampling/upsampling step. |
dense_unet_config.pool_size
|
batch_normalize |
bool
|
Use batch normalization layers, which are typically applied after convolutions. |
dense_unet_config.batch_normalize
|
dropout_rate |
float
|
The 0-1 proportion of layer weights to randomly set to 0 in each training step. |
dense_unet_config.dropout_rate
|
kernel_initializer |
str
|
Method for initializing weights. See keras.io/api/layers/initializers |
dense_unet_config.kernel_initializer
|
augmentation_layer |
bool
|
Whether the augmentation layer is on/off |
dense_unet_config.augmentation_layer
|
feature_transform |
bool
|
Apply 1x1 convolutions as the first model layer |
dense_unet_config.feature_transform
|
kernel_initializer |
str
|
Method for initializing layer weights (see keras.io/api/layers/initializers). |
dense_unet_config.kernel_initializer
|
kernel_regularizer |
bool
|
if the conv layer should apply regularization on the weights of kernel |
unet_config.kernel_regularizer
|
bias_regularizer |
bool
|
if the conv layer should apply regularization on the bias values |
unet_config.bias_regularizer
|
activity_regularizer |
bool
|
if the conv layer should apply regularization on the layer output |
unet_config.activity_regularizer
|
regularization_type |
bool
|
norm of regularizer (l1, l2 or l1_l2) |
unet_config.regularization_type
|
regularization_lambda |
bool
|
regularization parameter is a scaler that defines the amount of regularization that is applied to the kernel weights, bias, and output. lambda reduces the variance of the estiamted parameters. Increasing this value encourages weights to towards smaller values. Very large lambda will prevent model from learning. |
unet_config.regularization_lambda
|
Returns:
Type | Description |
---|---|
keras.models.Model
|
Keras Dense U-Net model. |
Source code in myco/architectures.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
|
get_arch_config(name)
¶
Return the configuration parameters for a specific architecture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the model architecture. Get available options from get_arch_names(). |
required |
Returns:
Type | Description |
---|---|
dict
|
Default architecture parameters as a dictionary. |
Source code in myco/architectures.py
671 672 673 674 675 676 677 678 679 680 681 |
|
get_arch_model(name)
¶
Return an un-compiled keras model for a specific architecture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the model architecture. Get available options from get_arch_names(). |
required |
Returns:
Type | Description |
---|---|
keras.models.Model
|
The corresponding keras model. |
Source code in myco/architectures.py
684 685 686 687 688 689 690 691 692 693 694 |
|
get_arch_names()
¶
Return a list of valid model architecture names
Source code in myco/architectures.py
666 667 668 |
|
get_regularizer(norm='l1', l=1.0)
¶
Add regularization to a convolution layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
norm |
str
|
the type of normalization to apply choices: [l1, l2, l1_l2, both] |
'l1'
|
l |
float
|
the regularization lambda penalty to apply |
1.0
|
Returns:
Type | Description |
---|---|
tf.keras.regularizers.Regularizer
|
beta coefficient regularization layer |
Source code in myco/architectures.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
pad_2d(layer, padding_dim=[1, 1], padding_mode='CONSTANT')
¶
Pads the input layer using the mode and padding dimensions
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
Layer
|
input layer to be padded |
required |
padding_dim |
Tuple[int, int]
|
dimensions of 2d padding including height and width |
[1, 1]
|
padding_mode |
str
|
padding mode using either symmetric, reflect or constant(zero padded) |
'CONSTANT'
|
Returns:
Type | Description |
---|---|
padded layer |
Source code in myco/architectures.py
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 |
|
residuals_layer(input, building_block)
¶
Add a residual estimation layer
openaccess.thecvf.com/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
Layer
|
the block's input layer prior to 2D convolutions |
required |
building_block |
Layer
|
the post-2D convolutions layer |
required |
Returns:
Type | Description |
---|---|
Layer
|
the sum of the input layers |
Source code in myco/architectures.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
unet(input_shape, n_classes=unet_config.n_classes, n_filters=unet_config.n_filters, filter_growth=unet_config.filter_growth, kernel_size=unet_config.kernel_size, block_structure=unet_config.block_structure, internal_activation=unet_config.internal_activation, output_activation=unet_config.output_activation, output_dtype=unet_config.output_dtype, activation_parameters=unet_config.activation_parameters, padding=unet_config.padding, pool_size=unet_config.pool_size, batch_normalize=unet_config.batch_normalize, dropout_rate=unet_config.dropout_rate, kernel_initializer=unet_config.kernel_initializer, augmentation_layer=unet_config.augmentation_layer, feature_transform=unet_config.feature_transform, residuals_transform=unet_config.residuals_transform, kernel_regularizer=unet_config.kernel_regularizer, bias_regularizer=unet_config.bias_regularizer, activity_regularizer=unet_config.activity_regularizer, regularization_type=unet_config.regularization_type, regularization_lambda=unet_config.regularization_lambda)
¶
Create a U-Net architecture based on custom feature dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_shape |
Tuple[int, int, int]
|
Input feature dimensions, in [width, height, n_bands] order. |
required |
n_classes |
int
|
Number of unique input classes. Set to 1 for regression models. |
unet_config.n_classes
|
n_filters |
int
|
Filter count for initial convolutions. Increases if |
unet_config.n_filters
|
filter_growth |
bool
|
Doubles the number of convolution filters. |
unet_config.filter_growth
|
kernel_size |
Tuple[int, int]
|
Kernel size used for local neighborhood convolutions. |
unet_config.kernel_size
|
block_structure |
List[int]
|
Layer count in each network block. The tuple length is the number of
network blocks, and the value of each element is the number of layers in that block.
|
unet_config.block_structure
|
internal_activation |
str
|
Activation function used between layers. See Keras docs for details. |
unet_config.internal_activation
|
output_activation |
str
|
Activation function for the final output layer. |
unet_config.output_activation
|
output_dtype |
str
|
Data type of the final activation function. |
unet_config.output_dtype
|
activation_parameters |
dict
|
Parameters to pass to activation functions with non-standard keywords
(e.g. |
unet_config.activation_parameters
|
padding |
str
|
One of ["valid", "zeros", "symmetric", "reflect"]. valid means no padding. See www.tensorflow.org/api_docs/python/tf/pad for others. |
unet_config.padding
|
pool_size |
Tuple[int, int]
|
Pooling and upsampling size during each downsampling/upsampling step. |
unet_config.pool_size
|
batch_normalize |
bool
|
Use batch normalization layers, which are typically applied after convolutions. |
unet_config.batch_normalize
|
dropout_rate |
float
|
The 0-1 proportion of layer weights to randomly set to 0 in each training step. |
unet_config.dropout_rate
|
kernel_initializer |
str
|
Method for initializing weights. See keras.io/api/layers/initializers |
unet_config.kernel_initializer
|
augmentation_layer |
bool
|
Whether the augmentation layer is on/off |
unet_config.augmentation_layer
|
feature_transform |
bool
|
Apply 1x1 convolutions as the first model layer |
unet_config.feature_transform
|
residuals_transform |
bool
|
Apply a residuals shortcut layer |
unet_config.residuals_transform
|
kernel_initializer |
str
|
Method for initializing layer weights (see keras.io/api/layers/initializers). |
unet_config.kernel_initializer
|
kernel_regularizer |
bool
|
if the conv layer should apply regularization on the weights of kernel |
unet_config.kernel_regularizer
|
bias_regularizer |
bool
|
if the conv layer should apply regularization on the bias values |
unet_config.bias_regularizer
|
activity_regularizer |
bool
|
if the conv layer should apply regularization on the layer output |
unet_config.activity_regularizer
|
regularization_type |
bool
|
norm of regularizer (l1, l2 or l1_l2) |
unet_config.regularization_type
|
regularization_lambda |
bool
|
regularization parameter is a scaler that defines the amount of regularization that is applied to the kernel weights, bias, and output. lambda reduces the variance of the estiamted parameters. Increasing this value encourages weights to towards smaller values. Very large lambda will prevent model from learning. |
unet_config.regularization_lambda
|
Returns:
Type | Description |
---|---|
keras.models.Model
|
Keras U-Net model. |
Source code in myco/architectures.py
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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|