File size: 5,888 Bytes
ab6754d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97d7e53
ab6754d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
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}
}
```