blocks.hooks¶
Classes¶
ComputeCTCStats: Accumulate CTC labels, labels_mask and predictions in order to compute sentence error rate after each epoch.ComputeCTCStatsWithEOS: Same asComputeCTCStatsbut usedecode_ctc_prediction_with_eos()for decoding.SaveImages: Save images of the provided streams.VisualizeMasks: Join and save images with the corresponding segmentation masks after each batch.VisualizeMasksGT: Join and save image with dyed segmentation mask prediction.VisualizeRectangleProposals: Visualize proposed rectangle regions.ComputeRegionProposals: emloop hook interface.ComputeRegionStats: Compute rectangle RPN stats (recall and overlap ratio).VisualizeThresholdedPrediction: Save images overlayed by their semi-transparent prediction masks for each of the given thresholds. The entire batch is saved as a single image matrix whereThreshold: Threshold image.SaveNumpy: Dump numpy arrays.
-
class
blocks.hooks.ComputeCTCStats(labels='labels', predictions='predictions', labels_mask='labels_mask', decode_fn=<function ComputeCTCStats.<lambda>>, **kwargs)[source]¶ Bases:
emloop.hooks.accumulate_variables.AccumulateVariablesAccumulate CTC labels, labels_mask and predictions in order to compute sentence error rate after each epoch.
labels: 2d array of zero-padded target labels
labels_mask: 2d array of labels masks wherein ones mark the valid values
predictions: 3d array of predictions of shape (batch x position x probs) wherein probs have dim
num_classes + 1
compute ctc stats for each epoch¶hooks: - blocks.hooks.ComputeCTCStats

-
class
blocks.hooks.ComputeCTCStatsWithEOS(**kwargs)[source]¶ Bases:
blocks.hooks.compute_ctc_stats.ComputeCTCStatsSame as
ComputeCTCStatsbut usedecode_ctc_prediction_with_eos()for decoding.
-
class
blocks.hooks.SaveImages(output_dir, streams=['train'], variable='images', id_variable='ids', out_format='png', factor=1, img_count=None, batch_count=None, **kwargs)[source]¶ Bases:
hooks.AbstractHookSave images of the provided streams.
This hook can be used as a base class for the hooks, which need systematically save their results. For this purpose,
process_imgmethod andimage_suffixproperty should be overridden.Caution
Saving all the images may require considerable amount of time. Use this hook only for debugging or limit the number of saved images with
image_countorbatch_countparameters.Save images from test and valid streams, stored in variable x.¶hooks: - blocks.hooks.SaveImages: variable: x streams: [test, valid]
Save the first two images from the first ten batches (from the train stream).¶hooks: - blocks.hooks.SaveImages: img_count: 2 batch_count: 10

-
__init__(output_dir, streams=['train'], variable='images', id_variable='ids', out_format='png', factor=1, img_count=None, batch_count=None, **kwargs)[source]¶ - Parameters
output_dir (
str) – output directory where images will be savedvariable (
str) – name of the variable representing the source imageid_variable (
str) – name of the variable which represents a unique example idout_format (
str) – extension of the saved imagefactor (
float) – a constant by which the image is multipliedimg_count (
Optional[int]) – count of images which will be saved from each batch (firstimg_countimages will be saved)batch_count (
Optional[int]) – count of batches from which the images will be saved (firstbatch_countwill be processed)
-
after_batch(stream_name, batch_data)[source]¶ Save images in provided streams from selected variable. The amount of batches and images to be processed is possible to control by
batch_countandimg_countparameters.
-
after_epoch(epoch_id, **_)[source]¶ Set
_current_epoch_idwhich is used for distinguish between epochs directories. Call the_resetfunction.
-
property
image_suffix¶ The suffix of the saved image, used to distinguish between images from different hooks.
- Return type
-
-
class
blocks.hooks.VisualizeMasks(output_dir, mask_variable='mask', mask_factor=255, mask_opacity=0.3, color=(0, 255, 0), **kwargs)[source]¶ Bases:
blocks.hooks.save_images.SaveImagesJoin and save images with the corresponding segmentation masks after each batch.
Caution
Saving all the images may require considerable amount of time. Use this hook only for debugging or limit the number of saved images with
image_countorbatch_countparameters.Join and save image stored in variableimgswith mask stored in variablesegments.¶hooks: - blocks.hooks.VisualizeMasks: variable: imgs mask_variable: mask

-
__init__(output_dir, mask_variable='mask', mask_factor=255, mask_opacity=0.3, color=(0, 255, 0), **kwargs)[source]¶ Create new VisualizeMasks hook.
- Parameters
mask_variable (
str) – name of the variable representing the mask of imagemask_factor (
float) – constant by which the mask is multipliedmask_opacity (
float) – opacity of the mask used for composition with imagecolor (
Tuple[int,int,int]) – color of the mask used in the resulting image (BGR colorspace)
-
-
class
blocks.hooks.VisualizeMasksGT(output_dir, mask_variable='mask', predictions_variable='predictions', mask_opacity=0.8, mask_factor=255, predictions_factor=255, color_true=(0, 255, 0), color_false_pos=(0, 0, 255), color_false_neg=(255, 0, 0), **kwargs)[source]¶ Bases:
blocks.hooks.save_images.SaveImagesJoin and save image with dyed segmentation mask prediction.
Caution
Saving all the images may require considerable amount of time. Use this hook only for debugging or limit the number of saved images with
image_countorbatch_countparameters.Join image stored in variableimgswith dyed layer obtained from mask stored in variablesegmentsand predictions stored in variableresults.¶hooks: - blocks.hooks.VisualizeMasksGT: variable: imgs mask_variable: mask predictions_variable: results

