Error looping through data loader: Trying to resize storage that is not resizable

username: Gregory_Kondas
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b9a252951074a4ed02aeef4007fad92c181df6f2.png" data-download-href="https://discuss.pytorch.org/uploads/default/b9a252951074a4ed02aeef4007fad92c181df6f2" title="Screenshot_2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b9a252951074a4ed02aeef4007fad92c181df6f2.png" alt="Screenshot_2" data-base62-sha1="quc6BF43hWZXcjlvYIfOCEnN1aq" width="690" height="213" data-dominant-color="F0D0D0"><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">Screenshot_2</span><span class="informations">991×307 20.1 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>Here is my custom dataset:</p>
<p>class BirdsDataset(Dataset):<br>
“”“Dataset class for Bird images.”“”</p>
<pre><code>def __init__(self,partition):
    super().__init__()
    
    np.random.seed(42)
    random.seed(42)
    imgPath = "./CUB_200_2011/"
    classes_path = os.path.join(imgPath,"classes.txt")
    self.partition = partition
    
    #Create lookup table for img_id --&gt; class
    id_to_class_data = pd.read_csv(classes_path,sep=' ', names=['img_id', 'class'])
    self.id_lookup = {}
    for i, row in id_to_class_data.iterrows():
        self.id_lookup[row["img_id"]]= row["class"].split('.')[1]

    self.X, self.y = self._load_data()

#Lookup table getter function
def lookup(self,img_id):
    return self.id_lookup[img_id]
    

#Resizes shortest axis to become 256
# Example 1: 500x300 --&gt; 500x256 
# Example 2: 300x500 --&gt; 256x500 
def resizer(self,img):
    width, height = img.size
    if(height&lt;width):
        return img.resize((width,256))
    else:
        return img.resize((256,height))
#Center crops image along longer axis
# Example 1: 500x256 --&gt; 256x256 (center cropped)
# Example 2: 256x500 --&gt; 256x256 (center cropped)
def cropper(self,img):
    width, height = img.size
    if width &lt; height:
        extra_height = (height-256)/2
        return img.crop((0,extra_height,256, height-extra_height))
    elif height &lt; width:
        extra_width = (width-256)/2
        return img.crop((extra_width,0, width - extra_width, 256))
    return img
def __len__(self):
    """Return size of dataset."""
    return len(self.X)

def __getitem__(self, idx):
    """Return (image, label) pair at index `idx` of dataset."""
    return torch.from_numpy(np.array(self.X[idx])), torch.tensor(self.y[idx])
def _load_data(self):
    """Load a single data partition from file."""
    imgPath = "./CUB_200_2011/"
    images_path = os.path.join(imgPath,"images.txt")
    num = 11788
    
    imgPath = "./CUB_200_2011/"
    images_path = os.path.join(imgPath,"images.txt")
    labels_path = os.path.join(imgPath,"image_class_labels.txt")
    #imgPaths:= nx2: id, fileName
    img_paths = pd.read_csv(images_path, sep=' ',names=["img_id","path"])
    #img labels
    image_class_labels = pd.read_csv(labels_path,sep=' ', names=['img_id', 'y'])

    train_test_split = pd.read_csv(os.path.join(imgPath,'train_test_split.txt'),
                                   sep=' ', names=['img_id', 'is_training_img'])
    #combined paths and labels by id
    data = img_paths.merge(image_class_labels, on='img_id')
    data = data.merge(train_test_split, on='img_id')
    data = data.set_index("img_id")

    if self.partition not in ["train", "val", "test"]:
        raise ValueError("Partition {} does not exist".format(partition))

    if self.partition == "train":
        self.curr = data[data["is_training_img"] == 1]
    else:
        self.test_data = data[data["is_training_img"] == 0]
        if self.partition == "val":
            self.curr = self.test_data.iloc[:int(self.test_data.shape[0]/2),:]
        else:
            self.curr = self.test_data.iloc[int(self.test_data.shape[0]/2):,:]

    parentPath = os.path.join(imgPath,"images")
    #Create X &amp; y output
    X, y = [], []
    for i, row in self.curr.iterrows():
        label = row["y"]
        img = Image.open(os.path.join(parentPath, row["path"]))
        img = self.resizer(img)
        img = self.cropper(img)
        X.append(img)
        y.append(row["y"])
    print(len(X),len(y))

    return np.array(X, dtype=object), np.array(y)
</code></pre>
<p>I then run this code to create a dataset and data-loader instance</p>
<p>va_dataset = BirdsDataset(“val”)<br>
loader = DataLoader(va_dataset, batch_size=16, shuffle=False, num_workers=10, drop_last=True)</p>
<p>Then upon looping through (as shown below) the error message appears</p>
<p>for i, (X,y) in enumerate(loader):<br>
print(“X type:”,len(X))<br>
print(“y type:”, len(y))<br>
print(i)</p>