Welcome to Ballfish’s documentation!

ballfish.create_augmentation(operations: Sequence[Args]) Callable[[Datum, Random], Datum]

Main function to create augmentation function.

class ballfish.distribution.Require(name: str, minimum: float | None = None, maximum: float | None = None, is_int: bool = False)

A way to limit create_distribution() expected values

ballfish.distribution.create_distribution(kwargs: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float, require: Require | None = None) Callable[[Random], float]
Available Distributions

Name

Parameters

Distribution

uniform

a=0, b=0.5

_images/uniform_000_050.svg

uniform

a=-0.75, b=0.75

_images/uniform_075_075.svg

truncnorm

mu=0.0, sigma=0.75, delta=1.0

_images/truncnorm_000_075_100.svg

truncnorm

mu=0.4, sigma=0.3, delta=1.0

_images/truncnorm_040_030_100.svg

truncnorm

mu=0.0, sigma=0.5, a=0.0, b=1.0

_images/truncnorm_000_050_000_100.svg

constant

value=0.25

_images/constant_025.svg

randrange

start=-1, stop=2

_images/randrange_-1_2.svg

choice

values = [-0.1, 0.1, 1]

_images/choice.svg

choice

values = [(-0.1, 30), (0.1, 60), (1, 10)]

_images/choice_with_probability.svg
Parameters:

require – optional Require class that will ensure that distribution is in required range

Example

>>> ballfish.create_distribution({"name": "truncnorm", "a": -0.25, "b": 0.25})
<function create_distribution.<locals>._truncnorm at 0x7feb7e166b60>
class ballfish.transformation.Datum(source: Tensor | None = None, quad: tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]] | None = None, width: int | None = None, height: int | None = None, image: Tensor | None = None)

Input and output class for augmentation

source

Main input that Rasterize takes as input. Expected to be in (N, C, H, W) format. This input unfortunately must be in torch.float{32,64} formats because torch’s torch.nn.functional.grid_sample doesn’t work with other types.

quads: list[tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]]

rois

width

Width for Rasterize and for calculating projective transformations

height

Height

image

the output image, must be None when Rasterize is used

class ballfish.transformation.Projective1pt(x: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float, y: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Shifts one point of the quadrangle in random direction.

_images/projective1pt.svg
{
    "name": "projective1pt",
    "x": {"name": "truncnorm", "a": -0.25, "b": 0.25},
    "y": {"name": "uniform", "a": -0.25, "b": 0.25}
}
class ballfish.transformation.Projective4pt(x: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float, y: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Shifts four point of the quadrangle in random direction.

_images/projective4pt.svg
{
    "name": "projective4pt",
    "x": {"name": "truncnorm", "a": -0.25, "b": 0.25},
    "y": {"name": "uniform", "a": -0.25, "b": 0.25}
}
class ballfish.transformation.Flip(direction: Literal['horizontal', 'vertical', 'primary_diagonal', 'secondary_diagonal'] = 'horizontal')

Flips the quadrangle vertically or horizontally. Only changes points order, that is, visually the quadrangle doesn’t change, but its visualization does.

For diagonal names see: https://en.wikipedia.org/wiki/Main_diagonal

static horizontal(q: tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]) tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]
_images/flip_horizontal.svg
{"name": "flip", "direction": "horizontal"}
static vertical(q: tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]) tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]
_images/flip_vertical.svg
{"name": "flip", "direction": "vertical"}
static primary_diagonal(q: tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]) tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]
_images/flip_primary_diagonal.svg
{"name": "flip", "direction": "primary_diagonal"}
static secondary_diagonal(q: tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]) tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]
_images/flip_secondary_diagonal.svg
{"name": "flip", "direction": "secondary_diagonal"}
class ballfish.transformation.PaddingsAddition(top: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, right: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, bottom: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, left: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0)

Adds random padding to the quadrangle sides.

_images/paddings_addition.svg
{
    "name": "paddings_addition",
    "top": {"name": "uniform", "a": 0, "b": 0.25},
    "right": {"name": "uniform", "a": 0, "b": 0.25},
    "bottom": {"name": "uniform", "a": 0, "b": 0.25},
    "left": {"name": "uniform", "a": 0, "b": 0.25}
}
class ballfish.transformation.ProjectivePaddingsAddition(top: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, right: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, bottom: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, left: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0)

Same as PaddingsAddition, but addition respects original projective transformation.

