<jupyter_start><jupyter_text>utils> Provides different util functions<jupyter_code>#export
import json
from copy import deepcopy
import numpy as np
from PIL import Image

from icevision.core.mask import EncodedRLEs, MaskArray

from pycocotools import mask as mask_utils<jupyter_output><empty_output><jupyter_text>Test data setup<jupyter_code>import icedata
test_data_path_instance_segmentation = icedata.pennfudan.load_data()
test_instance_segmentation_parser = icedata.pennfudan.parser(data_dir=test_data_path_instance_segmentation)
test_instance_segmentation_train_records, test_instance_segmentation_valid_records = test_instance_segmentation_parser.parse()
test_instance_segmentation_class_map = test_instance_segmentation_train_records[0].detection.class_map<jupyter_output><empty_output><jupyter_text>Instance segmentation<jupyter_code>#export
def erles_to_string(erles):
    erles_copy = deepcopy(erles)
    erles_copy["counts"] = erles_copy["counts"].decode("utf-8")
    return json.dumps(erles_copy)
#hide
erles_to_string(test_instance_segmentation_train_records[0].as_dict()["detection"]["masks"].erles[0])
#export
def erles_to_counts_to_utf8(erles):
    erles_copy = deepcopy(erles)
    for entry in erles_copy:
        entry["counts"] = entry["counts"].decode("utf-8")
    return erles_copy
#hide
erles_to_counts_to_utf8(test_instance_segmentation_train_records[0].as_dict()["detection"]["masks"].erles)
#export
def string_to_erles(erles_string):
    erles = json.loads(erles_string)
    erles["counts"] = erles["counts"].encode()
    return erles
#hide
test_erles = test_instance_segmentation_train_records[0].as_dict()["detection"]["masks"].erles
erles_string = json.dumps(erles_to_counts_to_utf8(test_erles)[0])
string_to_erles(erles_string)
#export
def correct_mask(mask_array, pad_x, pad_y, width, height):
    # correct mask
    corrected_mask_array = mask_array.transpose(2, 0, 1)
    if round(pad_x/2) > 0:
        corrected_mask_array=corrected_mask_array[:,:,round(pad_x/2):round(-pad_x/2)]
    if round(pad_y/2) > 0:
        corrected_mask_array=corrected_mask_array[:,round(pad_y/2):round(-pad_y/2),:]
    corrected_mask_array = np.array(Image.fromarray(corrected_mask_array[0,:,:]).resize([width, height], Image.NEAREST))
    corrected_mask_array = np.expand_dims(corrected_mask_array, 0)
    # convert mask array to mask and get erles (only one erles exist!)
    corrected_mask = MaskArray(corrected_mask_array)
    return corrected_mask
#hide
test_mask = np.zeros([1, 30, 30])
test_mask_corrected = correct_mask(test_mask, 10, 0, 30, 20)
assert test_mask_corrected.data.shape == (1, 20, 30)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11106913486966377, 'fraction_numerical': 0.015489233094068758, 'mean_word_length': 7.7105263157894735, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '158306', 'license': 'Apache-2.0', 'n_tokens_mistral': 981, 'n_tokens_neox': 948, 'n_words': 143, 'path': 'nbs/utils.ipynb', 'repo_name': 'fstroth/icevision_dashboard', 'size': '2647'}