Let me re-ask a different question (still on segmentation). I’m trying to do a custom dataset right now, databunch and Learner generated as so:
ds12 = DataBlock(blocks=(ImageBlock, ImageBlock(cls=PILMask)),
get_items=get_image_files,
splitter=RandomSplitter(),
get_y=lambda o: path/'masks_machine'/f'{o.stem}{o.suffix}')
dbunch = ds12.databunch(path/'img', bs=4, item_tfms=Resize(224),
batch_tfms=[*aug_transforms(size=224), Normalize(*imagenet_stats)])
codes = ['Person', 'Background', 'Other']
dbunch.vocab = codes
learn = unet_learner(dbunch, resnet50, metrics=accuracy)
When I do learn.lr_find()
I’m getting a size mismatch error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-17-d81c6bd29d71> in <module>()
----> 1 learn.lr_find()
8 frames
/usr/local/lib/python3.6/dist-packages/fastai2/callback/schedule.py in lr_find(self, start_lr, end_lr, num_it, stop_div, show_plot)
188 n_epoch = num_it//len(self.dbunch.train_dl) + 1
189 cb=LRFinder(start_lr=start_lr, end_lr=end_lr, num_it=num_it, stop_div=stop_div)
--> 190 with self.no_logging(): self.fit(n_epoch, cbs=cb)
191 if show_plot: self.recorder.plot_lr_find()
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
279 try:
280 self.epoch=epoch; self('begin_epoch')
--> 281 self._do_epoch_train()
282 self._do_epoch_validate()
283 except CancelEpochException: self('after_cancel_epoch')
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in _do_epoch_train(self)
254 try:
255 self.dl = self.dbunch.train_dl; self('begin_train')
--> 256 self.all_batches()
257 except CancelTrainException: self('after_cancel_train')
258 finally: self('after_train')
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in all_batches(self)
232 def all_batches(self):
233 self.n_iter = len(self.dl)
--> 234 for o in enumerate(self.dl): self.one_batch(*o)
235
236 def one_batch(self, i, b):
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in one_batch(self, i, b)
238 try:
239 self._split(b); self('begin_batch')
--> 240 self.pred = self.model(*self.xb); self('after_pred')
241 if len(self.yb) == 0: return
242 self.loss = self.loss_func(self.pred, *self.yb); self('after_loss')
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)
/usr/local/lib/python3.6/dist-packages/fastai2/layers.py in forward(self, x)
374 for l in self.layers:
375 res.orig = x
--> 376 nres = l(res)
377 # We have to remove res.orig to avoid hanging refs and therefore memory leaks
378 res.orig = None
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)
/usr/local/lib/python3.6/dist-packages/fastai2/layers.py in forward(self, x)
389 "Merge a shortcut with the result of the module by adding them or concatenating them if `dense=True`."
390 def __init__(self, dense:bool=False): self.dense=dense
--> 391 def forward(self, x): return torch.cat([x,x.orig], dim=1) if self.dense else (x+x.orig)
392
393 #Cell
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 224 and 1205 in dimension 2 at /pytorch/aten/src/THC/generic/THCTensorMath.cu:71
In short seems to be a bug in the unet architecture I believe. Actually it seems to be an issue with the call to the sizes, my databunch seems to not be resizing accordingly.
size = dbunch.train_ds[0][0].size
gave me (1205,800)
however the call to size = dbunch.one_batch()[0].shape[-2:]
does work correctly, which would then tell me that my item transform wasn’t applied in time for the unet to grab it?