Skip to content

ColorJitter's get_params doesn't have same arguments as __init__ #905

@mdfirman

Description

@mdfirman

According to the docstring, ColorJitter.get_params has arguments same as that of __init__.

However, while ColorJitter.__init__ can accept tuples or floats as parameters, get_params only accepts tuples:

In [2]: from torchvision.transforms import ColorJitter

In [3]: ColorJitter((0.8, 0.9), (0.8, 0.9), (0.7, 0.9), (0.1, 0.2))
Out[3]: ColorJitter(brightness=(0.8, 0.9), contrast=(0.8, 0.9), saturation=(0.7, 0.9), hue=(0.1, 0.2))

In [4]: ColorJitter(0.9, 0.9, 0.9, 0.1)
Out[4]: ColorJitter(brightness=[0.09999999999999998, 1.9], contrast=[0.09999999999999998, 1.9], saturation=[0.09999999999999998, 1.9], hue=[-0.1, 0.1])

In [5]: ColorJitter.get_params((0.8, 0.9), (0.8, 0.9), (0.7, 0.9), (0.1, 0.2))
Out[5]:
Compose(
    Lambda()
    Lambda()
    Lambda()
    Lambda()
)

In [6]: ColorJitter.get_params(0.9, 0.9, 0.9, 0.1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-32bc75c83828> in <module>
----> 1 ColorJitter.get_params(0.9, 0.9, 0.9, 0.1)

~/anaconda3/envs/pytorch1/lib/python3.6/site-packages/torchvision/transforms/transforms.py in get_params(brightness, contrast, saturation, hue)
    805
    806         if brightness is not None:
--> 807             brightness_factor = random.uniform(brightness[0], brightness[1])
    808             transforms.append(Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))
    809

TypeError: 'float' object is not subscriptable

This bug means that workarounds are required to support earlier and later torchvision versions.

I feel that get_params should call self._check_input on the input arguments, just like __init__. I don't however know if this would have any knock-on input on anything else.

(This issue I think was introduced around here.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions