Instructions to use Liiesl/text-styling-classificationv1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- timm
How to use Liiesl/text-styling-classificationv1 with timm:
import timm model = timm.create_model("hf-hub:Liiesl/text-styling-classificationv1", pretrained=True) - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -49,7 +49,7 @@ The model outputs **8 multi-task tensors**:
|
|
| 49 |
|
| 50 |
## 🚀 Quickstart Inference
|
| 51 |
|
| 52 |
-
###
|
| 53 |
|
| 54 |
```python
|
| 55 |
import numpy as np
|
|
@@ -108,77 +108,6 @@ elif bg_type_idx == 1:
|
|
| 108 |
|
| 109 |
---
|
| 110 |
|
| 111 |
-
### 2. PyTorch Definition & Inference
|
| 112 |
-
|
| 113 |
-
```python
|
| 114 |
-
import torch
|
| 115 |
-
import torch.nn as nn
|
| 116 |
-
from timm.models import create_model
|
| 117 |
-
from timm.data.transforms_factory import transforms_imagenet_eval
|
| 118 |
-
from PIL import Image
|
| 119 |
-
|
| 120 |
-
class TextStylingModel(nn.Module):
|
| 121 |
-
def __init__(self):
|
| 122 |
-
super().__init__()
|
| 123 |
-
self.backbone = create_model('edgenext_small', pretrained=False, num_classes=0)
|
| 124 |
-
self.spatial_pool = nn.AdaptiveAvgPool2d((2, 5))
|
| 125 |
-
in_dim = 304 * 2 * 5
|
| 126 |
-
|
| 127 |
-
self.bottleneck = nn.Sequential(
|
| 128 |
-
nn.Linear(in_dim, 256),
|
| 129 |
-
nn.BatchNorm1d(256),
|
| 130 |
-
nn.Hardswish(),
|
| 131 |
-
nn.Dropout(0.2)
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
self.head_flags = nn.Linear(256, 5)
|
| 135 |
-
self.head_bg_type = nn.Linear(256, 3)
|
| 136 |
-
self.head_text_color = nn.Linear(256, 3)
|
| 137 |
-
self.head_effect_color = nn.Linear(256, 3)
|
| 138 |
-
self.head_bg_color = nn.Linear(256, 3)
|
| 139 |
-
self.head_bg_color_a = nn.Linear(256, 3)
|
| 140 |
-
self.head_bg_color_b = nn.Linear(256, 3)
|
| 141 |
-
self.head_bg_direction = nn.Linear(256, 2)
|
| 142 |
-
|
| 143 |
-
def forward(self, x):
|
| 144 |
-
x = self.backbone.forward_features(x)
|
| 145 |
-
x = self.spatial_pool(x)
|
| 146 |
-
x = torch.flatten(x, 1)
|
| 147 |
-
x = self.bottleneck(x)
|
| 148 |
-
|
| 149 |
-
flags = self.head_flags(x)
|
| 150 |
-
bg_type = self.head_bg_type(x)
|
| 151 |
-
text_color = torch.clamp(self.head_text_color(x), 0.0, 1.0)
|
| 152 |
-
effect_color = torch.clamp(self.head_effect_color(x), 0.0, 1.0)
|
| 153 |
-
bg_color = torch.clamp(self.head_bg_color(x), 0.0, 1.0)
|
| 154 |
-
bg_color_a = torch.clamp(self.head_bg_color_a(x), 0.0, 1.0)
|
| 155 |
-
bg_color_b = torch.clamp(self.head_bg_color_b(x), 0.0, 1.0)
|
| 156 |
-
|
| 157 |
-
bg_direction = self.head_bg_direction(x)
|
| 158 |
-
dnorm = bg_direction.norm(dim=-1, keepdim=True).clamp(min=1e-6)
|
| 159 |
-
bg_direction = bg_direction / dnorm
|
| 160 |
-
|
| 161 |
-
return flags, bg_type, text_color, effect_color, bg_color, bg_color_a, bg_color_b, bg_direction
|
| 162 |
-
|
| 163 |
-
# Load model weights
|
| 164 |
-
model = TextStylingModel()
|
| 165 |
-
checkpoint = torch.load("model_best.pth.tar", map_location="cpu")
|
| 166 |
-
# If EMA weights exist, prioritize them:
|
| 167 |
-
state_dict = checkpoint.get("state_dict_ema", checkpoint.get("state_dict"))
|
| 168 |
-
model.load_state_dict(state_dict)
|
| 169 |
-
model.eval()
|
| 170 |
-
|
| 171 |
-
# Transform
|
| 172 |
-
transform = transforms_imagenet_eval(img_size=(64, 160), crop_pct=1.0, interpolation='bicubic')
|
| 173 |
-
img = Image.open("sample_text.png").convert("RGB")
|
| 174 |
-
img_t = transform(img).unsqueeze(0)
|
| 175 |
-
|
| 176 |
-
with torch.inference_mode():
|
| 177 |
-
preds = model(img_t)
|
| 178 |
-
```
|
| 179 |
-
|
| 180 |
-
---
|
| 181 |
-
|
| 182 |
## 🛠️ Training Details
|
| 183 |
|
| 184 |
- **Loss Function**: Multi-Task Loss combining:
|
|
@@ -201,12 +130,4 @@ with torch.inference_mode():
|
|
| 201 |
- `bg_color` is only valid when `bg_type == Solid (0)`.
|
| 202 |
- `bg_color_a`, `bg_color_b`, and `bg_direction` are only valid when `bg_type == Gradient (1)`.
|
| 203 |
- For `bg_type == Artwork (2)`, no background color regression is guaranteed.
|
| 204 |
-
```
|
| 205 |
-
|
| 206 |
-
---
|
| 207 |
-
|
| 208 |
-
### Recommended Files to Upload to the Hugging Face Repository:
|
| 209 |
-
1. `README.md` (The Model Card above)
|
| 210 |
-
2. `text_styling_model.onnx` (The exported ONNX model)
|
| 211 |
-
3. `model_best.pth.tar` (The PyTorch checkpoint containing weights and EMA states)
|
| 212 |
-
4. *(Optional)* `sample_text.png` (A sample cropped image for testing)
|
|
|
|
| 49 |
|
| 50 |
## 🚀 Quickstart Inference
|
| 51 |
|
| 52 |
+
### ONNX Runtime (Recommended for Deployment)
|
| 53 |
|
| 54 |
```python
|
| 55 |
import numpy as np
|
|
|
|
| 108 |
|
| 109 |
---
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
## 🛠️ Training Details
|
| 112 |
|
| 113 |
- **Loss Function**: Multi-Task Loss combining:
|
|
|
|
| 130 |
- `bg_color` is only valid when `bg_type == Solid (0)`.
|
| 131 |
- `bg_color_a`, `bg_color_b`, and `bg_direction` are only valid when `bg_type == Gradient (1)`.
|
| 132 |
- For `bg_type == Artwork (2)`, no background color regression is guaranteed.
|
| 133 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|