-
Notifications
You must be signed in to change notification settings - Fork 11
/
preprocess-4band-atlantic-forest-data.py
147 lines (119 loc) · 4.42 KB
/
preprocess-4band-atlantic-forest-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
import numpy as np
import os
import rioxarray as rxr
#
# Pre-Process GeoTIFF Atlantic Forest dataset
#
# Ingest images
base_dir3 = r"./ATLANTIC FOREST/"
## Training images
training_images_list3 = os.listdir(r"{}Training/image/".format(base_dir3))[0:250]
training_masks_list3 = []
training_images3 = []
for n in training_images_list3:
training_masks_list3.append(n)
a = (np.array(rxr.open_rasterio(r"{}Training/image/{}".format(base_dir3,n))))
a = (a-np.min(a)) / (np.max(a)-np.min(a))
training_images3.append(a)
## Training masks
training_masks3 = []
for n in training_masks_list3:
a = (np.array(rxr.open_rasterio(r"{}Training/label/{}".format(base_dir3,n))))
training_masks3.append(a)
## Test images
test_images_list3 = os.listdir(r"{}Test/image/".format(base_dir3))
test_masks_list3 = []
test_images3 = []
for n in test_images_list3:
test_masks_list3.append(n)
a = (np.array(rxr.open_rasterio(r"{}Test/image/{}".format(base_dir3,n))))
a = (a-np.min(a)) / (np.max(a)-np.min(a))
test_images3.append(a)
## Test masks
test_masks3 = []
for n in test_masks_list3:
a = (np.array(rxr.open_rasterio(r"{}Test/mask/{}".format(base_dir3,n))))
test_masks3.append(a)
## Validation images
validation_images_list3 = os.listdir(r"{}Validation/images/".format(base_dir3))
validation_masks_list3 = []
validation_images3 = []
for n in validation_images_list3:
validation_masks_list3.append(n)
a = (np.array(rxr.open_rasterio(r"{}Validation/images/{}".format(base_dir3,n))))
a = (a-np.min(a)) / (np.max(a)-np.min(a))
validation_images3.append(a)
## Validation masks
validation_masks3 = []
for n in validation_masks_list3:
a = (np.array(rxr.open_rasterio(r"{}Validation/masks/{}".format(base_dir3,n))))
validation_masks3.append(a)
# Pre-process data
for i in range(len(training_images3)):
#training_images3[i] = training_images3[i].reshape(1,512,512,4)
training_images3[i] = training_images3[i].astype('float32')
training_images3[i] = training_images3[i].T
for i in range(len(training_masks3)):
#training_masks3[i] = training_masks3[i][:512,:512]
training_masks3[i] = training_masks3[i].reshape(1,512,512,1)
training_masks3[i] = training_masks3[i].T
for i in range(len(validation_images3)):
#validation_images3[i] = validation_images3[i].reshape(1,512,512,4)
validation_images3[i] = validation_images3[i].astype('float32')
validation_images3[i] = validation_images3[i].T
for i in range(len(validation_masks3)):
#validation_masks3[i] = validation_masks3[i][:512,:512]
validation_masks3[i] = validation_masks3[i].reshape(1,512,512,1)
validation_masks3[i] = validation_masks3[i].T
for i in range(len(test_images3)):
#test_images3[i] = test_images3[i].reshape(1,512,512,4)
test_images3[i] = test_images3[i].astype('float32')
test_images3[i] = test_images3[i].T
for i in range(len(test_masks3)):
#test_masks3[i] = test_masks3[i][:512,:512]
test_masks3[i] = test_masks3[i].reshape(1,512,512,1)
test_masks3[i] = test_masks3[i].T
for i in range(len(training_images3)):
training_images3[i] = training_images3[i].reshape(-1,512,512,4)
for i in range(len(validation_images3)):
validation_images3[i] = validation_images3[i].reshape(-1,512,512,4)
for i in range(len(test_images3)):
test_images3[i] = test_images3[i].reshape(-1,512,512,4)
#
# Output as .npy files
#
os.mkdir('atlantic-processed-large')
os.chdir('atlantic-processed-large')
os.mkdir('training')
os.mkdir('test')
os.mkdir('validation')
os.chdir('training')
os.mkdir('images')
os.mkdir('masks')
os.chdir('images')
for i in range(len(training_images3)):
np.save(training_images_list3[i], training_images3[i])
os.chdir('../masks')
for i in range(len(training_masks3)):
np.save(training_masks_list3[i], training_masks3[i])
os.chdir('..')
os.chdir('../test')
os.mkdir('images')
os.mkdir('masks')
os.chdir('images')
for i in range(len(test_images3)):
np.save(test_images_list3[i], test_images3[i])
os.chdir('../masks')
for i in range(len(test_masks3)):
np.save(test_masks_list3[i], test_masks3[i])
os.chdir('..')
os.chdir('../validation')
os.mkdir('images')
os.mkdir('masks')
os.chdir('images')
for i in range(len(validation_images3)):
np.save(validation_images_list3[i], validation_images3[i])
os.chdir('../masks')
for i in range(len(validation_masks3)):
np.save(validation_masks_list3[i], validation_masks3[i])
os.chdir('..')