-
__init__(output_dir, mask_variable='mask', predictions_variable='predictions', mask_opacity=0.8, mask_factor=255, predictions_factor=255, color_true=(0, 255, 0), color_false_pos=(0, 0, 255), color_false_neg=(255, 0, 0), **kwargs)[source]¶ Create new VisualizeMasksGT hook.
- Parameters
mask_variable (
str) – name of the variable representing the mask of imagepredictions_variable (
str) – name of the variable representing the predictions (model output)mask_opacity (
float) – opacity of the mask used for composition with imagemask_factor (
float) – the multiplier of the maskpredictions_factor (
float) – the multiplier of the predictions variablecolor_true (
Tuple[int,int,int]) – color for the true positive and true negative pixels (BGR colorspace)color_false_pos (
Tuple[int,int,int]) – color for the false positive pixels (BGR colorspace)color_false_neg (
Tuple[int,int,int]) – color for the false negative pixels (BGR colorspace)
-
after_batch(stream_name, batch_data)[source]¶ Assert mask and prediction variables are in batch data.
-
property
image_suffix¶ Image suffix in the form:
<variable>_<mask_variable>_<predictions_variable>.- Return type
-
-
class
blocks.hooks.VisualizeRectangleProposals(output_dir, images_name='images', rectangles_name='rectangle_proposals', expected_rectangles_name='target_regions', errors_only=False, **kwargs)[source]¶ Bases:
hooks.AbstractHookVisualize proposed rectangle regions.

-
class
blocks.hooks.ComputeRegionProposals(dataset, non_maximum_suppression=0.3, min_probability=None, top_k=None, classifier_probabilities_name='classifier_probabilities', regression_predictions_name='regression_predictions', proposed_regions_name='rectangle_proposals', **kwargs)[source]¶ Bases:
hooks.AbstractHook
-
__init__(dataset, non_maximum_suppression=0.3, min_probability=None, top_k=None, classifier_probabilities_name='classifier_probabilities', regression_predictions_name='regression_predictions', proposed_regions_name='rectangle_proposals', **kwargs)[source]¶ Create new
ComputeRectangleProposalhook.- Parameters
dataset (
RectangleRPNDataset) – RPN dataset which created the anchorsnon_maximum_suppression (
Optional[float]) – if specified, use nms with the given thresholdmin_probability (
Optional[float]) – if specified, filter out regions with probability lower than the specified thresholdtop_k (
Optional[int]) – if specified, output only top k regionsclassifier_probabilities_name (
str) – name of the classifier probabilities variableregression_predictions_name (
str) – name of the regression predictions variableproposed_regions_name (
str) – name of the output proposed regions variable
-
-
class
blocks.hooks.ComputeRegionStats(dataset, expected_rectangles_name='target_regions', predicted_rectangles_name='rectangle_proposals', **kwargs)[source]¶ Bases:
hooks.AbstractHookCompute rectangle RPN stats (recall and overlap ratio).

-
class
blocks.hooks.VisualizeThresholdedPrediction(output_dir, image_variable, probability_variable, n_batches=1, n_epochs=1, thresholds=None, mask_opacity=0.5, color=(0, 255, 0), **kwargs)[source]¶ Bases:
hooks.AbstractHookSave images overlayed by their semi-transparent prediction masks for each of the given thresholds. The entire batch is saved as a single image matrix where columns correspond to given thresholds and rows to images in batch.

-
__init__(output_dir, image_variable, probability_variable, n_batches=1, n_epochs=1, thresholds=None, mask_opacity=0.5, color=(0, 255, 0), **kwargs)[source]¶ - Parameters
output_dir (
str) – output directory where masks will be savedimage_variable (
str) – name of the variable representing the source imageprobability_variable (
str) – name of the variable representing the probability imagen_batches (
int) – count of batches from which the masks will be savedn_epochs (
int) – count of epochs from which the masks will be savedthresholds (
Optional[Iterable[float]]) – list of probability thresholds from which the masks will be savedmask_opacity (
float) – opacity of the mask used for composition with imagecolor (
Tuple[int,int,int]) – color of the mask used for composition with image
-
after_epoch(epoch_id, **kwargs)[source]¶ After epoch event.
This event is triggered after every epoch wherein all the streams were iterated. The
epoch_dataobject is initially empty and shared among all the hooks.- Parameters
epoch_id (
int) – finished epoch idepoch_data – epoch data flowing through all hooks
-
-
class
blocks.hooks.Threshold(input_variable, output_variable, threshold, **kwargs)[source]¶ Bases:
hooks.AbstractHookThreshold image.

