Giter Club home page Giter Club logo

nsfwnet's Introduction

Update - This project is deprecated and you shouldn't use it.

NsfwNET

.NET Standard library for classifying pornographic images.

This project bundles two different trained network models for binary classification of pornographic and non-pornographic imagery. One is a Resnet model by Yahoo, known as the "open NSFW" model. The other is a Squeezenet 1.1 type model, custom trained by me. Pay attention to the licenses on these models as they are different.

Straight To The Point

Here's the output of the test program as of version 1.1.1:

Squeezenet classifier has cutoff of 0.8.
Testing against a total of 1164 images, 582 from each class.
Classifier with type Squeezenet classified pornographic images with an accuracy of 98.1099656357388%.
Classifier with type Squeezenet classified non-pornographic images with an accuracy of 98.4536082474227%.
Classifier with type Squeezenet took an average of 11.9312714776632 msec per image to classify.

Resnet classifier has cutoff of 0.2.
Testing against a total of 1164 images, 582 from each class.
Classifier with type Resnet classified pornographic images with an accuracy of 99.1408934707904%.
Classifier with type Resnet classified non-pornographic images with an accuracy of 97.2508591065292%.
Classifier with type Resnet took an average of 33.9493127147766 msec per image to classify.

SqueezeNet classifier was 2.84392314888105 times faster.

Everything Else

This project is available as a .NET Standard 2.0 project on Nuget. The Nuget package bundles the necessary model data and is configured to automatically stage those files, along with native, architecture specific dll's inside your parent project output directory.

NsfwNET uses OpenCV's DNN module to load and use either Yahoo's NSFW pre-trained caffe model, or my bundled SqueezNet 1.1 caffe model. A simple, convenient .NET library with a single exported class wraps this functionality nicely. The .NET classifier class has a single modifiable property, Cutoff, which allows you to set the value at which all classifications results exceeding this value are determined to be pornography. This value defaults to 0.2 for Yahoo's model, because in my manual testing, this is the most accurate setting. For my SqueeNet 1.1 model, this value defaults to 0.8 for the same reason.

The NSFW model emits a single output, which is a probability of pornographic content within the image, ranging from 0.0 to 1.0. The cutoff should be set within this range, although the library will not stop you from setting values outside this range.

Please note that, while the project is .NET Standard 2.0, only Windows native binaries have been built into the package. As such, the package will not run on other platforms presently. However, it's not complicated to build OpenCV with the DNN module for your platform and modify the PInvoke code and nuget package to emit platform specific binaries as well. I'll most likely be doing this sometime down the road, but that's not guaranteed.

Lastly it should be noted that this package bundles OpenCV 3.3.1 compiled as a single, world DLL, WITHOUT the non-free addons.

nsfwnet's People

Contributors

technikempire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nsfwnet's Issues

Do a minimal build of openCV

Right now we bundle in a massive cv::world dll that isn't necessary. We can skim a lot of fat off of this by building individual models and only bundling necessary dll's.

Compile OpenCV with libJPEG-Turbo

Running a perf test, I've found that openCV's standard libjpeg takes ~60% more CPU time than the actual DNN forward pass execution. That's absolutely terrible, so terrible, I'm marking this as a bug.

Recompile OpenCV with LAPACK/OpenBLAS

Apparently we can get an extreme speedup if we compile OpenCV with OpenBLAS, which includes LAPACK. The bottleneck for the DNN module is apparently cv::dnn::gemm(...), as apparently the OpenCV implementation sucks for performance.

Users have reported between ~8X and ~40X speedup, and we're currently around 34hz, so it'll be interesting to see what this change brings.

Doesn't work

OpenCv 3.4.1 doesn't even have a a opencv_core341.lib which is referenced in this project. It only has a opencv_core341_world341.lib. Also, the correct library path is $(OPENCV_3_4_1)$(PlatformTargetAsMSBuildArchitecture)\vc15\lib not $(OPENCV_3_3)\build$(PlatformTargetAsMSBuildArchitecture)\install\lib. And you didn't define $(OPENCV_3_3) anywhere either. You should have included a project property file for that even if each user needs to change its value.

Make optional MKL/MKLDNN back end

MKL-DNN backend is reported to be twice as fast as cv::dnn with resnet-50. If we had this as an optional back end, then the yahoo classifier would be a viable option for mid-range+ computers.

Only issue is that MKL carries a DLL weight of 140+ megs, roughly 35 compressed. So we'd be bloating installers by ~40MB. However, the point is, it carries this heavy weight because it has ultra optimized algorithms fitting a massive range of architectures and microarchitectures.

