With the rapid development of the Internet, more and more pictures and videos appear on the Internet, especially UCG products, which stimulate people’s enthusiasm to upload pictures and videos. For example, wechat uploads more than 1 billion pictures every day. Everyone can upload, which creates a regulatory problem. Without content moderation, pornographic images and videos can proliferate. Tumblr, which has a reputation for openness, recently bowed to pressure to restrict the sharing of pornographic images. Not to mention domestic, content audit is UCG can not get around the difficulty. Do you still remember the professional yellow division a few years ago? It is said that millions of annual salary, watching porn every day to vomit, but few people recently mentioned this occupation, this should be supervision of the occupation, because of the emergence of artificial intelligence and fast death. (Of course, it’s not totally dead. Judging pornography is a subjective matter, and some art and pornography have blurred boundaries that require human judgment.)

I wrote an article about using artificial intelligence to detect pornographic pictures before. I also tried to add pornographic picture filtering function in the browser, but the reasoning speed was too slow (it took several seconds to detect an image in the test of Google Nexus 4 used at that time), which made it impossible to do real-time filtering. Recently, I have been working on Nvidia Jetson Nano and TensorRT, an inference acceleration framework, so I want to try it out and see if I can apply some acceleration methods to accelerate inference.

Although my ultimate goal is Jetson Nano, TensorRT works with almost any Nvidia graphics card, so I’ll try it on the PC first for convenience. Don’t have an Nvidia graphics card? That’s ok. Check out the two posts I posted earlier:

  1. Google GPU cloud computing platform, free and easy to use
  2. Install TensorRT on Google Colab

open_nsfw

The deep learning model adopted in this paper is Yahoo’s open source deep learning pornographic image detection model Open_NSFW, where NSFW stands for Not Suitable for Work, and this project is based on caffe framework. Since I mainly work with Tensorflow, I found a Tensorflow implementation of the model online, forked it out, and added a script for the TensorRT framework. You can use the following command to get the code:

git clone https://github.com/mogoweb/tensorflow-open_nsfw.git
Copy the code

In model.py, we can see the open_NSfw model definition. Data /open_nsfw-weights.npy is the Tensorflow weight obtained by using the tool from Yahoo Open_NSfw cafee weight. Used directly in reasoning processes. The classify_nsfw.py script can be used for single-image reasoning:

python classify_nsfw.py -m data/open_nsfw-weights.npy test.jpg
Copy the code

Note: The script provides two ways to decode image files, one using pil. image, skimage for image processing, known as yahoo_image_loader, and one using tensorflow image processing functions. Because the original Open_NSFW model is preprocessed and trained by pil. image and SKimage, the results decoded by different libraries have slight differences, which will affect the final results, yahoo_image_loader is generally preferred. Of course, if you are going to train your models yourself, you can choose any image library.

There are scripts in the Tools directory that can export models to frozen Graph, Saved Model, and Tflite formats, so that we can easily deploy them on the server side and apply them to the mobile side.

Opt is the code I wrote that uses the TensorRT framework for acceleration, which I describe in detail below.

It is derived as TensorRT model

Currently TensorRT as part of the Tensorflow get Google official support, they are wrapped in Tensorflow. Contrib. TensorRT, to join in your code:

import tensorflow.contrib.tensorrt as trt
Copy the code

You can use TensorRT, and with Google’s support, exporting to TensorRT is fairly simple:

        trt_graph = trt.create_inference_graph(
                input_graph_def=frozen_graph_def,
                outputs=[output_node_name],
                max_batch_size=1,
                max_workspace_size_bytes=1 << 25,
                precision_mode='FP16',
                minimum_segment_size=50
        )

        graph_io.write_graph(trt_graph, export_base_path, 'trt_' + graph_name, as_text=False)
Copy the code

Among them:

  • Input_graph_def is defined for the Tensorflow model graph that needs to be exported
  • Outputs is the output node name
  • Max_batch_size is the maximum batch size limit, which is determined by the GPU memory size due to graphics memory limitation. Generally, the value can be 8 or 16
  • Precision_mode is the model precision, with options of FP32, FP16 and INT8. The higher the precision, the slower the inference speed, which also depends on the GPU.

Graph_io.write_graph writes the graph to a file that can be loaded in subsequent code.

Refer to the opt/export_trt.py file for the complete code.

The test data

Due to regulatory restrictions, there are no public data sets available for download, but there are open source projects on Github that provide scripts to download from the web. I used github.com/alexkimxyz/… Scripts in this open source project. The project provides drawings, Hentai, Neutral, Porn and Sexy images, which can be divided into training sets and test sets, and checks whether the images are valid (some links may not be accessible due to crawling from the network).

Note that this image has a large number of downloads, so be careful not to overload your hard drive. While this is a large amount of data (tens of thousands of images) to train your own model, it’s nothing compared to the millions of images that Yahoo reportedly used to train its Open_NSFW model.

Comparison of inference speed

In the opt directory, I have added two scripts for loading and reasoning of the two models, benchmark_classify_nsfw.py and benchmark_classify_trt.py, which are almost identical to each other, yes, In addition to benchmark_classify_trt.py there is one more line of code:

import tensorflow.contrib.tensorrt as trt
Copy the code

Add this import statement to tell TensorFlow to use the TensorRT framework, otherwise you will get the following error:

tensorflow.python.framework.errors_impl.NotFoundError: Op type not registered 'TRTEngineOp' in binary running on alex-550-279cn. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
Copy the code

Take 2000 test pictures for testing, on my GTX 960, the reasoning speed is as follows:

Unoptimized model: 53 s Optimized model using TensorRT: 54 sCopy the code

If you download a larger data set, you can test a few more images to see the optimization.

On the Google Colab, I put a Jupter Notebook, interested students can use the Google Colab try, file address: colab.research.google.com/drive/1vH-G… You can also check out my full script and Notebook on Github:

Github.com/mogoweb/ten…

Click to read the original article to jump to the project.

An aside:

The threshold of the main flow of wechat official account has been greatly reduced. I have opened an advertisement at the bottom of the article on the official account, hoping that it will not affect everyone’s reading experience. I’ve always wondered if anyone would click on this AD, and maybe I’ll get the answer later.

You can also read:

  1. Using ARTIFICIAL intelligence to detect pornographic images
  2. Google GPU cloud computing platform, free and easy to use
  3. Install TensorRT on Google Colab