muellerzr / fastinference

A collection of inference modules for fastai2
https://muellerzr.github.io/fastinference
Apache License 2.0
89 stars 16 forks source link

"NameError: name '_ConstantFunc' is not defined" #17

Closed lucasgabrielce closed 4 years ago

lucasgabrielce commented 4 years ago

from fastinference.inference import * (worked fine)

%%time for i in range(40): learn.predict(img)

This code returned:

~/miniconda3/envs/fastai2/lib/python3.6/site-packages/fastinference/inference/inference.py in predict(x, item, with_input, rm_type_tfms)
     79 def predict(x:Learner, item, with_input=False, rm_type_tfms=None):
     80         dl = x.dls.test_dl([item], rm_type_tfms=rm_type_tfms, num_workers=0)
---> 81         res = x.get_preds(dl=dl, with_input=with_input, with_decoded=True)
     82         return res

~/miniconda3/envs/fastai2/lib/python3.6/site-packages/fastinference/inference/inference.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, raw, act, inner, reorder, cbs, **kwargs)
     55     if reorder and hasattr(dl, 'get_idxs'):
     56         idxs = dl.get_idxs()
---> 57         dl = dl.new(get_idxs = _ConstantFunc(idxs))
     58     cb = GatherPredsCallback(with_input=with_input, with_loss=with_loss, **kwargs)
     59     ctx_mgrs = self.validation_context(cbs=L(cbs)+[cb], inner=inner)

NameError: name '_ConstantFunc' is not defined

It seems that to run a predict with fastinference I need to have a databunch loaded with the learner itself.

The steps I followed: 1 - Import fastai 2 - Load a pre-trained model (this model was exported with.export()) with learn = load_learner('PATH_OF_LEARNER', cpu=False) 3 - Predict with a image loaded with opencv (cv2.imread(image_path)) 4 - Import fastinference with from fastinference.inference import * (no error returned) 5 - Repeat step 3 and got the error.

muellerzr commented 4 years ago

Good catch! Thanks! I reverted the get_preds to what fastai uses, but forgot an import. I’ll fix the import then would appreciate it if you could let me know how it compares to fastai :)

muellerzr commented 4 years ago

Pushed the fix, let me know if upgrading it via pip helps

BTW: It's untested if cv2 works. The fastai library (and this library) are tested with Pillow

lucasgabrielce commented 4 years ago

I pushed the fix via pip with pip install fastinference -U, however now I can't import it.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-10-40720c496b58> in <module>
----> 1 from fastinference.inference import *

~/miniconda3/envs/fastai2/lib/python3.6/site-packages/fastinference/inference/__init__.py in <module>
----> 1 from .inference import *
      2 from .text import *

~/miniconda3/envs/fastai2/lib/python3.6/site-packages/fastinference/inference/inference.py in <module>
      9 
     10 # Cell
---> 11 from fastai.learner import _ConstantFuncnstantFunc
     12 from fastai.vision.all import *
     13 from fastai.tabular.all import *

ImportError: cannot import name '_ConstantFuncnstantFunc'

I tried to downgrade my fastai version (2.0.13 to 2.0.8 and to 2.0.7) and it did not work, same error.

About the cv2 I can use it normally with fastai predict (cv2.imread to feed it). I'm gonna be careful and start to use PIL if possible. Thanks.

muellerzr commented 4 years ago

facepalm no I hit tab complete and didn't pay attention

muellerzr commented 4 years ago

Re: cv2, if it works don't worry about it then :) Just know fastai does it's stuff with Pillow

muellerzr commented 4 years ago

Okay, just pushed another update.

lucasgabrielce commented 4 years ago

I updated and could not only import it but run it also! I'm going to do some experiments now with some data and I'll update you asap. Thank you!

About the cv2 again, haha: I remember that my friend and I, in our work, we tried to use PIL but it was slower than cv2. But cv2 uses BGR in default not RGB, so this is a problem if you forget to call it the right way, that's why I said I'd try to use PIL in a daily basis with fastai.

[]'s from Brazil! Thank you again

muellerzr commented 4 years ago

Make sure to also use the performance-boosted Pillow SIMD: https://pypi.org/project/Pillow-SIMD/