-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdata.py
116 lines (88 loc) · 3.54 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import numpy as np
import cv2
from glob import glob
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from albumentations import HorizontalFlip, GridDistortion, OpticalDistortion, ChannelShuffle, CoarseDropout, CenterCrop, Crop, Rotate
""" Creating a directory """
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def load_data(path, split=0.1):
""" Loading the images and masks """
X = sorted(glob(os.path.join(path, "images", "*.jpg")))
Y = sorted(glob(os.path.join(path, "masks", "*.png")))
""" Spliting the data into training and testing """
split_size = int(len(X) * split)
train_x, test_x = train_test_split(X, test_size=split_size, random_state=42)
train_y, test_y = train_test_split(Y, test_size=split_size, random_state=42)
return (train_x, train_y), (test_x, test_y)
def augment_data(images, masks, save_path, augment=True):
H = 512
W = 512
for x, y in tqdm(zip(images, masks), total=len(images)):
""" Extract the name """
name = x.split("/")[-1].split(".")[0]
""" Reading the image and mask """
x = cv2.imread(x, cv2.IMREAD_COLOR)
y = cv2.imread(y, cv2.IMREAD_COLOR)
""" Augmentation """
if augment == True:
aug = HorizontalFlip(p=1.0)
augmented = aug(image=x, mask=y)
x1 = augmented["image"]
y1 = augmented["mask"]
x2 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)
y2 = y
aug = ChannelShuffle(p=1)
augmented = aug(image=x, mask=y)
x3 = augmented['image']
y3 = augmented['mask']
aug = CoarseDropout(p=1, min_holes=3, max_holes=10, max_height=32, max_width=32)
augmented = aug(image=x, mask=y)
x4 = augmented['image']
y4 = augmented['mask']
aug = Rotate(limit=45, p=1.0)
augmented = aug(image=x, mask=y)
x5 = augmented["image"]
y5 = augmented["mask"]
X = [x, x1, x2, x3, x4, x5]
Y = [y, y1, y2, y3, y4, y5]
else:
X = [x]
Y = [y]
index = 0
for i, m in zip(X, Y):
try:
""" Center Cropping """
aug = CenterCrop(H, W, p=1.0)
augmented = aug(image=i, mask=m)
i = augmented["image"]
m = augmented["mask"]
except Exception as e:
i = cv2.resize(i, (W, H))
m = cv2.resize(m, (W, H))
tmp_image_name = f"{name}_{index}.png"
tmp_mask_name = f"{name}_{index}.png"
image_path = os.path.join(save_path, "image", tmp_image_name)
mask_path = os.path.join(save_path, "mask", tmp_mask_name)
cv2.imwrite(image_path, i)
cv2.imwrite(mask_path, m)
index += 1
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
""" Load the dataset """
data_path = "people_segmentation"
(train_x, train_y), (test_x, test_y) = load_data(data_path)
print(f"Train:\t {len(train_x)} - {len(train_y)}")
print(f"Test:\t {len(test_x)} - {len(test_y)}")
""" Create directories to save the augmented data """
create_dir("new_data/train/image/")
create_dir("new_data/train/mask/")
create_dir("new_data/test/image/")
create_dir("new_data/test/mask/")
""" Data augmentation """
augment_data(train_x, train_y, "new_data/train/", augment=True)
augment_data(test_x, test_y, "new_data/test/", augment=False)