Skip to contents

This is a torch module with This is a simple CNN with three convolutional layers and two dense layers. It works well with the default dimensions 32x192 from the captcha_transform_image() function. It also uses batch normalization in the forward method. This function can be used either to fit a Captcha model using the luz workflow suggested in vignette("advanced") or as a base code to develop custom models.

Usage

net_captcha(
  input_dim,
  output_ndigits,
  output_vocab_size,
  vocab,
  transform,
  dropout = c(0.25, 0.25),
  dense_units = 400
)

Arguments

input_dim

(integer, integer): image input dimensions.

output_ndigits

number of tokens for each Captcha.

output_vocab_size

number of unique token values.

vocab

token labels.

transform

input transform function (for prediction purposes)

dropout

(float, float) AlexNet dropout values.

dense_units

Number of dense units

Value

object of classes CAPTCHA-CNN and nn_module. It works as a predictive function and as the input to a luz fitting workflow.

Examples


if (torch::torch_is_installed()) {
  # raw image
  captcha_file <- system.file(
    "examples/captcha/tjmg.jpeg",
    package = "captcha"
  )

  # initializes model. The output_ndigits, output_vocab_size and vocab
  # parameters are compatible to the TJMG Captcha.
  model <- net_captcha(
    input_dim = c(32, 192),
    output_ndigits = 5,
    output_vocab_size = 10,
    vocab = 0:9,
    transform = captcha_transform_image,
    dropout = c(.25, .25),
    dense_units = 400
  )
  model

  transformed <- model$transform(captcha_file)

  # tensor of size 1 x output_ndigits x output_vocab_size
  prediction <- model(transformed)
  dim(prediction)

  # get the predicted labels
  # the model is awful, because it is not fitted yet.
  indices <- as.numeric(torch::torch_argmax(prediction, 3))
  label <- paste(model$vocab[indices], collapse = "")
}