Explore using Intel's OpenVino inference engine backend for OpenCV

OpenCV folks are making major investments to move to OpenVino's inference engine backend in OpenCV. They're committed, as evidenced by the fact that it is the default DNN backend when included in compilation.

I would simply include it, but after installing the SDK I'm bewildered by the enormous sums of legal material included. It is not the Intel simplified license by any means, there are a half dozen third-party components and as such the legalities are presently unclear.

I've opened a discussion on this point here:

http://answers.opencv.org/question/197273/license-discussion-around-including-intel-reference-engine/

Change license file to mpl 2.0

The source code declares mpl 2.0 which is what the license is, despite the fact that the LICENSE file is saying bsd. Need to update that file.

Work on linux?

How can I get it work on linux?
I am running it as .net core on Debian, and I got
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'NsfwNetNative.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libNsfwNetNative.dll: cannot open shared object file: No such file or directory

Move mat type conversion to after resize

We very stupidly perform a mat type conversion before resizing. We are wastefuly recalculating tens of thousands of pixels that we end up deleting the line after.

Swap the order of these operations and watch in awe as we gain roughly a 20 percent increase in performance.

Bad jpg terminate application in v1.1, but in 1.0.1 just print CV error

Testing new release (program.cs) I found a misformed jpg image terminate console appplication. In the previous versión just output CV error and continues to classify.

The error:
OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0) in cv::resize, file C:\dev\lib\opencv-3.3.1\modules\imgproc\src\resize.cpp, line 3289

Same way, file types other tan images (e.g. .zip) will do the same, and the previous version just print the error.

Attached sample problematic image.

0002323_carved
0002323_Carved.zip

GPU version

Is possible to take advantage of a GPU with your library with OpenCV DNN module?
Do you expect better result?
I plan to use it to classify between 100,000 and 700,000 images each time.

Explore using Intel's precompiled FFMPEG with Cisco's OpenH264 library

Once #18 is complete and functional, investigate this (title). As long as the Windows Media Foundation support in OpenCV is proper, we shouldn't have to, but just in case, look into FFMPEG as a backend.

Windows Media Foundation should automatically lift patented codecs from the operating system, which covers us under the patent license conditions (I've actually talked with MPEG-LA directly to confirm this). So hopefully it just works. But, Cisco is also a holder of the same patents (MPEG-4/H264) and their open source (BSD license) library would thus also be covered, because we would inherit "user" patent coverage and it falls on Cisco to pay the patent royalties (I think!?!). Anyway hopefully WMF just works.

Yahoo's nsfw model linked in submodule doesn't load

Hi. Thanks so much for making this available! It's been a great example for me to follow as I try to get Yahoo's nsfw model working in my own project.

I can load and run your model in Contrib/NsfwSqueezenet/ just fine, but when I try to load Yahoo's model under Contrib/OpenNSFW/, I get the parse errors below from OpenCV's dnn::readNetFromCaffe(...).

Any suggestions? Thanks!

OpenCV Error: Unspecified error (FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse NetParameter file: /home/sbouma/scratch/NsfwNET/NsfwNetNative/Contrib/OpenNSFW/nsfw_model/resnet_50_1by2_nsfw.caffemodel) in ReadNetParamsFromBinaryFileOrDie, file /home/sbouma/opencv-3.4.0/modules/dnn/src/caffe/caffe_io.cpp, line 1159
terminate called after throwing an instance of 'cv::Exception'
what(): /home/sbouma/opencv-3.4.0/modules/dnn/src/caffe/caffe_io.cpp:1159: error: (-2) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse NetParameter file: /home/sbouma/scratch/NsfwNET/NsfwNetNative/Contrib/OpenNSFW/nsfw_model/resnet_50_1by2_nsfw.caffemodel in function ReadNetParamsFromBinaryFileOrDie

Add video IO class

Add in a class that enables users to load video from a URL or from a file and then:

  • classify frames
  • skip frames
  • classify without marshaling data (pass the classifier native instance to the classify_x functions)
  • enable users to manually marshal the last frame classified
  • set or get any option available to them provided by opencv
  • For now, use Windows Media Foundation as the backend, but explore the idea of using Intel's precompiled FFMPEG, which uses Cisco's OpenH264 library (need to make sure there's nothing else patented in FFMPEG).

Possible bug in opencv 3.4.0

Looks like someone might have done something naughty and ruined resnet support in CV::dnn 3.4.0. Have to recompile 3.3.1 to check.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.