Views
No views yet
OwenElliott/image-safety-classifier-s is a lightweight image classification model designed to categorise images as either NSFW, NSFL, or SFW. NSFW images contain pornographic or highly suggestive content, NSFL images contain gore, and SFW images are everything else. The small size of this model and the SwiftFormer architecture make this model suitable for edge deployment or latency critical applications.Marqo/nsfw-image-detection-384 and any images which were confidently predicted to be in the wrong class were manually reviewed.| Model Name | Parameters | Accuracy |
|---|---|---|
OwenElliott/image-safety-classifier-xs | 3.5M | 97.76% |
OwenElliott/image-safety-classifier-s (this model) | 6.1M | 97.99% |
OwenElliott/image-safety-classifier-m | 12.1M | 98.06% |
OwenElliott/image-safety-classifier-l | 28.5M | 98.20% |


pip install timm1from urllib.request import urlopen
2from PIL import Image
3import timm
4import torch
5
6img = Image.open(urlopen(
7 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
8))
9
10model = timm.create_model("OwenElliott/image-safety-classifier-s", pretrained=True)
11model = model.eval()
12
13data_config = timm.data.resolve_model_data_config(model)
14transforms = timm.data.create_transform(**data_config, is_training=False)
15
16with torch.no_grad():
17 output = model(transforms(img).unsqueeze(0)).softmax(dim=-1).cpu()
18
19class_names = model.pretrained_cfg["label_names"]
20print("Probabilities:", output[0])
21print("Class:", class_names[output[0].argmax()])1pip install onnxruntime pillow numpy
2# For GPU: pip install onnxruntime-gpu1from urllib.request import urlopen
2from PIL import Image
3import numpy as np
4import onnxruntime as ort
5
6img = Image.open(urlopen(
7 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
8))
9
10# The model accepts 224x224 images
11img = img.convert("RGB").resize((224, 224), Image.BILINEAR)
12
13# Convert to [1, 3, 224, 224] float32 in 0-255
14pixels = np.array(img, dtype=np.float32).transpose(2, 0, 1)[np.newaxis]
15
16# Run inference (use fp16 variant for GPU acceleration)
17# image colour channel normalisation is baked into the ONNX models
18from huggingface_hub import hf_hub_download
19model_path = hf_hub_download("OwenElliott/image-safety-classifier-s", "image-safety-classifier-s.onnx")
20sess = ort.InferenceSession(model_path)
21probs = sess.run(None, {{"image": pixels}})[0][0]
22
23class_names = ["NSFL", "NSFW", "SFW"]
24print("Probabilities:", dict(zip(class_names, probs)))
25print("Class:", class_names[np.argmax(probs)])@InProceedings{Shaker_2023_ICCV,
author = {Shaker, Abdelrahman and Maaz, Muhammad and Rasheed, Hanoona and Khan, Salman and Yang, Ming-Hsuan and Khan, Fahad Shahbaz},
title = {SwiftFormer: Efficient Additive Attention for Transformer-based Real-time Mobile Vision Applications},
booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
year = {2023},
}@misc{rw2019timm,
author = {Ross Wightman},
title = {PyTorch Image Models},
year = {2019},
publisher = {GitHub},
journal = {GitHub repository},
doi = {10.5281/zenodo.4414861},
howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
}