mdietrichstein / tensorflow-open_nsfw

Tensorflow Implementation of Yahoo's Open NSFW Model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

write a python client

Kingeng opened this issue · comments

Hi
@mdietrichstein @delta9

and thank you for this repository.
could you please do me a favor & tell how can i write a client for this model using python
i use tensorflow web and inception tutorial every thing is ok when i serve other model but for this one i don't know what should i do

can you tell me how should i change this inception code :

from __future__ import print_function
from grpc.beta import implementations
import tensorflow as tf
import numpy as np

from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
import base64
import json
from tensorflow.python.saved_model.signature_constants import PREDICT_INPUTS
tf.app.flags.DEFINE_string('server', 'localhost:9000',
                           'PredictionService host:port')
tf.app.flags.DEFINE_string('image', '', 'path to image in JPEG format')
FLAGS = tf.app.flags.FLAGS
def main(_):
  host, port = FLAGS.server.split(':')
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  with open(FLAGS.image, "rb") as f:
    data = f.read()
    data = base64.urlsafe_b64encode(data)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'test'
    request.model_spec.signature_name = 'serving_default'
    data = tf.contrib.util.make_tensor_proto(data,shape=[1])
    req=request.inputs['input'].CopyFrom(data)
    result = stub.Predict(request, 10.0)  # 10 secs timeout
    print(result)
if __name__ == '__main__':
  tf.app.run()

and i get this error :


Traceback (most recent call last):
  File "/home/serving/tensorflow_serving/example/porndetect_client.py", line 64, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/serving/tensorflow_serving/example/porndetect_client.py", line 57, in main
    result = stub.Predict(request, 10.0)  # 10 secs timeout
  File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 309, in __call__
    self._request_serializer, self._response_deserializer)
  File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 195, in _blocking_unary_unary
    raise _abortion_error(rpc_error_call)
grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input tensor alias not found in signature: input. Inputs expected to be in the set {inputs}.")

  • Special Thanks *

How can i insert 'inputs:{' at the begging of data ?
I think this can solve the problem but i can't do it

Hi @Kingeng ,

You might want to write a wrapper around yahoo/open_nsfw instead because unfortunately this conversion often returns different results.

Maybe have a look at my fork here if you want to still give it a try: https://github.com/delta9/tensorflow-open_nsfw

Run tools/export_serving_model.py to export the model which accepts raw bytes and try a client like this:

https://github.com/sebastian-schlecht/tensorflow-serving-python

I hope this helps!

@delta9

Thanks for reply
can you tell me how fast is the cafe version ? is it like tensorflow serve or can i use it in production ?

i want to close all sites that maybe have NSFW content at work for all IPs.

i try your nodejs code but i dont know about it & after get some error i prefer to work on python

The error show details 'input tensor alias not found in signature: input'. You may should check the 'signature' in the code that save model. Try to modify prediction_signature
prediction_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={'images': predict_inputs_tensor_info, 'other_parameter': xxx}, outputs={ 'classes': classes_output_tensor_info, 'scores': scores_output_tensor_info }, method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME ))
. Then, when you request, try request.inputs['images'].CopyFrom(data) request.inputs['other_parameter'].CopyFrom(data_other_parameter)

Hey @Kingeng, @xf4fresh

I also suspect that you might have to change the input name in your code.

If you take a look at the export script you can see that it uses PREDICT_INPUTS as the input key. PREDICT_INPUTS is an alias for inputs as you can see in signature_constants.py#L49.

I'm not familiar with the API you are using, but you could try changing req=request.inputs['input'].CopyFrom(data) to req=request.inputs['inputs'].CopyFrom(data)

Let me know if it helps.

@mdietrichstein @xf4fresh

Thanks for your reply
i checked that before but it's not worked.
because of problems and what i read about this model

decide to make a new classifier for NSFW
and recently i finetuned inception v4 (https://github.com/tensorflow/models/tree/master/research/slim) and after this i want to try classify NSFW with nasnet-a_large

hope to get better result without any problem

Again, Thanks all of you for reply .

decide to make a new classifier for NSFW

Awesome. I wish you good luck and hope that you release your classifier on github :)