Skip to content

Functions to apply to input/output of networks


tensorflow functions to add into input or output signatures of model

argmax_predictions(predictions)

Return argmax of given predictions for each sample

Parameters:

Name Type Description Default
predictions tf.Tensor

outputs of the network

required

Returns:

Name Type Description
out tf.Tensor

indices of maximal value for each prediction row

Source code in conftrainer/modifications/tf_functions.py
@tf.function
def argmax_predictions(predictions: tf.Tensor) -> tf.Tensor:
    """
    Return argmax of given predictions for each sample

    Parameters
    ----------
    predictions : tf.Tensor
        outputs of the network

    Returns
    -------
    out : tf.Tensor
        indices of maximal value for each prediction row
    """
    outputs = tf.argmax(predictions, axis=-1)
    return tf.cast(outputs, dtype=tf.float32)

base64_to_img(base64)

Decode a base64 encoded image onto jpeg

Parameters:

Name Type Description Default
base64 tf.Tensor, str

base64 to decode

required

Returns:

Name Type Description
out tf.Tensor

decoded image

Source code in conftrainer/modifications/tf_functions.py
@tf.function(input_signature=[tf.TensorSpec(None, tf.string)])
def base64_to_img(base64):
    """
    Decode a base64 encoded image onto jpeg

    Parameters
    ----------
    base64 : tf.Tensor, str
        base64 to decode

    Returns
    -------
    out : tf.Tensor
        decoded image
    """
    image_bytes = tf.io.decode_base64(base64)
    image = tf.io.decode_image(image_bytes,
                               channels=3,
                               dtype=tf.dtypes.uint8,
                               expand_animations=False
                               )

    return image

img_to_base64(image, return_string=True)

Encode a single image to base64 format

Parameters:

Name Type Description Default
image tf.Tensor

input image

required
return_string bool

whether to return regular string or a tf.string

True

Returns:

Name Type Description
out tf.Tensor

base64 encoding of an image

Source code in conftrainer/modifications/tf_functions.py
@tf.function(input_signature=[tf.TensorSpec(None, tf.variant)])
def img_to_base64(image, return_string=True):
    """
    Encode a single image to base64 format

    Parameters
    ----------
    image : tf.Tensor
        input image
    return_string : bool, default: True
        whether to return regular string or a tf.string

    Returns
    -------
    out : tf.Tensor
        base64 encoding of an image
    """
    img_bytes = tf.image.encode_png(image)
    base64 = tf.io.encode_base64(img_bytes)
    if return_string:
        return base64.numpy().decode()
    return base64

read_from_disk(path)

Read and decode an image from disk

Parameters:

Name Type Description Default
path str

path to the file to read

required

Returns:

Name Type Description
out tf.Tensor

an image

Source code in conftrainer/modifications/tf_functions.py
@tf.function(input_signature=[tf.TensorSpec(None, tf.string)])
def read_from_disk(path):
    """
    Read and decode an image from disk

    Parameters
    ----------
    path : str
        path to the file to read

    Returns
    -------
    out : tf.Tensor
        an image
    """
    image_bytes = tf.io.read_file(path)
    image = tf.io.decode_image(image_bytes,
                               channels=3,
                               dtype=tf.dtypes.uint8,
                               expand_animations=False
                               )
    return image

rescale(images, scale, offset)

Rescale and normalize image arrays by given factors

Parameters:

Name Type Description Default
images tf.Tensor

a batch of images

required
scale float

a float to divide the inputs into

required
offset float

a float to add to all inputs

required

Returns:

Name Type Description
out tf.Tensor

preprocessed batch of images

Source code in conftrainer/modifications/tf_functions.py
@tf.function
def rescale(images: tf.Tensor, scale: float, offset: float) -> tf.Tensor:
    """
    Rescale and normalize image arrays by given factors

    Parameters
    ----------
    images : tf.Tensor
        a batch of images
    scale : float
        a float to divide the inputs into
    offset : float
        a float to add to all inputs

    Returns
    -------
    out : tf.Tensor
        preprocessed batch of images
    """
    return images / scale + offset

options: docstring_style: numpy members_order: source