Skip to content

Commit

Permalink
Fix: PIL.Image is not a type
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Mar 28, 2024
1 parent a710ea9 commit fb44b88
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions src/uform/numpy_preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os import PathLike
from typing import Dict, List, Union

from PIL import Image
from PIL.Image import Image, BICUBIC
from tokenizers import Tokenizer
import numpy as np

Expand All @@ -20,12 +20,8 @@ def __init__(self, config: Dict, tokenizer_path: PathLike):
self._tokenizer.no_padding()
self._pad_token_idx = config["text_encoder"]["padding_idx"]

self.image_mean = np.array(
[0.48145466, 0.4578275, 0.40821073], dtype=np.float32
)[None, None]
self.image_std = np.array(
[0.26862954, 0.26130258, 0.27577711], dtype=np.float32
)[None, None]
self.image_mean = np.array([0.48145466, 0.4578275, 0.40821073], dtype=np.float32)[None, None]
self.image_std = np.array([0.26862954, 0.26130258, 0.27577711], dtype=np.float32)[None, None]

def preprocess_text(self, texts: Union[str, List[str]]) -> Dict[str, np.ndarray]:
"""Transforms one or more strings into dictionary with tokenized strings and attention masks.
Expand Down Expand Up @@ -85,16 +81,14 @@ def _resize_crop_normalize(self, image: Image):
width = int(width / height * self._image_size)
height = self._image_size

image = image.resize((width, height), resample=Image.BICUBIC)
image = image.resize((width, height), resample=BICUBIC)

left = (width - self._image_size) / 2
top = (height - self._image_size) / 2
right = (width + self._image_size) / 2
bottom = (height + self._image_size) / 2

image = image.convert("RGB").crop((left, top, right, bottom))
image = (
np.array(image).astype(np.float32) / 255.0 - self.image_mean
) / self.image_std
image = (np.array(image).astype(np.float32) / 255.0 - self.image_mean) / self.image_std

return np.transpose(image, (2, 0, 1))
2 changes: 1 addition & 1 deletion src/uform/torch_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict, List, Union

import torch
from PIL import Image
from PIL.Image import Image
from tokenizers import Tokenizer
from torch import Tensor
from torchvision.transforms import (
Expand Down

0 comments on commit fb44b88

Please sign in to comment.