pms-nvidia-processor 2.0.0__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. {pms_nvidia_processor-2.0.0 → pms_nvidia_processor-2.1.0}/PKG-INFO +1 -1
  2. pms_nvidia_processor-2.1.0/pms_nvidia_processor/__init__.py +13 -0
  3. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/base/config.py +69 -0
  4. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dependency.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/base/dependency.py +4 -9
  5. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/base/logger.py +42 -0
  6. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_nvidia_processor_base.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/base/processor.py +9 -8
  7. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/config.py +42 -0
  8. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/context.py +41 -0
  9. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_color_resnet_post_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/processor_post.py +8 -9
  10. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/processor_post_v2.py +209 -0
  11. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_color_resnet_pre_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/processor_pre.py +5 -6
  12. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/color_resnet/processor_pre_v2.py +114 -0
  13. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dpir/config.py +26 -0
  14. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dpir/context.py +30 -0
  15. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dpir_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dpir/processor.py +7 -7
  16. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dpir/processor_v2.py +154 -0
  17. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_asm_sr/config.py +27 -0
  18. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_asm_sr/context.py +42 -0
  19. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dru_asm_sr_f3_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_asm_sr/processor_f3.py +7 -7
  20. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_asm_sr/processor_f3_v2.py +180 -0
  21. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_deinter/config.py +27 -0
  22. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_deinter/context.py +37 -0
  23. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dru_rbpn_deinter_f3_gloss_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_deinter/processor_f3_gloss.py +7 -7
  24. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_deinter/processor_f3_gloss_v2.py +161 -0
  25. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/config.py +53 -0
  26. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/context.py +80 -0
  27. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dru_rbpn_sr_f3_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/processor_f3.py +7 -7
  28. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/processor_f3_v2.py +161 -0
  29. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_dru_rbpn_sr_f5_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/processor_f5.py +7 -7
  30. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/dru_rbpn_sr/processor_f5_v2.py +161 -0
  31. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/fisf/config.py +27 -0
  32. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/fisf/context.py +37 -0
  33. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_fisf_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/fisf/processor.py +7 -9
  34. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/fisf/processor_v2.py +212 -0
  35. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/gg/config.py +26 -0
  36. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/gg/context.py +30 -0
  37. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_gg_processor.py → pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/gg/processor.py +7 -7
  38. pms_nvidia_processor-2.1.0/pms_nvidia_processor/processor/gg/processor_v2.py +157 -0
  39. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/batch.py +25 -0
  40. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/caster.py +24 -0
  41. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/normalizer/__init__.py +4 -0
  42. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/normalizer/_factory.py +44 -0
  43. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/normalizer/_multi_normalizer.py +84 -0
  44. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/normalizer/_normalization_param.py +8 -0
  45. pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility/normalizer/_single_normalizer.py +43 -0
  46. {pms_nvidia_processor-2.0.0/pms_nvidia_processor → pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility}/patcher/_patch_collection.py +11 -11
  47. {pms_nvidia_processor-2.0.0/pms_nvidia_processor → pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility}/patcher/_patch_position.py +1 -1
  48. {pms_nvidia_processor-2.0.0/pms_nvidia_processor → pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility}/patcher/_patcher.py +9 -8
  49. {pms_nvidia_processor-2.0.0 → pms_nvidia_processor-2.1.0}/pyproject.toml +1 -1
  50. pms_nvidia_processor-2.0.0/pms_nvidia_processor/__init__.py +0 -11
  51. pms_nvidia_processor-2.0.0/pms_nvidia_processor/_const.py +0 -290
  52. {pms_nvidia_processor-2.0.0 → pms_nvidia_processor-2.1.0}/README.md +0 -0
  53. {pms_nvidia_processor-2.0.0/pms_nvidia_processor → pms_nvidia_processor-2.1.0/pms_nvidia_processor/utility}/patcher/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pms-nvidia-processor
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: lib for pms nvidia processors
5
5
  Author: HyeongSeok Kim
