width, height = 1600, 256

    mask = rle2mask(rle, (width, height))

    assert mask.shape == (height, width)

    inversed_rle = mask2rle(mask)

    assert rle == inversed_rle


@given(mask=h_arrays(
    dtype=np.uint8,
    shape=(np.random.randint(1, 10), np.random.randint(1, 11)),
    elements=h_int(min_value=0, max_value=255),
))
def test_mask2one_hot(mask):
    num_classes = mask.max() + 1

    one_hot_mask = one_hot(mask, num_classes)

    assert np.all(mask == reverse_one_hot(one_hot_mask))


@given(mask=h_arrays(
    dtype=np.uint8,
    shape=(np.random.randint(1, 11), np.random.randint(1, 10)),
    elements=h_int(min_value=0, max_value=255),
))
def test_mask2one_hot_with_limit(mask):
    width, height = 1600, 256

    mask = rle2mask(rle, (width, height))

    assert mask.shape == (height, width)

    inversed_rle = mask2rle(mask)

    assert rle == inversed_rle


@given(mask=h_arrays(dtype=np.uint8,
                     shape=(np.random.randint(1, 1000),
                            np.random.randint(1, 1000)),
                     elements=h_int(0, 1)))
def test_kaggle_rle(mask):
    height, width = mask.shape

    kaggle_rle = kaggle_rle_encode(mask)
    coco_rle = coco_rle_encode(mask)
    assert coco_rle == kaggle2coco(kaggle_rle, height, width)
    assert np.all(mask == kaggle_rle_decode(kaggle_rle, height, width))
    assert np.all(mask == coco_rle_decode(coco_rle, height, width))


def test_coco2binary_mask():
    height, width = 130, 120
    mask = np.zeros((height, width))
    mask[50:70, 30:90] = 1
    dxsin = dx * math.sin(angle)
    dycos = dy * math.cos(angle)
    dysin = dy * math.sin(angle)

    center = np.array(center)

    return [
        center + np.array([-dxcos + dysin, -dxsin - dycos]),
        center + np.array([dxcos + dysin, dxsin - dycos]),
        center + np.array([dxcos - dysin, dxsin + dycos]),
        center + np.array([-dxcos - dysin, -dxsin + dycos]),
    ]


@given(
    length_a=h_int(min_value=1, max_value=100),
    width_a=h_int(min_value=1, max_value=100),
    center_ax=h_float(min_value=-100, max_value=100),
    center_ay=h_float(min_value=-100, max_value=100),
    length_b=h_int(min_value=1, max_value=100),
    width_b=h_int(min_value=1, max_value=100),
    center_bx=h_float(min_value=-100, max_value=100),
    center_by=h_float(min_value=-100, max_value=100),
)
def test_vs_shapely_zero_angle(length_a, width_a, center_ax, center_ay,
                               length_b, width_b, center_bx, center_by):
    rectangle_a = to_coords((center_ax, center_ay), length_a, width_a, 0)
    rectangle_b = to_coords((center_bx, center_by), length_b, width_b, 0)

    rectangle_a_shapely = Polygon(rectangle_a + [rectangle_a[0]])
    rectangle_b_shapely = Polygon(rectangle_b + [rectangle_b[0]])
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.image_utils import pad, unpad


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)), factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)), factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.img_tools import pad, unpad


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)
Exemple #6
0
    width, height = 1600, 256

    mask = rle2mask(rle, (width, height))

    assert mask.shape == (height, width)

    inversed_rle = mask2rle(mask)

    assert rle == inversed_rle


@given(
    mask=h_arrays(
        dtype=np.uint8,
        shape=(np.random.randint(1, 10), np.random.randint(1, 11)),
        elements=h_int(min_value=0, max_value=255),
    )
)
def test_mask2one_hot(mask):
    num_classes = mask.max() + 1

    one_hot_mask = one_hot(mask, num_classes)

    assert np.all(mask == reverse_one_hot(one_hot_mask))


@given(
    mask=h_arrays(
        dtype=np.uint8,
        shape=(np.random.randint(1, 11), np.random.randint(1, 10)),
        elements=h_int(min_value=0, max_value=255),
import math

from hypothesis import given
from hypothesis.strategies import integers as h_int, floats as h_float

from iglovikov_helper_functions.utils.box_utils import resize, fix_center, _get_center, _get_left_right, _get_coords

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 99


@given(position=h_int(min_value=0, max_value=IMAGE_WIDTH), width=h_int(min_value=1, max_value=200))
def test_fix_center(position, width):
    new_position = fix_center(position, IMAGE_WIDTH, width=width)

    if width < IMAGE_WIDTH:
        assert math.ceil(width / 2) <= new_position < IMAGE_WIDTH - math.floor(width / 2) + 1

    if width > IMAGE_WIDTH:
        assert new_position == math.ceil(IMAGE_WIDTH / 2)


@given(
    x_min=h_int(min_value=0, max_value=int(IMAGE_WIDTH * 3 / 4) - 1),
    y_min=h_int(min_value=0, max_value=int(IMAGE_HEIGHT * 5 / 6) - 1),
    x_max=h_int(min_value=int(IMAGE_WIDTH * 3 / 4), max_value=IMAGE_WIDTH - 1),
    y_max=h_int(min_value=int(IMAGE_HEIGHT * 5 / 6), max_value=IMAGE_HEIGHT - 1),
    resize_coeff=h_float(min_value=1, max_value=10),
)
def test_resize(x_min, y_min, x_max, y_max, resize_coeff):
    x_min_1, y_min_1, x_max_1, y_max_1 = resize(
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.image_utils import (
    pad,
    pad_to_size,
    unpad,
    unpad_from_size,
)


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)