|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2023 |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +# persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | +# Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 16 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | +# SOFTWARE. |
| 18 | +""" |
| 19 | +This module implements resizing for images and object detection bounding boxes in PyTorch. |
| 20 | +""" |
| 21 | +import logging |
| 22 | +from typing import Dict, List, Optional, TYPE_CHECKING, Tuple, Union |
| 23 | + |
| 24 | +from tqdm.auto import tqdm |
| 25 | + |
| 26 | +from art.preprocessing.preprocessing import PreprocessorPyTorch |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + # pylint: disable=C0412 |
| 30 | + import torch |
| 31 | + from art.utils import CLIP_VALUES_TYPE |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class ImageResizePyTorch(PreprocessorPyTorch): |
| 37 | + """ |
| 38 | + This module implements resizing for images and object detection bounding boxes in PyTorch. |
| 39 | + """ |
| 40 | + |
| 41 | + params = ["height", "width", "channels_first", "label_type", "interpolation", "clip_values", "verbose"] |
| 42 | + |
| 43 | + label_types = ["classification", "object_detection"] |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + height: int, |
| 48 | + width: int, |
| 49 | + channels_first: bool = True, |
| 50 | + label_type: str = "classification", |
| 51 | + interpolation: str = "bilinear", |
| 52 | + clip_values: Optional["CLIP_VALUES_TYPE"] = None, |
| 53 | + apply_fit: bool = True, |
| 54 | + apply_predict: bool = False, |
| 55 | + verbose: bool = False, |
| 56 | + ): |
| 57 | + """ |
| 58 | + Create an instance of ImageResizePyTorch. |
| 59 | +
|
| 60 | + :param height: The height of the resized image. |
| 61 | + :param width: The width of the resized image. |
| 62 | + :param channels_first: Set channels first or last. |
| 63 | + :param label_type: String defining the label type. Currently supported: `classification`, `object_detection` |
| 64 | + :param interpolation: String defining the resizing method. Currently supported: `nearest`, `linear`, |
| 65 | + `bilinear`, `bicubic`, `trilinear`, `area`, `nearest-exact` |
| 66 | + :param clip_values: Tuple of the form `(min, max)` representing the minimum and maximum values allowed |
| 67 | + for features. |
| 68 | + :param apply_fit: True if applied during fitting/training. |
| 69 | + :param apply_predict: True if applied during predicting. |
| 70 | + :param verbose: Show progress bars. |
| 71 | + """ |
| 72 | + super().__init__(is_fitted=True, apply_fit=apply_fit, apply_predict=apply_predict) |
| 73 | + self.height = height |
| 74 | + self.width = width |
| 75 | + self.channels_first = channels_first |
| 76 | + self.label_type = label_type |
| 77 | + self.interpolation = interpolation |
| 78 | + self.clip_values = clip_values |
| 79 | + self.verbose = verbose |
| 80 | + self._check_params() |
| 81 | + |
| 82 | + def forward( |
| 83 | + self, |
| 84 | + x: "torch.Tensor", |
| 85 | + y: Optional[Union["torch.Tensor", List[Dict[str, "torch.Tensor"]]]] = None, |
| 86 | + ) -> Tuple["torch.Tensor", Optional[Union["torch.Tensor", List[Dict[str, "torch.Tensor"]]]]]: |
| 87 | + """ |
| 88 | + Resize `x` and adjust bounding boxes for labels `y` accordingly. |
| 89 | +
|
| 90 | + :param x: Input samples. A list of samples is also supported. |
| 91 | + :param y: Label of the samples `x`. |
| 92 | + :return: Transformed samples and labels. |
| 93 | + """ |
| 94 | + import torch |
| 95 | + |
| 96 | + x_preprocess_list = [] |
| 97 | + y_preprocess: Optional[Union[torch.Tensor, List[Dict[str, torch.Tensor]]]] |
| 98 | + if y is not None and self.label_type == "object_detection": |
| 99 | + y_preprocess = [] |
| 100 | + else: |
| 101 | + y_preprocess = y |
| 102 | + |
| 103 | + for i, x_i in enumerate(tqdm(x, desc="ImageResizePyTorch", disable=not self.verbose)): |
| 104 | + if not self.channels_first: |
| 105 | + x_i = torch.permute(x_i, (2, 0, 1)) |
| 106 | + |
| 107 | + # Resize image: requires a batch so create batch of size 1 |
| 108 | + x_resized = torch.nn.functional.interpolate( |
| 109 | + x_i.unsqueeze(0), size=(self.height, self.width), mode=self.interpolation |
| 110 | + ).squeeze() |
| 111 | + |
| 112 | + if not self.channels_first: |
| 113 | + x_resized = torch.permute(x_resized, (1, 2, 0)) |
| 114 | + |
| 115 | + x_preprocess_list.append(x_resized) |
| 116 | + |
| 117 | + if self.label_type == "object_detection" and y is not None: |
| 118 | + y_resized: Dict[str, torch.Tensor] = {} |
| 119 | + |
| 120 | + # Copy labels and ensure types |
| 121 | + if isinstance(y, list) and isinstance(y_preprocess, list): |
| 122 | + y_i = y[i] |
| 123 | + if isinstance(y_i, dict): |
| 124 | + y_resized = {k: torch.clone(v) for k, v in y_i.items()} |
| 125 | + else: |
| 126 | + raise TypeError("Wrong type for `y` and label_type=object_detection.") |
| 127 | + else: |
| 128 | + raise TypeError("Wrong type for `y` and label_type=object_detection.") |
| 129 | + |
| 130 | + # Calculate scaling factor |
| 131 | + _, height, width = x_i.shape |
| 132 | + height_scale = self.height / height |
| 133 | + width_scale = self.width / width |
| 134 | + |
| 135 | + # Resize bounding boxes |
| 136 | + y_resized["boxes"][:, 0] *= width_scale |
| 137 | + y_resized["boxes"][:, 1] *= height_scale |
| 138 | + y_resized["boxes"][:, 2] *= width_scale |
| 139 | + y_resized["boxes"][:, 3] *= height_scale |
| 140 | + |
| 141 | + y_preprocess.append(y_resized) |
| 142 | + |
| 143 | + x_preprocess = torch.stack(x_preprocess_list) |
| 144 | + if self.clip_values is not None: |
| 145 | + x_preprocess = torch.clamp(x_preprocess, self.clip_values[0], self.clip_values[1]) # type: ignore |
| 146 | + |
| 147 | + return x_preprocess, y_preprocess |
| 148 | + |
| 149 | + def _check_params(self) -> None: |
| 150 | + if self.height <= 0: |
| 151 | + raise ValueError("The desired image height must be positive.") |
| 152 | + |
| 153 | + if self.width <= 0: |
| 154 | + raise ValueError("The desired image width must be positive") |
| 155 | + |
| 156 | + if self.clip_values is not None: |
| 157 | + if len(self.clip_values) != 2: |
| 158 | + raise ValueError("`clip_values` should be a tuple of 2 floats containing the allowed data range.") |
| 159 | + |
| 160 | + if self.clip_values[0] >= self.clip_values[1]: |
| 161 | + raise ValueError("Invalid `clip_values`: min >= max.") |
0 commit comments