Instructions to use electblake/clothing_type_classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- timm
How to use electblake/clothing_type_classifier with timm:
import timm model = timm.create_model("hf-hub:electblake/clothing_type_classifier", pretrained=True) - Notebooks
- Google Colab
- Kaggle
Add standard Transformers image preprocessing
Browse files- README.md +9 -22
- config.json +4 -9
- 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`, `
|
| 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 |
-
|
| 110 |
-
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 30 |
-
"
|
| 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 |
-
"
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|