code: # Copyright (C) 2021 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing,
  # software distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions
  # and limitations under the License.
  
  import cv2
  import numpy as np
  from typing import Any, Dict
  
  from openvino.model_zoo.model_api.models import SegmentationModel
  from openvino.model_zoo.model_api.models.types import NumericalValue
  from ote_sdk.utils.segmentation_utils import create_hard_prediction_from_soft_prediction
  
  
  class BlurSegmentation(SegmentationModel):
      __model__ = 'blur_segmentation'
  
      def __init__(self, model_adapter, configuration=None, preload=False):
          super().__init__(model_adapter, configuration, preload)
  
      @classmethod
      def parameters(cls):
          parameters = super().parameters()
          parameters.update({
              'soft_threshold': NumericalValue(default_value=0.5, min=0.0, max=1.0),
              'blur_strength': NumericalValue(value_type=int, default_value=1, min=0, max=25)
          })
  
          return parameters
  
      def postprocess(self, outputs: Dict[str, np.ndarray], metadata: Dict[str, Any]):
          predictions = outputs[self.output_blob_name].squeeze()
          soft_prediction = np.transpose(predictions, axes=(1, 2, 0))
  
          hard_prediction = create_hard_prediction_from_soft_prediction(
              soft_prediction=soft_prediction,
              soft_threshold=self.soft_threshold,
              blur_strength=self.blur_strength
          )
          hard_prediction = cv2.resize(hard_prediction, metadata['original_shape'][1::-1], 0, 0, interpolation=cv2.INTER_NEAREST)
          soft_prediction = cv2.resize(soft_prediction, metadata['original_shape'][1::-1], 0, 0, interpolation=cv2.INTER_NEAREST)
          metadata['soft_predictions'] = soft_prediction
  
          return hard_prediction
   # Copyright (C) 2021 Intel Corporation
  # SPDX-License-Identifier: Apache-2.0
  #
  
  import importlib
  from collections import OrderedDict
  from contextlib import contextmanager
  
  import torch
  
  
  _is_nncf_enabled = importlib.util.find_spec('nncf') is not None
  
  
  def is_nncf_enabled():
      return _is_nncf_enabled
  
  
  def check_nncf_is_enabled():
      if not is_nncf_enabled():
          raise RuntimeError('Tried to use NNCF, but NNCF is not installed')
  
  
  def get_nncf_version():
      if not is_nncf_enabled():
          return None
      import nncf
      return nncf.__version__
  
  
  def load_checkpoint(model, filename, map_location=None, strict=False):
      """Load checkpoint from a file or URI.
  
      Args:
          model (Module): Module to load checkpoint.
          filename (str): Either a filepath or URL or modelzoo://xxxxxxx.
          map_location (str): Same as :func:`torch.load`.
          strict (bool): Whether to allow different params for the model and
              checkpoint.
  
      Returns:
          dict or OrderedDict: The loaded checkpoint.
      """
      from nncf.torch import load_state
  
      checkpoint = torch.load(filename, map_location=map_location)
      # get state_dict from checkpoint
      if isinstance(checkpoint, OrderedDict):
          state_dict = checkpoint
      elif isinstance(checkpoint, dict) and 'state_dict' in checkpoint:
          state_dict = checkpoint['state_dict']
      else:
          raise RuntimeError(
              'No state_dict found in checkpoint file {}'.format(filename))
      _ = load_state(model, state_dict, strict)
      return checkpoint
  
  
  @contextmanager
  def nullcontext():
      """
      Context which does nothing
      """
      yield
  
  
  def no_nncf_trace():
      """
      Wrapper for original NNCF no_nncf_trace() context
      """
  
      if is_nncf_enabled():
          from nncf.torch.dynamic_graph.context import no_nncf_trace as original_no_nncf_trace
          return original_no_nncf_trace()
      return nullcontext()
  
  
  def is_in_nncf_tracing():
      if not is_nncf_enabled():
          return False
  
      from nncf.torch.dynamic_graph.context import get_current_context
  
      ctx = get_current_context()
  
      if ctx is None:
          return False
      return ctx.is_tracing
  
  def is_accuracy_aware_training_set(nncf_config):
      if not is_nncf_enabled():
          return False
      from nncf.config.utils import is_accuracy_aware_training
      is_acc_aware_training_set = is_accuracy_aware_training(nncf_config)
      return is_acc_aware_training_set
  
apis: numpy.transpose
apis: torch.load