I’m using fastai
library with custom PyTorch
dataset, which I specified the following way (...
are there for the purpose of clarity):
from fastai import *
class CustomDataset(Dataset):
def __init__(self, input_path):
...
def __len__(self):
...
def __getitem__(self, idx):
...
return (
torch.tensor(image, dtype=torch.float),
torch.tensor(target, dtype=torch.long)
)
train_dset = CustomDataset(train_path)
valid_dset = CustomDataset(valid_path)
train_dl = DataLoader(train_dset, bs=16, shuffle=True)
valid_dl = DataLoader(valid_dset, bs=16, shuffle=True)
dls = DataLoaders(train_dl, valid_dl)
Then I’m using those dataloaders to train the model with Learner
:
learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
and I pass learn
object to ClassificationInterpretation
class:
interp = ClassificationInterpretation.from_learner(learn)
However, this results in the AttributeError
with not so clear details:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-98-aa7f7b70a42b> in <module>
----> 1 interp = ClassificationInterpretation.from_learner(learn)
~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/interpret.py in from_learner(cls, learn, ds_idx, dl, act)
40 _,_,losses = learn.get_preds(dl=dl, with_input=False, with_loss=True, with_decoded=False,
41 with_preds=False, with_targs=False, act=act)
---> 42 return cls(learn, dl, losses, act)
43
44 def top_losses(self, k=None, largest=True, items=False):
~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/interpret.py in __init__(self, learn, dl, losses, act)
78 def __init__(self, learn, dl, losses, act=None):
79 super().__init__(learn, dl, losses, act)
---> 80 self.vocab = self.dl.vocab
81 if is_listy(self.vocab): self.vocab = self.vocab[-1]
82
~/anaconda3/envs/python3/lib/python3.6/site-packages/fastcore/basics.py in __getattr__(self, k)
387 if self._component_attr_filter(k):
388 attr = getattr(self,self._default,None)
--> 389 if attr is not None: return getattr(attr,k)
390 raise AttributeError(k)
391 def __dir__(self): return custom_dir(self,self._dir())
~/anaconda3/envs/python3/lib/python3.6/site-packages/torch/utils/data/dataset.py in __getattr__(self, attribute_name)
81 return function
82 else:
---> 83 raise AttributeError
84
85 @classmethod
AttributeError:
It seems like both data loaders should have additional attribute vocab
which might be there for DataBlock
output, but not for my custom definition of data loaders. Is that really a problem? How this property should look like and how it could be added to make it compatible with ClassificationInterpreter
?