|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, List, Optional, Tuple |
| 16 | + |
| 17 | +import numpy |
| 18 | +import onnx |
| 19 | +from PIL import Image |
| 20 | +from torchvision import transforms |
| 21 | + |
| 22 | +from deepsparse.image_classification.constants import ( |
| 23 | + IMAGENET_RGB_MEANS, |
| 24 | + IMAGENET_RGB_STDS, |
| 25 | +) |
| 26 | +from deepsparse.pipelines.computer_vision import ComputerVisionSchema |
| 27 | +from deepsparse.v2.operators import Operator |
| 28 | + |
| 29 | + |
| 30 | +class ImageClassificationInput(ComputerVisionSchema): |
| 31 | + """ |
| 32 | + Input model for image classification |
| 33 | + """ |
| 34 | + |
| 35 | + |
| 36 | +__all__ = ["ImageClassificationPreProcess"] |
| 37 | + |
| 38 | + |
| 39 | +class ImageClassificationPreProcess(Operator): |
| 40 | + """ |
| 41 | + Image Classification pre-processing operator. This Operator is expected to process |
| 42 | + the user inputs and prepare them for the engine. Inputs to this Operator are |
| 43 | + expected to follow the ImageClassificationInput schema. |
| 44 | + """ |
| 45 | + |
| 46 | + input_schema = ImageClassificationInput |
| 47 | + output_schema = None |
| 48 | + |
| 49 | + def __init__(self, model_path: str, image_size: Optional[Tuple[int]] = None): |
| 50 | + self.model_path = model_path |
| 51 | + self._image_size = image_size or self._infer_image_size() |
| 52 | + non_rand_resize_scale = 256.0 / 224.0 # standard used |
| 53 | + self._pre_normalization_transforms = transforms.Compose( |
| 54 | + [ |
| 55 | + transforms.Resize( |
| 56 | + tuple( |
| 57 | + [ |
| 58 | + round(non_rand_resize_scale * size) |
| 59 | + for size in self._image_size |
| 60 | + ] |
| 61 | + ) |
| 62 | + ), |
| 63 | + transforms.CenterCrop(self._image_size), |
| 64 | + ] |
| 65 | + ) |
| 66 | + |
| 67 | + def run(self, inp: ImageClassificationInput, **kwargs) -> Dict: |
| 68 | + """ |
| 69 | + Pre-Process the Inputs for DeepSparse Engine |
| 70 | +
|
| 71 | + :param inputs: input model |
| 72 | + :return: list of preprocessed numpy arrays |
| 73 | + """ |
| 74 | + |
| 75 | + if isinstance(inp.images, numpy.ndarray): |
| 76 | + image_batch = inp.images |
| 77 | + else: |
| 78 | + if isinstance(inp.images, str): |
| 79 | + inp.images = [inp.images] |
| 80 | + |
| 81 | + image_batch = list(map(self._preprocess_image, inp.images)) |
| 82 | + |
| 83 | + # build batch |
| 84 | + image_batch = numpy.stack(image_batch, axis=0) |
| 85 | + |
| 86 | + original_dtype = image_batch.dtype |
| 87 | + image_batch = numpy.ascontiguousarray(image_batch, dtype=numpy.float32) |
| 88 | + |
| 89 | + if original_dtype == numpy.uint8: |
| 90 | + image_batch /= 255 |
| 91 | + # normalize entire batch |
| 92 | + image_batch -= numpy.asarray(IMAGENET_RGB_MEANS).reshape((-1, 3, 1, 1)) |
| 93 | + image_batch /= numpy.asarray(IMAGENET_RGB_STDS).reshape((-1, 3, 1, 1)) |
| 94 | + |
| 95 | + return {"engine_inputs": [image_batch]} |
| 96 | + |
| 97 | + def _preprocess_image(self, image) -> numpy.ndarray: |
| 98 | + if isinstance(image, List): |
| 99 | + # image given as raw list |
| 100 | + image = numpy.asarray(image) |
| 101 | + if image.dtype == numpy.float32: |
| 102 | + # image is already processed, append and continue |
| 103 | + return image |
| 104 | + # assume raw image input |
| 105 | + # put image in PIL format for torchvision processing |
| 106 | + image = image.astype(numpy.uint8) |
| 107 | + if image.shape[0] < image.shape[-1]: |
| 108 | + # put channel last |
| 109 | + image = numpy.einsum("cwh->whc", image) |
| 110 | + image = Image.fromarray(image) |
| 111 | + elif isinstance(image, str): |
| 112 | + # load image from string filepath |
| 113 | + image = Image.open(image).convert("RGB") |
| 114 | + elif isinstance(image, numpy.ndarray): |
| 115 | + image = image.astype(numpy.uint8) |
| 116 | + if image.shape[0] < image.shape[-1]: |
| 117 | + # put channel last |
| 118 | + image = numpy.einsum("cwh->whc", image) |
| 119 | + image = Image.fromarray(image) |
| 120 | + |
| 121 | + if not isinstance(image, Image.Image): |
| 122 | + raise ValueError( |
| 123 | + f"inputs to {self.__class__.__name__} must be a string image " |
| 124 | + "file path(s), a list representing a raw image, " |
| 125 | + "PIL.Image.Image object(s), or a numpy array representing" |
| 126 | + f"the entire pre-processed batch. Found {type(image)}" |
| 127 | + ) |
| 128 | + |
| 129 | + # apply resize and center crop |
| 130 | + image = self._pre_normalization_transforms(image) |
| 131 | + image_numpy = numpy.array(image) |
| 132 | + image.close() |
| 133 | + |
| 134 | + # make channel first dimension |
| 135 | + image_numpy = image_numpy.transpose(2, 0, 1) |
| 136 | + return image_numpy |
| 137 | + |
| 138 | + def _infer_image_size(self) -> Tuple[int, ...]: |
| 139 | + """ |
| 140 | + Infer and return the expected shape of the input tensor |
| 141 | +
|
| 142 | + :return: The expected shape of the input tensor from onnx graph |
| 143 | + """ |
| 144 | + onnx_model = onnx.load(self.model_path) |
| 145 | + input_tensor = onnx_model.graph.input[0] |
| 146 | + return ( |
| 147 | + input_tensor.type.tensor_type.shape.dim[2].dim_value, |
| 148 | + input_tensor.type.tensor_type.shape.dim[3].dim_value, |
| 149 | + ) |
0 commit comments