Upload processor
Browse files- image_processing.py +68 -0
- preprocessor_config.json +2 -2
image_processing.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
from torch import Tensor
|
| 5 |
+
from transformers import BaseImageProcessorFast
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class LSPDetrImageProcessor(BaseImageProcessorFast):
|
| 9 |
+
image_mean = [0.485, 0.456, 0.406]
|
| 10 |
+
image_std = [0.229, 0.224, 0.225]
|
| 11 |
+
do_rescale = True
|
| 12 |
+
do_normalize = True
|
| 13 |
+
return_tensors = "pt"
|
| 14 |
+
|
| 15 |
+
def post_process(self, outputs: dict[str, Tensor]) -> list[dict[str, Tensor]]:
|
| 16 |
+
"""Converts the raw output into polygons.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
A list of dictionaries, each containing:
|
| 20 |
+
- "polygons": A tensor of shape (N, num_radial_distances, 2) representing the polygons.
|
| 21 |
+
- "labels": A tensor of shape (N,) representing the labels for each polygon.
|
| 22 |
+
"""
|
| 23 |
+
radial_distances = outputs["radial_distances"].expm1()
|
| 24 |
+
|
| 25 |
+
t = torch.linspace(0, 1, radial_distances.size(-1) + 1, device=self.device)[:-1]
|
| 26 |
+
cos = torch.cos(2 * torch.pi * t)
|
| 27 |
+
sin = torch.sin(2 * torch.pi * t)
|
| 28 |
+
|
| 29 |
+
polar = radial_distances.unsqueeze(-1) * torch.stack([sin, cos], dim=-1)
|
| 30 |
+
polygons = outputs["absolute_points"].unsqueeze(-2) + polar
|
| 31 |
+
|
| 32 |
+
labels = outputs["logits"].argmax(dim=-1)
|
| 33 |
+
non_no_object_indices = labels != outputs["logits"].size(-1) - 1
|
| 34 |
+
|
| 35 |
+
return [
|
| 36 |
+
{"polygons": polygons[b, indices], "labels": labels[b, indices]}
|
| 37 |
+
for b, indices in enumerate(non_no_object_indices)
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
def post_process_instance(
|
| 41 |
+
self,
|
| 42 |
+
results: list[dict[str, Tensor]],
|
| 43 |
+
height: int,
|
| 44 |
+
width: int,
|
| 45 |
+
) -> list[dict[str, Tensor]]:
|
| 46 |
+
"""Converts the output into actual instance segmentation predictions.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
results: Results list obtained by `post_process`, to which "masks" results will be added.
|
| 50 |
+
height: Height of the input image.
|
| 51 |
+
width: Width of the input image.
|
| 52 |
+
"""
|
| 53 |
+
for i, result in enumerate(results):
|
| 54 |
+
masks = torch.zeros(
|
| 55 |
+
(len(result["polygons"]), height, width),
|
| 56 |
+
dtype=torch.bool,
|
| 57 |
+
device=result["polygons"].device,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
for i, polygon in enumerate(result["polygons"]):
|
| 61 |
+
img = Image.fromarray(masks[i].cpu().numpy())
|
| 62 |
+
canvas = ImageDraw.Draw(img)
|
| 63 |
+
canvas.polygon(xy=polygon.flatten().tolist(), outline=1, fill=1)
|
| 64 |
+
masks[i] = torch.from_numpy(np.asarray(img))
|
| 65 |
+
|
| 66 |
+
results[i]["masks"] = masks
|
| 67 |
+
|
| 68 |
+
return results
|
preprocessor_config.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"auto_map": {
|
| 3 |
-
"AutoImageProcessor": "
|
| 4 |
},
|
| 5 |
"crop_size": null,
|
| 6 |
"data_format": "channels_first",
|
|
@@ -16,7 +16,7 @@
|
|
| 16 |
0.456,
|
| 17 |
0.406
|
| 18 |
],
|
| 19 |
-
"image_processor_type": "
|
| 20 |
"image_std": [
|
| 21 |
0.229,
|
| 22 |
0.224,
|
|
|
|
| 1 |
{
|
| 2 |
"auto_map": {
|
| 3 |
+
"AutoImageProcessor": "image_processing.LSPDetrImageProcessor"
|
| 4 |
},
|
| 5 |
"crop_size": null,
|
| 6 |
"data_format": "channels_first",
|
|
|
|
| 16 |
0.456,
|
| 17 |
0.406
|
| 18 |
],
|
| 19 |
+
"image_processor_type": "LSPDetrImageProcessor",
|
| 20 |
"image_std": [
|
| 21 |
0.229,
|
| 22 |
0.224,
|