yahoo开源了用于检测图片是否包含不适宜工作场所(NSFW)内容的深度神经网络项目https://github.com/yahoo/open_nsfw,GitHub 库中包含了网络的 Caffe 模型的代码。检测具有攻击性或成人内容的图像是研究人员进行了几十年的一个难题。随着计算机视觉技术和深度学习的发展,算法已经成熟,雅虎的这个模型能以更高的精度分辨色情图像。 由于 NSFW 界定其实是很主观的,有的人反感的东西可能其他人并不觉得如何。雅虎的这个深度神经网络只关注NSFW内容的一种类型,即色情图片。在网上看到了tensorflow实现的版本,进行了测试。
一、原理:来自(https://yahooeng.tumblr.com/post/151148689421/open-sourcing-a-deep-learning-solution-for)
从作者的一些技术文档中可以看出,其实质上是利用了CNN的一些图像分类模型来实现二分类问题(色情与否)。
Training a deep neural network for NSFW classification
We train the models using a dataset of positive (i.e. NSFW) images and negative (i.e. SFW – suitable/safe for work) images. We are not releasing the training images or other details due to the nature of the data, but instead we open source the output model which can be used for classification by a developer.
另外从下面这段话,可以看出。用了ResNet的方法来实现。
While training, the images were resized to 256x256 pixels, horizontally flipped for data augmentation, and randomly cropped to 224x224 pixels, and were then fed to the network. For training residual networks, we used scale augmentation as described in the ResNet paper [1], to avoid overfitting. We evaluated various architectures to experiment with tradeoffs of runtime vs accuracy.
二、实验测试:
将网上搜索的一些图片放到文件夹下,个性访问文件夹的方式来实现对文件夹下面的所有文件进行判断。
print('加载测试图片...')
for lists in os.listdir(args.input_file):
path = os.path.join(args.input_file, lists)
if os.path.splitext(path)[1] == '.jpg':
print(path)
# 图片加载
image = fn_load_image(path)
# 检测
predictions = \
sess.run(model.predictions,
feed_dict={model.input: image})
print("'{}'图片的检测结果为:".format(path))
print("\tSFW 得分:\t{}\n\tNSFW 得分:\t{}".format(*predictions[0]))
具体结果如下:
../data/3.jpg:比较奇怪的是,这张图片居然两个得分基本相当,可见其训练数据集主要是女性。
'../data/3.jpg'图片的检测结果为:
SFW 得分: 0.48673054575920105
NSFW 得分: 0.5132695436477661
../data/2.jpg(来自https://www.google.com.hk/%E8%89%B2%E6%83%85%E5%9B%BE%E7%89%87)
'../data/2.jpg'图片的检测结果为:
SFW 得分: 0.9827728271484375
NSFW 得分: 0.017227165400981903
../data/4.jpg:这张照片对艺术照片进行了处理,发现居然被判别为正常。
'../data/4.jpg'图片的检测结果为:
SFW 得分: 0.9892839789390564
NSFW 得分: 0.010715976357460022
../data/5.jpg(来自于http://upload.chinaz.com/2016/1110/6361438730342766349613345.jpeg)
'../data/5.jpg'图片的检测结果为:
SFW 得分: 0.15458464622497559
NSFW 得分: 0.8454152941703796
../data/1.jpg(https://www.google.com.hk/%E8%89%B2%E6%83%85%E5%9B%BE%E7%89%87)
'../data/1.jpg'图片的检测结果为:
SFW 得分: 0.15014633536338806
NSFW 得分: 0.8498536348342896
(注:所有图片均来自于互联网图片搜索,只用于实验用途。)