Skip to main content
NSFW Detector logo

NSFW Detection Machine Learning Model

All Contributors

Trained on 60+ Gigs of data to identify:

  • drawings - safe for work drawings (including anime)
  • hentai - hentai and pornographic drawings
  • neutral - safe for work neutral images
  • porn - pornographic images, sexual acts
  • sexy - sexually explicit images, not pornography

This model powers NSFW JS - More Info

Current Status:

93% Accuracy with the following confusion matrix, based on Inception V3. nsfw confusion matrix

Review the _art folder for previous incarnations of this model.

Requirements:

keras (tested with versions > 2.0.0) tensorflow >= 2.1.0

Usage

For programmatic use of the library.

from nsfw_detector import predict
model = predict.load_model('./nsfw_mobilenet2.224x224.h5')

# Predict single image
predict.classify(model, '2.jpg')
# {'2.jpg': {'sexy': 4.3454722e-05, 'neutral': 0.00026579265, 'porn': 0.0007733492, 'hentai': 0.14751932, 'drawings': 0.85139805}}

# Predict multiple images at once
predict.classify(model, ['/Users/bedapudi/Desktop/2.jpg', '/Users/bedapudi/Desktop/6.jpg'])
# {'2.jpg': {'sexy': 4.3454795e-05, 'neutral': 0.00026579312, 'porn': 0.0007733498, 'hentai': 0.14751942, 'drawings': 0.8513979}, '6.jpg': {'drawings': 0.004214506, 'hentai': 0.013342537, 'neutral': 0.01834045, 'porn': 0.4431829, 'sexy': 0.5209196}}

# Predict for all images in a directory
predict.classify(model, '/Users/bedapudi/Desktop/')

If you've installed the package or use the command-line this should work, too...

# a single image
nsfw-predict --saved_model_path mobilenet_v2_140_224 --image_source test.jpg

# an image directory
nsfw-predict --saved_model_path mobilenet_v2_140_224 --image_source images

# a single image (from code/CLI)
python3 nsfw_detector/predict.py --saved_model_path mobilenet_v2_140_224 --image_source test.jpg

Download

Please feel free to use this model to help your products!

If you'd like to say thanks for creating this, I'll take a donation for hosting costs.

Latest Models Zip (v1.1)

https://github.com/GantMan/nsfw_model/releases/tag/1.1.0

Original Inception v3 Model (v1.0)

Original Mobilenet v2 Model (v1.0)

PyTorch Version

Kudos to the community for creating a PyTorch version with resnet! https://github.com/yangbisheng2009/nsfw-resnet

Training Folder Contents

Simple description of the scripts used to create this model:

  • inceptionv3_transfer/ - Folder with all the code to train the Keras based Inception v3 transfer learning model. Includes constants.py for configuration, and two scripts for actual training/refinement.
  • mobilenetv2_transfer/ - Folder with all the code to train the Keras based Mobilenet v2 transfer learning model.
  • visuals.py - The code to create the confusion matrix graphic
  • self_clense.py - If the training data has significant inaccuracy, self_clense helps cross validate errors in the training data in reasonable time. The better the model gets, the better you can use it to clean the training data manually.

e.g.

cd training
# Start with all locked transfer of Inception v3
python inceptionv3_transfer/train_initialization.py

# Continue training on model with fine-tuning
python inceptionv3_transfer/train_fine_tune.py

# Create a confusion matrix of the model
python visuals.py

Extra Info

There's no easy way to distribute the training data, but if you'd like to help with this model or train other models, get in touch with me and we can work together.

Advancements in this model power the quantized TFJS module on https://nsfwjs.com/

My twitter is @GantLaborde - I'm a School Of AI Wizard New Orleans. I run the twitter account @FunMachineLearn

Learn more about me and the company I work for.

Special thanks to the nsfw_data_scraper for the training data. If you're interested in a more detailed analysis of types of NSFW images, you could probably use this repo code with this data.

If you need React Native, Elixir, AI, or Machine Learning work, check in with us at Infinite Red, who make all these experiments possible. We're an amazing software consultancy worldwide!

Cite

@misc{man,
  title={Deep NN for NSFW Detection},
  url={https://github.com/GantMan/nsfw_model},
  journal={GitHub},
  author={Laborde, Gant}}

Contributors

Thanks goes to these wonderful people (emoji key):


Gant Laborde

💻 📖 🤔

Bedapudi Praneeth

💻 🤔

This project follows the all-contributors specification. Contributions of any kind welcome!

Changes

1.1.1

  • break out numpy (nd array) function
  • remove classic app run modes for argparse
  • one more example in README for running
  • turn down verbosity in image load via file
  • fix requirements for clean system (needs PIL)

1.1.0

  • update to tensorflow 2.1.0 and updated mobilenet-based model

1.0.0

  • initial creation

Metadata

Release files for nsfw-detector 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nsfw-detector 1.1.1
File Size Uploaded Provenance Actions
nsfw_detector-1.1.1.tar.gz 7.7 kB Without provenance Details for nsfw_detector-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nsfw-detector 1.1.1
File Interpreter ABI Platform Actions
nsfw_detector-1.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for nsfw_detector-1.1.1-py2.py3-none-any.whl

Total release size: 15.1 kB

Release files / nsfw_detector-1.1.1.tar.gz

Download URL nsfw_detector-1.1.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3ed40757c9073112efda56b144c6f0465383f52ede0ac09db5e51fc73815cdec
BLAKE2b-256 checksum
How to use checksums
77c1d6fd5156cd17ae590b317c75cfe345d5829bc46deac78263aeae136d9f9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Release files / nsfw_detector-1.1.1-py2.py3-none-any.whl

Download URL nsfw_detector-1.1.1-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
91aee149fb67c95f784c5fcde50eb4c43c0a4dc3b0aad5f504507da2110dbf8f
BLAKE2b-256 checksum
How to use checksums
08b5bcac8fa8e8c1911ee1ab87c59ae0e29f8aabbc50e3525f83bf2d6ed0fff8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

Security for nsfw-detector 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page