-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_prepare.py
106 lines (92 loc) · 4.14 KB
/
dataset_prepare.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
import torch
import h5py
import numpy as np
from scipy.io import loadmat
def normalization(data):
_range = np.max(data) - np.min(data)
return (data - np.min(data)) / _range
def read_data(dataset):
if dataset == 'Germany':
file_name1 = r'./VH_cut.mat'
file_name2 = r'./VV_cut.mat'
file_name3 = r'./label.mat'
else:
raise ValueError("Unknown dataset")
with h5py.File(file_name1, 'r') as f:
f = h5py.File(file_name1, 'r')
# print(f.keys())
vh = np.transpose(f['VH'])
with h5py.File(file_name2, 'r') as f:
f = h5py.File(file_name2, 'r')
# print(f.keys())
vv = np.transpose(f['VV'])
label_data = loadmat(file_name3)
label = label_data['label'] ## for 2017~2018
# label = label_data['label_s2'] ## for 2020~2021
## normalize data
vh1 = normalization(vh)
vv1 = normalization(vv)
return vh1, vv1, label
def slide_crop(image, patch, overlay):
height, width = image.shape[0], image.shape[1]
crop_list = []
# middle region
for i in range(int((height - patch * overlay) / (patch * (1 - overlay)))):
for j in range(int((width - patch * overlay) / (patch * (1 - overlay)))):
if len(image.shape) == 2:
cropped = image[int(i * patch * (1 - overlay)) : int(i * patch * (1 - overlay)) + patch,
int(j * patch * (1 - overlay)) : int(j * patch * (1 - overlay)) + patch]
else:
cropped = image[int(i * patch * (1 - overlay)) : int(i * patch * (1 - overlay)) + patch,
int(j * patch * (1 - overlay)) : int(j * patch * (1 - overlay)) + patch, :]
crop_list.append(cropped)
# last column region
for i in range(int((height - patch * overlay) / (patch * (1 - overlay)))):
if len(image.shape) == 2:
cropped = image[int(i * patch * (1 - overlay)) : int(i * patch * (1 - overlay)) + patch,
(width - patch) : width]
else:
cropped = image[int(i * patch * (1 - overlay)) : int(i * patch * (1 - overlay)) + patch,
(width - patch) : width, :]
crop_list.append(cropped)
# last row region
for j in range(int((width - patch * overlay) / (patch * (1 - overlay)))):
if len(image.shape) == 2:
cropped = image[(height - patch) : height,
int(j * patch * (1 - overlay)) : int(j * patch * (1 - overlay)) + patch]
else:
cropped = image[(height - patch) : height,
int(j * patch * (1 - overlay)) : int(j * patch * (1 - overlay)) + patch, :]
crop_list.append(cropped)
# bottom right region
if len(image.shape) == 2:
cropped = image[(height - patch) : height,
(width - patch) : width]
else:
cropped = image[(height - patch) : height,
(width - patch) : width, :]
crop_list.append(cropped)
print('Number of Cropped Image: {}'.format(len(crop_list)))
if overlay == 0:
ColumnOver = int((height - patch * overlay) % (patch * (1 - overlay)) + patch * overlay // 2)
RowOver = int((width - patch * overlay) % (patch * (1 - overlay)) + patch * overlay // 2)
print('Number of ColumnOver and RowOver: {}, {}'.format(ColumnOver, RowOver))
return np.array(crop_list), ColumnOver, RowOver
else:
return np.array(crop_list)
def main():
dataset = 'Germany'
vh, vv, label = read_data(dataset)
patch, overlay = 128, 0.5
# for training dataset
vh_list = slide_crop(vh, patch, overlay)
vv_list = slide_crop(vv, patch, overlay)
label_list = slide_crop(label, patch, overlay)
# non-overlay crop for valid data
vh_test_list, ColumnOver, RowOver = slide_crop(vh, patch, overlay=0)
vv_test_list, _, _ = slide_crop(vv, patch, overlay=0)
label_test_list, _, _ = slide_crop(label, patch, overlay=0)
np.savez('train_list.npz', vh_list=vh_list, vv_list=vv_list, label_list=label_list,
vh_test_list=vh_test_list, vv_test_list=vv_test_list, label_test_list=label_test_list)
if __name__ == '__main__':
main()