6
6
  Author-email: tiryul@gmail.com
@@ -0,0 +1,13 @@
1
+ from .processor.dpir.processor_v2 import DPIRProcessor
2
+ from .processor.dru_rbpn_sr.processor_f3_v2 import DRURBPNSRF3Processor
3
+ from .processor.dru_rbpn_sr.processor_f5_v2 import DRURBPNSRF5Processor
4
+ from .processor.dru_asm_sr.processor_f3_v2 import DRUASMSRF3Processor
5
+ from .processor.dru_rbpn_deinter.processor_f3_gloss_v2 import (
6
+ DRURBPNDEINTERF3GLOSSProcessor,
7
+ )
8
+ from .processor.fisf.processor_v2 import FISFProcessor
9
+ from .processor.color_resnet.processor_pre_v2 import COLORRESNETPREProcessor
10
+ from .processor.color_resnet.processor_post_v2 import COLORRESNETPOSTProcessor
11
+ from .processor.gg.processor_v2 import GGProcessor
12
+
13
+ __version__ = "2.1.0"
@@ -0,0 +1,69 @@
1
+ from .dependency import *
2
+
3
+
4
+ @dataclass
5
+ class PatcherIOConfig:
6
+ patch_size: int
7
+ upscale_ratio: int
8
+ number_of_input_channels: int
9
+ number_of_output_channels: int
10
+ input_overlab_length: int
11
+
12
+ @property
13
+ def output_overlab_length(self) -> int:
14
+ return self.input_overlab_length * self.upscale_ratio
15
+
16
+ @property
17
+ def input_shape(self) -> tuple[int, int, int]:
18
+ return (
19
+ self.patch_size,
20
+ self.patch_size,
21
+ self.number_of_input_channels,
22
+ )
23
+
24
+ @property
25
+ def output_shape(self) -> tuple[int, int, int]:
26
+ output_patch_size = (
27
+ self.patch_size - self.input_overlab_length * 2
28
+ ) * self.upscale_ratio
29
+ return (
30
+ output_patch_size,
31
+ output_patch_size,
32
+ self.number_of_output_channels,
33
+ )
34
+
35
+ def build_patcher_params(
36
+ self,
37
+ input_vector: np.ndarray,
38
+ output_vector: np.ndarray,
39
+ ) -> dict[str, object]:
40
+ input_vector_shape = input_vector.shape
41
+ output_vector_shape = output_vector.shape
42
+ return {
43
+ "input_vector_shape": input_vector_shape, # type: ignore
44
+ "input_patch_shape": self.input_shape,
45
+ "input_overlap_length": self.input_overlab_length,
46
+ "output_vector_shape": output_vector_shape, # type: ignore
47
+ "output_patch_shape": self.output_shape,
48
+ "output_overlap_length": self.output_overlab_length,
49
+ }
50
+
51
+
52
+ @dataclass
53
+ class TRTIOConfig:
54
+ patch_size: int
55
+ upscale_ratio: int
56
+ number_of_input_channels: int
57
+ number_of_output_channels: int
58
+
59
+ @property
60
+ def input_shape(self) -> tuple[int, int, int]:
61
+ return (self.number_of_input_channels, self.patch_size, self.patch_size)
62
+
63
+ @property
64
+ def output_shape(self) -> tuple[int, int, int]:
65
+ return (
66
+ self.number_of_output_channels,
67
+ self.patch_size * self.upscale_ratio,
68
+ self.patch_size * self.upscale_ratio,
69
+ )
@@ -1,23 +1,18 @@
1
1
  from typing import (
2
- List,
3
- Optional,
4
- Tuple,
5
- Iterable,
6
2
  Literal,
7
- Dict,
8
- Any,
9
3
  TypeVar,
10
- Union,
11
4
  Generic,
12
5
  )
13
- from abc import ABCMeta, abstractmethod
6
+ from abc import ABCMeta, abstractmethod, ABC
14
7
  from dataclasses import dataclass
15
8
  import os
16
9
  import time
17
10
  import uvloop
