Skip to content
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

Avoid calling torch.rand in Transformation.forward() #3066

Closed
datumbox opened this issue Nov 30, 2020 · 1 comment
Closed

Avoid calling torch.rand in Transformation.forward() #3066

datumbox opened this issue Nov 30, 2020 · 1 comment

Comments

@datumbox
Copy link
Contributor

datumbox commented Nov 30, 2020

The preferred way to structure the Transformation classes is to put the initialization of random weights/params in a static get_params() method. The method should receive any hyper parameter necessary for the sampling and it should return all the necessary random variables. This method should be called by forward() during the transformation process. This is an example of how this would look:

@staticmethod
def get_params(img: Tensor, output_size: Tuple[int, int]) -> Tuple[int, int, int, int]:

i, j, h, w = self.get_params(img, self.size)

Unfortunately many forward() methods call directly torch.rand. Here are a few examples:

def forward(self, img):
if self.p < torch.rand(1):

if torch.rand(1) < self.p:

if torch.rand(1) < self.p:

if torch.rand(1) < self.p:

if torch.rand(1) < self.p:

if torch.rand(1) < self.p:

There might be potentially others. We should refactor the codebase so that all of the above calls happen within a static get_params() method. See #3065 RandomInvert for an example on how to structure it.

cc @vfdev-5

@datumbox datumbox changed the title Refactor Transformations to avoid calling torch.rand on forward() Avoid calling torch.rand in Transformation.forward() Nov 30, 2020
@datumbox
Copy link
Contributor Author

Closing this as we need to rethink the plan to cover for cases such as object detection. See #3286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant