Git Product home page Git Product logo

Comments (6)

winstonaws avatar winstonaws commented on August 27, 2024

input_fn is the correct function to override, so you're on the right track. Here's a simpler example from our tests which uses it in this way:

def input_fn(serialized_data, content_type):

When you define an input_fn, the raw request is passed in as the first argument, so you should have full control over the deserialization logic. Your function can do anything, but it just needs to return a dict of tensor name: tensor of the same shape as defined in your serving_input_fn.

From just the part of the stacktrace you provided, it's hard to tell whether the exception is occurring in your input_fn, or after. I'd recommend adding some debugging print statements inside your input_fn to determine that. If you're still having the problem after verifying you're deserializing correctly, can you provide the full stacktrace, and any debugging info you've obtained from you print statement, such as what you're returning from your input_fn?

from sagemaker-python-sdk.

mihi-r avatar mihi-r commented on August 27, 2024

I did some debugging and it looks like the issue is coming from the deserialization. First, I tested the model without the input_fn and preprocessed the data before sending it in:

data = ["blah blah blah"]
data = np.array(list(vocab_processor.transform(data)))
predictor.predict(data)

This works fine and I get back predictions.

The second time, I added back the input_fn and removed all of the preprocessing that was present before in my post (so I can test the unpickling) and preprocessed the data beforehand like above. Here's my modified input_fn:

def input_fn(serialized_input, content_type):
    deserialized_input = pickle.loads(serialized_input)
    return deserialized_input

This didn't work and I get the same error. Here's the entire stacktrace:

Traceback (most recent call last):
File "/opt/amazon/lib/python2.7/site-packages/container_support/serving.py", line 165, in _invoke
self.transformer.transform(content, input_content_type, requested_output_content_type)
File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 255, in transform
return self.transform_fn(data, content_type, accepts), accepts
File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 179, in f
input = input_fn(serialized_data, content_type)
File "/opt/ml/code/text_classification_cnn.py", line 156, in input_fn
deserialized_input = pickle.loads(serialized_input)
File "/usr/lib/python2.7/pickle.py", line 1388, in loads
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 864, in load
dispatchkey
KeyError: '['
[2018-03-16 18:11:25,795] ERROR in serving: '['
2018-03-16 18:11:25,795 ERROR - model server - '['

from sagemaker-python-sdk.

mihi-r avatar mihi-r commented on August 27, 2024

After further debugging, I found out I wasn't pickling correctly. Once I get that resolved, this method should work. Thanks for your help!

from sagemaker-python-sdk.

Brunods1001 avatar Brunods1001 commented on August 27, 2024

Hello @mihi-r, I am having the exact same key error message and I also think it's a deserialization issue. I followed the pickling shown in the aws documentation but it isn't working. Have you had any luck?

from sagemaker-python-sdk.

mihi-r avatar mihi-r commented on August 27, 2024

@Brunods1001 Yes, for me, simply pickling and and deserializing how it states in the documentation worked for me. When I first tried using input_fn, I was thinking the model would serialize it for you but in fact, you'll need to serialize it and deserialize it manually.

from sagemaker-python-sdk.

jesterhazy avatar jesterhazy commented on August 27, 2024

This issue has been closed due to inactivity. If you still have questions, please re-open it.

from sagemaker-python-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.