Skip to content

Commit

Permalink
fix both document and a bug for RandomCrop (#1389)
Browse files Browse the repository at this point in the history
* fix both document and a bug for RandomCrop

`RandomCrop` pad first and then crop, not what is said in the document or even CIFAR tutorials.

further, an error occurs with the default `pad=None`
```
>>> for i in train_data:break
... 
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/data/dataloader.py", line 450, in _worker_fn
    batch = batchify_fn([_worker_dataset[i] for i in samples])
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/data/dataloader.py", line 450, in <listcomp>
    batch = batchify_fn([_worker_dataset[i] for i in samples])
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/data/dataset.py", line 219, in __getitem__
    return self._fn(*item)
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/data/dataset.py", line 230, in __call__
    return (self._fn(x),) + args
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/block.py", line 693, in __call__
    out = self.forward(*args)
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/nn/basic_layers.py", line 55, in forward
    x = block(x)
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/block.py", line 693, in __call__
    out = self.forward(*args)
  File "/home/neutron/.local/lib/python3.8/site-packages/gluoncv/data/transforms/block.py", line 75, in forward
    return image.random_crop(nd.array(x_pad), *self._args)[0]
UnboundLocalError: local variable 'x_pad' referenced before assignment
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/neutron/.local/lib/python3.8/site-packages/mxnet/gluon/data/dataloader.py", line 505, in __next__
    batch = pickle.loads(ret.get(self._timeout))
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 771, in get
    raise self._value
UnboundLocalError: local variable 'x_pad' referenced before assignment
```



This PR is intend to fix both the document and the BUG which caused `pad` cannot be optional.

* make pylint happy.

make pylint happy.

* happy-2

happy-2

* remove monkey patch

I just think monkey patch may goes faster than the useless switch in the forward step

* make checkers happy
  • Loading branch information
Neutron3529 authored Aug 10, 2020
1 parent 481019b commit 1e90f7e
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions gluoncv/data/transforms/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RandomCrop(Block):
Inputs:
- **data**: input tensor with (Hi x Wi x C) shape.
Outputs:
- **out**: output tensor with ((H+2*pad) x (W+2*pad) x C) shape.
- **out**: output tensor with (size[0] x size[1] x C) or (size x size x C) shape.
"""

def __init__(self, size, pad=None, interpolation=2):
Expand All @@ -62,18 +62,13 @@ def __init__(self, size, pad=None, interpolation=2):
if isinstance(size, numeric_types):
size = (size, size)
self._args = (size, interpolation)
if isinstance(pad, int):
self.pad = ((pad, pad), (pad, pad), (0, 0))
else:
self.pad = pad

self.pad = ((pad, pad), (pad, pad), (0, 0)) if isinstance(pad, int) else pad
def forward(self, x):
if self.pad:
x_pad = np.pad(x.asnumpy(), self.pad,
mode='constant', constant_values=0)

return image.random_crop(nd.array(x_pad), *self._args)[0]

return image.random_crop(nd.array(
np.pad(x.asnumpy(), self.pad, mode='constant', constant_values=0)), *self._args)[0]
else:
return image.random_crop(x, *self._args)[0]

class RandomErasing(Block):
"""Randomly erasing the area in `src` between `s_min` and `s_max` with `probability`.
Expand Down

0 comments on commit 1e90f7e

Please sign in to comment.