How to crop image and mask in patches?

mgloria: <p>I am using the classic CamVid dataset (<a href="http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamSeq01/" rel="nofollow noopener">http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamSeq01/</a>) for object segmentation. It is basically a dataset with videoframes (think of it just as images) and their corresponding segmentation mask.</p>
<p>I would like now to try something else: I would like to crop each frame and its segmentation mask in e.g. 100 patches centered at random positions from the image with a given size (e.g. 224*224) and save the results in the corresponding image / label folders. Note that both the image and the label have to be cropped in the same way for it to work. Does anybody know how to achieve this with few lines of code?</p>

ptrblck: <p>You could use the functional API of <code>torchvision.transforms</code> as described in <a href="https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606/7">this post</a>.</p>

mgloria: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/31e6197819979c02e25346cafb8b11b59ac03269.png" data-download-href="https://discuss.pytorch.org/uploads/default/31e6197819979c02e25346cafb8b11b59ac03269" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/3/31e6197819979c02e25346cafb8b11b59ac03269_2_690x276.png" alt="image" data-base62-sha1="77qnG9ne7wz6FTj1vw63Wxxyl4Z" width="690" height="276" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/3/31e6197819979c02e25346cafb8b11b59ac03269_2_690x276.png, https://discuss.pytorch.org/uploads/default/optimized/2X/3/31e6197819979c02e25346cafb8b11b59ac03269_2_1035x414.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/3/31e6197819979c02e25346cafb8b11b59ac03269_2_1380x552.png 2x" data-dominant-color="F5F5F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1642×657 137 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a>  RandomResizeCrop works but RandomCrop returns me an error…</p>

ptrblck: <p>Your code snippet won’t crop the images, as</p>
<ul>
<li>you have an assert statement, which checks both image shapes are equal</li>
<li>you calling <code>get_params</code> with the same input and output size, thus it will be static and return the parameters to use the complete image (<a href="https://github.com/pytorch/vision/blob/19315e313511fead3597e23075552255d07fcb2a/torchvision/transforms/transforms.py#L437" rel="nofollow noopener">line of code</a>)</li>
</ul>

mgloria: <p>but <a class="mention" href="/u/ptrblck">@ptrblck</a>  both images (mask and image) do have the same size (for this reason the assert does not fail). Moreover, get_params is getting the new size passed (224, 320)… isn’t it?</p>

ptrblck: <p>You should pass <code>inputSize</code> to the transformation to samples from <code>[224, 320]</code>.</p>