-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
executable file
·148 lines (117 loc) · 4.31 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Data preparation script.
"""
from __future__ import print_function
import os
import numpy as np
from PIL import Image
from skimage.io import imread
import cv2
data_path = "./data/"
image_rows = 420
image_cols = 580
dim = (image_cols, image_rows)
def create_train_data():
train_data_path = os.path.join(data_path, "train")
images = os.listdir(train_data_path)
total = int(len(images) / 2)
imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
i = 0
print("-" * 30)
print("Creating training images...")
print("-" * 30)
for image_name in images:
if "mask" in image_name:
continue
image_mask_name = image_name.split(".")[0] + "_mask.tif"
img = cv2.imread(
os.path.join(train_data_path, image_name), cv2.IMREAD_GRAYSCALE
)
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
# print (img.shape)
img_mask = cv2.imread(
os.path.join(train_data_path, image_mask_name), cv2.IMREAD_GRAYSCALE
)
img_mask = cv2.resize(img_mask, dim, interpolation=cv2.INTER_AREA)
img = np.array([img])
img_mask = np.array([img_mask])
imgs[i] = img
imgs_mask[i] = img_mask
if i % 100 == 0:
print("Done: {0}/{1} images".format(i, total))
i += 1
print("Loading done.")
os.makedirs(os.path.join(data_path, 'file'), exist_ok=True)
np.save(data_path + "file/train.npy", imgs)
np.save(data_path + "file/train_mask.npy", imgs_mask)
print("Saving to .npy files done.")
def create_test_data():
train_data_path = os.path.join(data_path, "test")
images = os.listdir(train_data_path)
total = int(len(images) / 2)
imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
i = 0
print("-" * 30)
print("Creating testing images...")
print("-" * 30)
for image_name in images:
if "mask" in image_name:
continue
image_mask_name = image_name.split(".")[0] + "_mask.tif"
img = cv2.imread(
os.path.join(train_data_path, image_name), cv2.IMREAD_GRAYSCALE
)
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
img_mask = cv2.imread(
os.path.join(train_data_path, image_mask_name), cv2.IMREAD_GRAYSCALE
)
img_mask = cv2.resize(img_mask, dim, interpolation=cv2.INTER_AREA)
img = np.array([img])
img_mask = np.array([img_mask])
imgs[i] = img
imgs_mask[i] = img_mask
if i % 50 == 0:
print("Done: {0}/{1} images".format(i, total))
i += 1
print("Loading done.")
np.save(data_path + "file/test.npy", imgs)
np.save(data_path + "file/test_mask.npy", imgs_mask)
print("Saving to .npy files done.")
def create_valid_data():
train_data_path = os.path.join(data_path, "validation")
images = os.listdir(train_data_path)
total = int(len(images) / 2)
imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
i = 0
print("-" * 30)
print("Creating validation images...")
print("-" * 30)
for image_name in images:
if "mask" in image_name:
continue
image_mask_name = image_name.split(".")[0] + "_mask.tif"
img = cv2.imread(
os.path.join(train_data_path, image_name), cv2.IMREAD_GRAYSCALE
)
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
img_mask = cv2.imread(
os.path.join(train_data_path, image_mask_name), cv2.IMREAD_GRAYSCALE
)
img_mask = cv2.resize(img_mask, dim, interpolation=cv2.INTER_AREA)
img = np.array([img])
img_mask = np.array([img_mask])
imgs[i] = img
imgs_mask[i] = img_mask
if i % 50 == 0:
print("Done: {0}/{1} images".format(i, total))
i += 1
print("Loading done.")
np.save(data_path + "file/validation.npy", imgs)
np.save(data_path + "file/validation_mask.npy", imgs_mask)
print("Saving to .npy files done.")
if __name__ == "__main__":
create_train_data()
create_test_data()
create_valid_data()