Tensorflow Implementation of Yahoo's Open NSFW Model

本文介绍了一个使用 TensorFlow 重写的 Yahoo NSFW 分类器实现。该模型使用了从 Caffe 转换过来的权重,并提供了一个 Python 脚本来进行图像分类。支持 jpeg 图像输入,可以通过不同的参数调整加载图像的方式。

This repository contains an implementation of Yahoo's Open NSFW Classifier rewritten in tensorflow.

The original caffe weights have been extracted using Caffe to TensorFlow. You can find them at data/open_nsfw-weights.npy.

Prerequisites

All code should be compatible with Python 3.6 and Tensorflow 1.0.0. The model implementation can be found in model.py.

Usage

> python classify_nsfw.py -m data/open_nsfw-weights.npy test.jpg

Results for 'test.jpg'
	SFW score:	0.9355766177177429
	NSFW score:	0.06442338228225708

Note: Currently only jpeg images are supported.

classify_nsfw.py accepts some optional parameters you may want to play around with:

usage: classify_nsfw.py [-h] -m MODEL_WEIGHTS [-l {yahoo,tensorflow}]
                        [-t {tensor,base64_jpeg}]
                        input_jpeg_file

positional arguments:
  input_file            Path to the input image. Only jpeg images are
                        supported.

optional arguments:
  -h, --help            show this help message and exit
  -m MODEL_WEIGHTS, --model_weights MODEL_WEIGHTS
                        Path to trained model weights file
  -l {yahoo,tensorflow}, --image_loader {yahoo,tensorflow}
                        image loading mechanism
  -t {tensor,base64_jpeg}, --input_type {tensor,base64_jpeg}
                        input type

-l/--image-loader

The classification tool supports two different image loading mechanisms.

  • yahoo (default) tries to replicate the image loading mechanism used by the original caffe implementation, differs a bit though. See Caveats below.
  • tensorflow is an image loader which uses tensorflow api's exclusively (no dependencies on PILskimage, etc.).

Note: Classification results may vary depending on the selected image loader!

-t/--input_type

Determines if the model internally uses a float tensor (tensor - [None, 224, 224, 3] - default) or a base64 encoded string tensor (base64_jpeg - [None, ]) as input. If base64_jpeg is used, then the tensorflow image loader will be used, regardless of the -l/--image-loader argument.

Tools

The tools folder contains some utility scripts to test the model.

export_model.py

Exports the model using the standard tensorflow export api (SavedModel). The export can be used to deploy the model on Google Cloud ML EngineTensorflow Serving or on mobile (haven't tried that one yet).

create_predict_request.py

Takes an input image and spits out an json file suitable for prediction requests to a Open NSFW Model deployed on Google Cloud ML Engine (gcloud ml-engine predict).

Caveats

Image loading differences

The classification results sometimes differ more and sometimes less from the original caffe implementation, depending on the image loader and input image. I haven't been able to figure out the cause for this yet. Any help on this would be appreciated.

转载于:https://www.cnblogs.com/native-hadoop/p/7728695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值