Upload processor
Browse files- image_processing_biomedclip.py +26 -0
- preprocessor_config.json +28 -0
image_processing_biomedclip.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List, Tuple
|
| 2 |
+
from PIL.Image import Image
|
| 3 |
+
from open_clip.factory import PreprocessCfg, image_transform_v2
|
| 4 |
+
from transformers import ImageProcessingMixin
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
class _ValidKwargs:
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
class BiomedCLIPImageProcessor(ImageProcessingMixin):
|
| 11 |
+
valid_kwargs = _ValidKwargs
|
| 12 |
+
|
| 13 |
+
def __init__(self, preprocess_cfg: Dict[str, Any],
|
| 14 |
+
is_train: bool = False, **kwargs):
|
| 15 |
+
super().__init__(**kwargs)
|
| 16 |
+
self.preprocess_cfg = preprocess_cfg
|
| 17 |
+
self.is_train = is_train
|
| 18 |
+
self.preprocess = None # avoid hasattr hack
|
| 19 |
+
|
| 20 |
+
def __call__(self, images, return_tensors="pt"):
|
| 21 |
+
if not isinstance(images, (list, tuple)):
|
| 22 |
+
images = [images]
|
| 23 |
+
if self.preprocess is None:
|
| 24 |
+
self.preprocess = image_transform_v2(PreprocessCfg(**self.preprocess_cfg), is_train=self.is_train)
|
| 25 |
+
pixels = torch.stack([self.preprocess(im) for im in images], dim=0)
|
| 26 |
+
return {"pixel_values": pixels}
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoImageProcessor": "image_processing_biomedclip.BiomedCLIPImageProcessor"
|
| 4 |
+
},
|
| 5 |
+
"image_processor_type": "BiomedCLIPImageProcessor",
|
| 6 |
+
"is_train": false,
|
| 7 |
+
"preprocess": null,
|
| 8 |
+
"preprocess_cfg": {
|
| 9 |
+
"fill_color": 0,
|
| 10 |
+
"interpolation": "bicubic",
|
| 11 |
+
"mean": [
|
| 12 |
+
0.48145466,
|
| 13 |
+
0.4578275,
|
| 14 |
+
0.40821073
|
| 15 |
+
],
|
| 16 |
+
"mode": "RGB",
|
| 17 |
+
"resize_mode": "shortest",
|
| 18 |
+
"size": [
|
| 19 |
+
224,
|
| 20 |
+
224
|
| 21 |
+
],
|
| 22 |
+
"std": [
|
| 23 |
+
0.26862954,
|
| 24 |
+
0.26130258,
|
| 25 |
+
0.27577711
|
| 26 |
+
]
|
| 27 |
+
}
|
| 28 |
+
}
|