Provides faster inference scripts
This function is almost the exact same as fastai
's. The big difference is we can return our raw outputs or our class names.
Note: to speed up inference, multi-processing will slow you down. In production use
num_workers=0
unless you have a large amount of vision data being passed at one timepath = untar_data(URLs.PETS)
fnames = get_image_files(path/'images')
pat = r'(.+)_\d+.jpg$'
batch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]
item_tfms = RandomResizedCrop(460, min_scale=0.75, ratio=(1.,1.))
bs=64
dls = ImageDataLoaders.from_name_re(path, fnames, pat, batch_tfms=batch_tfms,
item_tfms=item_tfms, bs=bs)
learn = cnn_learner(dls, resnet18, metrics=accuracy)
o = learn.predict(fnames[0], with_input=False)
o
imgs, probs, classes, clas_idx = learn.get_preds(dl=learn.dls.test_dl(fnames[:3]), with_input=True, with_decoded=True)
The first index will contain our raw transformed images:
TensorImage(imgs[0]).show()
Second the probabilities
preds[1][0]
And third our class names:
preds[2]
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)
df = pd.read_csv(path/'adult.csv')
splits = RandomSplitter()(range_of(df))
cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race']
cont_names = ['age', 'fnlwgt', 'education-num']
procs = [Categorify, FillMissing, Normalize]
y_names = 'salary'
to = TabularPandas(df, procs=procs, cat_names=cat_names, cont_names=cont_names,
y_names=y_names, splits=splits)
dls = to.dataloaders()
learn = tabular_learner(dls, layers=[200,100])
inp, probs, name, cat_idx = learn.predict(df.iloc[0], with_input=True)
inp
For tabular, our input is a TabularPandas
row. We can decode that row utilizing the DataLoaders
decode
function to generate it, and show it:
learn.dls.decode(inp).show()