MCPcopy Create free account
hub / github.com/ml-explore/mlx-examples / CLIPImageProcessor

Class CLIPImageProcessor

clip/image_processor.py:12–58  ·  view source on GitHub ↗

A simple port of https://github.com/huggingface/transformers/blob/main/src/transformers/models/clip/image_processing_clip.py.

Source from the content-addressed store, hash-verified

10
11
12class CLIPImageProcessor:
13 """
14 A simple port of
15 https://github.com/huggingface/transformers/blob/main/src/transformers/models/clip/image_processing_clip.py.
16 """
17
18 def __init__(
19 self,
20 crop_size: int = 224,
21 do_center_crop: bool = True,
22 do_normalize: bool = True,
23 do_resize: bool = True,
24 image_mean: List[float] = [0.48145466, 0.4578275, 0.40821073],
25 image_std: List[float] = [0.26862954, 0.26130258, 0.27577711],
26 size: int = 224,
27 **kwargs
28 ) -> None:
29 self.crop_size = crop_size
30 self.do_center_crop = do_center_crop
31 self.do_normalize = do_normalize
32 self.do_resize = do_resize
33 self.image_mean = mx.array(image_mean)
34 self.image_std = mx.array(image_std)
35 self.size = size
36
37 def __call__(self, images: List[Image]) -> mx.array:
38 return mx.concatenate(
39 [self._preprocess(image)[None] for image in images], axis=0
40 )
41
42 def _preprocess(self, image: Image) -> mx.array:
43 if self.do_resize:
44 image = resize(image, self.size)
45 if self.do_center_crop:
46 image = center_crop(image, (self.crop_size, self.crop_size))
47 image = mx.array(np.array(image))
48 image = rescale(image)
49 if self.do_normalize:
50 image = normalize(image, self.image_mean, self.image_std)
51 return image
52
53 @staticmethod
54 def from_pretrained(path: str):
55 path = Path(path)
56 with open(path / "preprocessor_config.json", encoding="utf-8") as f:
57 config = json.load(f)
58 return CLIPImageProcessor(**config)
59
60
61def resize(image: Image, short_size: int) -> Image:

Callers 1

from_pretrainedMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected