Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues saving models with TSMetaDataset Dataloader #317

Closed
ykim opened this issue Dec 16, 2021 · 3 comments
Closed

Issues saving models with TSMetaDataset Dataloader #317

ykim opened this issue Dec 16, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@ykim
Copy link

ykim commented Dec 16, 2021

On trying to save a model that uses a dataloader from TSMetaDatasets(TSMetaDataset), I got the following error:

Traceback (most recent call last):
  File "cli.py", line 15, in <module>
    cli()
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/Users/ykim/Code/Gomanzanas/tsai-explore/explore.py", line 158, in classify_directionality
    learn.export(os.path.join(run_dir, "model", "learner.pkl"))
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/fastai/learner.py", line 369, in export
    self.dls = self.dls.new_empty()
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/fastai/data/core.py", line 146, in new_empty
    loaders = [dl.new(dl.dataset.new_empty()) for dl in self.loaders]
  File "/Users/ykim/.local/share/virtualenvs/tsai-explore-3_McIf8_/lib/python3.8/site-packages/fastai/data/core.py", line 146, in <listcomp>
    loaders = [dl.new(dl.dataset.new_empty()) for dl in self.loaders]
AttributeError: 'TSMetaDataset' object has no attribute 'new_empty'

After looking around, it looks like #215 is related. Any suggestions on how I can get around this?

@oguiza oguiza added the bug Something isn't working label Dec 16, 2021
@oguiza
Copy link
Contributor

oguiza commented Dec 23, 2021

Hi @ykim, it seems like a true bug so you won't be able to work around it. It needs to be fixed. I won't be able to work on it though for the next few days.

@oguiza
Copy link
Contributor

oguiza commented Mar 24, 2023

Well, the next few days have become > 1yr. In any case, I want to document that I've found a way to fix the issue. Here's the documented solution:

  1. You prepare the metadatasets:
from tsai.data.metadatasets import TSMetaDataset, TSMetaDatasets

vocab = alphabet[:10]
dsets = []
for i in range(3):
    size = np.random.randint(50, 150)
    X = torch.rand(size, 5, 50)
    y = vocab[torch.randint(0, 10, (size,))]
    tfms = [None, TSClassification(vocab=vocab)]
    dset = TSDatasets(X, y, tfms=tfms)
    dsets.append(dset)



metadataset = TSMetaDataset(dsets)
splits = TimeSplitter()(metadataset)
metadatasets = TSMetaDatasets(metadataset, splits=splits)
dls = TSDataLoaders.from_dsets(metadatasets.train, metadatasets.valid)
xb, yb = dls.train.one_batch()
xb, yb
  1. Train the model as you'd do with any other model in tsai:
learn = ts_learner(dls, arch="TSTPlus")
learn.fit_one_cycle(1)
  1. Export the model when training is complete:
learn.export("test.pkl") # this has been fixed now and it should work
  1. For inference, you'd prepare a dataloader like you did when you trained the initial model:
vocab = alphabet[:10]
dsets = []
for i in range(2):
    size = np.random.randint(50, 150)
    X = torch.rand(size, 5, 50)
    y = vocab[torch.randint(0, 10, (size,))]
    tfms = [None, TSClassification(vocab=vocab)]
    dset = TSDatasets(X, y, tfms=tfms)
    dsets.append(dset)
metadataset = TSMetaDataset(dsets)
dl = TSDataLoader(metadataset)
  1. You load the saved learner and use fastai's get_preds method:
learn = load_learner("test.pkl")
learn.get_preds(dl=dl)

@oguiza
Copy link
Contributor

oguiza commented Mar 24, 2023

This issue has been fixed in GitHub. The solution will be available in the next pip/ conda release (0.3.6).

@oguiza oguiza closed this as completed Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants