-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenmap_compressor.py
65 lines (49 loc) · 1.99 KB
/
enmap_compressor.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
import rasterio
import numpy as np
import keras
import matplotlib.pyplot as plt
import keras
from keras.models import load_model, Model
from numba import jit
input_path = "enmap/ENMAP01-____L2A-DT000326721_20170626T102020Z_001_V000204_20200406T201930Z-SPECTRAL_IMAGE.tif"
output_path = "output.tif"
def compress_enmap_image(input_path, output_path):
"""
Compresses an enmap image with 208 bands to one of 3 bands.
Model used is an autoencoder where the encoder is a 1D CNN and
the decoder is an ANN.
inputs:
input_path: string, a path to the input enmap image.
output_path: string, path to save the bottleneck image to.
"""
with rasterio.open(input_path) as src:
img = src.read()
kwargs = src.profile
img = np.moveaxis(img, 0, 2)
model = load_model("compression_ae_3bottleneck.h5")
x, y, z = img.shape
@jit(nopython=True, fastmath=True)
def prepare_data(img):
img = (img / 10000).astype(np.float32)
return img
# Convert to reflectance.
img = prepare_data(img)
# Get rid of null values as well as oversaturated pixels.
img[img <= 0] = 0
img[img >= 1] = 1
img = img.reshape(x * y, z, 1)
layer_name = 'bottleneck'
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(img, batch_size=512)
intermediate_output = intermediate_output.reshape((x, y, 3))
kwargs["count"] = 3
kwargs["dtype"] = "float32"
plt.figure(figsize=(12, 12))
plt.imshow(intermediate_output)
plt.show()
intermediate_output = np.moveaxis(intermediate_output, 2, 0)
with rasterio.open(output_path, "w", **kwargs) as dst:
dst.write(intermediate_output)
if __name__ == "__main__":
compress_enmap_image(input_path, output_path)