I have a problem with productionizing a segmentation model on Google App Engine.
To begin with, I followed the official tutorial and succesfully deployed Jeremy’s bear classification model. I also managed to run a different categorical model using ImageDataBunch.load_empty instead of ImageDataBunch.single_from_classes. (That would be teenage mutant ninja turtle recognition. )
Now, on to the segmentation example. I want the app to display a segmented picture. I modified the crucial part of server.py as follows:
@app.route('/analyze', methods=['POST'])
async def analyze(request):
[...]
return JSONResponse({'result':img.show(y=learn.predict(img)[0], figsize=(8,8))})
The app would always return “Result = null” (I run the app server locally). Trying to get to the bottom of the issue, I replaced the last line of the above snippet with
return JSONResponse({'result':learn.predict(img)[0]})
and learned that “Object of type ‘ImageSegment’ is not JSON serializable”. Here’s the full error message:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/zbigniew/.local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/zbigniew/.local/lib/python3.6/site-packages/uvicorn/middleware/asgi2.py", line 7, in __call__
await instance(receive, send)
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/middleware/errors.py", line 125, in asgi
raise exc from None
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/middleware/errors.py", line 103, in asgi
await asgi(receive, _send)
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/middleware/cors.py", line 138, in simple_response
await inner(receive, send)
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/exceptions.py", line 74, in app
raise exc from None
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/exceptions.py", line 63, in app
await instance(receive, sender)
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/routing.py", line 41, in awaitable
response = await func(request)
File "app/server.py", line 48, in analyze
return JSONResponse({'result':learn.predict(img)[0]})
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/responses.py", line 43, in __init__
self.body = self.render(content)
File "/home/zbigniew/.local/lib/python3.6/site-packages/starlette/responses.py", line 150, in render
separators=(",", ":"),
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'ImageSegment' is not JSON serializable
I guess I should replace JSONResponse with something else, but the problem is I know next to nothing about JavaScript and find its code hard to read. I googled a bit and it seems that a possible workaround is to convert the image to base64 string, but I don’t quite know how to do it in this particular setting. Perhaps there is a simpler solution and someone could help me out?