V1.0 data augmentation - AssertionError: List len mismatch (0 vs 2)


(Ramon) #1

Hi there,

I’m trying to get a grip on the new fastai version.

I failed trying to show examples of data augmentations for dogs/cats. Any idea what’s going on?

data = image_data_from_folder(PATH, ds_tfms=get_transforms(), tfms=imagenet_norm)
ds = data.train_ds
data_tfms = DatasetTfm(ds, ds.tfms)
x,y = next(iter(data_tfms))


AssertionError Traceback (most recent call last)
in ()
1 ds = data.train_ds
2 data_tfms = DatasetTfm(ds, ds.tfms)
----> 3 x,y = next(iter(data_tfms))

~/fastai/fastai/vision/data.py in getitem(self, idx)
188 def getitem(self,idx:int)->Tuple[ItemBase,Any]:
189 “Return tfms(x),y.”
–> 190 x,y = self.ds[idx]
191 x = apply_tfms(self.tfms, x, **self.kwargs)
192 if self.tfm_y: y = apply_tfms(self.tfms, y, **self.y_kwargs)

~/fastai/fastai/vision/data.py in getitem(self, idx)
189 “Return tfms(x),y.”
190 x,y = self.ds[idx]
–> 191 x = apply_tfms(self.tfms, x, **self.kwargs)
192 if self.tfm_y: y = apply_tfms(self.tfms, y, **self.y_kwargs)
193 return x, y

~/fastai/fastai/vision/image.py in apply_tfms(tfms, x, do_resolve, xtra, size, mult, do_crop, padding_mode, **kwargs)
450 for tfm in tfms:
451 if tfm.tfm in xtra: x = tfm(x, **xtra[tfm.tfm])
–> 452 elif tfm in size_tfms: x = tfm(x, size=size, padding_mode=padding_mode)
453 else: x = tfm(x)
454 return x

~/fastai/fastai/vision/image.py in call(self, x, *args, **kwargs)
366 def call(self, x:Image, *args, **kwargs)->Image:
367 “Randomly execute our tfm on x.”
–> 368 return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x
369
370

~/fastai/fastai/vision/image.py in call(self, p, is_random, *args, **kwargs)
310 def call(self, *args:Any, p:float=1., is_random:bool=True, **kwargs:Any)->Image:
311 “Calc now if args passed; else create a transform called prob p if random.”
–> 312 if args: return self.calc(*args, **kwargs)
313 else: return RandTransform(self, kwargs=kwargs, is_random=is_random, p=p)
314

~/fastai/fastai/vision/image.py in calc(self, x, *args, **kwargs)
315 def calc(self, x:Image, *args:Any, **kwargs:Any)->Image:
316 “Apply to image x, wrapping it if necessary.”
–> 317 if self._wrap: return getattr(x, self._wrap)(self.func, *args, **kwargs)
318 else: return self.func(x, *args, **kwargs)
319

~/fastai/fastai/vision/image.py in pixel(self, func, *args, **kwargs)
152 def pixel(self, func:PixelFunc, *args, **kwargs)->‘Image’:
153 “Equivalent to image.px = func(image.px).”
–> 154 self.px = func(self.px, *args, **kwargs)
155 return self
156

~/fastai/fastai/vision/transform.py in crop_pad(x, size, padding_mode, row_pct, col_pct)
90 “Crop and pad tfm - row_pct,col_pct sets focal point.”
91 padding_mode = _pad_mode_convert[padding_mode]
—> 92 size = listify(size,2)
93 if x.shape[1:] == size: return x
94 rows,cols = size

~/fastai/fastai/core.py in listify(p, q)
84 n = q if type(q)==int else len§ if q is None else len(q)
85 if len§==1: p = p * n
—> 86 assert len§==n, f’List len mismatch ({len§} vs {n})’
87 return list§
88

AssertionError: List len mismatch (0 vs 2)


(Ramon) #2

I found out that the images are not cropped because this line:

size_tfms = [o for o in tfms if isinstance(o.tfm,TfmCrop)]

always returns an empty list while there are instances of TfmCrop in there

eg I printed

print(tfms[len(tfms)-1].tfm.class.name, isinstance(tfms[len(tfms)-1].tfm, TfmCrop))

which returns TfmCrop and False?? Why is that? Digging further


(Ramon) #3

This does work:

size_tfms = [o for o in tfms if o.tfm.class.name is ‘TfmCrop’]
instead of
size_tfms = [o for o in tfms if isinstance(o.tfm,TfmCrop)]
but that’s probably hacky python code


(Ramon) #4

Next to above,

the line below is failing on my machine as well:
vision.py, line 92 --> size = listify(size,2)
size seems to be an integer at that point, but listify is failing
setting size to eg size = [224,224] does the trick

But why is “size = listify(size,2)” failing?


(Jeremy Howard) #5

I think you need to add a (e.g) size=224 param in your get_transforms().


#6

And probably in image_data_from_folder too.


(Jeremy Howard) #7

@sgugger this is something we really need to fix. At least better error messages - ideally better behavior! Maybe size=224 should be default? …At least if you’re using crop_pad et al transforms…


#8

I checked and get_transfom doesn’t take a size actually (it’s inferred in apply_tfms) so it’s mostly in the image_data method that this is missing.
Will see how we can have better error message popping for this.