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

I got an error like this

Traceback (most recent call last):
  File "vis_pic.py", line 38, in <module>
    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

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

Hi,

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.

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

Bests

1 Like

thank you so much.
I changed the code like this

data=tfs.RandomCrop((h,w))(data)

it works.
Thanks again for your reply