Skip to main content

A contracting path, pooled, with one dense layer on top

Description

The model block for task = “classification”, and the one model constructor in this package that is not named after an architecture. u_net(), linknet(), res_u_net() and u_net_plus_plus() choose between four decoders that read the same features; a classifier has nothing after the pooling but a dense layer, so the contracting path is the architecture and encoder is the whole answer. The engine has no architecture field here for that reason, and the name follows its own build_classifier.

Usage

classifier(
  name,
  input_shape,
  channels = NULL,
  blocks = NULL,
  filters = NULL,
  block_width = NULL,
  pooling = NULL,
  head_dropout = NULL,
  dropout = NULL,
  batch_normalization = NULL,
  separable_conv = NULL,
  spatial_dropout = NULL,
  activation = NULL,
  initialiser = NULL,
  loss = NULL,
  metrics = NULL,
  optimizer = NULL,
  callbacks = NULL,
  augmentation = NULL,
  epochs = NULL,
  batch_size = NULL,
  weights = NULL,
  fit = NULL,
  encoder = NULL,
  pretrained = NULL,
  normalise = NULL,
  freeze_encoder = NULL,
  encoder_learning_rate = NULL
)

Arguments

name A name for the model, used in the score table and in file names.
input_shape The size every image is read at, c(height, width). Unlike a segmentation model nothing has to divide by 2^blocks - there is no decoder to line up with - but every side must still be at least 2^blocks, or the deepest level has nothing left to pool.
channels Input channels: 3 for colour, 1 for greyscale.
blocks How many times the resolution is halved before the features are pooled, so how much context the answer is made from. Ignored, and refused, when encoder names a backbone - the backbone’s own depth is then the only one there is.
filters Filters in the first block, doubled at every level, so this and blocks set how wide the pooled feature vector is: filters * 2^blocks. Ignored, and refused, with encoder.
block_width Convolutions per block, at every level. Raising it adds depth without adding levels, which is the lever to reach for when the objects are small enough that halving the resolution again would lose them - a crop of one cell is exactly that case. Ignored, and refused, with encoder.
pooling How the feature map becomes one vector per image. “avg” averages evidence over the whole frame, which is what every classification head since the networks that replaced the flattened dense layer does; “max” takes the strongest response anywhere, which is the one to reach for when the thing being looked for is small and somewhere in the picture rather than the picture itself.
head_dropout Drop this fraction of the pooled vector before the dense layer. Separate from dropout, which sits between convolutions and reaches nothing at all when the contracting path is a pretrained backbone with its own regularisation - this is then the only one left.
dropout, batch_normalization, separable_conv, spatial_dropout Regularisation and shape of the contracting path, as in u_net().
activation, initialiser The non-linearity between convolutions, and how the weights start.
loss The objective. Fewer are admissible here than anywhere else in this package, and the two that are - loss_cce() and loss_focal() - are the ones that score each answer on its own rather than scoring an overlap. available_losses(“classification”) lists them and the engine’s refusal names them.
metrics What to report beside the loss. metric_auc() is the engine’s default and that is a measurement rather than a preference: at a 3% prevalence a calibrated model never crosses the threshold, so accuracy, balanced accuracy and F1 all report a real signal and a collapse as the same number. See metric_auc().
optimizer, callbacks, augmentation, epochs, batch_size, weights, fit As in u_net(). A callback watching val_<metric> is refused when nothing in metrics will produce it, and there is no foreground to watch - a classification epoch has no background class and reports no such share.
encoder A pretrained backbone instead of our own contracting path, by name - see encoders for the table and what it is actually good for. With this, blocks, filters and block_width are refused.
pretrained, freeze_encoder, encoder_learning_rate How the backbone arrives and how it is trained, as in u_net().
normalise Normalise the input with the backbone’s own ImageNet statistics, defaulting to pretrained. Set it TRUE with pretrained = FALSE to reload weights you exported from a pretrained encoder without a network: the statistics come from the backbone’s configuration rather than from its weights. See u_net for the whole of it; it is recorded in the weights’ fingerprint either way, and setting it TRUE needs encoder, because the statistics belong to a backbone.

Details

n_class is not an argument, for the reason it is not one on u_net() either: the number of classes comes from classes in classification_data(), and a model that also declared it could only ever disagree with its own data.

Value

A model specification, to be passed to platypus_spec().

See Also

classification_data(), metric_auc()

Examples

library("rplatypus")

classifier("cnn", input_shape = c(128, 128), blocks = 3, filters = 16)
$name
[1] "cnn"

$input_shape
[1] 128 128

$blocks
[1] 3

$filters
[1] 16

attr(,"task")
[1] "classification"
# A pretrained backbone instead of ours, with its early levels held still
classifier("resnet", input_shape = c(224, 224), encoder = "resnet34",
           pretrained = TRUE, freeze_encoder = 2)
$name
[1] "resnet"

$input_shape
[1] 224 224

$encoder
[1] "resnet34"

$pretrained
[1] TRUE

$freeze_encoder
[1] 2

attr(,"task")
[1] "classification"