data,rect=transforms.RandomCrop((h,w))(data) I get an error,somebody help me?

username: xyry
cooked: <p>The code is list below</p>
<pre><code class="lang-python">import os
import torch
import numpy as np
from torch.autograd import Variable
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader
from PIL import Image
from torchvision import transforms as tfs
from datetime import datetime

import matplotlib.pyplot as plt
def random_crop(data,label,crop_size):
    h,w=crop_size
    data,rect=tfs.RandomCrop((h,w))(data)
    label =tfs.FixedCrop(*rect)(label)
    return data,label

im_show1=Image.open("/home/ypl/dataset/VOC/VOCdevkit/VOC2012/JPEGImages/2007_000032.jpg")
label_show1=Image.open("/home/ypl/dataset/VOC/VOCdevkit/VOC2012/SegmentationClass/2007_000032.png")

im_show2=Image.open("/home/ypl/dataset/VOC/VOCdevkit/VOC2012/JPEGImages/2007_000645.jpg")
label_show2=Image.open("/home/ypl/dataset/VOC/VOCdevkit/VOC2012/SegmentationClass/2007_000645.png")

_, figs = plt.subplots(2, 2, figsize=(10, 8))
crop_im1,crop_label1=random_crop(im_show1,label_show1,(200,300))
figs[0][0].imshow(crop_im1)
figs[0][0].axes.get_xaxis().set_visible(False)
figs[0][0].axes.get_yaxis().set_visible(False)
figs[0][1].imshow(crop_label1)
figs[0][1].axes.get_xaxis().set_visible(False)
figs[0][1].axes.get_yaxis().set_visible(False)
</code></pre>
<p>I got an error like this</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "vis_pic.py", line 38, in &lt;module&gt;
    crop_im1,crop_label1=random_crop(im_show1,label_show1,(200,300))
  File "vis_pic.py", line 19, in random_crop
    data,rect=transform_train(data)
TypeError: 'Image' object is not iterable
</code></pre>
<p>I have no idea to do, can anybody help me? I just want to random crop the pic and show them</p>

username: Nikronic
cooked: <p>Hi,</p>
<aside class="quote no-group" data-username="xyry" data-post="1" data-topic="88928">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/x/c57346/48.png" class="avatar"> xyry:</div>
<blockquote>
<p><code>data,rect=tfs.RandomCrop((h,w))(data)</code></p>
</blockquote>
</aside>
<p><code>RandomCrop</code> does not return a tuple. It only crops the image and returns a <code>Image</code> object containing the cropped image. If you want the exact crop indices, you need to first create a <code>RandomCrop</code> object, then use its static method <code>.get_params(image, output_size)</code> which returns starting indices <code>i</code> and <code>j</code> and and the length of height and width <code>th</code> and <code>tw</code> respectively.  After this, you can call <code>torch.nn.functional.crop(img, i, j, th, tw)</code> to get cropped image.</p>
<p>I think my explanation was a little bit complex, in this case you can read the source of <code>RandomCrop</code>, but I have just added the main methods here:</p>
<pre><code class="lang-auto">    def get_params(img, output_size):
        """Get parameters for ``crop`` for a random crop.

        Args:
            img (PIL Image): Image to be cropped.
            output_size (tuple): Expected output size of the crop.

        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
        w, h = _get_image_size(img)
        th, tw = output_size
        if w == tw and h == th:
            return 0, 0, h, w

        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
        return i, j, th, tw

    def __call__(self, img):
        """
        Args:
            img (PIL Image): Image to be cropped.

        Returns:
            PIL Image: Cropped image.
        """
        if self.padding is not None:
            img = F.pad(img, self.padding, self.fill, self.padding_mode)

        # pad the width if needed
        if self.pad_if_needed and img.size[0] &lt; self.size[1]:
            img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
        # pad the height if needed
        if self.pad_if_needed and img.size[1] &lt; self.size[0]:
            img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)

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

        return F.crop(img, i, j, h, w)
</code></pre>
<p>Bests</p>

username: xyry
cooked: <p>thank you so much.<br>
I changed the code like this</p>
<pre><code class="lang-python">data=tfs.RandomCrop((h,w))(data)

</code></pre>
<p>it works.<br>
Thanks again for your reply</p>