首页 > 代码库 > caffe+鉴黄︱yahoo+open_nsfw 中resnet_50_1by2、遇到问题

caffe+鉴黄︱yahoo+open_nsfw 中resnet_50_1by2、遇到问题

NSFW:Not Suitable for Work; SFW:Suitable for Work
github:https://github.com/yahoo/open_nsfw
技术分享

.


一、NSFW来历

1、模型来历

finetuned来源于ImageNet 1000 class dataset。使用的框架是:resnet 50 1by2

2、色情指数

雅虎公开的此算法为概率0-1值,二分类。
分数<0.2代表大致安全;分数>0.8代表很高可能性不适合办公环境观看。
当然,这个阈值根据实际情况确定。
.
.


二、caffe的实践

.

1、GPU报错

参考来源:ResNet-50 Testing

 Check failed: target_blobs.size() == source_layer.blobs_size() (5 vs. 3) Incompatible number of blobs for layer bn_conv1

笔者在实践GPU时候遇到这个问题,那么网上的说法为:

It looks like that Batchnorm layer and adjacent Scale layer are
integrated in single Batchnorm layer in NVIDIA caffe. However original
BVLC/caffe doesn’t integrate them. That means any networks and
pretrained models using batchnorm and scale layers with original Caffe
cannot use in NVIDIA caffe and DIGITS.

original Caffe包含:Batchnorm层和adjacent Scale层分开的;
NVIDIA Caffe/DIGITS包含:Batchnorm层和adjacent Scale层合并在一个BN层中;
所以在预测过程中不能使用NVIDIA Caffe,也就是GPU。

那么之后,调用了CPU就可以了,但是比较慢,一张图12s左右(K80)
.

2、resnet-50_1by2中:BN层中的use_global_stats

(参考博客:caffe Resnet-50 finetune 所有代码+需要注意的地方)
1.在训练时所有BN层要设置use_global_stats: false(也可以不写,caffe默认是false)
2.在测试时所有BN层要设置use_global_stats: true
影响:
1.训练如果不设为false,会导致模型不收敛
2.测试如果不设置为true,会导致准确率极低
区别:
use_global_stats: false是使用了每个Batch里的数据的均值和方差;
use_global_stats: true是使用了caffe内部的均值和方差。

其中:resnet_50_1by2,resnet_50,resnet68_1by2等这些框架的caffemodel+deploy可见github:https://github.com/jay-mahadeokar/pynetbuilder/tree/master/models/imagenet
.

3、命令行调用SNFW

python ./classify_nsfw.py  --model_def nsfw_model/deploy.prototxt  --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel  INPUT_IMAGE_PATH 
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

    caffe+鉴黄︱yahoo+open_nsfw 中resnet_50_1by2、遇到问题