Upload model
Browse files- image_processing_vqmodel.py +63 -0
- preprocessor_config.json +9 -0
image_processing_vqmodel.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers.image_processing_utils import BaseImageProcessor
|
5 |
+
from transformers.utils import logging
|
6 |
+
|
7 |
+
logger = logging.get_logger(__name__)
|
8 |
+
|
9 |
+
|
10 |
+
class VQModelImageProcessor(BaseImageProcessor): # type: ignore
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
size: int = 256,
|
14 |
+
convert_rgb: bool = False,
|
15 |
+
resample: Image.Resampling = Image.Resampling.LANCZOS,
|
16 |
+
) -> None:
|
17 |
+
self.size = size
|
18 |
+
self.convert_rgb = convert_rgb
|
19 |
+
self.resample = resample
|
20 |
+
|
21 |
+
def __call__(self, image: Image.Image) -> dict:
|
22 |
+
return self.preprocess(image)
|
23 |
+
|
24 |
+
def preprocess(self, image: Image.Image) -> dict:
|
25 |
+
width, height = image.size
|
26 |
+
size = (self.size, self.size)
|
27 |
+
image = image.resize(size, resample=self.resample)
|
28 |
+
image = image.convert("RGBA")
|
29 |
+
|
30 |
+
if self.convert_rgb:
|
31 |
+
# Paste RGBA image on white background
|
32 |
+
image_new = Image.new("RGB", image.size, (255, 255, 255))
|
33 |
+
image_new.paste(image, mask=image.split()[3])
|
34 |
+
image = image_new
|
35 |
+
|
36 |
+
return {
|
37 |
+
"image": self.to_tensor(image),
|
38 |
+
"width": width,
|
39 |
+
"height": height,
|
40 |
+
}
|
41 |
+
|
42 |
+
def to_tensor(self, image: Image.Image) -> torch.Tensor:
|
43 |
+
x = np.array(image) / 127.5 - 1.0
|
44 |
+
x = x.transpose(2, 0, 1).astype(np.float32)
|
45 |
+
return torch.as_tensor(x)
|
46 |
+
|
47 |
+
def postprocess(
|
48 |
+
self,
|
49 |
+
x: torch.Tensor,
|
50 |
+
width: int | None = None,
|
51 |
+
height: int | None = None,
|
52 |
+
) -> Image.Image:
|
53 |
+
x_np = x.numpy().transpose(1, 2, 0)
|
54 |
+
x_np = (x_np + 1.0) * 127.5
|
55 |
+
x_np = np.clip(x_np, 0, 255).astype(np.uint8)
|
56 |
+
image = Image.fromarray(x_np)
|
57 |
+
|
58 |
+
# Resize image
|
59 |
+
width = width or self.size
|
60 |
+
height = height or self.size
|
61 |
+
image = image.resize((width, height), resample=self.resample)
|
62 |
+
|
63 |
+
return image
|
preprocessor_config.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"auto_map": {
|
3 |
+
"AutoImageProcessor": "image_processing_vqmodel.VQModelImageProcessor"
|
4 |
+
},
|
5 |
+
"convert_rgb": true,
|
6 |
+
"image_processor_type": "VQModelImageProcessor",
|
7 |
+
"resample": 1,
|
8 |
+
"size": 256
|
9 |
+
}
|