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 files48 unmodified lines4950515253545552 unmodified lines10810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318416 unmodified lines20120220320420520620720820921021121248 unmodified lines## 🚀 Quickstart Inference### 1. ONNX Runtime (Recommended for Deployment)```pythonimport numpy as np52 unmodified lines---### 2. PyTorch Definition & Inference```pythonimport torchimport torch.nn as nnfrom timm.models import create_modelfrom timm.data.transforms_factory import transforms_imagenet_evalfrom PIL import Imageclass TextStylingModel(nn.Module):def __init__(self):super().__init__()self.backbone = create_model('edgenext_small', pretrained=False, num_classes=0)self.spatial_pool = nn.AdaptiveAvgPool2d((2, 5))in_dim = 304 * 2 * 5self.bottleneck = nn.Sequential(nn.Linear(in_dim, 256),nn.BatchNorm1d(256),nn.Hardswish(),nn.Dropout(0.2))self.head_flags = nn.Linear(256, 5)self.head_bg_type = nn.Linear(256, 3)self.head_text_color = nn.Linear(256, 3)self.head_effect_color = nn.Linear(256, 3)self.head_bg_color = nn.Linear(256, 3)self.head_bg_color_a = nn.Linear(256, 3)self.head_bg_color_b = nn.Linear(256, 3)self.head_bg_direction = nn.Linear(256, 2)def forward(self, x):x = self.backbone.forward_features(x)x = self.spatial_pool(x)x = torch.flatten(x, 1)x = self.bottleneck(x)flags = self.head_flags(x)bg_type = self.head_bg_type(x)text_color = torch.clamp(self.head_text_color(x), 0.0, 1.0)effect_color = torch.clamp(self.head_effect_color(x), 0.0, 1.0)bg_color = torch.clamp(self.head_bg_color(x), 0.0, 1.0)bg_color_a = torch.clamp(self.head_bg_color_a(x), 0.0, 1.0)bg_color_b = torch.clamp(self.head_bg_color_b(x), 0.0, 1.0)bg_direction = self.head_bg_direction(x)dnorm = bg_direction.norm(dim=-1, keepdim=True).clamp(min=1e-6)bg_direction = bg_direction / dnormreturn flags, bg_type, text_color, effect_color, bg_color, bg_color_a, bg_color_b, bg_direction# Load model weightsmodel = TextStylingModel()checkpoint = torch.load("model_best.pth.tar", map_location="cpu")# If EMA weights exist, prioritize them:state_dict = checkpoint.get("state_dict_ema", checkpoint.get("state_dict"))model.load_state_dict(state_dict)model.eval()# Transformtransform = transforms_imagenet_eval(img_size=(64, 160), crop_pct=1.0, interpolation='bicubic')img = Image.open("sample_text.png").convert("RGB")img_t = transform(img).unsqueeze(0)with torch.inference_mode():preds = model(img_t)```---## 🛠️ Training Details- **Loss Function**: Multi-Task Loss combining:16 unmodified lines- `bg_color` is only valid when `bg_type == Solid (0)`.- `bg_color_a`, `bg_color_b`, and `bg_direction` are only valid when `bg_type == Gradient (1)`.- For `bg_type == Artwork (2)`, no background color regression is guaranteed.```---### Recommended Files to Upload to the Hugging Face Repository:1. `README.md` (The Model Card above)2. `text_styling_model.onnx` (The exported ONNX model)3. `model_best.pth.tar` (The PyTorch checkpoint containing weights and EMA states)4. *(Optional)* `sample_text.png` (A sample cropped image for testing)48 unmodified lines4950515253545552 unmodified lines10810911011111211316 unmodified lines13013113213348 unmodified lines## 🚀 Quickstart Inference### ONNX Runtime (Recommended for Deployment)```pythonimport numpy as np52 unmodified lines---## 🛠️ Training Details- **Loss Function**: Multi-Task Loss combining:16 unmodified lines- `bg_color` is only valid when `bg_type == Solid (0)`.- `bg_color_a`, `bg_color_b`, and `bg_direction` are only valid when `bg_type == Gradient (1)`.- For `bg_type == Artwork (2)`, no background color regression is guaranteed.```
README.md+2-81