Preprocessing

This library also implements some general transformations useful for getting data ready to be put into a model. They work on ArrayItems and, for ImageToTensor and OneHot, on the corresponding spatial items.

Arrays

Preprocessing steps are transformations, so they compose with |> like any other:

using DataAugmentation, Images

image = Image(rand(RGB{Float32}, 40, 50))
tfm = ImageToTensor() |> Normalize((0.5, 0.5, 0.5), (0.2, 0.2, 0.2))
size(itemdata(tfm(image)))
(50, 40, 3)

One-hot encoding a mask with 4 classes adds a dimension for the classes:

mask = MaskMulti(rand(1:4, 8, 8), 1:4)
tfm = OneHot()
size(itemdata(tfm(mask)))
(8, 8, 4)

Tabular data

A row of a table is wrapped in a TabularItem(data, columns), where columns are the names of the columns of data. Three transformations prepare rows for a model; they are not exported, so import them from DataAugmentation:

  • DataAugmentation.FillMissing(dict, cols) fills missing values in the columns cols with the values given in dict.
  • DataAugmentation.Categorify(dict, cols) label encodes the values in the columns cols using the unique values given in dict. A value is replaced by its position in that list plus one, and a missing value by 1.
  • DataAugmentation.NormalizeRow(dict, cols) normalizes the values in the columns cols with the mean and standard deviation given in dict.
using DataAugmentation: FillMissing, Categorify, NormalizeRow

cols = [:age, :city, :income]
item = TabularItem((; zip(cols, [missing, "rome", 52_000.0])...), cols)

tfm = FillMissing(Dict(:age => 40), [:age]) |>
    Categorify(Dict(:city => ["milan", "rome"]), [:city]) |>
    NormalizeRow(Dict(:income => (50_000.0, 10_000.0)), [:income])
itemdata(tfm(item))
(age = 40, city = 3, income = 0.2)