18
11
  import asyncio
19
12
  import numpy as np
20
- from loguru import logger
13
+ import loguru
14
+ import enum
15
+ from itertools import chain
21
16
  import cv2
22
17
  import pms_tensorrt as TRT
23
18
  from pms_inference_engine import IEngineProcessor, EngineIOData, register
@@ -0,0 +1,42 @@
1
+ from .dependency import *
2
+
3
+
4
+ class LogLevel(enum.Enum):
5
+ TRACE = enum.auto()
6
+ DEBUG = enum.auto()
7
+ INFO = enum.auto()
8
+ WARNING = enum.auto()
9
+ ERROR = enum.auto()
10
+ CRITICAL = enum.auto()
11
+
12
+
13
+ class ILogger(ABC):
14
+
15
+ @abstractmethod
16
+ def log(self, level: LogLevel, message: str, **kwargs): ...
17
+
18
+ def trace(self, message: str, **kwargs):
19
+ self.log(LogLevel.TRACE, message, **kwargs)
20
+
21
+ def debug(self, message: str, **kwargs):
22
+ self.log(LogLevel.DEBUG, message, **kwargs)
23
+
24
+ def info(self, message: str, **kwargs):
25
+ self.log(LogLevel.INFO, message, **kwargs)
26
+
27
+ def warning(self, message: str, **kwargs):
28
+ self.log(LogLevel.WARNING, message, **kwargs)
29
+
30
+ def error(self, message: str, **kwargs):
31
+ self.log(LogLevel.ERROR, message, **kwargs)
32
+
33
+ def critical(self, message: str, **kwargs):
34
+ self.log(LogLevel.CRITICAL, message, **kwargs)
35
+
36
+
37
+ class LoguruLogger(ILogger):
38
+ def __init__(self):
39
+ self._logger = loguru.logger
40
+
41
+ def log(self, level: LogLevel, message: str):
42
+ return self._logger.log(level.name, message)
@@ -1,6 +1,5 @@
1
- from ._dependency import *
2
- from ._const import *
3
- from . import patcher
1
+ from .dependency import *
2
+ from .logger import LoguruLogger, LogLevel, ILogger
4
3
 
5
4
 
6
5
  class NVIDIAProcessorBase(IEngineProcessor[InputTypeT, OutputTypeT], metaclass=ABCMeta):
@@ -10,20 +9,22 @@ class NVIDIAProcessorBase(IEngineProcessor[InputTypeT, OutputTypeT], metaclass=A
10
9
  index: int,
11
10
  concurrency: int,
12
11
  model_path: str,
13
- device: Union[Literal["auto"], int] = "auto",
12
+ device: Literal["auto"] | int = "auto",
13
+ logger: ILogger | None = None,
14
14
  ):
15
15
  # set member var
16
16
  self.index = index
17
17
  self.model_path = model_path
18
18
  self._concurrency = concurrency
19
19
  self._session = None
20
- self.logger = logger
20
+
21
+ self.logger = LoguruLogger() if logger == None else logger
21
22
  # set loop policy
22
23
  asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
23
24
 
24
25
  # set device_id
25
26
  device_count = TRT.get_device_count()
26
- if device is "auto":
27
+ if device == "auto":
27
28
  if "CUDA_VISIBLE_DEVICES" in os.environ:
