def image_to_internal(self, image: Imagelike) -> torch.Tensor: """Transform an image into a torch Tensor. """ if isinstance(image, torch.Tensor): image_tensor = image elif isinstance(image, np.ndarray): # at this point we need to know the range (if further # preprocessing, e.g., normalization, is required ...) if False and (0 <= image).all(): if (image <= 1).all(): # image is range 0.0 - 1.0 pass elif (image <= 255).all(): # image is range 0 - 255 pass # Question: channel first or channel last? # H X W X C ==> C X H X W # image = np.transpose(image, (2, 0, 1)) # preprocess_numpy expects numpy.ndarray of correct size, # dtype float and values in range [0.0, 1.0]. # It performs the following operations: # 1. [no resizing] # 2. numpy.ndarray -> torch.Tensor # 3. normalization [0.0, 1.0] -> torch.imagenet_range image_tensor = self.preprocess_numpy(image) # old: explicit transformation: # H X W X C ==> C X H X W # image = np.transpose(image, (2, 0, 1)) # # image = torch.from_numpy(image) # image = image.add(-self.imagenet_mean_.view(3, 1, 1)).\ # div(self.imagenet_std_.view(3, 1, 1)) # # add batch dimension: C X H X W ==> B X C X H X W # image = image.unsqueeze(0) else: # the _image_to_internal function expects as input a PIL image! image = Image.as_pil(image) # image should be PIL Image. Got <class 'numpy.ndarray'> image_tensor = self._image_to_internal(image) if image_tensor.dim() == 4: # image is already batch image_batch = image_tensor elif image_tensor.dim() == 3: # create a mini-batch as expected by the model # by adding a batch dimension: C X H X W ==> B X C X H X W image_batch = image_tensor.unsqueeze(0) else: raise ValueError(f"Data of invalid shape {image.shape} cannot " "be transformed into an internal torch image.") # move the input and model to GPU for speed if available image_batch = image_batch.to(self._device) return image_batch
def test_pillow(self): """Test :py:class:`Size` type. """ module = importlib.import_module('dltb.thirdparty.pil') self.assertIn('pil', Image.supported_formats()) image = Image(self.example_image_filename) pil = Image.as_pil(image) self.assertIsInstance(pil, module.PIL.Image.Image)