jagennath-hari commited on
Commit
5cb2a8d
·
1 Parent(s): 6398828

intial json for preprocessor

Browse files
preprocessor_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "image_processor_type": "DFineProcessor",
3
+ "size": 640,
4
+ "do_resize": true,
5
+ "do_pad": true,
6
+ "return_tensor": "pt",
7
+ "return_ratio": true,
8
+ "return_padding": true,
9
+ "return_orig_size": true
10
+ }
processor_config.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "processor_class": "DFineProcessor"
3
- }
4
-
 
 
 
 
 
processor_dfine.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ProcessorMixin
2
+ from PIL import Image
3
+ import torchvision.transforms as T
4
+ import torch
5
+ import numpy as np
6
+
7
+ class DFineProcessor(ProcessorMixin):
8
+ def __init__(self, size=640):
9
+ self.size = size
10
+
11
+ def resize_with_aspect_ratio(self, image):
12
+ original_width, original_height = image.size
13
+ ratio = min(self.size / original_width, self.size / original_height)
14
+ new_width = int(original_width * ratio)
15
+ new_height = int(original_height * ratio)
16
+ image = image.resize((new_width, new_height), Image.BILINEAR)
17
+
18
+ new_image = Image.new("RGB", (self.size, self.size))
19
+ pad_w = (self.size - new_width) // 2
20
+ pad_h = (self.size - new_height) // 2
21
+ new_image.paste(image, (pad_w, pad_h))
22
+
23
+ return new_image, ratio, pad_w, pad_h
24
+
25
+ def __call__(self, images):
26
+ if not isinstance(images, (list, tuple)):
27
+ images = [images]
28
+
29
+ tensors, orig_sizes, ratios, pad_ws, pad_hs = [], [], [], [], []
30
+
31
+ for image in images:
32
+ if isinstance(image, np.ndarray):
33
+ image = Image.fromarray(image)
34
+ elif not isinstance(image, Image.Image):
35
+ raise ValueError("Input must be PIL.Image, numpy.ndarray, or list of them.")
36
+
37
+ resized, ratio, pad_w, pad_h = self.resize_with_aspect_ratio(image)
38
+ tensor = T.ToTensor()(resized)
39
+
40
+ tensors.append(tensor)
41
+ orig_sizes.append([resized.size[1], resized.size[0]])
42
+ ratios.append(ratio)
43
+ pad_ws.append(pad_w)
44
+ pad_hs.append(pad_h)
45
+
46
+ batch_tensor = torch.stack(tensors)
47
+ return {
48
+ "images": batch_tensor,
49
+ "orig_target_sizes": torch.tensor(orig_sizes),
50
+ "ratio": torch.tensor(ratios),
51
+ "pad_w": torch.tensor(pad_ws),
52
+ "pad_h": torch.tensor(pad_hs),
53
+ }
54
+
55
+ def save_pretrained(self, save_directory):
56
+ # Optional: save size or metadata here if needed
57
+ pass
58
+
59
+ @classmethod
60
+ def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
61
+ # Optionally load metadata like `size` from processor_config.json
62
+ return cls()