library("rplatypus")
masks <- predict(fit, "unet") # an array, on the model's grid
masks <- predict(fit, "unet", space = "source") # a list, on each scan's own grid
# The two worst cases, without holding the other 198.
cases <- evaluate_cases(fit, split = "validation")
worst <- cases$case[order(cases$dice)][1:2]
two <- predict(fit, split = "validation", cases = worst)
# Or walk the split, keeping one number per image and none of the masks.
coverage <- predict(fit, split = "validation", each = function(case, mask) mean(mask > 1))
# `space = "source"` returns a list and not an array, which is the API being
# honest: scans differ in size, a stacked array needs one shape, and resizing
# them to match is how a mask ends up describing anatomy it was not computed from.Predict masks, boxes, objects or labels
Description
One function per fit rather than one per task, because the question - what does this model say about this split - is the same one. What comes back is not: a mask for segmentation, a table of boxes for detection, a label map of objects for instances, and a row per image for classification. The arguments that describe a grid are refused where there is no grid, by name, rather than accepted and ignored.
Usage
## S3 method for class 'platypus_fit'
predict(
object,
model = NULL,
split = "test",
type = c("class", "probability", "channels"),
space = c("model", "source"),
cases = NULL,
each = NULL,
decode = NULL,
...
)
Arguments
object
|
A platypus_fit().
|
model
|
Which model, by the name given in the specification. Defaults to the first one trained. |
split
|
Which data to predict on. |
type
|
On an instance fit, With This page named “class” gives one row per image with the answer and how sure the model was, and “probability” gives one row per image and one column per class. “channels” is refused - a classifier has no channels, only one number per class, and those numbers are the probabilities.
|
space
|
“model” or “source”; see above. Note that the two return different shapes of thing, an array and a list, because they are different things. Refused on a detection, instance or classification fit, each for its own reason - boxes are in the source image’s coordinates already, an object identity cannot survive resampling, and a label is not on a grid at all.
|
cases
|
Case names to predict, as evaluate_cases() reports them. With this the result is a named list of just those, in the order asked for. A name that is not in the split is an error rather than a missing entry. Both this and each exist so that a split of full-resolution masks never has to be in memory at once, so both are refused on a classification fit, where the whole answer is one row per image.
|
each
|
A function called as each(case, mask) once per image, instead of the predictions being returned together. Whatever it returns is collected under the case’s name; return NULL from it and nothing accumulates. This is the form that does not need the split to fit in memory.
|
decode
|
Which decoding turns the predicted channels into objects, by the name used in the model’s decode list. Unset takes the model’s first, which is the engine’s own default rather than one restated here. Instance models only; for anything else it is refused, because a semantic or detection fit decodes nothing.
|
…
|
Must be empty; a misspelt argument is refused rather than ignored, which is what it used to be. maju116/rplatypus#315.
|
Value
With space = “model”: for “class”, an integer array of image x height x width, classes numbered from 1 to match the colormap; for “probability”, the same with a trailing class axis. Tiled models return images at their original size, reassembled.
With space = “source”: a list of such arrays, one per scan, each on that scan’s grid, named by case. The name and the mask come out of one step of the engine’s own stream, so they cannot be misaligned - which matters because the usual next move is to join them to something else per case, and a list paired by position is one dropped batch away from naming the wrong picture.
On an instance fit: a list of integer label maps named by case, objects numbered from 1 and 0 for background, one entry per image. A list rather than an array for the reason the detection one is a list, and named rather than ordered because a list paired against case names by position is one dropped batch away from naming the wrong picture. Draw them with overlay_instances() and measure them with instance_sizes().
On a classification fit: for “class”, a data frame of one row per image with key (the name the split calls it, which is what joins this to anything else), label (what it was labelled, or NA on a split that carries no labelling), predicted and probability - the probability of the class that was chosen, not of the right one. For “probability”, a numeric matrix of images by classes, with the keys as row names and the class names as columns, in the order classes was written.
With labels_from, where a label is a set, “class” is a longer frame instead: one row per image per class, with key, class, and label and predicted as logicals. It is a different answer rather than the same answer with a longer string, and this is the shape that says so - an image with none of the findings is a row of FALSE per class rather than an empty cell, which could also mean unread. Summarise per class with one tapply, or reshape it if you want it wide.
There is no probability column on that route, and it is missing on purpose: that column is the chosen class’s own number, and with independent questions nothing was chosen. “probability” is the whole of the answer there and comes back as the same matrix, one column per class.
Which grid the answer is on
space = “model” returns everything on the grid the model worked at, stacked into one array. That is what training saw, and it is the only form that can be a single array, because a stack needs one shape.
space = “source” maps each prediction back onto the grid of the scan it was computed from - undoing the resampling and the crop - and therefore returns a list, one mask per scan. This is what you want for anything that has to meet the original data: laying a mask over the scan in a viewer, writing it beside the scan with save_volumes(), or measuring it in millilitres of the scan’s own voxels. Scans differ in size, so the list is not a limitation being worked around - resizing them to a common shape is exactly how a mask ends up describing anatomy it was not computed from.
Two things are lost mapping back, and neither can be recovered. Interpolation softens a boundary, so the mask returns slightly smoother than the model drew it. And where reading cropped anatomy away, the mask comes back padded with background - which means not examined, not nothing there. Give the model an input_shape that covers the anatomy if that distinction matters.
One at a time, when the split will not fit
The default returns everything together, which for a tiled run is the whole split at full resolution: 200 fundus photographs at 2048 x 2048 is about 6.7 GB before anything is drawn, and the engine’s own predict was killed by exactly that. Two arguments avoid it.
cases names the ones wanted. each hands them over one at a time and keeps only what the function gives back. They compose: cases says which, each says what to do with them.
The saving is in memory and not in time. The engine’s stream is sequential, so asking for two cases out of two hundred still runs the model over two hundred - it simply never holds more than one answer at once.