-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Make transforms.functional_tensor
functions differential w.r.t. their parameters
#4995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Make operations differential w.r.t. hyper-parameters, which is extremely helpful for AutoAugment search.
💊 CI failures summary and remediationsAs of commit 9ce5d09 (more details on the Dr. CI page):
🕵️ 4 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
Job | Step | Action |
---|---|---|
Run tests | 🔁 rerun | |
Build torchvision C++ distribution and test | 🔁 rerun |
1 job timed out:
cmake_linux_gpu
🚧 1 fixed upstream failure:
These were probably caused by upstream breakages that were already fixed.
Please rebase on the viable/strict
branch (expand for instructions)
If your commit is older than viable/strict
, run these commands:
git fetch https://github.com/pytorch/pytorch viable/strict
git rebase FETCH_HEAD
- unittest_linux_cpu_py3.8 on Dec 09 from 10:35am to 11:09am (d716c42 - 8aa3174)
This comment was automatically generated by Dr. CI (expand for details).
Please report bugs/suggestions to the (internal) Dr. CI Users group.
@ain-soph Thanks for opening this. What you propose is interesting but it might be worth discussing the API prior writing the PR. This is to avoid a situation where you spend lots of time working on the feature and not being able to merge due to some unforeseen limitation (JIT might give us headaches for example). Since you already opened a PR, we can discuss this here as well if you prefer. @vfdev-5 Please share any thoughts on the endeavour. Concerning the autoaugment behaviour, I would like to understand more why you say the behaviour is inconsistent.
My understanding is that the lines at question always look for the min/max across H and W no matter the input. The next 2 lines ensure that we won't divide by 0. Could you provide an example that show-cases the issue? |
import torch
import torchvision.transforms.functional as F
a=torch.rand(1,3,32,32)
a=a/2+0.3
(F.autocontrast(a)[0]==F.autocontrast(a[0])).all() # True (because eq_idxs are empty)
a[0,2]=0.7 # set a channel to be constant
(F.autocontrast(a)[0]==F.autocontrast(a[0])).all() # False We should expect it to be True. Because A potential fix might be unsqueeze when Red channel is constant while Green and Blue channels are not,
|
Almost done (except JIT, which I'm totally unfamiliar). But there is one issue about the vision/torchvision/transforms/functional_tensor.py Lines 694 to 704 in 47281bb
I have debugged and find However, it relies on I tried |
@ain-soph I agree with @datumbox that prior to the PR it would be better to open a feature request issue and give motivations about the requested feature. According to the code in the PR, your idea is to propagate gradients through the transformations and make sure that arguments could be of type As for grid_sample op and grad propagation for grid, this is done by pytorch. For example: import torch
from torch.nn.functional import grid_sample
img = torch.rand(2, 3, 4, 4, requires_grad=True)
grid = torch.rand(2, 6, 6, 2, requires_grad=True)
out = grid_sample(img, grid, align_corners=False)
assert out.requires_grad
out.mean().backward()
img.grad is not None, grid.grad is not None
> True, True |
@vfdev-5 Thanks for your response! The backward compatibility is guaranteed for sure. I only worried that the codes might be a little diffuse (e.g., the new added You are right, I'll later propose a feature request issue linked to this PR. I'm currently doing some extensive research based on Faster Autoaugment, and I think adding the backward feature wrt magnitudes would be more convenient and support future research as well. |
transforms.functional_tensor
functions differential w.r.t. their parameters
|
@ain-soph due to historical reasons it was first implemented rotate and then affine op. Rotate API is not completely redundant compared to affine op, as rotate has flag
Yes, I think default values could be helpful in this case. |
yes, it is expected (https://github.com/pytorch/pytorch/blob/ca945d989ac56e0070f82fbe657d078610043ea3/aten/src/ATen/native/cpu/GridSamplerKernel.cpp#L806). Also, take a look at section 3.3 "Differentiable Image Sampling" of this paper where they derive forward pass formulas for nearest and bilinear modes and how to compute grid gradients for bilinear case.
I think we should not change default value to cover particular case of non-zero grid grads. For you use-case you can specify interpolation mode and thus obtain will obtain non-zero grads. I could prototype a bit on making F.rotate differentiable and looks like it could be rather easily done. JIT tests currently is not passing due to addition type support but IMO this could be solved. vfdev-5@1214e6f |
@ain-soph let's make the following to avoid debugging on the CI, I'll finish up the code for |
@vfdev-5 Oops, sorry if it causes any trouble when I debug... I try to debug locally, but it seems I have to do some extra work from source code to final binary version. I'll appreciate if you can help me with JIT issues. I have no experience with JIT previously. I'm still facing a ton of errors, even after I have fixed a bunch of them. |
@vfdev-5 I've get my local environment ready for debugging, and it seems all tests have passed except the new differentiable test that you added. Here are the only 2 issues remaining:
I wonder if it's necessary to make the type of those arguments to be |
And another CI error that I can't reproduce at local environment (These tests passed sucessfully at my local machine):
I'll try to add more test scenarios after solving the current issues. |
close in favor of #5110 |
Linked Issue: #5000
Make operations in
torchvision.transforms.functional_tensor
differential w.r.t. hyper-parameters, which is helpful for Faster AutoAugment search (hyper-parameters are learnable parameters via backward).Some operations are not differential (e.g., Posterize), which might require users to write their own implementations.
Todo List:
torchvision.transforms.functional
)torchvision.transforms.functional
min==max
#4999 )autocontrast
seems to be incorrect. Need further check.vision/torchvision/transforms/functional_tensor.py
Lines 935 to 945 in 18cf5ab
The input Image could be either
(N, C, H, W)
or(C, H, W)
, which makes L940 to rely on different dimensions. This is inconsistent behavior for these 2 cases.Other implementations as reference:
https://github.com/moskomule/dda/blob/master/dda/functional.py#L203
https://pillow.readthedocs.io/en/stable/_modules/PIL/ImageOps.html#autocontrast
If any maintainer thinks this worth doing, I'll continue to work on it.