Diff of /dataloader/data.py [000000] .. [dce3d9]

Switch to unified view

a b/dataloader/data.py
1
import os
2
import cv2
3
from glob import glob
4
from tqdm import tqdm
5
from sklearn.model_selection import train_test_split
6
from albumentations import HorizontalFlip, VerticalFlip, Rotate
7
8
9
def create_dir(path):
10
    """
11
    Create a directory.
12
    """
13
    if not os.path.exists(path):
14
        os.makedirs(path)
15
16
17
def load_data(path, split=0.2):
18
    """
19
    Load the images and masks
20
    """
21
    images = sorted(glob(f"{path}/*/image/*.png"))
22
    masks = sorted(glob(f"{path}/*/mask/*.png"))
23
24
    """ Split the data """
25
    split_size = int(len(images) * split)
26
    train_x, valid_x = train_test_split(images, test_size=split_size, random_state=42)
27
    train_y, valid_y = train_test_split(masks, test_size=split_size, random_state=42)
28
29
    return (train_x, train_y), (valid_x, valid_y)
30
31
32
def augment_data(images, masks, save_path, augment=True):
33
    """
34
    Performing data augmentation.
35
    """
36
    IMG_HEIGHT = 512
37
    IMG_WIDTH = 512
38
39
    for idx, (x, y) in tqdm(enumerate(zip(images, masks)), total=len(images)):
40
        """Extracting the directory and image name"""
41
        directory_name = x.split("/")[-3]
42
        name = directory_name + "_" + x.split("/")[-1].split(".")[0]
43
44
        """ Read the image and mask """
45
        x = cv2.imread(x, cv2.IMREAD_COLOR)
46
        y = cv2.imread(y, cv2.IMREAD_COLOR)
47
48
        if augment == True:
49
            aug = HorizontalFlip(p=1.0)  # Applying Horizontal Flip 100%
50
            augmented = aug(image=x, mask=y)
51
            x1 = augmented["image"]
52
            y1 = augmented["mask"]
53
54
            aug = VerticalFlip(p=1)  # Applying Vertical Flip 100%
55
            augmented = aug(image=x, mask=y)
56
            x2 = augmented["image"]
57
            y2 = augmented["mask"]
58
59
            aug = Rotate(limit=45, p=1.0)  # Applying Rotation till 45 degress
60
            augmented = aug(image=x, mask=y)
61
            x3 = augmented["image"]
62
            y3 = augmented["mask"]
63
64
            X = [x, x1, x2, x3]
65
            Y = [y, y1, y2, y3]
66
67
        else:
68
            X = [x]
69
            Y = [y]
70
71
        idx = 0
72
        for i, m in zip(X, Y):
73
            i = cv2.resize(i, (IMG_WIDTH, IMG_HEIGHT))
74
            m = cv2.resize(m, (IMG_WIDTH, IMG_HEIGHT))
75
            m = m / 255.0
76
            m = (m > 0.5) * 255
77
78
            if len(X) == 1:
79
                tmp_image_name = f"{name}.jpg"
80
                tmp_mask_name = f"{name}.jpg"
81
            else:
82
                tmp_image_name = f"{name}_{idx}.jpg"
83
                tmp_mask_name = f"{name}_{idx}.jpg"
84
85
            image_path = os.path.join(save_path, "image/", tmp_image_name)
86
            mask_path = os.path.join(save_path, "mask/", tmp_mask_name)
87
88
            cv2.imwrite(image_path, i)
89
            cv2.imwrite(mask_path, m)
90
91
            idx += 1
92
93
94
if __name__ == "__main__":
95
    """
96
    Load the dataset
97
    """
98
    dataset_path = os.path.join("data", "train")
99
    (train_x, train_y), (valid_x, valid_y) = load_data(dataset_path, split=0.2)
100
    print("Train: ", len(train_x))
101
    print("Valid: ", len(valid_x))
102
103
    """ Create the directories """
104
    create_dir("new_data/train/image/")
105
    create_dir("new_data/train/mask/")
106
    create_dir("new_data/valid/image/")
107
    create_dir("new_data/valid/mask/")
108
109
    """ Augment the data """
110
    augment_data(
111
        train_x, train_y, "new_data/train/", augment=True
112
    )  # Applying Data Augmentation for training data
113
    augment_data(valid_x, valid_y, "new_data/valid/", augment=False)