anvil.server.SerializationError: Cannot serialize return value from function

Hi!

So I’m trying to connect Anvil to my image classifier that’s in google collab and my Anvil code is very similar to the dog vs cat image classifier that’s used in the Anvil tutorial except mine is classifying brain tumors (yes or no) with brain MRIs (.jpg). This is the error I get with the code I have:

" anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize <class ‘tensorflow.python.framework.ops.EagerTensor’> object at msg[‘response’] at [Form1, line 15](javascript:void(0))"

I’m relatively new to python so I’m not sure what to try? This is my Anvil related code from my google collab:


@anvil.server.callable
def classify_image(file):
  
    with anvil.media.TempFile(file) as filename:``` 

Welcome!

The error message says it all. Your server function is trying to return an instance of tensorflow.python.framework.ops.EagerTensor and that cannot be serialized back to the client. In general, you can only return items from server to client (or send them from client to server) if they can be serialized. Regular Python types can be, arbitrary classes cannot.

Instead, work with the object on the server to pull out the data you want to return to the client. Presumably that’s the score, based on your client code.

That’s as far as I can give advice, though, since I don’t know the tenserflow classes, so don’t know how to pull the score itself out.

2 Likes

Hi jshaffstall!

Thank you so much for your response and apologies for my delayed reply. Between then and now, I’ve tried to learn exactly what python classes are and how that affects it being serialized and though I have a better picture now, I’m still a bit unsure what exactly my next steps should be. And from your suggestion about working with the object on the server to pull out the data I want from the client, I’m a bit stuck with that logic because that’s what I believed I was doing? I’m using the server to input an image, which then gets sent to my client notebook to be processed by a function, and then return those values from the function back to the server? I’m attaching a screenshot from my client if that is helpful in any way:

Please let me know if there is anything else I can provide for clarification. I apologize if these are simple questions, I’m currently somewhere on the python learning curve so I’m still learning and unlearning a ton every day.

Like I said, I don’t know the framework you’re using, so can’t say what you need to do to pull a serializable value out of the score object. But if you print out type(score) you’ll find it’s an object instance. There will be some method on that object to pull out a primitive data type (e.g. a float, probably) that represents the actual score.

Thank you! With your help I was able to diagnose the issue on both the client + server-side and clean up my code appropriately so now everything runs smoothly!

(I fed it an image I used for training just as a quick trial so that’s why it’s 100%)

Thank you again

2 Likes