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

The code is list below

import os
import torch
import numpy as np
from torch.autograd import Variable
from torch import nn
import torch.nn.functional as F
from 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):
    label =tfs.FixedCrop(*rect)(label)
    return data,label"/home/ypl/dataset/VOC/VOCdevkit/VOC2012/JPEGImages/2007_000032.jpg")"/home/ypl/dataset/VOC/VOCdevkit/VOC2012/SegmentationClass/2007_000032.png")"/home/ypl/dataset/VOC/VOCdevkit/VOC2012/JPEGImages/2007_000645.jpg")"/home/ypl/dataset/VOC/VOCdevkit/VOC2012/SegmentationClass/2007_000645.png")

_, figs = plt.subplots(2, 2, figsize=(10, 8))

I got an error like this

Traceback (most recent call last):
  File "", line 38, in <module>
  File "", line 19, in random_crop
TypeError: 'Image' object is not iterable

I have no idea to do, can anybody help me? I just want to random crop the pic and show them


RandomCrop does not return a tuple. It only crops the image and returns a Image object containing the cropped image. If you want the exact crop indices, you need to first create a RandomCrop object, then use its static method .get_params(image, output_size) which returns starting indices i and j and and the length of height and width th and tw respectively. After this, you can call torch.nn.functional.crop(img, i, j, th, tw) to get cropped image.

I think my explanation was a little bit complex, in this case you can read the source of RandomCrop, but I have just added the main methods here:

    def get_params(img, output_size):
        """Get parameters for ``crop`` for a random crop.

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

            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):
            img (PIL Image): Image to be cropped.

            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] < 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] < 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)


1 Like

thank you so much.
I changed the code like this


it works.
Thanks again for your reply