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
ToEltype(T)converts the element type of anyDataAugmentation.AbstractArrayItemtoT.ImageToTensorconverts a 2D image to anArrayIteminWHCorder. A data loader adds the batch dimension for Flux. See Using with Flux.PermuteDims(perm)permutes the dimensions of anArrayItem, for example to turnWHCintoHWC.Normalize(means, stds)normalizes image tensors along the channel dimension.NormalizeIntensity()normalizes an array with its own mean and standard deviation.LinearTransformation(matrix; mean)subtractsmeanfrom a flattened array and multiplies it by a squarematrix, for example to whiten it.OneHotto one-hot encode multi-class masks (MaskMultis);onehot(x, n)one-hot encodes a single class index.MapElem(f)applies a function to every element of an array item.
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 columnscolswith the values given indict.DataAugmentation.Categorify(dict, cols)label encodes the values in the columnscolsusing the unique values given indict. A value is replaced by its position in that list plus one, and a missing value by1.DataAugmentation.NormalizeRow(dict, cols)normalizes the values in the columnscolswith the mean and standard deviation given indict.
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)