Skip to content

Commit

Permalink
Allow constant value configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
faustomorales committed Nov 21, 2022
1 parent b4b81f2 commit 4bd4939
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions mira/classifiers/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, categories, model_name="RN50", device="cpu"):
"method": "fit",
"width": self.model.model.visual.input_resolution,
"height": self.model.model.visual.input_resolution,
"cval": 0,
}
self.preprocess = torchvision.transforms.Compose(
[
Expand Down
1 change: 1 addition & 0 deletions mira/classifiers/torchvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(
"method": "fit",
"width": 224,
"height": 224,
"cval": 0,
}
self.categories = core.Categories.from_categories(categories)
self.model = TVW(
Expand Down
39 changes: 32 additions & 7 deletions mira/core/resizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@
SideOptions = tx.Literal["longest", "shortest"]
FixedSizeConfig = tx.TypedDict(
"FixedSizeConfig",
{"method": tx.Literal["fit", "pad", "force"], "width": int, "height": int},
{
"method": tx.Literal["fit", "pad", "force"],
"width": int,
"height": int,
"cval": int,
},
)
VariableSizeConfig = tx.TypedDict(
"VariableSizeConfig",
{"method": tx.Literal["pad_to_multiple"], "base": int, "max": typing.Optional[int]},
{
"method": tx.Literal["pad_to_multiple"],
"base": int,
"max": typing.Optional[int],
"cval": int,
},
)
AspectPreservingConfig = tx.TypedDict(
"AspectPreservingConfig",
Expand Down Expand Up @@ -76,7 +86,7 @@ def fit_side(


def fit(
image: ArrayType, height: int, width: int, force: bool
image: ArrayType, height: int, width: int, force: bool, cval=0
) -> typing.Tuple[ArrayType, typing.Tuple[float, float], typing.Tuple[int, int]]:
"""Fit an image to a specific size, padding where necessary to maintain
aspect ratio.
Expand Down Expand Up @@ -108,9 +118,17 @@ def fit(
)
if pad_y > 0 or pad_x > 0:
padded = (
torch.nn.functional.pad(resized, (0, pad_x, 0, pad_y))
torch.nn.functional.pad(
resized, (0, pad_x, 0, pad_y), mode="constant", value=cval
)
if use_torch_ops
else np.pad(resized, ((0, pad_y), (0, pad_x), (0, 0)))
else np.pad(
resized,
((0, pad_y), (0, pad_x))
+ (((0, 0),) if len(resized.shape) == 3 else tuple()),
mode="constant",
constant_values=cval,
)
)
else:
padded = resized
Expand Down Expand Up @@ -258,10 +276,11 @@ def resize(
if resize_config["method"] == "none":
# pylint: disable=unexpected-keyword-arg
pad_dimensions = sizes_arr.max(axis=0, keepdims=True) - sizes_arr
cval = resize_config.get("cval", 0)
padded = (
torch.cat(
[
torch.nn.functional.pad(i, (0, pad_x, 0, pad_y)).unsqueeze(0) # type: ignore
torch.nn.functional.pad(i, (0, pad_x, 0, pad_y), mode="constant", value=cval).unsqueeze(0) # type: ignore
if pad_y >= 0 and pad_x >= 0
else fit(
typing.cast(torch.Tensor, i),
Expand All @@ -277,7 +296,13 @@ def resize(
if use_torch_ops
else np.concatenate(
[
np.pad(i, ((0, pad_y), (0, pad_x), (0, 0)))[np.newaxis]
np.pad(
i,
((0, pad_y), (0, pad_x))
+ (((0, 0),) if len(i.shape) == 3 else tuple()),
mode="constant",
constant_values=cval,
)[np.newaxis]
if pad_y >= 0 and pad_x >= 0
else fit(
i, height=raw_height + pad_y, width=raw_width + pad_x, force=False
Expand Down
1 change: 1 addition & 0 deletions mira/detectors/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def initialize_basic(
"method": "pad_to_multiple",
"base": 128,
"max": None,
"cval": 0,
}
# In mira, backbone has meaning because we use it to skip
# training these weights. But the FPN includes feature extraction
Expand Down
1 change: 1 addition & 0 deletions mira/detectors/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(
"method": "pad_to_multiple",
"base": 64,
"max": None,
"cval": 0,
}
self.categories = mc.Categories.from_categories(categories)
self.model = SMPWrapper(
Expand Down
13 changes: 10 additions & 3 deletions tests/test_resizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def verify_results(values, xlim, ylim, tensor_mode):
@pytest.mark.parametrize("examples,tensor_mode", typed_examples)
def test_resize_pad(examples, tensor_mode):
resized, scales, sizes = resize(
x=examples, resize_config={"method": "pad", "height": 256, "width": 256}
x=examples,
resize_config={"method": "pad", "height": 256, "width": 256, "cval": 0},
)
assert (resized.shape[2:] if tensor_mode else resized.shape[1:3]) == (256, 256)
assert (scales == 1).all()
Expand All @@ -46,7 +47,12 @@ def test_resize_pad(examples, tensor_mode):
def test_resize_pad_to_multiple(examples, tensor_mode):
resized, scales, sizes = resize(
x=examples,
resize_config={"method": "pad_to_multiple", "base": 512, "max": None},
resize_config={
"method": "pad_to_multiple",
"base": 512,
"max": None,
"cval": 0,
},
)
assert (resized.shape[2:] if tensor_mode else resized.shape[1:3]) == (512, 512)
assert (scales == 1).all()
Expand All @@ -57,7 +63,8 @@ def test_resize_pad_to_multiple(examples, tensor_mode):
@pytest.mark.parametrize("examples,tensor_mode", typed_examples)
def test_resize_fit(examples, tensor_mode):
resized, scales, sizes = resize(
x=examples, resize_config={"method": "fit", "height": 128, "width": 128}
x=examples,
resize_config={"method": "fit", "height": 128, "width": 128, "cval": 0},
)
assert (resized.shape[2:] if tensor_mode else resized.shape[1:3]) == (128, 128)
np.testing.assert_allclose(scales[:, 0], np.array([128 / 256, 128 / 256, 128 / 56]))
Expand Down

0 comments on commit 4bd4939

Please sign in to comment.