Paul commited on
Upload processor
Browse files- image_processing_swin.py +45 -0
- preprocessor_config.json +3 -0
image_processing_swin.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torchvision import transforms
|
| 2 |
+
from transformers import ViTImageProcessor
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
class MyCustomSwinProcessor(ViTImageProcessor):
|
| 7 |
+
def resize_and_pad(self, image, target_size=224):
|
| 8 |
+
"""Resize image preserving aspect ratio, then pad to target size."""
|
| 9 |
+
# Get original dimensions
|
| 10 |
+
w, h = image.size
|
| 11 |
+
|
| 12 |
+
# Calculate scaling factor to fit within target_size while preserving aspect ratio
|
| 13 |
+
scale = min(target_size / w, target_size / h)
|
| 14 |
+
|
| 15 |
+
# New dimensions after scaling
|
| 16 |
+
new_w = int(w * scale)
|
| 17 |
+
new_h = int(h * scale)
|
| 18 |
+
|
| 19 |
+
# Resize the image
|
| 20 |
+
image = image.resize((new_w, new_h), Image.BILINEAR)
|
| 21 |
+
|
| 22 |
+
# Calculate padding needed
|
| 23 |
+
pad_w = target_size - new_w
|
| 24 |
+
pad_h = target_size - new_h
|
| 25 |
+
|
| 26 |
+
# Distribute padding evenly on both sides
|
| 27 |
+
left = pad_w // 2
|
| 28 |
+
right = pad_w - left
|
| 29 |
+
top = pad_h // 2
|
| 30 |
+
bottom = pad_h - top
|
| 31 |
+
|
| 32 |
+
# Pad with white because its the dataset default background color
|
| 33 |
+
return transforms.functional.pad(image, (left, top, right, bottom), fill=255)
|
| 34 |
+
|
| 35 |
+
image_transform = transforms.Compose([
|
| 36 |
+
transforms.ToTensor(),
|
| 37 |
+
# ImageNet normalization
|
| 38 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 39 |
+
])
|
| 40 |
+
|
| 41 |
+
def preprocess(self, images, **kwargs):
|
| 42 |
+
images = [self.resize_and_pad(image, target_size=224) for image in images]
|
| 43 |
+
images = [self.image_transform(image) for image in images]
|
| 44 |
+
images = torch.stack(images)
|
| 45 |
+
return super().preprocess(images, **kwargs)
|
preprocessor_config.json
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
{
|
|
|
|
|
|
|
|
|
|
| 2 |
"do_convert_rgb": null,
|
| 3 |
"do_normalize": true,
|
| 4 |
"do_rescale": true,
|
|
|
|
| 1 |
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoImageProcessor": "image_processing_swin.MyCustomSwinProcessor"
|
| 4 |
+
},
|
| 5 |
"do_convert_rgb": null,
|
| 6 |
"do_normalize": true,
|
| 7 |
"do_rescale": true,
|