How to create a dataloader for sequence of images as an input to CNN+LSTM?

Hello all,

I have to implement CNN+LSTM for crop classification on image sequence data. I need to generate a dataloader to return list of sequence of images. Lets say, if my sequence length is 10 and batch size is 8, it should return 8 lists of 10 images.

Need help!

Many thanks in advance!

1 Like

Hi @tejasri19, I hope you have already figured out the solution, but here is my solution.

I am assuming the following

  1. You have the order images
  2. You are trying to learn a spatio-temporal model for classification using ConvLSTM

What’s the format of the labels? Do you need labels for all images in a batch?
If you have 10 images in a batch and do you need label for each corresponding image?

I have worked on a similar problem, and I am attaching my code below. The below code produces the labels for all images in a batch, but you can easily modify it as per requirements.

import torch, torch, torchvision
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
from glob import glob
torch.manual_seed(2000)
import torchvision.transforms.functional as TF
import pandas as pd


class SeqImageDataset(Dataset):
    def __init__(self, dataset_path, label_path, img_transforms=None, sequence_length=3):
        
        self.img_transforms = img_transforms
        self.sequence_length = sequence_length
        self.labels = pd.read_csv(label_path)
        self.files = sorted(glob(f'{dataset_path}/*.png'))
        print(f'Loaded {len(self.files)} images from {dataset_path} dataset')
        
    def __len__(self):
        return len(self.files) - self.sequence_length
    

    def __getitem__(self, idx):
        images = [
            torchvision.io.read_image(self.files[idx+i], mode=torchvision.io.ImageReadMode.GRAY) for i in range(self.sequence_length)
        ]
        images = torch.stack(images, dim=0)
        labels = self.labels.iloc[idx : idx + self.sequence_length]            

        if self.img_transforms:
            images = self.img_transforms(images)
            
        return images, labels
    


trainset = SeqImageDataset('data/train', 'data/train.csv')
train_loader = DataLoader(trainset, batch_size=8, shuffle=True)
    
    
1 Like