get_transforms() with default settings threw the error below for histology images
"RuntimeError: B should have at least 2 dimensions, but has 1 dimensions instead"
I’m using images from this Kaggle competition:
First I set all the arguments for get_transforms() to zero to get a baseline
tfms = get_transforms(do_flip=False,
flip_vert=False,
max_rotate=0.,
max_zoom=0.,
max_lighting=0.,
max_warp=0.,
p_affine=0.,
p_lighting=0.)
Everything, I did below worked:
data = (ImageItemList.from_df(df=df, path=path, cols='fpaths')
.random_split_by_pct(valid_pct=0.2, seed=10)
.label_from_df(cols='class_label')
.transform(tfms, size=49)
.databunch(bs=128))
data.show_batch(rows=3, figsize=(7,7), hide_axis=False)
learn = create_cnn(data, models.resnet34, metrics=[error_rate, accuracy])
learn.fit_one_cycle(6)
Then I tried using the default options for get_transforms() and got the error:
tfms = get_transforms()
data = (ImageItemList.from_df(df=df, path=path, cols='fpaths')
.random_split_by_pct(valid_pct=0.2, seed=10)
.label_from_df(cols='class_label')
.transform(tfms, size=49)
.databunch(bs=128))
data.show_batch(rows=3, figsize=(7,7), hide_axis=False)
"RuntimeError: B should have at least 2 dimensions, but has 1 dimensions instead"
Finally, I narrowed the cause of the problem down to max_warp() by manually entering all of the defaults and changing each to zero, one at a time:
tfms = get_transforms(do_flip=True,
flip_vert=False,
max_rotate=10.,
max_zoom=1.1,
max_lighting=0.2,
max_warp=0.,
p_affine=0.75,
p_lighting=0.75)
Thought I’d share this in case anyone else ran into the same issue.
It is impressive that fastai knows that these images should not have max_warp applied! Is this a bug?
I wonder if this same error will be thrown when I want to look at 3D images of cells and organelles? … TBD
The full error text was:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-25-0a63e3fd5550> in <module>
----> 1 data.show_batch(rows=3, figsize=(7,7), hide_axis=False)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in show_batch(self, rows, ds_type, **kwargs)
151 def show_batch(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, **kwargs)->None:
152 "Show a batch of data in `ds_type` on a few `rows`."
--> 153 x,y = self.one_batch(ds_type, True, True)
154 if self.train_ds.x._square_show: rows = rows ** 2
155 xs = [self.train_ds.x.reconstruct(grab_idx(x, i, self._batch_first)) for i in range(rows)]
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in one_batch(self, ds_type, detach, denorm)
134 w = self.num_workers
135 self.num_workers = 0
--> 136 try: x,y = next(iter(dl))
137 finally: self.num_workers = w
138 if detach: x,y = to_detach(x),to_detach(y)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in __iter__(self)
68 def __iter__(self):
69 "Process and returns items from `DataLoader`."
---> 70 for b in self.dl:
71 y = b[1][0] if is_listy(b[1]) else b[1]
72 yield self.proc_batch(b)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
466 self.reorder_dict[idx] = batch
467 continue
--> 468 return self._process_next_batch(batch)
469
470 next = __next__ # Python 2 compatibility
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
487 self._put_indices()
488 if isinstance(batch, _utils.ExceptionWrapper):
--> 489 raise batch.exc_type(batch.exc_msg)
490 return batch
491
RuntimeError: Traceback (most recent call last):
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 99, in <listcomp>
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/data_block.py", line 486, in __getitem__
x = x.apply_tfms(self.tfms, **self.tfmargs)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py", line 113, in apply_tfms
else: x = tfm(x)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py", line 498, in __call__
return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py", line 445, in __call__
if args: return self.calc(*args, **kwargs)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py", line 450, in calc
if self._wrap: return getattr(x, self._wrap)(self.func, *args, **kwargs)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py", line 167, in coord
self.flow = func(self.flow, *args, **kwargs)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/transform.py", line 227, in symmetric_warp
return _perspective_warp(c, targ_pts, invert)
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/transform.py", line 213, in _perspective_warp
return _apply_perspective(c, _find_coeffs(_orig_pts, targ_pts))
File "/home/ubuntu/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/transform.py", line 194, in _find_coeffs
return torch.gesv(B,A)[0][:,0]
RuntimeError: B should have at least 2 dimensions, but has 1 dimensions instead