wucng commited on
Commit
efcd44d
1 Parent(s): 85cc471

Upload processor

Browse files
image_processing_resnet.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import albumentations as A
4
+ from albumentations.pytorch.transforms import ToTensorV2
5
+ import PIL.Image
6
+ import numpy as np
7
+ from functools import partial
8
+ from typing import Dict, List, Optional, Union
9
+ from datasets import load_dataset, DatasetDict, Image # pip install datasets
10
+ from torch.utils.data import DataLoader
11
+ import torch
12
+
13
+ # 自定义 ImageProcessor 为了与 pipeline使用
14
+ from transformers import ViTImageProcessor
15
+ from transformers.image_utils import PILImageResampling, ChannelDimension
16
+ from transformers.image_processing_utils import get_size_dict
17
+
18
+ class ResnetImageProcessor(ViTImageProcessor):
19
+ """
20
+ >>> # tfs = A.Compose([A.Resize(256, 256), A.CenterCrop(224, 224)])
21
+ >>> # 如果传入 参数 tfs=tfs 在调用save_pretrained会报错
22
+ >>> # 本地使用
23
+ >>> mean = [0.485, 0.456, 0.406];std = [0.229, 0.224, 0.225]
24
+ >>> image_processor = ResnetImageProcessor(size=(224, 224), image_mean=mean, image_std=std)
25
+ >>> image_processor.save_pretrained("custom-resnet")
26
+ >>> image_processor = ResnetImageProcessor.from_pretrained("custom-resnet")
27
+
28
+ >>> # push_to_hub
29
+ >>> # hub登录
30
+ >>> from huggingface_hub import notebook_login;notebook_login()
31
+ >>> # or huggingface-cli login
32
+
33
+ >>> ResnetImageProcessor.register_for_auto_class()
34
+ >>> mean = [0.485, 0.456, 0.406];std = [0.229, 0.224, 0.225]
35
+ >>> image_processor = ResnetImageProcessor(size=(224, 224), image_mean=mean, image_std=std)
36
+ >>> image_processor.save_pretrained("custom-resnet")
37
+ >>> # image_processor = ResnetImageProcessor.from_pretrained("custom-resnet")
38
+ >>> # 如果要执行 push_to_hub 需要将 custom-resnet/preprocessor_config.json 中的 "image_processor_type" 改成 "ViTImageProcessor"
39
+ >>> # 默认的 ResnetImageProcessor 没有注册到 AutoImageProcessor
40
+ >>> # 否则从 使用 AutoImageProcessor 加载 会报错了
41
+ >>> image_processor.push_to_hub('custom-resnet')
42
+
43
+ >>> # 从 huggingface hub 加载
44
+ >>> from transformers import AutoImageProcessor
45
+ >>> AutoImageProcessor.register(config_class='wucng/custom-resnet/config.json',image_processor_class=ResnetImageProcessor)
46
+ >>> image_processor = AutoImageProcessor.from_pretrained('wucng/custom-resnet', trust_remote_code=True)
47
+ """
48
+
49
+ def resize(
50
+ self,
51
+ image: np.ndarray,
52
+ size: Dict[str, int],
53
+ resample: PILImageResampling = PILImageResampling.BILINEAR,
54
+ data_format: Optional[Union[str, ChannelDimension]] = None,
55
+ input_data_format: Optional[Union[str, ChannelDimension]] = None,
56
+ **kwargs,
57
+ ) -> np.ndarray:
58
+ size = get_size_dict(size)
59
+ output_size = (size["height"], size["width"])
60
+ height, width = size["height"], size["width"]
61
+
62
+ tfs = kwargs.get('tfs', None)
63
+ if tfs is None:
64
+ ratio = 256 / 224
65
+ tfs = A.Compose([A.Resize(int(ratio * height), int(ratio * width)), A.CenterCrop(height, width)])
66
+ return tfs(image=image)['image']
preprocessor_config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "image_processing_resnet.ResnetImageProcessor"
4
+ },
5
+ "do_normalize": true,
6
+ "do_rescale": true,
7
+ "do_resize": true,
8
+ "image_mean": [
9
+ 0.485,
10
+ 0.456,
11
+ 0.406
12
+ ],
13
+ "image_processor_type": "ResnetImageProcessor",
14
+ "image_std": [
15
+ 0.229,
16
+ 0.224,
17
+ 0.225
18
+ ],
19
+ "resample": 2,
20
+ "rescale_factor": 0.00392156862745098,
21
+ "size": {
22
+ "height": 224,
23
+ "width": 224
24
+ }
25
+ }