Created
February 20, 2022 07:15
-
-
Save hamelsmu/69e702d2f8bcaf3af011dfa478329511 to your computer and use it in GitHub Desktop.
How to make batch predictions in fastai
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@patch | |
def predict_batch(self:Learner, item, rm_type_tfms=None, with_input=False): | |
dl = self.dls.test_dl(item, rm_type_tfms=rm_type_tfms, num_workers=0) | |
inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True) | |
i = getattr(self.dls, 'n_inp', -1) | |
inp = (inp,) if i==1 else tuplify(inp) | |
dec_inp, nm = zip(*self.dls.decode_batch(inp + tuplify(dec_preds))) | |
res = preds,nm,dec_preds | |
if with_input: res = (dec_inp,) + res | |
return res |
Actually, this prediction method works for both:
def predict(self, item, rm_type_tfms=None, with_input=False):
dl = self.dls.test_dl(item, rm_type_tfms=rm_type_tfms, num_workers=0)
inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
i = getattr(self.dls, 'n_inp', -1)
inp = (inp,) if i==1 else tuplify(inp)
dec = self.dls.decode_batch(inp + tuplify(dec_preds))
dec_inp,dec_targ = (tuple(map(detuplify, d)) for d in zip(*dec.map(lambda x: (x[:i], x[i:]))))
res = dec_targ,dec_preds,preds
if with_input: res = (dec_inp,) + res
return res
Other notes h/t zach:
learn.dls.vocab
or learn.dls.categorize.vocab
is another way to get the class names.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This monkey patches a new function
predict_batch
ontolearner