Hi,
Thanks for the above suggestion, but I don’t think the problem was with the getters itself. I did a bit of tweaking and now my current code looks like this
box_block = DataBlock(blocks=(ImageBlock, BBoxBlock, BBoxLblBlock),
splitter=RandomSplitter(),
get_x=ColReader(0, pref=path/'stage_2_train_images', suff='.png'),
get_y=[ColReader(5), ColReader(4, label_delim=';')],
item_tfms=item_tfms,
batch_tfms=batch_tfms,
n_inp=1)
But when I do box_block.summary(df_final), I hit an error which seems like it has something to do with the bb_pad done on the BBoxes. The .summary() output along with the error looks like this
Setting-up type transforms pipelines
Collecting items from PatientID … BBoxes
0 0004cfab-14fd-4e49-80ba-63a80b6bddd6 … [[0.0, 0.0, 0.0, 0.0]]
1 00313ee0-9eaa-42f4-b0ab-c148ed3241cd … [[0.0, 0.0, 0.0, 0.0]]
2 00322d4d-1c29-4943-afc9-b6754be640eb … [[0.0, 0.0, 0.0, 0.0]]
3 003d8fa0-6bf1-40ed-b54c-ac657f8495c5 … [[0.0, 0.0, 0.0, 0.0]]
4 00436515-870c-4b36-a041-de91049b9ab4 … [[152.0, 264.0, 531.0, 477.0], [152.0, 562.0, 605.0, 818.0]]
… … … …
26679 c1e73a4e-7afe-4ec5-8af6-ce8315d7a2f2 … [[418.0, 666.0, 641.0, 852.0], [504.0, 316.0, 777.0, 495.0]]
26680 c1ec14ff-f6d7-4b38-b0cb-fe07041cbdc8 … [[464.0, 609.0, 748.0, 849.0], [298.0, 185.0, 677.0, 413.0]]
26681 c1edf42b-5958-47ff-a1e7-4f23d99583ba … [[0.0, 0.0, 0.0, 0.0]]
26682 c1f6b555-2eb1-4231-98f6-50a963976431 … [[0.0, 0.0, 0.0, 0.0]]
26683 c1f7889a-9ea9-4acb-b64c-b737c929599a … [[393.0, 570.0, 738.0, 831.0], [424.0, 233.0, 780.0, 434.0]]
[26684 rows x 6 columns]
Found 26684 items
2 datasets of sizes 21348,5336
Setting up Pipeline: ColReader -> PILBase.create
Setting up Pipeline: ColReader -> TensorBBox.create
Setting up Pipeline: ColReader -> MultiCategorize
Building one sample
Pipeline: ColReader -> PILBase.create
starting from
PatientID aeeacc25-019f-4e78-b9e5-829c5d916fc2
dicom …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.dcm
png …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.png
Target 0
Classification No Lung Opacity / Not Normal
BBoxes [[0.0, 0.0, 0.0, 0.0]]
Name: 16440, dtype: object
applying ColReader gives
…/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.png
applying PILBase.create gives
PILImage mode=RGB size=1024x1024
Pipeline: ColReader -> TensorBBox.create
starting from
PatientID aeeacc25-019f-4e78-b9e5-829c5d916fc2
dicom …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.dcm
png …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.png
Target 0
Classification No Lung Opacity / Not Normal
BBoxes [[0.0, 0.0, 0.0, 0.0]]
Name: 16440, dtype: object
applying ColReader gives
[[0. 0. 0. 0.]]
applying TensorBBox.create gives
TensorBBox of size 1x4
Pipeline: ColReader -> MultiCategorize
starting from
PatientID aeeacc25-019f-4e78-b9e5-829c5d916fc2
dicom …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.dcm
png …/input/rsna/stage_2_train_images/aeeacc25-019f-4e78-b9e5-829c5d916fc2.png
Target 0
Classification No Lung Opacity / Not Normal
BBoxes [[0.0, 0.0, 0.0, 0.0]]
Name: 16440, dtype: object
applying ColReader gives
[No Lung Opacity / Not Normal]
applying MultiCategorize gives
TensorMultiCategory([2])
Final sample: (PILImage mode=RGB size=1024x1024, TensorBBox([[0., 0., 0., 0.]]), TensorMultiCategory([2]))
Setting up after_item: Pipeline: BBoxLabeler -> PointScaler -> Resize -> ToTensor
Setting up before_batch: Pipeline: bb_pad
Setting up after_batch: Pipeline: IntToFloatTensor -> AffineCoordTfm -> LightingTfm -> Normalize
Building one batch
Applying item_tfms to the first sample:
Pipeline: BBoxLabeler -> PointScaler -> Resize -> ToTensor
starting from
(PILImage mode=RGB size=1024x1024, TensorBBox of size 1x4, TensorMultiCategory([2]))
applying BBoxLabeler gives
(PILImage mode=RGB size=1024x1024, TensorBBox of size 1x4, TensorMultiCategory([2]))
applying PointScaler gives
(PILImage mode=RGB size=1024x1024, TensorBBox of size 1x4, TensorMultiCategory([2]))
applying Resize gives
(PILImage mode=RGB size=224x224, TensorBBox of size 1x4, TensorMultiCategory([2]))
applying ToTensor gives
(TensorImage of size 3x224x224, TensorBBox of size 1x4, TensorMultiCategory([2]))
Adding the next 3 samples
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:3289: UserWarning: Default grid_sample and affine_grid behavior has changed to align_corners=False since 1.3.0. Please specify align_corners=True if the old behavior is desired. See the documentation of grid_sample for details.
warnings.warn("Default grid_sample and affine_grid behavior has changed "
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:3226: UserWarning: Default grid_sample and affine_grid behavior has changed to align_corners=False since 1.3.0. Please specify align_corners=True if the old behavior is desired. See the documentation of grid_sample for details.
warnings.warn("Default grid_sample and affine_grid behavior has changed "
Applying before_batch to the list of samples
Pipeline: bb_pad
starting from
[(TensorImage of size 3x224x224, TensorBBox of size 1x4, TensorMultiCategory([2])), (TensorImage of size 3x224x224, TensorBBox of size 1x4, TensorMultiCategory([2])), (TensorImage of size 3x224x224, TensorBBox of size 1x4, TensorMultiCategory([2])), (TensorImage of size 3x224x224, TensorBBox of size 2x4, TensorMultiCategory([1]))]
applying bb_pad failed.
IndexError Traceback (most recent call last)
in ()
----> 1 box_block.summary(df_final)
9 frames
/usr/local/lib/python3.6/dist-packages/fastai2/data/block.py in summary(self, source, bs, show_batch, **kwargs)
171 if len([f for f in dls.train.before_batch.fs if f.name != ‘noop’])!=0:
172 print("\nApplying before_batch to the list of samples")
–> 173 s = _apply_pipeline(dls.train.before_batch, s)
174 else: print("\nNo before_batch transform to apply")
175
/usr/local/lib/python3.6/dist-packages/fastai2/data/block.py in _apply_pipeline(p, x)
131 except Exception as e:
132 print(f" applying {name} failed.")
–> 133 raise e
134 return x
135
/usr/local/lib/python3.6/dist-packages/fastai2/data/block.py in _apply_pipeline(p, x)
127 name = f.name
128 try:
–> 129 x = f(x)
130 if name != “noop”: print(f" applying {name} gives\n {_short_repr(x)}")
131 except Exception as e:
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in call(self, x, **kwargs)
70 @property
71 def name(self): return getattr(self, ‘_name’, _get_name(self))
—> 72 def call(self, x, **kwargs): return self._call(‘encodes’, x, **kwargs)
73 def decode (self, x, **kwargs): return self._call(‘decodes’, x, **kwargs)
74 def repr(self): return f’{self.name}: {self.encodes} {self.decodes}’
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
80 def _call(self, fn, x, split_idx=None, **kwargs):
81 if split_idx!=self.split_idx and self.split_idx is not None: return x
—> 82 return self._do_call(getattr(self, fn), x, **kwargs)
83
84 def _do_call(self, f, x, **kwargs):
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in _do_call(self, f, x, **kwargs)
84 def _do_call(self, f, x, **kwargs):
85 if not is_tuple(x):
—> 86 return x if f is None else retain_type(f(x, **kwargs), x, f.returns_none(x))
87 res = tuple(self.do_call(f, x, **kwargs) for x in x)
88 return retain_type(res, x)
/usr/local/lib/python3.6/dist-packages/fastcore/dispatch.py in call(self, *args, **kwargs)
96 if not f: return args[0]
97 if self.inst is not None: f = MethodType(f, self.inst)
—> 98 return f(*args, **kwargs)
99
100 def get(self, inst, owner):
/usr/local/lib/python3.6/dist-packages/fastai2/vision/data.py in bb_pad(samples, pad_idx)
31 def bb_pad(samples, pad_idx=0):
32 “Function that collect samples
of labelled bboxes and adds padding with pad_idx
.”
—> 33 samples = [(s[0], *clip_remove_empty(*s[1:])) for s in samples]
34 max_len = max([len(s[2]) for s in samples])
35 def _f(img,bbox,lbl):
/usr/local/lib/python3.6/dist-packages/fastai2/vision/data.py in (.0)
31 def bb_pad(samples, pad_idx=0):
32 “Function that collect samples
of labelled bboxes and adds padding with pad_idx
.”
—> 33 samples = [(s[0], *clip_remove_empty(*s[1:])) for s in samples]
34 max_len = max([len(s[2]) for s in samples])
35 def _f(img,bbox,lbl):
/usr/local/lib/python3.6/dist-packages/fastai2/vision/data.py in clip_remove_empty(bbox, label)
26 bbox = torch.clamp(bbox, -1, 1)
27 empty = ((bbox[…,2] - bbox[…,0])*(bbox[…,3] - bbox[…,1]) < 0.)
—> 28 return (bbox[~empty], label[~empty])
29
30 # Cell
IndexError: The shape of the mask [2] at index 0 does not match the shape of the indexed tensor [1] at index 0
I am not sure how to work around this padding issue now. Thanks in advance for any support from anyone, since I am pretty new to this and have been stuck with this (and similar issues for days now.)