Vision: incorrect aspect ratio in RandomResizedCrop

Created on 5 Oct 2018  路  8Comments  路  Source: pytorch/vision

https://github.com/pytorch/vision/blob/86001a871d3335046e2dca7715d9babf73e6956f/torchvision/transforms/transforms.py#L565
Inverted aspect ratio may be out of the range specified.
For example, RandomResizedCrop.get_params(img, ratio=(0.75,0.75)) can return something like (41, 17, 153, 204) where aspect ratio is 1.33.

bug needs discussion

All 8 comments

Looks like you are right.
But I don't think it's simply a matter of removing the line you pointed out, because the sampling will not be uniform over the aspect ratios.
To give an example of why, imagine that we use ratios of 0.1 and 10. Randomly sampling over this interval will generally lead to samples with aspect ratio > 1, while one would expect it to be sampling with the same probability both cases (and that's why we have the line you mentioned).

How about

if random.random() < 0.5 and min(ratio) <= 1/aspect_ratio <= max(ratio):
    w, h = h, w

@fmassa what do u think about the proposed solution ?

The solution you implemented prevents obviously invalid crops from being generated, but the distribution is uneven. Actually, the best way to deal with it is to use a uniform distribution _on a logarithmic scale_.
This automatically handles symmetry (logarithm of a reciprocal is a negative) and provides nice, smooth distributions and a sound mathematical basis.
Also, the mean of the distribution is the geometric mean of the ends of the interval (a, b):
exp( (log(a) + log(a)) / 2) = exp( log(ab) / 2) = exp(ab)^(1/2) = sqrt(ab)

@mzient would you be able to send a PR improving this?

I'll have to clear it with the company, but otherwise I would be glad to do so.

Let me know if it would be complicated, and I can make a PR myself

See: #799

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ibtingzon picture ibtingzon  路  3Comments

iacolippo picture iacolippo  路  4Comments

varagrawal picture varagrawal  路  3Comments

bodokaiser picture bodokaiser  路  3Comments

Abolfazl-Mehranian picture Abolfazl-Mehranian  路  3Comments