How to make my label(y).shape = torch.Size([ ])

Hi,
My dataset frame and label sizes are below what i want to understand is how to make label(y)size = torch.Size([ ]) currently the code outputs Frame(x) shape :: torch.Size([32, 3, 224, 224]) Label(y) shape :: torch.Size([32])

This is my code:

def getitem(self, idx):

    if torch.is_tensor(idx):
        idx = idx.tolist()
    frames = []
    for fr in range(idx, idx+self.seq_len):  # frames from idx to idx+seq_len
        img_name = os.path.join(self.frames_list.iloc[fr, 0])  # path to image
        image = Image.open(img_name)
        image = image.resize((self.img_width, self.img_height))

        if self.transform:
            image = self.transform(image)
        frames.append(image)

    frames = torch.stack(frames)
    frames = frames.permute(0, 1, 2, 3)
    frames = torch.squeeze(frames, dim=1)
    frames = (frames-torch.mean(frames))/torch.std(frames)*255
    lab = np.array(self.frames_list.iloc[idx:idx + self.seq_len, 1])
    labels = torch.tensor(lab, dtype=torch.float)
    #labels = torch.empty(lab, dtype=torch.float)
    #labels = torch.zeros(lab, dtype=torch.float)
    sample = (frames, labels)
    return sample 

You can index the label tensor to create a scalar:

label = torch.randint(0, 10, (32,))
print(label.shape)
# torch.Size([32])
print(label[0].shape)
# torch.Size([])
1 Like