RandomAdjustSharpness gives IndexError: tuple index out of range

While using RandomAdjustSharpness, my code throws the following error - IndexError: tuple index out of range

Here is my code -

import math, random
from sklearn.datasets import load_sample_images
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
import torchvision.transforms as transforms
import matplotlib.pyplot as plt


def random_crop(imgs):


    imgs = torch.tensor(imgs)
    change = torch.nn.Sequential(
        transforms.RandomCrop(427),
        transforms.RandomAdjustSharpness(1, p=1)
    )
    imgs = change(imgs).numpy()
    return imgs

###Obtaining a random image and preprocessing it!##
dataset = load_sample_images()
first_img_data = dataset.images[0]
first_img_data  = first_img_data.reshape(-1, 427, 640)
first_img_data = first_img_data[1, :, :]
#first_img_data = first_img_data[0:84, 0:84].reshape(-1, 84,84)
# first_img_data = torch.tensor(first_img_data)
plt.figure()
plt.imshow(np.squeeze(first_img_data))

foo = random_crop(first_img_data)
plt.figure()

plt.imshow(np.squeeze(foo))
plt.show()

I cannot reproduce the error using:

def random_crop(imgs):
    imgs = torch.tensor(imgs)
    change = torch.nn.Sequential(
        transforms.RandomCrop(427),
        transforms.RandomAdjustSharpness(1, p=1)
    )
    imgs = change(imgs).numpy()
    return imgs


first_img_data = torch.randn(3, 500, 500)
foo = random_crop(first_img_data)
plt.figure()
plt.imshow(np.squeeze(foo.transpose(1, 2, 0)))
plt.show()
1 Like

Oh, that’s surprising as I thought that the example put up could be directly executed (it uses scikit-learn to import images). However, I got the answer from here - pytorch - RandomAdjustSharpness gives IndexError: tuple index out of range - Stack Overflow

Thanks a lot for your help :slight_smile: