-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_loader.py
More file actions
169 lines (138 loc) · 6.47 KB
/
Copy pathdata_loader.py
File metadata and controls
169 lines (138 loc) · 6.47 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from PIL import Image
import numpy as np
import os
import re
import scipy.misc
import random
import sys
import csv
def train_test_val_split(frame_data,mask_data, name_list, result_path):
n = len(frame_data)
a = int(n*0.75)
b = int(n*0.85)
# record test_names list in a csv
test_names = name_list[b:]
print('len test_files', len(test_names))
with open(result_path+'/test_names.csv', 'w') as myfile:
wr = csv.writer(myfile, dialect='excel')
wr.writerow(test_names)
x_train,x_val,x_test = frame_data[:a],frame_data[a:b],frame_data[b:]
y_train,y_val,y_test = mask_data[:a],mask_data[a:b],mask_data[b:]
return x_train, x_val, x_test, y_train, y_val, y_test
def is_feature_present(input_array):
return (np.sum(input_array)>50) # select the image with more than 50 pixel label
def load_feature_data(frame_dir, mask_dir, feature_type='erosion', dim=128, first_time=False):
'''load frames and masks into two numpy array respectively
-----
condition: with feature
arguments:
frame_dir, mask_dir,
feature_type: str, either erosion or building
dim: width and height of the image
process: always resize to 128x128 as model input
normalize on local image maxx and minn
-----
'''
low=0.1
hi=1.0
frames = []
masks = []
name_list = []
frame_names = os.listdir(frame_dir)
frame_names.sort(key=lambda var:[int(x) if x.isdigit() else x
for x in re.findall(r'[^0-9]|[0-9]+', var)]) # sort frame_names
poly_dir = mask_dir.replace('labels_retile','mask_retile')
print("** load image from directory loop starts:")
for i in range(len(frame_names)):
frame_file = frame_names[i]
# if len(frames)>1000:
# break
"""find mapped frame and mask path"""
frame_path = os.path.join(frame_dir, frame_file)
"""load image from tif and remove useless data"""
if feature_type=='erosion':
mask_path = os.path.join(mask_dir, frame_file)
x = np.load(frame_path)
# frame_array = np.concatenate((x[:,:,0:2], np.expand_dims(x[:,:,-1], axis=2)),axis=-1)
frame_array = x[:,:,-1]
label_array = np.load(mask_path)
else:
mask_path = os.path.join(mask_dir, frame_file.replace('fillnodata','building_label'))
poly_path = os.path.join(poly_dir, frame_file.replace('mclean_fillnodata','mask'))
# print(poly_path)
#### for 128_0ver
# mask_path = os.path.join(mask_dir, frame_file.replace('DEM','label'))
if(first_time):
if(frame_file[-3:]=='tif'):
if not os.path.exists(mask_path):
# os.remove(frame_path)
continue
poly_array = np.array(Image.open(poly_path))
if (0 in poly_array): # if there is a 0 in the polygon indicator file
os.remove(frame_path)
os.remove(mask_path)
os.remove(poly_path)
print('remove 3files',frame_file)
continue
frame_array = np.array(Image.open(frame_path))
label_array = np.array(Image.open(mask_path))
else:
os.remove(frame_path)
if os.path.exists(mask_path):
os.remove(mask_path)
print('remove2',frame_file)
continue
# check the dimension, if dimension wrong, remove
dims = frame_array.shape
if dims[0]!=dim or dims[1]!=dim: # remove the file if the frame has less than 3 unique data
os.remove(mask_path)
os.remove(frame_path)
print('remove3 dimension',frame_file)
continue
else: # not first time
frame_array = np.array(Image.open(frame_path))
label_array = np.array(Image.open(mask_path))
# both erosion and builiding, we check if feature is present
if not is_feature_present(label_array):
continue
"""Resize to dim"""
if frame_array.shape[0]!=dim:
frame_array = np.array(Image.fromarray(frame_array).resize((dim,dim), Image.BILINEAR))
label_array = np.array(Image.fromarray(label_array).resize((dim,dim), Image.NEAREST))
"""Try preprocess : Normalization"""
try:
minn, maxx = np.min(frame_array[frame_array > 0]), np.max(frame_array[frame_array > 0])
frame_array[frame_array > 0] = low + (frame_array[frame_array > 0] - minn) * (hi - low) / (maxx - minn)
except:
continue
name_list.append(frame_names[i])
frames.append(frame_array)
masks.append(label_array)
"""Form array and name_list"""
print(len(frames), len(masks))
frames, masks = np.array(frames), np.array(masks)
"""Extend to 4 dimensions for training """
if(frames.ndim != 4):
frames = frames.reshape((len(frames), dim, dim, 1))
masks = masks.reshape((len(masks),dim, dim, 1))
return frames, masks, name_list
def load_data(opt):
"""
Load data to a dictionary containing train, val, test
Return: Data_dict
"""
frame_data, mask_data, name_list = load_feature_data(opt.frame_path, opt.mask_path, opt.dataset, opt.dim)
print(np.min(frame_data),np.max(frame_data),np.unique(mask_data))
input_train, input_val, input_test, label_train, label_val, label_test = \
train_test_val_split(frame_data,mask_data,name_list, opt.result_path)
n_train, n_test, n_val = len(input_train), len(input_test), len(input_val)
print('***** #train: #test: #val = %d : %d :%d ******'%(n_train, n_test, n_val))
Data_dict = {
'train':[input_train.astype('float32'),
label_train.astype('float32')],
'val':[input_val.astype('float32'),
label_val.astype('float32')],
'test':[input_test.astype('float32'),
label_test.astype('float32')]
}
return Data_dict