28
29
  devices = [
29
30
  int(s) for s in os.environ["CUDA_VISIBLE_DEVICES"].split(",")
@@ -33,7 +34,7 @@ class NVIDIAProcessorBase(IEngineProcessor[InputTypeT, OutputTypeT], metaclass=A
33
34
  else:
34
35
  device_id = index % device_count
35
36
  else:
36
- assert type(device) is int, f"ERROR, The device's type is NOT int."
37
+ assert type(device) == int, f"ERROR, The device's type is NOT int."
37
38
  assert device > -1, f"ERROR, The device must be 'x > -1' but {device}."
38
39
  device_id = device
39
40
  self.device_id = device_id
@@ -47,7 +48,7 @@ class NVIDIAProcessorBase(IEngineProcessor[InputTypeT, OutputTypeT], metaclass=A
47
48
  def initialize_trt_session(
48
49
  self,
49
50
  required_batch_size: int,
50
- io_shape: Dict[str, Tuple[List[int], np.dtype]],
51
+ io_shape: dict[str, tuple[list[int], np.dtype]],
51
52
  ):
52
53
  model_path = self.model_path
53
54
  device_id = self.device_id
@@ -0,0 +1,42 @@
1
+ from ..base.config import *
2
+
3
+
4
+ class ColorResnetPreConfig:
5
+ NUMBER_OF_INPUT_CHANNELS: int = 3
6
+ NUMBER_OF_OUTPUT_CHANNELS: int = 30
7
+ UPSCALE_RATIO: int = 1
8
+ PATCH_SIZE = 256
9
+ MAX_BATCH_SIZE = 1
10
+ MIN_BATCH_SIZE = 1
11
+ OPT_BATCH_SIZE = 1
12
+
13
+ TRT_SHAPE_INPUT = (NUMBER_OF_INPUT_CHANNELS, PATCH_SIZE, PATCH_SIZE)
14
+ TRT_SHAPE_OUTPUT = (NUMBER_OF_OUTPUT_CHANNELS,)
15
+
16
+
17
+ class ColorResnetPostConfig:
18
+ NUMBER_OF_FRAMES = 1
19
+ NUMBER_OF_INPUT_CHANNELS: int = 3 * NUMBER_OF_FRAMES
20
+ NUMBER_OF_MODEL_OUTPUT_CHANNELS: int = 30
21
+ NUMBER_OF_OUTPUT_CHANNELS: int = 3
22
+ UPSCALE_RATIO: int = 1
23
+ PATCH_SIZE = 512
24
+ MAX_BATCH_SIZE = 1
25
+ MIN_BATCH_SIZE = 1
26
+ OPT_BATCH_SIZE = 1
27
+ INPUT_OVERLAB_LENGTH = 16 * 2
28
+
29
+ PATCHER_CONFIG = PatcherIOConfig(
30
+ patch_size=PATCH_SIZE,
31
+ upscale_ratio=UPSCALE_RATIO,
32
+ number_of_input_channels=NUMBER_OF_INPUT_CHANNELS,
33
+ number_of_output_channels=NUMBER_OF_OUTPUT_CHANNELS,
34
+ input_overlab_length=INPUT_OVERLAB_LENGTH,
35
+ )
36
+ TRT_CONFIG = TRTIOConfig(
37
+ patch_size=PATCH_SIZE,
38
+ upscale_ratio=UPSCALE_RATIO,
39
+ number_of_input_channels=NUMBER_OF_INPUT_CHANNELS,
40
+ number_of_output_channels=NUMBER_OF_OUTPUT_CHANNELS,
41
+ )
42
+ TRT_SHAPE_MODEL_OUTPUT = (NUMBER_OF_MODEL_OUTPUT_CHANNELS,)
@@ -0,0 +1,41 @@
1
+ from .config import *
2
+ from ..base.dependency import *
3
+ from ..base.processor import *
4
+ from ...utility import patcher, normalizer
5
+
6
+
7
+ class ColorResnetPreContext:
8
+
9
+ def __init__(
10
+ self,
11
+ input_vector: np.ndarray,
12
+ output_buffer: np.ndarray,
13
+ ) -> None:
14
+ config = ColorResnetPreConfig
15
+ self.input_vector = input_vector
16
+ self.output_vector: np.ndarray = np.zeros_like(output_buffer, np.float32)
17
+
18
+
19
+ class ColorResnetPostContext:
20
+ def __init__(
21
+ self,
22
+ input_vector: np.ndarray,
23
+ input_patcher: patcher.Patcher,
24
+ ) -> None:
25
+ config = ColorResnetPostConfig
26
+
27
+ self.input_vector = input_vector
28
+ self.input_pathcer = input_patcher
29
+ self.padded_input_image = patcher.pad_vector(
30
+ vector=self.input_vector,
31
+ overlap_length=config.PATCHER_CONFIG.input_overlab_length,
32
+ mode="edge",
33
+ )
34
+ self.input_patches: list[np.ndarray] = self.input_pathcer.slice(
35
+ self.padded_input_image
36
+ )
37
+ self.output_patches: np.ndarray = np.zeros(
38
+ (len(self.input_patches), config.PATCH_SIZE, config.PATCH_SIZE, 3),
39
+ np.float32,
40
+ )
41
+ self.output_vector: np.ndarray = np.zeros_like(self.input_vector, np.float32)
@@ -1,12 +1,11 @@
1
- from datetime import datetime
2
- from ._dependency import *
3
- from ._const import *
4
- from . import patcher
5
- from ._nvidia_processor_base import NVIDIAProcessorBase
1
+ from .config import *
2
+ from ..base.dependency import *
3
+ from ..base.processor import *
4
+ from ...utility import patcher
6
5
 
7
6
 
8
7
  def _pre_processing(
9
- batch_input_images: List[np.ndarray],
8
+ batch_input_images: list[np.ndarray],
10
9
  input_buffer: np.ndarray,
11
10
  ) -> None:
12
11
  b = len(batch_input_images)
@@ -40,7 +39,7 @@ def _post_processing_merged(
40
39
  return np.clip(np.multiply(output_image, 255), 0, 255).astype(np.uint8)
41
40
 
42
41
 
43
- def _get_scale_factor(output_image: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
42
+ def _get_scale_factor(output_image: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
44
43
  def _rgb2grayscale(rgb: np.ndarray):
45
44
  return (
46
45
  0.299 * rgb[..., :, :, 0:1]
@@ -78,7 +77,7 @@ class COLORRESNETPOSTProcessor(NVIDIAProcessorBase[EngineIOData, EngineIOData]):
78
77
  index: int,
79
78
  model_path: str,
80
79
  filter_coefficient: np.ndarray,
81
- device: Union[Literal["auto"], int] = "auto",
80
+ device: Literal["auto"] | int = "auto",
82
81
  ):
83
82
  # super
84
83
  super().__init__(
@@ -92,7 +91,7 @@ class COLORRESNETPOSTProcessor(NVIDIAProcessorBase[EngineIOData, EngineIOData]):
92
91
  self.is_scale_factor_calculated = False
93
92
  assert True
94
93
 
95
- async def inference(self, batch_input_data: List[np.ndarray]) -> List[np.ndarray]:
94
+ async def inference(self, batch_input_data: list[np.ndarray]) -> list[np.ndarray]:
96
95
  session = self.session
97
96
  patch_size = self.config.PATCH_SIZE
98
97
  # batch_input_data = [np.zeros_like(batch_input_data[0]), *batch_input_data]
@@ -0,0 +1,209 @@
1
+ from pms_nvidia_processor.processor.color_resnet.context import ColorResnetPostContext
2
+ from .config import *
3
+ from ..base.dependency import *
4
+ from ..base.processor import *
5
+ from ...utility import patcher, normalizer, batch, caster
6
+
7
+
8
+ def _pre_processing(
9
+ batch_input_images: list[np.ndarray],
10
+ input_buffer: np.ndarray,
11
+ ) -> None:
12
+ b = len(batch_input_images)
13
+ for batch_idx in range(b):
14
+ image = batch_input_images[batch_idx]
15
+ h, w, c = image.shape
16
+ for channel_idx in range(c):
17
+ input_buffer[batch_idx, channel_idx, :h, :w] = image[:, :, channel_idx]
18
+
19
+
20
+ def _post_processing_patch(
21
+ output_buffer: np.ndarray, # BxCxHxW
22
+ output_image: np.ndarray, # BxHxWxC
23
+ ) -> None:
24
+ b, h, w, c = output_image.shape
25
+ pred = output_buffer[:b, :, :h, :w]
26
+ for i in range(3):
27
+ np.copyto(src=pred[:, i, :, :], dst=output_image[:, :, :, i])
28
+
29
+
30
+ def _post_processing_merged(
31
+ output_image: np.ndarray,
32
+ scale_min: np.ndarray,
33
+ scale_max: np.ndarray,
34
+ ):
35
+ np.subtract(output_image, scale_min, out=output_image)
36
+ np.divide(output_image, scale_max - scale_min, out=output_image)
37
+
38
+
39
+ def _get_scale_factor(output_image: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
40
+ def _rgb2grayscale(rgb: np.ndarray):
41
+ return (
42
+ 0.299 * rgb[..., :, :, 0:1]
43
+ + 0.587 * rgb[..., :, :, 1:2]
44
+ + 0.114 * rgb[..., :, :, 2:3]
45
+ )
46
+
47
+ scale_max = (
48
+ np.amax(
49
+ _rgb2grayscale(output_image),
50
+ # axis=(-3, -2, -1),
51
+ axis=(-2, -1, -3),
52
+ keepdims=True,
53
+ )
54
+ + 0.0015
55
+ )
56
+ scale_min = (
57
+ np.amin(
58
+ _rgb2grayscale(output_image),
59
+ # axis=(-3, -2, -1),
60
+ axis=(-2, -1, -3),
61
+ keepdims=True,
62
+ )
63
+ - 0.0015
64
+ )
65
+ return scale_min, scale_max
66
+
67
+
68
+ @register
69
+ class COLORRESNETPOSTProcessor(NVIDIAProcessorBase[EngineIOData, EngineIOData]):
70
+
71
+ def __init__(
72
+ self,
73
+ concurrency: int,
74
+ index: int,
75
+ model_path: str,
76
+ filter_coefficient: np.ndarray,
77
+ device: Literal["auto"] | int = "auto",
78
+ normalization: str = "color-basic",
79
+ ):
80
+ # super
81
+ super().__init__(
82
+ concurrency=concurrency,
83
+ index=index,
84
+ model_path=model_path,
85
+ device=device,
86
+ )
87
+ self.filter_coefficient = filter_coefficient
88
+ self.config = ColorResnetPostConfig
89
+ self.is_scale_factor_calculated = False
90
+ self.normalizer = normalizer.NormalizerFactory.create_multi(normalization)
91
+
92
+ async def inference(
93
+ self,
94
+ batch_input_data: list[np.ndarray],
95
+ batch_output_data: np.ndarray,
96
+ ):
97
+ session = self.session
98
+
99
+ _pre_processing(
100
+ batch_input_images=batch_input_data,
101
+ input_buffer=self.input_buffer,
102
+ )
103
+ session.run()
104
+ _post_processing_patch(
105
+ output_buffer=self.output_buffer,
106
+ output_image=batch_output_data,
107
+ )
108
+
109
+ async def _run(self, input_data: EngineIOData) -> EngineIOData:
110
+ max_batch_size = 1
111
+ # 여기서 patching
112
+ input_vector = self.caster.cast(input_data.frame)
113
+ self.normalizer.normalize(input_vector, 2)
114
+ context = ColorResnetPostContext(input_vector, self.patcher)
115
+
116
+ for batch_input_patches, batch_output_patches in zip(
117
+ batch.BatchTool.batch_list(context.input_patches, max_batch_size),
118
+ batch.BatchTool.batch_vector(context.output_patches, max_batch_size),
119
+ ):
120
+ await self.inference(
121
+ batch_input_data=batch_input_patches,
122
+ batch_output_data=batch_output_patches,
123
+ )
124
+
125
+ self.patcher.merge(
126
+ output_vector=context.output_vector,
127
+ patches=context.output_patches,
128
+ )
129
+
130
+ if not self.is_scale_factor_calculated:
131
+ self.scale_min, self.scale_max = _get_scale_factor(context.output_vector)
132
+ self.is_scale_factor_calculated = True
133
+ _post_processing_merged(
134
+ output_image=context.output_vector,
135
+ scale_min=self.scale_min,
136
+ scale_max=self.scale_max,
137
+ )
138
+ self.normalizer.denormalize(context.output_vector, 2)
139
+ return EngineIOData(
140
+ frame_id=input_data.frame_id,
141
+ frame=self.caster.uncast(context.output_vector),
142
+ )
143
+
144
+ def _ready_processor(self) -> bool:
145
+ return True
146
+
147
+ def _bind_io(self, input_data: EngineIOData):
148
+ self.caster = caster.Caster(input_data.frame)
149
+
150
+ patcher_config = self.config.PATCHER_CONFIG
151
+ trt_config = self.config.TRT_CONFIG
152
+
153
+ input_image: np.ndarray = input_data.frame # type: ignore
154
+ padded_input_image = patcher.pad_vector(
155
+ input_image,
156
+ overlap_length=patcher_config.input_overlab_length,
157
+ )
158
+ output_image: np.ndarray = np.zeros(input_image.shape, np.float32)
159
+ self.patcher = patcher.Patcher(
160
+ **patcher_config.build_patcher_params(
161
+ input_vector=padded_input_image,
162
+ output_vector=output_image,
163
+ )
164
+ )
165
+ n_patches = len(self.patcher.slice(input_vector=padded_input_image))
166
+
167
+ # set io shape
168
+ # self.batch_size = min(n_patches + 1, self.config.MAX_BATCH_SIZE - 1)
169
+ self.batch_size = self.config.MAX_BATCH_SIZE
170
+ self.io_shapes = {
171
+ "input": (
172
+ [self.batch_size, *trt_config.input_shape],
173
+ np.float32,
174
+ ),
175
+ "model_output": (
176
+ [1, *self.config.TRT_SHAPE_MODEL_OUTPUT],
177
+ np.float32,
178
+ ),
179
+ "output": (
180
+ [self.batch_size, *trt_config.output_shape],
181
+ np.float32,
182
+ ),
183
+ }
184
+
185
+ # init trt engine
186
+ self.initialize_trt_session(
187
+ required_batch_size=self.batch_size,
188
+ io_shape=self.io_shapes,
189
+ )
190
+
191
+ # set io buffer
192
+ self.input_buffer = self.session._input_bindings[0].host_buffer.reshape(
193
+ self.io_shapes["input"][0]
194
+ )
195
+ self.filter_coefficient_buffer = self.session._input_bindings[
196
+ 1
197
+ ].host_buffer.reshape(self.io_shapes["model_output"][0])
198
+ self.output_buffer = self.session._output_bindings[0].host_buffer.reshape(
199
+ self.io_shapes["output"][0]
200
+ )
201
+ self.filter_coefficient_buffer[:] = self.filter_coefficient[:]
202
+ self.session.run()
203
+ return True
204
+
205
+ def _get_live(self) -> bool:
206
+ return True
207
+
208
+ def _get_concurrency(self) -> int:
209
+ return self._concurrency
@@ -1,8 +1,7 @@
1
- from typing import Any
2
- from ._dependency import *
3
- from ._const import *
4
- from . import patcher
5
- from ._nvidia_processor_base import NVIDIAProcessorBase
1
+ from .config import *
2
+ from ..base.dependency import *
3
+ from ..base.processor import *
4
+ from ...utility import patcher
6
5
 
7
6
 
8
7
  def _pre_processing(
@@ -35,7 +34,7 @@ class COLORRESNETPREProcessor(NVIDIAProcessorBase[EngineIOData, EngineIOData]):
35
34
  concurrency: int,
36
35
  index: int,
37
36
  model_path: str,
38
- device: Union[Literal["auto"], int] = "auto",
37
+ device: Literal["auto"] | int = "auto",
39
38
  ):
40
39
  # super
41
40
  super().__init__(
@@ -0,0 +1,114 @@
1
+ from .config import *
2
+ from ..base.dependency import *
3
+ from ..base.processor import *
4
+ from .context import ColorResnetPreContext
5
+ from ...utility import normalizer, caster
6
+
7
+
8
+ def _pre_processing(
9
+ input_image: np.ndarray, input_buffer: np.ndarray, input_size: int
10
+ ) -> None:
11
+ resize = cv2.resize(
12
+ input_image, (input_size, input_size), interpolation=cv2.INTER_CUBIC
13
+ )
14
+ h, w, c = resize.shape
15
+ for channel_idx in range(c):
16
+ input_buffer[0, channel_idx, :h, :w] = resize[:, :, channel_idx]
17
+
18
+
19
+ def _post_processing(
20
+ output_buffer: np.ndarray, # BxCxHxW
21
+ output_data: np.ndarray, # BxHxWxC
22
+ ) -> None:
23
+ output_data[:] = output_buffer[:]
24
+
25
+
26
+ @register
27
+ class COLORRESNETPREProcessor(NVIDIAProcessorBase[EngineIOData, EngineIOData]):
28
+
29
+ def __init__(
30
+ self,
31
+ concurrency: int,
32
+ index: int,
33
+ model_path: str,
34
+ device: Literal["auto"] | int = "auto",
35
+ normalization: str = "color-basic",
36
+ ):
37
+ # super
38
+ super().__init__(
39
+ concurrency=concurrency,
40
+ index=index,
41
+ model_path=model_path,
42
+ device=device,
43
+ )
44
+
45
+ self.config = ColorResnetPreConfig
46
+ self.normalizer = normalizer.NormalizerFactory.create_multi(normalization)
47
+
48
+ async def inference(
49
+ self, input_data: np.ndarray, output_data: np.ndarray
50
+ ) -> np.ndarray:
51
+ session = self.session
52
+ _pre_processing(
53
+ input_image=input_data,
54
+ input_buffer=self.input_buffer,
55
+ input_size=self.config.PATCH_SIZE,
56
+ )
57
+ session.run()
58
+ _post_processing(
59
+ output_buffer=self.output_buffer,
60
+ output_data=output_data,
61
+ )
62
+
63
+ async def _run(self, input_data: EngineIOData) -> EngineIOData:
64
+ input_vector = self.caster.cast(input_data.frame)
65
+ self.normalizer.normalize(input_vector, 2)
66
+ context = ColorResnetPreContext(input_vector, self.output_buffer)
67
+
68
+ # batch inference
69
+ await self.inference(
70
+ input_data=context.input_vector, output_data=context.output_vector
71
+ )
72
+
73
+ return EngineIOData(frame_id=input_data.frame_id, frame=context.output_vector)
74
+
75
+ def _ready_processor(self) -> bool:
76
+ return True
77
+
78
+ def _bind_io(self, input_data: EngineIOData):
79
+ self.caster = caster.Caster(input_data.frame)
80
+
81
+ # set io shape
82
+ self.batch_size = self.config.MAX_BATCH_SIZE
83
+ self.io_shapes = {
84
+ "input": (
85
+ [self.batch_size, *self.config.TRT_SHAPE_INPUT],
86
+ np.float32,
87
+ ),
88
+ "output": (
89
+ [self.batch_size, *self.config.TRT_SHAPE_OUTPUT],
90
+ np.float32,
91
+ ),
92
+ }
93
+
94
+ # init trt engine
95
+ self.initialize_trt_session(
96
+ required_batch_size=self.batch_size,
97
+ io_shape=self.io_shapes,
98
+ )
99
+
100
+ # set io buffer
101
+ self.input_buffer = self.session._input_bindings[0].host_buffer.reshape(
102
+ *self.io_shapes["input"][0]
103
+ )
104
+ self.output_buffer = self.session._output_bindings[0].host_buffer.reshape(
105
+ *self.io_shapes["output"][0]
106
+ )
107
+
108
+ return True
109
+
110
+ def _get_live(self) -> bool:
111
+ return True
112
+
113
+ def _get_concurrency(self) -> int:
114
+ return self._concurrency