Errors and corrections in 005_dogs_cats.ipynb


(Wenfeng Feng) #1

Hi, I found several errors when I running 005_dogs_cats.ipynb.

When I run into this cell:

(x,y) = next(iter(data.valid_dl))

_,axs = plt.subplots(4,4,figsize=(12,12))
for i,ax in enumerate(axs.flatten()): show_image(data_denorm(x[i].cpu()), ax)

I got the following error messages:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-5de730d345ff> in <module>()
----> 1 (x,y) = next(iter(data.valid_dl))
      2 
      3 _,axs = plt.subplots(4,4,figsize=(12,12))
      4 for i,ax in enumerate(axs.flatten()): show_image(data_denorm(x[i].cpu()), ax)

/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    334                 self.reorder_dict[idx] = batch
    335                 continue
--> 336             return self._process_next_batch(batch)
    337 
    338     next = __next__  # Python 2 compatibility

/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
    355         self._put_indices()
    356         if isinstance(batch, ExceptionWrapper):
--> 357             raise batch.exc_type(batch.exc_msg)
    358         return batch
    359 

ValueError: Traceback (most recent call last):
  File "/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/.../fastai_v1/dev_nb/nb_002.py", line 294, in __getitem__
    return apply_tfms(self.tfms, x, **self.kwargs), y
  File "/.../fastai_v1/dev_nb/nb_002.py", line 418, in apply_tfms
    else:               x = tfm(x)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 267, in __call__
    return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x
  File "/.../fastai_v1/dev_nb/nb_002.py", line 222, in __call__
    if args: return self.calc(*args, **kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 226, in calc
    if tfm._wrap: return getattr(x, tfm._wrap)(tfm.func, *args, **kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 147, in pixel
    self.px = func(self.px, *args, **kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 126, in px
    self.refresh()
  File "/.../fastai_v1/dev_nb/nb_002.py", line 119, in refresh
    self._px = grid_sample(self._px, self.flow, **self.sample_kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 358, in grid_sample
    return F.grid_sample(x[None], coords, mode=mode, padding_mode=padding_mode)[0]
  File "/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/nn/functional.py", line 2092, in grid_sample
    raise ValueError("padding_mode needs to be 'zeros' or 'border', but got {}".format(padding_mode))
ValueError: padding_mode needs to be 'zeros' or 'border', but got reflection

I found this error is because function F.grid_sample doesn’t support argument option padding_mode='reflect', so I replaced the argument of function grid_sample in file nb_002.py to `padding_mode=‘zeros’.

But after that, the same cell got the following error messages:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-98-5de730d345ff> in <module>()
----> 1 (x,y) = next(iter(data.valid_dl))
      2 
      3 _,axs = plt.subplots(4,4,figsize=(12,12))
      4 for i,ax in enumerate(axs.flatten()): show_image(data_denorm(x[i].cpu()), ax)

/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    334                 self.reorder_dict[idx] = batch
    335                 continue
--> 336             return self._process_next_batch(batch)
    337 
    338     next = __next__  # Python 2 compatibility

/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
    355         self._put_indices()
    356         if isinstance(batch, ExceptionWrapper):
--> 357             raise batch.exc_type(batch.exc_msg)
    358         return batch
    359 

TypeError: Traceback (most recent call last):
  File "/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/.../Anaconda3-5.1/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 106, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/.../fastai_v1/dev_nb/nb_002.py", line 294, in __getitem__
    return apply_tfms(self.tfms, x, **self.kwargs), y
  File "/.../fastai_v1/dev_nb/nb_002.py", line 418, in apply_tfms
    else:               x = tfm(x)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 267, in __call__
    return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x
  File "/.../fastai_v1/dev_nb/nb_002.py", line 222, in __call__
    if args: return self.calc(*args, **kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 226, in calc
    if tfm._wrap: return getattr(x, tfm._wrap)(tfm.func, *args, **kwargs)
  File "/.../fastai_v1/dev_nb/nb_002.py", line 147, in pixel
    self.px = func(self.px, *args, **kwargs)
TypeError: crop_pad() missing 1 required positional argument: 'size'

So I changed the following function:

#export
def get_transforms(do_flip=False, flip_vert=False, max_rotate=0., max_zoom=1., max_lighting=0., max_warp=0.,
                   p_affine=0.75, p_lighting=0.5, xtra_tfms=None):
    res = [rand_crop()]
    if do_flip:    res.append(dihedral() if flip_vert else flip_lr(p=0.5))
    if max_warp:   res.append(symmetric_warp(magnitude=(-max_warp,max_warp), p=p_affine))
    if max_rotate: res.append(rotate(degrees=(-max_rotate,max_rotate), p=p_affine))
    if max_zoom>1: res.append(rand_zoom(scale=(1.,max_zoom), p=p_affine))
    if max_lighting:
        res.append(brightness(change=(0.5*(1-max_lighting), 0.5*(1+max_lighting)), p=p_lighting))
        res.append(contrast(scale=(1-max_lighting, 1/(1-max_lighting)), p=p_lighting))
    #       train                   , valid
    return (res + listify(xtra_tfms), [crop_pad()])  

to:

#export
def get_transforms(size, do_flip=False, flip_vert=False, max_rotate=0., max_zoom=1., max_lighting=0., max_warp=0.,
                   p_affine=0.75, p_lighting=0.5, xtra_tfms=None):
    res = [rand_crop(size=size)]
    if do_flip:    res.append(dihedral() if flip_vert else flip_lr(p=0.5))
    if max_warp:   res.append(symmetric_warp(magnitude=(-max_warp,max_warp), p=p_affine))
    if max_rotate: res.append(rotate(degrees=(-max_rotate,max_rotate), p=p_affine))
    if max_zoom>1: res.append(rand_zoom(scale=(1.,max_zoom), p=p_affine))
    if max_lighting:
        res.append(brightness(change=(0.5*(1-max_lighting), 0.5*(1+max_lighting)), p=p_lighting))
        res.append(contrast(scale=(1-max_lighting, 1/(1-max_lighting)), p=p_lighting))
    #       train                   , valid
    return (res + listify(xtra_tfms), [crop_pad(size=size)])  

And changed the following cell:

size=224

tfms = get_transforms(do_flip=True, max_rotate=10, max_zoom=1.2, max_lighting=0.3, max_warp=0.15)
tds = transform_datasets(train_ds, valid_ds, tfms, size=size)
data = DataBunch.create(*tds, bs=64, num_workers=8, tfms=data_norm)

TO:

size=224

tfms = get_transforms(size, do_flip=True, max_rotate=10, max_zoom=1.2, max_lighting=0.3, max_warp=0.15)
tds = transform_datasets(train_ds, valid_ds, tfms) #, size=size
data = DataBunch.create(*tds, bs=64, num_workers=8, tfms=data_norm)

After all of them, I got the following seemingly correct result:


#2

Be careful that you need to be on pytorch master to have the latest grid_sample function when running this notebook. We specifically had the pytorch team change it to do the data augmentation as we wanted it (explained in this topic) so we asked them to support different kinds of padding_mode and interpolations, as well as make it more efficient (this last change has just been merged into master I believe).


(Wenfeng Feng) #3

Thank you so much. I don’t catch up with the latest version of the F.grid_sample function.

How about the other messages that crop_pad() missing 1 required positional argument: 'size'? Is this also because I missed some new information?

Thanks.


#4

Haven’t followed the lastest developments in details, so this part may be a bug. @jeremy ?


(Jeremy Howard) #5

The 005 notebook runs fine with the current version of pytorch. Please wait until tomorrow (an important PR was just merged) and use the nightly, or build from source.


(Wenfeng Feng) #6

Cool, thank you for your excellent work, I will follow the new version.


(shubhamitra) #7

I installed fastai v1 today and get the same error for padding_mode and size .
I don’t see the changes required in the official github too ,is the fix for padding_mode not included in the official v1 ?


#8

If you get this error, it’s probably because you don’t have pytorch v1.0.


(Thomas) #9

isn’t the lastest pytorch 0.4.1?


(Michael) #10

0.4.1 is the latest stable version but for fastai v1 you need the preview version of PyTorch 1.0, see at their website https://pytorch.org where you can select between stable and preview.


(Thomas) #11

It’s sad that fastai is not usable in kagle anymore.