Yah yah … very true.
Btw, if you can solve/explain my issue in post #1 above … I’ll buy you a beer (a good kind … like the kind we brew in San Diego)
Yah yah … very true.
Btw, if you can solve/explain my issue in post #1 above … I’ll buy you a beer (a good kind … like the kind we brew in San Diego)
I am no longer replying to people reporting errors without showing the whole stack trace
I linked to it … here’s it again though … the whole gory thing …
Traceback (most recent call last):--------------------------------------------------------------------| 0.00% [0/1 00:00<00:00]
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: '_FakeLoader' object has no attribute 'noops'
INFO: 127.0.0.1:59886 - "POST /predict HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastapi/applications.py", line 149, in __call__
await super().__call__(scope, receive, send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/applications.py", line 102, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/routing.py", line 550, in __call__
await route.handle(scope, receive, send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastapi/routing.py", line 166, in app
dependant=dependant, values=values, is_coroutine=is_coroutine
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastapi/routing.py", line 119, in run_endpoint_function
return await dependant.call(**values)
File "./main.py", line 43, in predict
pred_class = inf_learn.predict(path/'0172.jpg')[0]
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 231, in predict
inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 219, in get_preds
self._do_epoch_validate(dl=dl)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 178, in _do_epoch_validate
dl,*_ = change_attrs(dl, names, old, has); self('after_validate')
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 124, in __call__
def __call__(self, event_name): L(event_name).map(self._call_one)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 372, in map
return self._new(map(g, self))
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 323, in _new
def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 41, in __call__
res = super().__call__(*((x,) + args), **kwargs)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 314, in __init__
items = list(items) if use_list else _listify(items)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 250, in _listify
if is_iter(o): return list(o)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastcore/foundation.py", line 216, in __call__
return self.fn(*fargs, **kwargs)
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 127, in _call_one
[cb(event_name) for cb in sort_by_run(self.cbs)]
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/learner.py", line 127, in <listcomp>
[cb(event_name) for cb in sort_by_run(self.cbs)]
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/callback/core.py", line 24, in __call__
if self.run and _run: getattr(self, event_name, noop)()
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/callback/core.py", line 95, in after_validate
if self.with_input: self.inputs = detuplify(to_concat(self.inputs, dim=self.concat_dim))
File "/Users/admin/Development/_tools/anaconda/envs/fastapi_test_app/lib/python3.7/site-packages/fastai2/torch_core.py", line 213, in to_concat
if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
IndexError: list index out of range
Yes, it’s in callback
Sorry did not see the link (and it’s not gory, it’s informative ).
Your problem is that the web app cannot load the exported model, which I suspect comes from a mismatch of fastai2 or PyTorch versions between training and exporting the model on one side, deploying on the other side. (the real error is AttributeError: '_FakeLoader' object has no attribute 'noops'
in the unpickling of the Learner
)
For mobile development on the edge, does Fastai2/Pytorch support exporting something equivalent to what TensorflowLite does?
TF Lite actually uses low level APIs. So it uses the NNAPI on Android and CoreML on iOS.
So can PyTorch mobile (which is what they do).
I guess one man’s “gory” is another’s “informative”.
So that’s interesting since I’m using PyTorch 1.14 on my dl rig (using a GPU) and in my FastAPI app (no GPU, just CPU).
As for fastai … I installed it via conda on my dl rig, but thru pip on my local machine where I’m building the FastAPI app. Both are set to use 0.0.15
So is there a issue where conda install packages are different from the pip install packages … even if we are locking them down to the same version?
There are no conda packages for fasta2 (yet), so I don’t think that’s the problem. To confirm this is the actual error, just try an app that only has the line load_learner
. Also, as usual, I’d need to see the full code you’re running
I avoid conda
altogether as it a little too magical for my taste.
Here is how I set things up: https://forums.fast.ai/t/setting-up-fastaiv2-locally/65856
from io import BytesIO
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
from fastai2.vision.all import *
path = Path(__file__).parent
app = FastAPI()
# --- fastai2 tests ---
inf_learn = torch.load(path/'export.pkl', map_location='cpu')
inf_learn.dls.cpu()
classes = inf_learn.dls.vocab
@app.post("/predict")
async def predict(image: UploadFile=File(...)):
img_bytes = await image.read()
img_np = np.array(Image.open(BytesIO(img_bytes)))
img = PILImage.create(img_bytes)
pred_class = inf_learn.predict(img_bytes)[0]
return {
"predicted_class": pred_class
}
and here is what I have version wise when I run conda list
:
fastai2 0.0.15 pypi_0 pypi
fastcore 0.1.16 pypi_0 pypi
torch 1.4.0 pypi_0 pypi
torchvision 0.5.0 pypi_0 pypi
… NOW … on my dl-rig I used the course’s environment.yml
to install things and it is a bit different:
fastai2 0.0.15 pypi_0 pypi
fastcore 0.1.16 pypi_0 pypi
pytorch 1.4.0 py3.7_cuda10.1.243_cudnn7.6.3_0 pytorch
torchvision 0.5.0 py37_cu101 pytorch
So is the conda version of pytorch 1.4 different than what we get from pip 1.4? That seems really confusing if that is the case.
are you using virtualenv?
So can you just try with removing everything but inf_learn = torch.load(path/'export.pkl', map_location='cpu')
(by the way there is load_learner
with a cpu arg now).
Yes. I use virtualenv
.
Yes, I tried that first … same error
inf_learn = load_learner(path/'export.pkl')
classes = inf_learn.dls.vocab
The classes
comes back all good … but the predict
returns the same stack trace.
Oh add something with num_workers=0 to your dl. It’s the multiprocessing that is broken there.
Where do I add that?
Eh… you’re using predict so I guess this is on me to fix predict…
Can you try
dl = learn.dls.test_dl([img_bytes])
dl = dl.new(num_workers=0)
learn,get_preds(dl=dl)
and see if it has the same bug?
Or just
dl = learn.dls.test_dl([img_bytes], num_workers=0)
learn,get_preds(dl=dl)
Well its not returning an error (good) … but its not returning anything besides null
dl = inf_learn.dls.test_dl([img_bytes], num_workers=0)
pred_class = inf_learn.get_preds(dl=dl)[0]
return { "predicted_class": pred_class }
returns
{
"predicted_class": {}
}
If I do this:
dl = inf_learn.dls.test_dl([img_bytes], num_workers=0)
output = inf_learn.get_preds(dl=dl)
return {
"get_preds": output,
"classes": classes
}
I get
{
"get_preds": [
{},
null
],
"classes": [
"black",
"grizzly",
"teddy"
]
}
Weird. It should be the predictions.
Pushed a fix in Learner.predict, if you have any way to install fastai master.
I’ll investigate more tomorrow morning.