Implementation:NVIDIA NeMo Curator NSFWScorer
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Computer Vision, Content Safety |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Provides NSFW (Not Safe For Work) content detection by scoring CLIP embeddings for sexually explicit content probability using a pre-trained neural network from LAION.
Description
The nsfw module contains three classes:
Normalization is a custom PyTorch layer that applies mean/variance normalization to input tensors using pre-computed statistics stored as registered buffers. It computes (x - mean) / sqrt(variance).
NSFWModel is a 4-layer feedforward neural network that processes 768-dimensional CLIP embeddings through a normalization layer, three hidden layers (768 -> 64 -> 512 -> 256) with ReLU activations, and a final output layer (256 -> 1) with sigmoid activation. The sigmoid output produces probability scores between 0 and 1. The forward pass runs under torch.no_grad() for inference efficiency.
NSFWScorer implements ModelInterface and provides the public interface for NSFW scoring. It loads weights from LAION's CLIP-based-NSFW-Detector (model ID laion/clip-autokeras-binary-nsfw), which are downloaded as a zip archive from GitHub and extracted to .pth format. The model automatically selects CUDA if available. On __call__, it accepts embeddings as either a torch.Tensor or numpy.ndarray and returns per-sample NSFW probability scores.
Usage
Use NSFWScorer as a safety component in content filtering pipelines. It works downstream of CLIP embedding extraction to flag potentially harmful visual content, enabling automated content moderation in data curation workflows.
Code Reference
Source Location
- Repository: NeMo-Curator
- File: nemo_curator/models/nsfw.py
- Lines: 1-187
Signature
class Normalization(nn.Module):
def __init__(self, shape: list[int]) -> None: ...
def forward(self, x: torch.Tensor) -> torch.Tensor: ...
class NSFWModel(nn.Module):
def __init__(self) -> None: ...
def forward(self, x: torch.Tensor) -> torch.Tensor: ...
class NSFWScorer(ModelInterface):
def __init__(self, model_dir: str) -> None: ...
@property
def conda_env_name(self) -> str: ...
@property
def model_id_names(self) -> list[str]: ...
@classmethod
def download_weights_on_node(cls, model_dir: str) -> None: ...
def setup(self) -> None: ...
def __call__(self, embeddings: torch.Tensor | npt.NDArray[np.float32]) -> torch.Tensor: ...
Import
from nemo_curator.models.nsfw import NSFWScorer
I/O Contract
Inputs (Constructor)
| Name | Type | Required | Description |
|---|---|---|---|
| model_dir | str | Yes | Path to the directory where model weights are stored or will be downloaded |
Inputs (__call__)
| Name | Type | Required | Description |
|---|---|---|---|
| embeddings | torch.Tensor or numpy.ndarray | Yes | CLIP embeddings with shape (batch_size, 768) as a torch tensor or numpy array |
Outputs
| Name | Type | Description |
|---|---|---|
| scores | torch.Tensor | Per-sample NSFW probability scores with shape (batch_size,), values between 0.0 (safe) and 1.0 (NSFW) |
Model Architecture
| Layer | Configuration |
|---|---|
| Normalization | Mean/variance normalization on shape [768] |
| Linear + ReLU | 768 -> 64 |
| Linear + ReLU | 64 -> 512 |
| Linear + ReLU | 512 -> 256 |
| Linear + Sigmoid | 256 -> 1 |
Pre-trained model: laion/clip-autokeras-binary-nsfw (downloaded from GitHub)
Weight Download
The download_weights_on_node classmethod handles the complete download workflow:
- Downloads a zip archive from the LAION GitHub repository
- Extracts the contents to model_dir/laion/clip-autokeras-binary-nsfw/
- Removes the zip file after extraction
- Weights are stored as a .pth file
Usage Examples
Basic Usage
from nemo_curator.models.nsfw import NSFWScorer
import torch
# Download weights first
NSFWScorer.download_weights_on_node("/path/to/models")
# Initialize and setup
scorer = NSFWScorer(model_dir="/path/to/models")
scorer.setup()
# Score CLIP embeddings
embeddings = torch.randn(10, 768) # batch of 10 CLIP embeddings
scores = scorer(embeddings)
print(scores.shape) # torch.Size([10])
print(scores) # values between 0.0 and 1.0
Filtering with Threshold
import numpy as np
from nemo_curator.models.nsfw import NSFWScorer
scorer = NSFWScorer(model_dir="/path/to/models")
scorer.setup()
embeddings = np.random.randn(100, 768).astype(np.float32)
scores = scorer(embeddings)
# Filter out NSFW content (score > 0.5)
safe_mask = scores < 0.5
print(f"Safe: {safe_mask.sum()}, NSFW: {(~safe_mask).sum()}")
Related Pages
- Environment:NVIDIA_NeMo_Curator_Python_Linux_Base
- NVIDIA_NeMo_Curator_ModelInterface -- Base class that NSFWScorer implements
- NVIDIA_NeMo_Curator_AestheticScorer -- Similar scoring model for aesthetic quality