Skip to main content

How much of each class a mask covers

Description

A quick sanity check before training: a foreground of half a percent is a problem the loss has to be chosen for, and it is better to know before the run than after.

Usage

mask_coverage(mask, labels = NULL, n_class = NULL)

Arguments

mask Class indices, counted from 1.
labels Optional names for the classes. Also what says how many classes there are, when the mask does not hold them all - see the note below.
n_class How many classes the mask is meant to have, when labels was not given. Unset means the largest value present, which is only right when every class occurs.

Value

A data frame with one row per class, including the classes with no pixels.

A class above the largest value present used to have no row at all, which was the one case this function is for: tabulate() sizes its table by the largest value it sees, so a three-class mask holding only 1 and 2 reported two classes and said nothing about the third. The highest-numbered class is the usual position of a rare foreground, so the row that went missing was the one worth reading. maju116/rplatypus#255.

Examples

library("rplatypus")

mask <- matrix(1L, 8, 8); mask[2:4, 2:4] <- 2L
mask_coverage(mask, labels = c("background", "nucleus"))
  class      label pixels fraction
1     1 background     55 0.859375
2     2    nucleus      9 0.140625
# A class with 0 pixels is the thing to look for: its target channel is zero
# everywhere, so there is no gradient towards it and the model is never shown
# what it is being asked to find.