blocks.utils

Functions

blocks.utils.resize_and_pad(image, target_width, target_height, upscale=True)[source]

Resize and pad (with zeros) the given image to the target width and height.

Parameters
  • image (ndarray) – input image

  • target_width (int) – target width

  • target_height (int) – target height

  • upscale (bool) – upscale the image if it is smaller in both dimensions

Return type

ndarray

Returns

resized and padded image

blocks.utils.create_clean_dir(dir_name)[source]

Create a clean directory with the given name in CWD.

Parameters

dir_name (str) – directory name

Return type

str

Returns

directory name

blocks.utils.raise_moved(name, new_name)[source]

Raise NameError with the function/feature moved/renamed info message.

Tip

Use this function when moving/renaming function/features. Our projects may rely on them!

Parameters
  • name (str) – old name of the function/feature

  • new_name (str) – new name of the function/feature

Raises

NameError – with the function/feature renamed/moved info message

blocks.utils.base64_to_cv2(string_base64, imread_flag=1)[source]

Transform base64-encoded string to OpenCV image.

Return type

ndarray

blocks.utils.bytes_to_base64(data)[source]

Transform bytes to base64-encoded string.

Return type

str

blocks.utils.cv2_to_base64(image, extension='.png')[source]

Transform OpenCV image to base64-encoded string.

Return type

str

blocks.utils.decode_ctc_prediction(ctc_prediction)[source]

Decode raw CTC prediction to the actual output assuming that the last prediction bit is reserved for the empty character.

Parameters

ctc_prediction (ndarray) – 2d array of time x probabilities

Return type

Tuple[list, list]

Returns

a tuple of (decoded predictions, indices of emitted characters)

blocks.utils.decode_ctc_prediction_with_eos(ctc_prediction)[source]

Same as decode_ctc_prediction() but cut the prediction (and indices) at <EOS> character assuming that <EOS> = dim-2.

Parameters

ctc_prediction – 2d array of time x probabilities

Returns

a tuple of (decoded predictions, indices of emitted characters)

blocks.utils.decode_rpn_predictions(dataset, classifier_probabilities, regression_predictions, non_maximum_suppression=0.3, min_probability=None, top_k=None)[source]

Decode the given RPN predictions to a list of regions.

Optionally:
  • use non maximum suppression

  • filter out regions with low probability

  • output only top k regions

Parameters
  • dataset (RPNDataset) – RPN dataset which created the anchors

  • classifier_probabilities (ndarray) – predicted classifier probabilities (in 0-1 interval)

  • regression_predictions (ndarray) – predicted diffs regression values

  • non_maximum_suppression (Optional[float]) – if specified, use nms with the given threshold

  • min_probability (Optional[float]) – if specified, filter out regions with probability lower than the specified threshold

  • top_k (Optional[int]) – if specified, output only top k regions

Return type

List[Sequence[float]]

Returns

list of decoded regions

blocks.utils.classmaker(left_metas=(), right_metas=())[source]
blocks.utils.try_import(class_fqn)[source]

Try to import and return the specified class. If it fails return an empty class instead.

example usage with hipipe dataset
import blocks as bl

class OdoCapDataset(bl.utils.try_import('my_module.Dataset'),
                    bl.datasets.RectangleRPNDataset,
                    metaclass=bl.utils.classmaker()):
Parameters

class_fqn (str) – class fully qualified name

Return type

type

Returns

specified class or an empty class

blocks.utils.get_balancing_weights(masks, correction=0.001)[source]

Compute weights balancing zeros and ones in the given masks tensor.

Warning

The masks tensor must be binary - i.e., contain only ones and zeros!

Parameters
  • masks (Tensor) – zero/one masks to be balanced

  • correction (float) – correction parameter to avoid divergence of weights

Return type

Tensor

Returns

weights balancing the given mask (having the same shape)

Classes

class blocks.utils.DemoOutputBuilder[source]

Bases: object

Building demo outputs to be displayed in Iterait demo web application.

example usage
output = blocks.utils.DemoOutputBuilder().add_image(im).add_table(['col1', 'col2'], [['lorem ipsum', im2]]).output
# output ~ demo-compatible json-serializable dict
Inheritance diagram of DemoOutputBuilder

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

add_image(image, extension='.jpg')[source]

Add image block to the output.

Return type

DemoOutputBuilder

add_image_transformation(original, transformed, extension='.jpg')[source]

Add image comparison block to the output.

Return type

DemoOutputBuilder

add_table(header, rows)[source]

Add table block to the output.

Parameters
  • header (Sequence[str]) – table header - a sequence of strings

  • rows (Sequence[Sequence[Union[str, ndarray]]]) – a sequence of rows wherein row is a sequence of strings/images

Return type

DemoOutputBuilder

property output

Return demo-compatible json-serializable dict.

@teyras suggests to use this in dataset’s postprocess_batch, shepherd runner or emloop model.

Return type

dict