_images/projective_paddings_addition.svg
{
    "name": "projective_paddings_addition",
    "top": {"name": "uniform", "a": 0, "b": 0.25},
    "right": {"name": "uniform", "a": 0, "b": 0.25},
    "bottom": {"name": "uniform", "a": 0, "b": 0.25},
    "left": {"name": "uniform", "a": 0, "b": 0.25}
}
class ballfish.transformation.Rotate(angle_deg: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Rotates the quadrangle around its center.

_images/rotate.svg
{
    "name": "rotate",
    "angle_deg": {"name": "uniform", "a": 0, "b": 360}
}
class ballfish.transformation.ProjectiveShift(x: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0, y: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float = 0.0)

Projectively shifts the quadrangle.

_images/projective_shift.svg
{
    "name": "projective_shift",
    "x": {"name": "truncnorm", "a": -3.1, "b": 3.1}
}
class ballfish.transformation.Scale(factor: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Scales the quadrangle to the factor specified in the distribution.

_images/scale.svg
{
    "name": "scale",
    "factor": {"name": "truncnorm", "a": 0.7, "b": 1.3}
}
class ballfish.transformation.Rasterize(mode: Literal['bilinear', 'nearest', 'bicubic'] = 'bilinear', padding_mode: Literal['zeros', 'border', 'reflection'] = 'zeros')

Rasterizes the image from quadrangle using projective transform and the size specified in Datum.

{"name": "rasterize"}
class ballfish.transformation.Noise(std: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float, mean: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float | None = None, type: Literal['heteroscedastic', 'homoscedastic'] = 'homoscedastic')

Adds normal noise to the image numpy.random.RandomState.normal.

_images/noise_homoscedastic.svg
{
    "name": "noise",
    "std": {"name": "truncnorm", "a": 0, "b": 0.1}
}
_images/noise_heteroscedastic.svg
{
    "name": "noise",
    "std": {"name": "truncnorm", "a": 0, "b": 0.1},
    "type": "heteroscedastic"
}
class ballfish.transformation.OperationTransform(per: Literal['channel', 'batch', 'tensor'])

Allow operation applying to “channel”, “batch” or “tensor”

class ballfish.transformation.Add(value: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float | Sequence[UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float], per: Literal['channel', 'batch', 'tensor'] = 'tensor')

Add the value to Datum.image

_images/add.svg
{
    "name": "add",
    "value": {
        "name": "truncnorm",
        "a": -0.333,
        "b": 0.333,
    }
}
class ballfish.transformation.Multiply(factor: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float | Sequence[UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float], per: Literal['channel', 'batch', 'tensor'] = 'tensor')

Multiply Datum.image by the factor

_images/multiply.svg
{
    "name": "multiply",
    "factor": {"name": "truncnorm", "a": 0.333, "b": 3.0}
}
class ballfish.transformation.Divide(value: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float | Sequence[UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float], per: Literal['channel', 'batch', 'tensor'] = 'tensor')

Divide Datum.image by the value.

{"name": "divide", "value": 255}
class ballfish.transformation.Pow(pow: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float | Sequence[UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float], per: Literal['channel', 'batch', 'tensor'] = 'tensor')

Raise Datum.image to the power of pow

_images/pow.svg
{
    "name": "pow",
    "pow": {"name": "truncnorm", "a": 0.6, "b": 3.0}
}
class ballfish.transformation.Log(base: Literal['2', 'e', '10'] = 'e')

Calculates one of the three logarithms for Datum.image

_images/log_e.svg

\(\ln\)

{"name": "log", "base": "e"}
class ballfish.transformation.Clip(min: float, max: float)

Clip Datum.image value to min and max

{"name": "clip", "min": 0.0, "max": 1.0}
class ballfish.transformation.Grayscale(num_output_channels: int = 1)

Average of all channels. Set num_output_channels to make number ou output channels not one.

_images/grayscale.svg
{"name": "grayscale"}
class ballfish.transformation.Sharpness(factor: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Makes image sharper.

_images/sharpness.svg
{
    "name": "sharpness",
    "factor": {"name": "truncnorm", "a": 0.5, "b": 12}
}
class ballfish.transformation.Shading(value: UniformParams | TruncnormParams | ConstantParams | RandrangeParams | ChoiceParams | float)

Makes a random band darker.

_images/shading.svg
{
    "name": "shading",
    "value": {"name": "truncnorm", "a": -0.5, "b": 0.5}
}
class ballfish.transformation.Resize(width: int, height: int, antialias: bool = True, interpolation: Literal['nearest', 'nearest_exact', 'bilinear', 'bicubic'] = 'bilinear')

Changes the size of a tensor. Usually this function is not needed because resizing is done in rasterize step.

_images/resize.svg
{"name": "resize", "width": 100, "height": 60}
class ballfish.transformation.OneOf(operations: list[Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args | Args])

Apply one of the specified operations. In this mode, operation probabilities are used as weights, 1.0 by default

_images/one_of.svg
{
    "name": "one_of",
    "operations": [
        {
            "name": "shading",
            "value": {
                "name": "truncnorm",
                "a": -0.5,
                "b": 0.5
            }
        },
        {
            "name": "noise",
            "std": {
                "name": "truncnorm",
                "a": 0,
                "b": 0.1,
            },
            "type": "heteroscedastic"
        }
    ]
}

Indices and tables