Skip to content

[add] A PairRandomCrop for both input and target. #221

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

Closed
wants to merge 5 commits into from

Conversation

Time1ess
Copy link

@Time1ess Time1ess commented Aug 8, 2017

RandomCrop does not support crop both image and target at same position, each time we call RandomCrop it will generate a new position which is not what we want.
This PairRandomCrop will remember the crop position of the image and use this same position to crop the target(vice versa).
It follows this idea:

Crop in the same worker process is sequential. Crop happens in different workers should not bother each other.

A dict is used to remember the crop position of the image, key is the os.getpid() and value is the position.
So each time we crop a image, PairRandomCrop will generate a new position, when it comes to the target, it will pop and reuse the position.

When we crop images in Semantic Segmentation and other related work,
we want to crop both input image and its target image, however,
RandomCrop generates a new random positon each time being called,
PairRandomCrop instead, will use the same position twice.
@fmassa
Copy link
Member

fmassa commented Aug 25, 2017

Hi,
Thanks for the PR. We are discussing in #230 possible ways of improving the transforms in torchvision. It would be good to have your opinion on that, so that we can discuss on the best approach.

@alykhantejani
Copy link
Contributor

Hi, this functionality can now be achieved in your own dataset using the new functional interface in torchvision.transforms i.e.

from torchvision.transforms import crop, RandomCrop
class MyDataset(Dataset):
    def __init__(self, crop_size):
         super(Dataset, self).__init__()
         self.crop_size = crop_size

    def __getitem__(self, idx):
        img = self.get_img(idx)
        target = self.get_target(idx)
        i, j, h, w = RandomCrop.get_params(img, self.crop_size)
        img = crop(img, i, j, h, w)
        target = crop(img, i, j, h, w)
        return img, target

rajveerb pushed a commit to rajveerb/vision that referenced this pull request Nov 30, 2023
* adding code for data generation

* fix imports in run_expansion

* more fixes, and added readme

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

Successfully merging this pull request may close these issues.

3 participants