Skip to content

Commit

Permalink
pylint & update model api version in docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
sungchul2 committed Jul 10, 2023
1 parent c6df647 commit dbbc231
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# See the License for the specific language governing permissions
# and limitations under the License.

from .model_wrappers import *
from .model_wrappers import * # noqa: F403
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# See the License for the specific language governing permissions
# and limitations under the License.

from .openvino_models import ImageEncoder, Decoder # noqa: F401
from .openvino_adapters import VisualPromptingOpenvinoAdapter # noqa: F401
from .openvino_models import Decoder, ImageEncoder # noqa: F401
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@


def resize_image_with_aspect_pad(input: Output, size, keep_aspect_ratio, interpolation, pad_value):
"""https://github.com/openvinotoolkit/model_api/blob/master/model_api/python/openvino/model_api/adapters/utils.py#L273-L341
"""
"""https://github.com/openvinotoolkit/model_api/blob/0.1.3/model_api/python/openvino/model_api/adapters/utils.py#L273-L341."""
h_axis = 1
w_axis = 2
w, h = size
Expand All @@ -46,12 +45,8 @@ def resize_image_with_aspect_pad(input: Output, size, keep_aspect_ratio, interpo
w_ratio = opset.divide(np.float32(w), iw)
h_ratio = opset.divide(np.float32(h), ih)
scale = opset.minimum(w_ratio, h_ratio)
nw = opset.convert(
opset.round(opset.multiply(iw, scale), "half_to_even"), destination_type="i32"
)
nh = opset.convert(
opset.round(opset.multiply(ih, scale), "half_to_even"), destination_type="i32"
)
nw = opset.convert(opset.round(opset.multiply(iw, scale), "half_to_even"), destination_type="i32")
nh = opset.convert(opset.round(opset.multiply(ih, scale), "half_to_even"), destination_type="i32")
new_size = opset.concat([opset.unsqueeze(nh, 0), opset.unsqueeze(nw, 0)], axis=0)
image = opset.interpolate(
input,
Expand Down Expand Up @@ -84,8 +79,7 @@ def resize_image_with_aspect_pad(input: Output, size, keep_aspect_ratio, interpo


def resize_image_with_aspect(size, interpolation, pad_value):
"""https://github.com/openvinotoolkit/model_api/blob/master/model_api/python/openvino/model_api/adapters/utils.py#L356-L365
"""
"""https://github.com/openvinotoolkit/model_api/blob/0.1.3/model_api/python/openvino/model_api/adapters/utils.py#L356-L365."""
return custom_preprocess_function(
partial(
resize_image_with_aspect_pad,
Expand All @@ -99,10 +93,11 @@ def resize_image_with_aspect(size, interpolation, pad_value):

class VisualPromptingOpenvinoAdapter(OpenvinoAdapter):
"""Openvino Adapter Wrappers of OTX Visual Prompting.
This class is to use fixed `fit_to_window` resize module.
When model API version in otx is upgraded, it can be removed.
"""

def embed_preprocessing(
self,
layout,
Expand All @@ -116,17 +111,14 @@ def embed_preprocessing(
scale=None,
input_idx=0,
):
"""https://github.com/openvinotoolkit/model_api/blob/master/model_api/python/openvino/model_api/adapters/openvino_adapter.py#L340-L411
"""
ppp = PrePostProcessor(self.model)
"""https://github.com/openvinotoolkit/model_api/blob/0.1.3/model_api/python/openvino/model_api/adapters/openvino_adapter.py#L340-L411."""
ppp = PrePostProcessor(self.model) # type: ignore[has-type]

# Change the input type to the 8-bit image
if dtype == type(int):
ppp.input(input_idx).tensor().set_element_type(Type.u8)

ppp.input(input_idx).tensor().set_layout(ov.Layout("NHWC")).set_color_format(
ColorFormat.BGR
)
ppp.input(input_idx).tensor().set_layout(ov.Layout("NHWC")).set_color_format(ColorFormat.BGR)

INTERPOLATION_MODE_MAP = {
"LINEAR": "linear",
Expand All @@ -152,9 +144,7 @@ def embed_preprocessing(
)

else:
raise ValueError(
f"Upsupported resize type in model preprocessing: {resize_mode}"
)
raise ValueError(f"Upsupported resize type in model preprocessing: {resize_mode}")

# Handle layout
ppp.input(input_idx).model().set_layout(ov.Layout(layout))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import cv2
import numpy as np
from openvino.model_api.adapters.inference_adapter import InferenceAdapter
from openvino.model_api.models import DetectionModel, ImageModel
from openvino.model_api.models import ImageModel
from openvino.model_api.models.types import (
BooleanValue,
ListValue,
Expand Down Expand Up @@ -96,18 +96,20 @@ def preprocess(self, inputs: Dict[str, Any], meta: Dict[str, Any]):
# TODO (sungchul): add condition to check whether using bbox or point
point_coords = self._apply_coords(bbox.reshape(-1, 2, 2), inputs["original_size"])
point_labels = np.array([2, 3], dtype=np.float32).reshape((-1, 2))
processed_prompts.append({
"point_coords": point_coords,
"point_labels": point_labels,
# TODO (sungchul): how to generate mask_input and has_mask_input
"mask_input": np.zeros((1, 1, 256, 256), dtype=np.float32),
"has_mask_input": np.zeros((1, 1), dtype=np.float32),
"orig_size": np.array(inputs["original_size"], dtype=np.float32).reshape((-1, 2)),
"label": label
})
processed_prompts.append(
{
"point_coords": point_coords,
"point_labels": point_labels,
# TODO (sungchul): how to generate mask_input and has_mask_input
"mask_input": np.zeros((1, 1, 256, 256), dtype=np.float32),
"has_mask_input": np.zeros((1, 1), dtype=np.float32),
"orig_size": np.array(inputs["original_size"], dtype=np.float32).reshape((-1, 2)),
"label": label,
}
)
return processed_prompts

def _apply_coords(self, coords: np.ndarray, original_size: Union[List[int], Tuple[int]]) -> np.ndarray:
def _apply_coords(self, coords: np.ndarray, original_size: Union[List[int], Tuple[int, int]]) -> np.ndarray:
"""Process coords according to preprocessed image size using image meta."""
old_h, old_w = original_size
new_h, new_w = self._get_preprocess_shape(original_size[0], original_size[1], self.image_size)
Expand All @@ -123,7 +125,7 @@ def _get_preprocess_shape(self, old_h: int, old_w: int, image_size: int) -> Tupl
new_w = int(new_w + 0.5)
new_h = int(new_h + 0.5)
return (new_h, new_w)

def _check_io_number(self, number_of_inputs, number_of_outputs):
pass

Expand All @@ -146,7 +148,7 @@ def postprocess(self, outputs: Dict[str, np.ndarray], meta: Dict[str, Any]) -> T
"""

def sigmoid(x):
return np.tanh(x * 0.5) * 0.5 + 0.5 # to avoid overflow
return np.tanh(x * 0.5) * 0.5 + 0.5 # to avoid overflow

soft_prediction = outputs[self.output_blob_name].squeeze()
soft_prediction = self.resize_and_crop(soft_prediction, meta["original_size"][0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

from otx.algorithms.common.configs import BaseConfig
from otx.api.configuration.elements import (
ParameterGroup,
add_parameter_group,
configurable_boolean,
configurable_float,
configurable_integer,
string_attribute,
configurable_boolean
)
from otx.api.configuration.model_lifecycle import ModelLifecycle

Expand All @@ -41,7 +42,7 @@ class __LearningParameters(BaseConfig.BaseLearningParameters):
description = header

@attrs
class __Postprocessing:
class __Postprocessing(ParameterGroup):
header = string_attribute("Postprocessing")
description = header

Expand Down Expand Up @@ -86,7 +87,7 @@ class __Postprocessing:
default_value=64,
affects_outcome_of=ModelLifecycle.INFERENCE,
)

orig_height = configurable_integer(
header="Original height",
description="Model input height before embedding processing.",
Expand Down
3 changes: 2 additions & 1 deletion src/otx/algorithms/visual_prompting/tasks/openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

import attr
import numpy as np
from openvino.model_api.adapters import OpenvinoAdapter, create_core
from openvino.model_api.adapters import create_core
from openvino.model_api.models import Model

from otx.algorithms.common.utils.logger import get_logger
from otx.algorithms.common.utils.utils import get_default_async_reqs_num
from otx.algorithms.visual_prompting.adapters.openvino import model_wrappers
from otx.algorithms.visual_prompting.adapters.openvino.model_wrappers import (
VisualPromptingOpenvinoAdapter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,20 @@ def test_apply_coords(self):
results = self.decoder._apply_coords(coords, original_size)

assert results.shape == (1, 2, 2)
assert np.all(results == np.array([[[0.5, 0.5], [1., 1.]]]))
assert np.all(results == np.array([[[0.5, 0.5], [1.0, 1.0]]]))

@e2e_pytest_unit
@pytest.mark.parametrize("old_h,old_w,image_size,expected",
@pytest.mark.parametrize(
"old_h,old_w,image_size,expected",
[
(4, 3, 6, (6, 5)),
(3, 4, 6, (5, 6)),
]
],
)
def test_get_preprocess_shape(self, old_h: int, old_w: int, image_size: int, expected: Tuple[int]):
"""Test _get_preprocess_shape."""
result = self.decoder._get_preprocess_shape(old_h, old_w, image_size)

assert result == expected

@e2e_pytest_unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ def test_load_checkpoint_from_local_checkpoint(self, mocker, monkeypatch, checkp
)
mocker.patch("builtins.open").__enter__.return_value = True
mocker.patch("torch.load", return_value=OrderedDict())
mocker_load_from_checkpoint = mocker.patch("otx.algorithms.visual_prompting.adapters.pytorch_lightning.models.visual_prompters.segment_anything.SegmentAnything.load_from_checkpoint")
mocker_load_from_checkpoint = mocker.patch(
"otx.algorithms.visual_prompting.adapters.pytorch_lightning.models.visual_prompters.segment_anything.SegmentAnything.load_from_checkpoint"
)
mocker_load_state_dict = mocker.patch(
"otx.algorithms.visual_prompting.adapters.pytorch_lightning.models.visual_prompters.segment_anything.SegmentAnything.load_state_dict"
)
Expand Down

0 comments on commit dbbc231

Please sign in to comment.