Learn.predict() raises 'match length mismatch' error in multitarget classification

Hi All,

I am trying to use GradCam (Chapter 18) code to work with a multitarget classification model for Weakly Supervised Object Detection. But stuck at the beginning of this task due to ‘length mismatch error’ when I run the following statement:
cls,idx,prob=learn.predict('data/train_images_tiny/clip_000003/00000.jpg')

The error log is below:


AssertionError Traceback (most recent call last)
Input In [15], in <cell line: 1>()
----> 1 cls,idx,prob=learn.predict(‘data/train_images_tiny/clip_000003/00000.jpg’)

File ~/mambaforge/lib/python3.9/site-packages/fastai/learner.py:292, in Learner.predict(self, item, rm_type_tfms, with_input)
290 i = getattr(self.dls, ‘n_inp’, -1)
291 inp = (inp,) if i==1 else tuplify(inp)
→ 292 dec = self.dls.decode_batch(inp + tuplify(dec_preds))[0]
293 dec_inp,dec_targ = map(detuplify, [dec[:i],dec[i:]])
294 res = dec_targ,dec_preds[0],preds[0]

File ~/mambaforge/lib/python3.9/site-packages/fastai/data/core.py:124, in TfmdDL.decode_batch(self, b, max_n, full)
119 def decode_batch(self,
120 b, # Batch to decode
121 max_n:int=9, # Maximum number of items to decode
122 full:bool=True # Whether to decode all transforms. If False, decode up to the point the item knows how to show itself
123 ):
→ 124 return self._decode_batch(self.decode(b), max_n, full)

File ~/mambaforge/lib/python3.9/site-packages/fastai/data/core.py:130, in TfmdDL._decode_batch(self, b, max_n, full)
128 f1 = self.before_batch.decode
129 f = compose(f1, f, partial(getcallable(self.dataset,‘decode’), full = full))
→ 130 return L(batch_to_samples(b, max_n=max_n)).map(f)

File ~/mambaforge/lib/python3.9/site-packages/fastcore/foundation.py:155, in L.map(self, f, gen, *args, **kwargs)
→ 155 def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))

File ~/mambaforge/lib/python3.9/site-packages/fastcore/basics.py:790, in map_ex(iterable, f, gen, *args, **kwargs)
788 res = map(g, iterable)
789 if gen: return res
→ 790 return list(res)

File ~/mambaforge/lib/python3.9/site-packages/fastcore/basics.py:775, in bind.call(self, *args, **kwargs)
773 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
774 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
→ 775 return self.func(*fargs, **kwargs)

File ~/mambaforge/lib/python3.9/site-packages/fastcore/basics.py:800, in compose.._inner(x, *args, **kwargs)
799 def _inner(x, *args, **kwargs):
→ 800 for f in funcs: x = f(x, *args, **kwargs)
801 return x

File ~/mambaforge/lib/python3.9/site-packages/fastai/data/core.py:466, in Datasets.decode(self, o, full)
→ 466 def decode(self, o, full=True): return tuple(tl.decode(o_, full=full) for o_,tl in zip(o,tuplify(self.tls, match=o)))

File ~/mambaforge/lib/python3.9/site-packages/fastcore/basics.py:70, in tuplify(o, use_list, match)
68 def tuplify(o, use_list=False, match=None):
69 “Make o a tuple”
—> 70 return tuple(listify(o, use_list=use_list, match=match))

File ~/mambaforge/lib/python3.9/site-packages/fastcore/basics.py:64, in listify(o, use_list, match, rest)
62 if is_coll(match): match = len(match)
63 if len(res)==1: res = res
match
—> 64 else: assert len(res)==match, ‘Match length mismatch’
65 return res

AssertionError: Match length mismatch

When I check the res in listify() through python debugger, it has 4 elements whereas match has a value of two.

ipdb> res

