electblake commited on
Commit
b69eefc
·
verified ·
1 Parent(s): 669d050

Add standard Transformers image preprocessing

Browse files
Files changed (3) hide show
  1. README.md +9 -22
  2. config.json +4 -9
  3. preprocessor_config.json +10 -14
README.md CHANGED
@@ -85,45 +85,32 @@ published classifier and is unnecessary when the input is already cropped.
85
 
86
  ## ONNX inference
87
 
88
- Install `huggingface_hub`, `onnxruntime`, `numpy`, `opencv-python-headless`, and
89
- `Pillow`, then run:
90
 
91
  ```python
92
  from huggingface_hub import hf_hub_download
93
- import cv2
94
  import numpy as np
95
  import onnxruntime as ort
96
  from PIL import Image, ImageOps
 
97
 
98
  REPO_ID = "electblake/clothing_type_classifier"
99
  LABELS = ["denim_shorts", "lingerie", "maid", "nurse", "swim_bikini"]
100
- HEIGHT = 384
101
- WIDTH = 256
102
-
103
  model_path = hf_hub_download(
104
  REPO_ID,
105
  "clothing_type-tf_efficientnetv2_s.in21k_ft_in1k.onnx",
106
  )
107
 
108
  image = ImageOps.exif_transpose(Image.open("person.jpg")).convert("RGB")
109
- image = np.asarray(image)
110
- scale = min(HEIGHT / image.shape[0], WIDTH / image.shape[1])
111
- resized_width = round(image.shape[1] * scale)
112
- resized_height = round(image.shape[0] * scale)
113
- interpolation = cv2.INTER_AREA if scale < 1 else cv2.INTER_CUBIC
114
- image = cv2.resize(image, (resized_width, resized_height), interpolation=interpolation)
115
-
116
- canvas = np.full((HEIGHT, WIDTH, 3), 127, dtype=np.uint8)
117
- top = (HEIGHT - resized_height) // 2
118
- left = (WIDTH - resized_width) // 2
119
- canvas[top : top + resized_height, left : left + resized_width] = image
120
-
121
- image = canvas.astype(np.float32) / 255.0
122
- image = (image - 0.5) / 0.5
123
- image = np.transpose(image, (2, 0, 1))[None, ...]
124
 
125
  session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
126
- logits = session.run(["logits"], {"image": image})[0][0]
 
 
 
127
  probabilities = np.exp(logits - logits.max())
128
  probabilities /= probabilities.sum()
129
 
 
85
 
86
  ## ONNX inference
87
 
88
+ Install `huggingface_hub`, `transformers`, `timm`, `torchvision`, `onnxruntime`,
89
+ `numpy`, and `Pillow`, then run:
90
 
91
  ```python
92
  from huggingface_hub import hf_hub_download
 
93
  import numpy as np
94
  import onnxruntime as ort
95
  from PIL import Image, ImageOps
96
+ from transformers import AutoImageProcessor
97
 
98
  REPO_ID = "electblake/clothing_type_classifier"
99
  LABELS = ["denim_shorts", "lingerie", "maid", "nurse", "swim_bikini"]
 
 
 
100
  model_path = hf_hub_download(
101
  REPO_ID,
102
  "clothing_type-tf_efficientnetv2_s.in21k_ft_in1k.onnx",
103
  )
104
 
105
  image = ImageOps.exif_transpose(Image.open("person.jpg")).convert("RGB")
106
+ image_processor = AutoImageProcessor.from_pretrained(REPO_ID)
107
+ inputs = image_processor(image, return_tensors="pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
110
+ logits = session.run(
111
+ ["logits"],
112
+ {"image": inputs["pixel_values"].numpy()},
113
+ )[0][0]
114
  probabilities = np.exp(logits - logits.max())
115
  probabilities /= probabilities.sum()
116
 
config.json CHANGED
@@ -24,15 +24,10 @@
24
  384,
25
  256
26
  ],
27
- "fixed_input_size": true,
28
- "interpolation": "bicubic",
29
- "resize_mode": "longest_max_size",
30
- "padding": "center",
31
- "pad_value": [
32
- 127,
33
- 127,
34
- 127
35
- ],
36
  "mean": [
37
  0.5,
38
  0.5,
 
24
  384,
25
  256
26
  ],
27
+ "fixed_input_size": true,
28
+ "interpolation": "bicubic",
29
+ "crop_pct": 1.0,
30
+ "crop_mode": "border",
 
 
 
 
 
31
  "mean": [
32
  0.5,
33
  0.5,
preprocessor_config.json CHANGED
@@ -12,17 +12,13 @@
12
  0.5,
13
  0.5
14
  ],
15
- "std": [
16
- 0.5,
17
- 0.5,
18
- 0.5
19
- ],
20
- "resize_mode": "longest_max_size",
21
- "padding": "center",
22
- "pad_value": [
23
- 127,
24
- 127,
25
- 127
26
- ]
27
- }
28
- }
 
12
  0.5,
13
  0.5
14
  ],
15
+ "std": [
16
+ 0.5,
17
+ 0.5,
18
+ 0.5
19
+ ],
20
+ "crop_pct": 1.0,
21
+ "crop_mode": "border"
22
+ },
23
+ "image_processor_type": "TimmWrapperImageProcessor"
24
+ }