fastai / fastai

The fastai deep learning library
http://docs.fast.ai
Apache License 2.0
26.08k stars 7.54k forks source link

get_preds: IndexError: tuple index out of range #3053

Open fortunto2 opened 3 years ago

fortunto2 commented 3 years ago

I trained the model resnet50 on my dataset, and now I want to get predictions from the loaded model.

I'm not sure, but there seems to be some strange error when using the result prediction (get_preds).

fastai: 2.1.8 fastcore: 1.3.10 torch: 1.7.0 torchvision: 0.8.1 cuda=10.1 (GPU used)

Linux(Debian)

import torch

import fastai
from fastai import *
from fastai.vision.all import *
from fastai.imports import *
from fastai.vision import *

path = Path("/home/jupyter/dataset/")

batch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]

dblock = DataBlock(blocks= (ImageBlock),
                   get_items=get_image_files,
                   item_tfms=Resize(224),
                   batch_tfms=batch_tfms,
                   )

files = get_image_files(path)
dls = dblock.dataloaders(path)

learn = load_learner("/home/jupyter/models/resnet50_export.pkl", cpu=False)

dl = dls.test_dl(files, bs=256, num_workers=4, shuffle=False)
dl.__dict__

{'after_item': Pipeline: Resize -- {'size': (224, 224), 'method': 'crop', 'pad_mode': 'reflection', 'resamples': (2, 0), 'p': 1.0} -> ToTensor,
 'before_batch': Pipeline: ,
 'after_batch': Pipeline: IntToFloatTensor -- {'div': 255.0, 'div_mask': 1} -> Flip -- {'size': 224, 'mode': 'bilinear', 'pad_mode': 'reflection', 'mode_mask': 'nearest', 'align_corners': True, 'p': 0.5} -> Brightness -- {'max_lighting': 0.2, 'p': 1.0, 'draw': None, 'batch': False} -> Normalize -- {'mean': tensor([[[[0.4850]],

          [[0.4560]],

          [[0.4060]]]], device='cuda:0'), 'std': tensor([[[[0.2290]],

          [[0.2240]],

          [[0.2250]]]], device='cuda:0'), 'axes': (0, 2, 3)},
 '__stored_args__': {'dataset': (#72143) [(PILImage mode=RGB size=457x600,),(PILImage mode=RGB size=276x600,),(PILImage mode=RGB size=384x512,),(PILImage mode=RGB size=287x600,),(PILImage mode=RGB size=500x465,),(PILImage mode=RGB size=470x600,),(PILImage mode=RGB size=500x533,),(PILImage mode=RGB size=449x600,),(PILImage mode=RGB size=449x600,),(PILImage mode=RGB size=960x720,)...],
  'bs': 256,
  'shuffle': False,
  'drop_last': False,
  'indexed': True,
  'n': 72143,
  'pin_memory': False,
  'timeout': 0,
  'device': device(type='cuda', index=0)},
 'dataset': (#72143) [(PILImage mode=RGB size=457x600,),(PILImage mode=RGB size=276x600,),(PILImage mode=RGB size=384x512,),(PILImage mode=RGB size=287x600,),(PILImage mode=RGB size=500x465,),(PILImage mode=RGB size=470x600,),(PILImage mode=RGB size=500x533,),(PILImage mode=RGB size=449x600,),(PILImage mode=RGB size=449x600,),(PILImage mode=RGB size=960x720,)...],
 'bs': 256,
 'shuffle': False,
 'drop_last': False,
 'indexed': True,
 'n': 72143,
 'pin_memory': False,
 'timeout': 0,
 'device': device(type='cuda', index=0),
 'rng': <random.Random at 0x5623ae4cb980>,
 'num_workers': 1,
 'offs': 0,
 'fake_l': <fastai.data.load._FakeLoader at 0x7f241093be90>,
 '_n_inp': 1,
 '_types': {tuple: [fastai.torch_core.TensorImage]}}

when use get_preds, get error

preds, y, losses = learn.get_preds(dl=dl, with_loss=True, reorder=False)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-99168ceffbce> in <module>
      1 # sf = SaveFeatures(learn.model.module[1][4])
----> 2 preds, y, losses = learn.get_preds(dl=dl, with_loss=True, reorder=False)

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, act, inner, reorder, cbs, **kwargs)
    235         if with_loss: ctx_mgrs.append(self.loss_not_reduced())
    236         with ContextManagers(ctx_mgrs):
--> 237             self._do_epoch_validate(dl=dl)
    238             if act is None: act = getattr(self.loss_func, 'activation', noop)
    239             res = cb.all_tensors()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
    185         if dl is None: dl = self.dls[ds_idx]
    186         self.dl = dl
--> 187         with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
    188 
    189     def _do_epoch(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    152 
    153     def _with_events(self, f, event_type, ex, final=noop):
--> 154         try:       self(f'before_{event_type}')       ;f()
    155         except ex: self(f'after_cancel_{event_type}')
    156         finally:   self(f'after_{event_type}')        ;final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    158     def all_batches(self):
    159         self.n_iter = len(self.dl)
--> 160         for o in enumerate(self.dl): self.one_batch(*o)
    161 
    162     def _do_one_batch(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    176         self.iter = i
    177         self._split(b)
--> 178         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    179 
    180     def _do_epoch_train(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    154         try:       self(f'before_{event_type}')       ;f()
    155         except ex: self(f'after_cancel_{event_type}')
--> 156         finally:   self(f'after_{event_type}')        ;final()
    157 
    158     def all_batches(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in __call__(self, event_name)
    130     def ordered_cbs(self, event): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, event)]
    131 
--> 132     def __call__(self, event_name): L(event_name).map(self._call_one)
    133 
    134     def _call_one(self, event_name):

/opt/conda/lib/python3.7/site-packages/fastcore/foundation.py in map(self, f, gen, *args, **kwargs)
    177     def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
    178 
--> 179     def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
    180     def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
    181     def filter(self, f=noop, negate=False, gen=False, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
    604     res = map(g, iterable)
    605     if gen: return res
--> 606     return list(res)
    607 
    608 # Cell

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in __call__(self, *args, **kwargs)
    594             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    595         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 596         return self.func(*fargs, **kwargs)
    597 
    598 # Cell

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _call_one(self, event_name)
    134     def _call_one(self, event_name):
    135         assert hasattr(event, event_name), event_name
--> 136         [cb(event_name) for cb in sort_by_run(self.cbs)]
    137 
    138     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in <listcomp>(.0)
    134     def _call_one(self, event_name):
    135         assert hasattr(event, event_name), event_name
--> 136         [cb(event_name) for cb in sort_by_run(self.cbs)]
    137 
    138     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     42                (self.run_valid and not getattr(self, 'training', False)))
     43         res = None
---> 44         if self.run and _run: res = getattr(self, event_name, noop)()
     45         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     46         return res

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in after_batch(self)
    114         else: (self.save_targs/str(self.iter)).save_array(targs[0])
    115         if self.with_loss:
--> 116             bs = find_bs(self.yb)
    117             loss = self.loss if self.loss.numel() == bs else self.loss.view(bs,-1).mean(1)
    118             self.losses.append(self.learn.to_detach(loss))

/opt/conda/lib/python3.7/site-packages/fastai/torch_core.py in find_bs(b)
    536 def find_bs(b):
    537     "Recursively search the batch size of `b`."
--> 538     return item_find(b).shape[0]
    539 
    540 # Cell

/opt/conda/lib/python3.7/site-packages/fastai/torch_core.py in item_find(x, idx)
    522 def item_find(x, idx=0):
    523     "Recursively takes the `idx`-th element of `x`"
--> 524     if is_listy(x): return item_find(x[idx])
    525     if isinstance(x,dict):
    526         key = list(x.keys())[idx] if isinstance(idx, int) else idx

IndexError: tuple index out of range

TTA work fine, but i want use get_preds (for features extract)

a1, target = learn.tta(dl=dl, n=1, beta=None, use_max=False)
y = a1.argmax(dim=-1)

Additional context

Model file: https://dropmefiles.com/VC0Ku

Further I ran into when pulling out signs with this problem. While I decided to stay on the grid resnet50

https://forums.fast.ai/t/efficientnet-timm-extract-features-in-fastai-v2/82889

resnet50 learner


batch_tfms = [*aug_transforms(size=224, max_warp=0, flip_vert=True, max_lighting=0.2), Normalize.from_stats(*imagenet_stats)]

bs=256

src = (ImageDataLoaders.from_df(
        df=df, valid_col='is_valid', 
        path=path, 
        folder='train', 
        item_tfms=Resize(224),
        batch_tfms=batch_tfms, bs=bs,        
    ))

learn = cnn_learner(src, models.resnet50, 
                    loss_func=CrossEntropyLossFlat(),
                    metrics=[error_rate, accuracy], 
                    model_dir="/home/jupyter/models/0212" )

learn.fit_one_cycle(10)
learn.export('/home/jupyter/models/0212/resnet50_export.pkl')
fortunto2 commented 3 years ago

worked with dataframe... but why with get_image_files problems?


batch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]
bs=256

dls = (ImageDataLoaders.from_df(
        df=train, 
        path=path, 
        item_tfms=Resize(224),
        batch_tfms=batch_tfms, bs=bs,        
    ))

dls.valid = dls.train

dl_test = dls.test_dl(train, 
                      with_labels=True, 
                      path=path, 
                      bs=256, 
                      num_workers=4, 
                      shuffle=False,
                      drop_last=False
                     )

preds = learn.get_preds(dl=dl_test, with_decoded=True, with_loss=True, reorder=False)