<filename>notebooks/dataset.py
# =============================================================================
# Organization  : TUM Campus Straubing
# Institution   : Bioinformatics
# Created By    : <NAME>
# Created Date  : Dienstag 11 30 15:00 GMT 2021
# Latest Update : DayName Month Day hh:mm:ss GMT YYYY
# =============================================================================
"""
Plant dataset definition used for pytorch data loader.
"""
# =============================================================================
# Imports
# =============================================================================
from pathlib import Path
import cv2
import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset
import albumentations as A
from albumentations.pytorch import ToTensorV2


class PlantDataset(Dataset):
    def __init__(self, root_path, split_csvs, transform=None):
        '''
        loads the dataset from a list of images and masks
        img_ls: list of image Paths to load
        labels: list of labels denoted by EPPO Code
        '''
        self.root_path = str(root_path)
        self.transform = transform
        self.img_list, self.un_labels, self.class_sample_count = self._read_multiple_csv(csv_files=split_csvs)
        print(f"Found {len(self.img_list)} image patches")

    def _read_split_csv(self, csv_file:str):
        split_df = pd.read_csv(f"{self.root_path}/data/splits/{csv_file}.csv", index_col=False)
        split_df = split_df.drop(split_df.columns[0], axis=1)
        img_list = split_df.img_path.to_list()
        
        return img_list, split_df.label

    def _read_multiple_csv(self, csv_files):
        '''
        csv_files: list of absolute paths to a csv-file generated by train_val_test_split.py
        Reads a list of images that need to be loaded in this dataset class.
        '''
        img_list = []
        labels = []
        print(f"Loading {len(csv_files)} csv files")
        for csv_file in csv_files:
            img_list_one_file, labels_one_file = self._read_split_csv(csv_file)
            img_list.append(img_list_one_file)
            labels.append(labels_one_file)

        # flatten lists
        img_list = [item for sublist in img_list for item in sublist]
        labels = [item for sublist in labels for item in sublist]
        
        unique_labels, class_sample_count = np.unique(np.array(labels), return_counts=True)
        print(f"unique labels: {unique_labels}")
        print(f"label counts: {class_sample_count}")
        # TODO: implement WeightedRandomSampler from class_sample_count
        return img_list, list(unique_labels), class_sample_count

    def __len__(self):
        return len(self.img_list)
        
    def __output_classes__(self):
        return self.un_labels

    def __getitem__(self, idx):
        label = str(self.img_list[idx]).split("/")[-1].split(".")[0].split("_")[0]
        image = cv2.imread(f"{self.root_path}/{str(self.img_list[idx])}")
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if self.transform is not None:
            augmentations = self.transform(image=image)
            image = augmentations["image"]
        target = torch.tensor(self.un_labels.index(label), dtype=torch.long) 
        
        return image, target
<filename>notebooks/dataset.py
# =============================================================================
# Organization  : TUM Campus Straubing
# Institution   : Bioinformatics
# Created By    : <NAME>
# Created Date  : Dienstag 11 30 15:00 GMT 2021
# Latest Update : DayName Month Day hh:mm:ss GMT YYYY
# =============================================================================
"""
Plant dataset definition used for pytorch data loader.
"""
# =============================================================================
# Imports
# =============================================================================
from pathlib import Path
import cv2
import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset
import albumentations as A
from albumentations.pytorch import ToTensorV2


class PlantDataset(Dataset):
    def __init__(self, root_path, split_csvs, transform=None):
        '''
        loads the dataset from a list of images and masks
        img_ls: list of image Paths to load
        labels: list of labels denoted by EPPO Code
        '''
        self.root_path = str(root_path)
        self.transform = transform
        self.img_list, self.un_labels, self.class_sample_count = self._read_multiple_csv(csv_files=split_csvs)
        print(f"Found {len(self.img_list)} image patches")

    def _read_split_csv(self, csv_file:str):
        split_df = pd.read_csv(f"{self.root_path}/data/splits/{csv_file}.csv", index_col=False)
        split_df = split_df.drop(split_df.columns[0], axis=1)
        img_list = split_df.img_path.to_list()
        
        return img_list, split_df.label

    def _read_multiple_csv(self, csv_files):
        '''
        csv_files: list of absolute paths to a csv-file generated by train_val_test_split.py
        Reads a list of images that need to be loaded in this dataset class.
        '''
        img_list = []
        labels = []
        print(f"Loading {len(csv_files)} csv files")
        for csv_file in csv_files:
            img_list_one_file, labels_one_file = self._read_split_csv(csv_file)
            img_list.append(img_list_one_file)
            labels.append(labels_one_file)

        # flatten lists
        img_list = [item for sublist in img_list for item in sublist]
        labels = [item for sublist in labels for item in sublist]
        
        unique_labels, class_sample_count = np.unique(np.array(labels), return_counts=True)
        print(f"unique labels: {unique_labels}")
        print(f"label counts: {class_sample_count}")
        # TODO: implement WeightedRandomSampler from class_sample_count
        return img_list, list(unique_labels), class_sample_count

    def __len__(self):
        return len(self.img_list)
        
    def __output_classes__(self):
        return self.un_labels

    def __getitem__(self, idx):
        label = str(self.img_list[idx]).split("/")[-1].split(".")[0].split("_")[0]
        image = cv2.imread(f"{self.root_path}/{str(self.img_list[idx])}")
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if self.transform is not None:
            augmentations = self.transform(image=image)
            image = augmentations["image"]
        target = torch.tensor(self.un_labels.index(label), dtype=torch.long) 
        
        return image, target
# ============================================================================= # Organization  : TUM Campus Straubing # Institution   : Bioinformatics # Created By    : <NAME> # Created Date  : Dienstag 11 30 15:00 GMT 2021 # Latest Update : DayName Month Day hh:mm:ss GMT YYYY # ============================================================================= Plant dataset definition used for pytorch data loader. # ============================================================================= # Imports # ============================================================================= loads the dataset from a list of images and masks         img_ls: list of image Paths to load         labels: list of labels denoted by EPPO Code csv_files: list of absolute paths to a csv-file generated by train_val_test_split.py         Reads a list of images that need to be loaded in this dataset class. # flatten lists # TODO: implement WeightedRandomSampler from class_sample_count