[TfmdLists: []
tfms - [ColReader – {‘cols’: ‘image_id’, ‘pref’: ‘’, ‘suff’: ‘’, ‘label_delim’: None}:
encodes: decodes: , PILBase.create:
encodes: (Path,object) → create
(str,object) → create
(Tensor,object) → create
(ndarray,object) → create
(bytes,object) → createdecodes: ], TfmdLists: []
tfms - [ColReader – {‘cols’: ‘y1_label’, ‘pref’: ‘’, ‘suff’: ‘’, ‘label_delim’: None}:
encodes: decodes: , Categorize – {‘vocab’: [‘bipolar dissector’, ‘bipolar forceps’, ‘cadiere forceps’, ‘clip applier’, ‘force bipolar’, ‘grasping retractor’, ‘monopolar curved scissors’, ‘nan’, ‘needle driver’, ‘permanent cautery hook/spatula’, ‘prograsp forceps’, ‘stapler’, ‘suction irrigator’, ‘tip-up fenestrated grasper’, ‘vessel sealer’], ‘sort’: True, ‘add_na’: False}:
encodes: (Tabular,object) → encodes
(object,object) → encodes
decodes: (Tabular,object) → decodes
(object,object) → decodes
], TfmdLists: []
tfms - [ColReader – {‘cols’: ‘y2_label’, ‘pref’: ‘’, ‘suff’: ‘’, ‘label_delim’: None}:
encodes: decodes: , Categorize – {‘vocab’: [‘bipolar dissector’, ‘bipolar forceps’, ‘cadiere forceps’, ‘clip applier’, ‘force bipolar’, ‘grasping retractor’, ‘monopolar curved scissors’, ‘nan’, ‘needle driver’, ‘permanent cautery hook/spatula’, ‘prograsp forceps’, ‘stapler’, ‘suction irrigator’, ‘tip-up fenestrated grasper’, ‘vessel sealer’], ‘sort’: True, ‘add_na’: False}:
encodes: (Tabular,object) → encodes
(object,object) → encodes
decodes: (Tabular,object) → decodes
(object,object) → decodes
], TfmdLists: []
tfms - [ColReader – {‘cols’: ‘y3_label’, ‘pref’: ‘’, ‘suff’: ‘’, ‘label_delim’: None}:
encodes: decodes: , Categorize – {‘vocab’: [‘bipolar dissector’, ‘bipolar forceps’, ‘cadiere forceps’, ‘clip applier’, ‘force bipolar’, ‘grasping retractor’, ‘monopolar curved scissors’, ‘nan’, ‘needle driver’, ‘permanent cautery hook/spatula’, ‘prograsp forceps’, ‘stapler’, ‘suction irrigator’, ‘tip-up fenestrated grasper’, ‘vessel sealer’], ‘sort’: True, ‘add_na’: False}:
encodes: (Tabular,object) → encodes
(object,object) → encodes
decodes: (Tabular,object) → decodes
(object,object) → decodes
], TfmdLists: []
tfms - [ColReader – {‘cols’: ‘y4_label’, ‘pref’: ‘’, ‘suff’: ‘’, ‘label_delim’: None}:
encodes: decodes: , Categorize – {‘vocab’: [‘bipolar dissector’, ‘bipolar forceps’, ‘cadiere forceps’, ‘clip applier’, ‘force bipolar’, ‘grasping retractor’, ‘monopolar curved scissors’, ‘nan’, ‘needle driver’, ‘permanent cautery hook/spatula’, ‘prograsp forceps’, ‘stapler’, ‘suction irrigator’, ‘tip-up fenestrated grasper’, ‘vessel sealer’], ‘sort’: True, ‘add_na’: False}:
encodes: (Tabular,object) → encodes
(object,object) → encodes
decodes: (Tabular,object) → decodes
(object,object) → decodes
]]

The structure of blocks in the dls is (ImageBlock, CategoryBlock, CategoryBlock, CategoryBlock, CategoryBlock).

Any ideas to resolve this issue? I’m really stuck at this error for a while. Appreciate the guidance.

Many Thanks and
Kind Regards,
Bilal

1 Like

Hey @bilalUWE were you able to generate GRADCAM for object detection problem, if yes,
can you share the code that you used to implement that?

Hi Harshit,

No, I couldn’t get the time to generate GradCAM.

I have run into the same issue. Has anyone else figured out how to get learn.predict working with multiple CategoryBlocks?