safety-classifier / README.md
dr-stone's picture
Claude Opus 5.5
Say what the model is for in one sentence
97d7e53
|
Raw History Blame Contribute Delete
5.89 kB
---
model_name: Live Safety Classifier
library_name: 'timm'
base_model: 'timm/mobilenetv4_conv_medium.e250_r384_in12k'
tags:
- image-classification
- content-safety
- timm
---
# Live Safety Classifier
Live Safety Classifier predicts the content-safety category of one image: `hot`, `nsfw`, or `safe`.
The model is fine-tuned from `timm/mobilenetv4_conv_medium.e250_r384_in12k`.
## What It Is For
This model is built for filtering images. An application can use it to find which safety category an image belongs to before running more expensive analysis.
## Files
| File | Purpose |
| :------------------------ | :------------------------------------------------------------- |
| `onnx/model.onnx` | ONNX model for CPU/server inference. |
| `onnx/model.fp16.onnx` | Optional FP16 ONNX candidate. |
| `onnx/model.onnx.data` | External ONNX weight data loaded beside `model.onnx`. |
| `model.safetensors` | PyTorch state dict for reproducibility and continued training. |
| `config.json` | Model identity, base model, output names, and safety labels. |
| `preprocess.json` | Resize and normalization contract used during training/export. |
| `train.json` | Sanitized training recipe for post-training or reproduction. |
| `inference/python.py` | Python helper for ONNX inference from image paths. |
| `inference/typescript.ts` | TypeScript helper for ONNX inference from image paths. |
| `inference/labels.json` | Safety labels used to decode model logits. |
| `README.md` | This model card and runnable inference reference. |
## Outputs
Read the `safety` output:
| Output | Shape | Meaning |
| :------- | :---------------------------- | :---------------------- |
| `safety` | `[batch, safety_class_count]` | Content-safety logits. |
Inference uses `argmax` in this version.
The model also has a phone classification head, which outputs `screen`. It is not used: only the safety head is.
## Test Results
| Model | Test | Accuracy | Balanced Acc | Macro F1 | Top-2 | Images |
| :-- | --: | --: | --: | --: | --: | --: |
| fp32 | full test | 0.9575 | 0.8941 | 0.8600 | 0.9921 | 23615 |
| fp32 | safety-balanced test | 0.8957 | 0.8957 | 0.8947 | 0.9847 | 3000 |
| fp16 | full test | 0.9575 | 0.8941 | 0.8600 | 0.9921 | 23615 |
| fp16 | safety-balanced test | 0.8957 | 0.8957 | 0.8947 | 0.9847 | 3000 |
## CPU Timing
| Model | Images/s | Load Mean | Model Mean | Total Mean | Total Median | Total P95 | Provider |
| :-- | --: | --: | --: | --: | --: | --: | --: |
| fp32 | 20.0538 | 0.12 ms | 49.52 ms | 49.87 ms | 57.84 ms | 85.82 ms | onnxruntime:CPUExecutionProvider |
| fp16 | 20.8351 | 0.11 ms | 47.69 ms | 48.00 ms | 48.60 ms | 63.65 ms | onnxruntime:CPUExecutionProvider |
Timing is measured with ONNX Runtime CPU execution on `Apple M4 Max (16 logical cores)`. Total latency includes image load/preprocess, model inference, and label decoding.
## Classification Labels
For the safety label list, see [`config.json`](config.json).
## Preprocessing
Use the preprocessing contract in `preprocess.json`.
1. Read the image.
2. Apply EXIF orientation.
3. Convert to RGB.
4. Resize so the longest side is `1024` pixels and keep the original aspect ratio.
5. Do not crop, stretch, square-pad before preprocessing, or horizontally flip.
6. Convert to `float32` in `[0, 1]`.
7. Normalize with the mean and standard deviation from `preprocess.json`.
8. Pad each normalized tensor to the batch maximum height and width, rounded up to a multiple of 32.
The exported model accepts dynamic batch, height, and width.
## ONNX Inference
The helpers return only `safety`.
Keep `onnx/model.onnx.data` beside `onnx/model.onnx`; ONNX Runtime loads the external tensor data when it opens the graph.
By default the helpers load the FP32 model at `onnx/model.onnx`. Pass `fp16` to load `onnx/model.fp16.onnx`.
### Download
Download the exported model folder from Hugging Face:
```python
from huggingface_hub import snapshot_download
model_dir = snapshot_download("buystufflive/safety-classifier")
```
### Python
Install the runtime dependencies:
```bash
python -m pip install numpy pillow onnxruntime
```
Import the helper from the exported model folder:
```python
from inference.python import Classifier, classify
print(classify("example.png"))
print(classify("example.png", model_format="fp16"))
classifier = Classifier(model_dir)
print(classifier.classify_batch(["one.png", "two.png"]))
fp16_classifier = Classifier(model_dir, model_format="fp16")
print(fp16_classifier.classify_batch(["one.png", "two.png"]))
```
Or run it directly:
```bash
python inference/python.py example.png another.png --model-format fp16
```
### TypeScript
Install the runtime dependencies:
```bash
bun add sharp onnxruntime-node
```
Import the helper from the exported model folder:
```ts
import { Classifier, classify } from './inference/typescript.ts';
console.log(await classify('example.png'));
console.log(await classify('example.png', modelDir, 'fp16'));
const classifier = await Classifier.create(modelDir);
console.log(await classifier.classifyBatch(['one.png', 'two.png']));
const fp16Classifier = await Classifier.create(modelDir, 'fp16');
console.log(await fp16Classifier.classifyBatch(['one.png', 'two.png']));
```
Or run it directly:
```bash
bun inference/typescript.ts example.png another.png --model-format fp16
```
## Citation
If you use this model, please cite:
```bibtex
@misc{safety-classifier,
title={Live Safety Classifier},
year={2026},
publisher={Live},
url={https://huggingface.co/buystufflive/safety-classifier}
}
```