timeseriesAI / tsai

Time series Timeseries Deep Learning Machine Learning Python Pytorch fastai | State-of-the-art Deep Learning library for Time Series and Sequences in Pytorch / fastai
https://timeseriesai.github.io/tsai/
Apache License 2.0
5.18k stars 646 forks source link

Inference with "only one sample" for TST? #449

Closed aashikazim closed 2 years ago

aashikazim commented 2 years ago

I was trying to do inference with only one sample at a time with the following provided code.

from tsai.inference import load_learner
clf = load_learner("models/clf.pkl")
probas, target, preds = clf.get_X_preds(X[splits[0]], y[splits[0]])

But it gives the following exception. Would you please suggest me a solution?

"Could not do one pass in your dataloader, there is something wrong in it"

Btw, I was getting the correct result for the provided sample.

vrodriguezf commented 2 years ago

Does learn.predict work for you?

williamsdoug commented 2 years ago

Hi @aashikazim, If I recall correctly,clf.get_X_preds expects a list of items. In order to the the correct dimension for a single item list, try using clf.get_X_preds(X[splits[0:1]], y[splits[0:1]])

oguiza commented 2 years ago

Hi @aashikazim, If I recall correctly, clf.get_X_preds expects a list of items. In order to the the correct dimension for a single item list, try using clf.get_X_preds(X[splits[0:1]], y[splits[0:1]])

Exactly. All you need to do is to pass the sample with a shape [1 x n vars x seq length]. If you have a 2d array (X_sample) you can just use X_sample[None].

aashikazim commented 2 years ago

Hi, @williamsdoug and @oguiza. Thanks for answering my question. I tried your suggestion. I don't know why it is not working in my side. Please have a look at my code if I am doing anything wrong? image

@vrodriguezf , thank you for the suggestion. I also tried that, it produced an error. image

williamsdoug commented 2 years ago

Hi @aashikazim. @oguiza has created a great set of tutorials for tsai located in the tutorial_nbs folder. You might want to look at 01_Intro_to_Time_Series_Classification which contains a section Inference on additional data located near the bottom of the notebook. Try running the tutorial notebook on Google Colab and verify the the dimension of the inference data in Oguiza's example and the dimensions of your data are similar.

oguiza commented 2 years ago

You may need to update to the most recent version of tsai. If you can upgrade to the latest version in GitHub using: pip install git+https://github.com/timeseriesAI/tsai.git

forhonourlx commented 2 years ago

Hi @aashikazim, If I recall correctly, clf.get_X_preds expects a list of items. In order to the the correct dimension for a single item list, try using clf.get_X_preds(X[splits[0:1]], y[splits[0:1]])

Exactly. All you need to do is to pass the sample with a shape [1 x n vars x seq length]. If you have a 2d array (X_sample) you can just use X_sample[None].

Hi @oguiza , I am also trying to Inference "one sample":

single_tstensor_X = single_tstensor_X[None,:]
#TSTensor(samples:1, vars:6, len:36, device=cuda:0)
test_ds = dataloaders.dataset.add_test(X=single_tstensor_X)
test_dl = valid_dl.new(test_ds)
test_probas, test_targets, test_preds = learn.get_preds(dl=test_dl, with_decoded=True, save_preds=None, save_targs=None)

That's not an comfortable way to make inference. Would you like to add a 1-line wrapper function to make inference easier? Thanks

oguiza commented 2 years ago

There's no need to do that. All you need is:

test_probas, test_targets, test_preds = learn.get_X_preds(X_test) # X_test is a 3d numpy array [n_samples x n_vars x seq_len]

Please, take a look at the documentation about inference.

forhonourlx commented 2 years ago

Understood. Many thanks!

oguiza commented 2 years ago

I'll close this issue based on the last comment. Please, reopen if necessary.