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.07k stars 633 forks source link

Cannot running examples for TypeError: can't convert np.ndarray of type numpy.str_. #701

Closed jsbyysheng closed 1 year ago

jsbyysheng commented 1 year ago

Cannot running examples

from tsai.all import *
X, y, splits = get_classification_data('ECG200', split_data=False)
batch_tfms = TSStandardize()
clf = TSClassifier(X, y, splits=splits, path='models', arch=InceptionTimePlus, batch_tfms=batch_tfms, metrics=accuracy, cbs=ShowGraph())
clf.fit_one_cycle(100, 3e-4)
clf.export("clf.pkl") 

How to reproduce the error? I used miniforge3 to create a env conda create -n timeseries python=3.10. Then I installed tsai by conda install -c timeseriesai tsai (in fact, I have created 3 environments and tried pip and mamba). Finally, I got

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 4
      2 X, y, splits = get_classification_data('LSST', split_data=False)
      3 batch_tfms = TSStandardize(by_sample=True)
----> 4 mv_clf = TSClassifier(X, y, splits=splits, path='models', arch=InceptionTimePlus, batch_tfms=batch_tfms, metrics=accuracy, cbs=ShowGraph())
      5 mv_clf.fit_one_cycle(10, 1e-2)
      6 mv_clf.export("mv_clf.pkl")

File [~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/tslearner.py:33](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a22525458333039305f6c6968616f776569227d.vscode-resource.vscode-cdn.net/home/yangsheng/timeseries/~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/tslearner.py:33), in TSClassifier.__init__(self, X, y, splits, tfms, inplace, sel_vars, sel_steps, weights, partial_n, train_metrics, valid_metrics, bs, batch_size, batch_tfms, pipelines, shuffle_train, drop_last, num_workers, do_setup, device, arch, arch_config, pretrained, weights_path, exclude_head, cut, init, loss_func, opt_func, lr, metrics, cbs, wd, wd_bn_bias, train_bn, moms, path, model_dir, splitter, verbose)
     30     bs = batch_size
     32 # DataLoaders
---> 33 dls = get_ts_dls(X, y=y, splits=splits, sel_vars=sel_vars, sel_steps=sel_steps, tfms=tfms, inplace=inplace, 
     34                  path=path, bs=bs, batch_tfms=batch_tfms, num_workers=num_workers, weights=weights, partial_n=partial_n, 
     35                  device=device, shuffle_train=shuffle_train, drop_last=drop_last)
     37 if loss_func is None:
     38     if hasattr(dls, 'loss_func'): loss_func = dls.loss_func

File [~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/data/core.py:1051](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a22525458333039305f6c6968616f776569227d.vscode-resource.vscode-cdn.net/home/yangsheng/timeseries/~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/data/core.py:1051), in get_ts_dls(X, y, splits, sel_vars, sel_steps, tfms, inplace, path, bs, batch_tfms, num_workers, device, shuffle_train, drop_last, weights, partial_n, sampler, sort, **kwargs)
   1049 splits = _check_splits(X, splits)
   1050 create_dir(path, verbose=False)
-> 1051 dsets = TSDatasets(X, y, splits=splits, sel_vars=sel_vars, sel_steps=sel_steps, tfms=tfms, inplace=inplace)
   1052 if weights is not None:
   1053     assert len(X) == len(weights), 'len(X) != len(weights)'

File [~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/data/core.py:471](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a22525458333039305f6c6968616f776569227d.vscode-resource.vscode-cdn.net/home/yangsheng/timeseries/~/mambaforge/envs/timeseries/lib/python3.10/site-packages/tsai/data/core.py:471), in TSDatasets.__init__(self, X, y, items, sel_vars, sel_steps, tfms, tls, n_inp, dl_type, inplace, **kwargs)
    469 if tfms is None or tfms == [None] * len(self.tls):
    470     for tl,typ in zip(self.tls, self.typs):
--> 471         tl.items = typ(tl.items)
    472     self.ptls = self.tls
    473     self.no_tfm = True

TypeError: can't convert np.ndarray of type numpy.str_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
jsbyysheng commented 1 year ago

Aha, I found if I downgraded to tsai==0.3.4, all works well.

oguiza commented 1 year ago

Hi @jsbyysheng , The error you see is due to a breaking change (#655) introduced in release 0.3.5. Before this change, when tfms were None (default), they were automatically added to the learners. In the case of TSClassifier this was added:

tfms = [None, TSClassification()]

The issue is that sometimes, you really want tfms=None. And could do this. So now, you need to manually use this when you instantiate TSClassifier. I recommend you to always use the last release and add the required transforms.

jsbyysheng commented 1 year ago

Glad to hear from you.