-
Notifications
You must be signed in to change notification settings - Fork 12
/
inference.py
179 lines (161 loc) · 4.67 KB
/
inference.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Python 2/3 compatiblity
from __future__ import print_function
from __future__ import division
import joblib
import os
from utils import convert_to_color_, convert_from_color_, get_device
from datasets import open_file
from model_utils import get_model, test
import numpy as np
import seaborn as sns
from skimage import io
import argparse
import torch
# Test options
parser = argparse.ArgumentParser(
description="Run deep learning experiments on" " various hyperspectral datasets"
)
parser.add_argument(
"--model",
type=str,
default=None,
help="Model to train. Available:\n"
"EndNet, "
"Early_fusion_CNN, "
"Middle_fusion_CNN, "
"Late_fusion_CNN, "
"Cross_fusion_CNN, "
"FusAtNet, "
"S2ENet, "
"Others, ",
)
parser.add_argument(
"--cuda",
type=int,
default=-1,
help="Specify CUDA device (defaults to -1, which learns on CPU)",
)
parser.add_argument(
"--checkpoint",
type=str,
default=None,
help="Weights to use for initialization, e.g. a checkpoint",
)
group_test = parser.add_argument_group("Test")
group_test.add_argument(
"--test_stride",
type=int,
default=1,
help="Sliding window step stride during inference (default = 1)",
)
group_test.add_argument(
"--image_hsi",
type=str,
default=None,
nargs="?",
help="Path to an image on which to run inference.",
)
group_test.add_argument(
"--image_lidar",
type=str,
default=None,
nargs="?",
help="Path to an image on which to run inference.",
)
group_test.add_argument(
"--only_test",
type=str,
default=None,
nargs="?",
help="Choose the data on which to test the trained algorithm ",
)
group_test.add_argument(
"--mat",
type=str,
default='HSI',
nargs="?",
help="In case of a .mat file, define the variable to call inside the file",
)
group_test.add_argument(
"--mat_lidar",
type=str,
default='LiDAR',
nargs="?",
help="In case of a .mat file, define the variable to call inside the file",
)
group_test.add_argument(
"--n_classes",
type=int,
default=None,
nargs="?",
help="When using a trained algorithm, specified the number of classes of this algorithm",
)
# Training options
group_train = parser.add_argument_group("Model")
group_train.add_argument(
"--patch_size",
type=int,
help="Size of the spatial neighbourhood (optional, if "
"absent will be set by the model)",
)
group_train.add_argument(
"--batch_size",
type=int,
help="Batch size (optional, if absent will be set by the model",
)
args = parser.parse_args()
CUDA_DEVICE = get_device(args.cuda)
MODEL = args.model
# Testing file
MAT = args.mat
MAT_LIDAR = args.mat_lidar
N_CLASSES = args.n_classes
INFERENCE = args.image_hsi
TEST_STRIDE = args.test_stride
CHECKPOINT = args.checkpoint
img_filename = os.path.basename(INFERENCE)
basename = MODEL + img_filename
dirname = os.path.dirname(INFERENCE)
img = open_file(INFERENCE)
if MAT is not None:
img = img[MAT]
# Normalization
img = np.asarray(img, dtype="float32")
img = (img - np.min(img)) / (np.max(img) - np.min(img))
img_lidar = open_file(args.image_lidar)
if MAT_LIDAR is not None:
img_lidar = img_lidar[MAT_LIDAR]
# Normalization
img_lidar = np.asarray(img_lidar, dtype="float32")
img_lidar = np.expand_dims(img_lidar, axis=2)
img_lidar = (img_lidar - np.min(img_lidar)) / (np.max(img_lidar) - np.min(img_lidar))
N_BANDS = (img.shape[-1], img_lidar.shape[-1])
hyperparams = vars(args)
hyperparams.update(
{
"n_classes": N_CLASSES,
"n_bands": N_BANDS,
"device": CUDA_DEVICE,
"ignored_labels": [0],
}
)
hyperparams = dict((k, v) for k, v in hyperparams.items() if v is not None)
palette = {0: (0, 0, 0)}
for k, color in enumerate(sns.color_palette("hls", N_CLASSES)):
palette[k + 1] = tuple(np.asarray(255 * np.array(color), dtype="uint8"))
invert_palette = {v: k for k, v in palette.items()}
def convert_to_color(x):
return convert_to_color_(x, palette=palette)
def convert_from_color(x):
return convert_from_color_(x, palette=invert_palette)
model, _, _, hyperparams = get_model(MODEL, **hyperparams)
model.load_state_dict(torch.load(CHECKPOINT, map_location=torch.device('cpu')))
probabilities = test(model, img, img_lidar, hyperparams)
prediction = np.argmax(probabilities, axis=-1)
filename = dirname + "/" + basename + ".tif"
io.imsave(filename, prediction)
basename = "color_" + basename
filename = dirname + "/" + basename + ".tif"
io.imsave(filename, convert_to_color(prediction))
# python inference.py --model S2ENet --checkpoint s2enet.pth --image_hsi D:/dataset/RS/multimodal/Houston2013/HSI.mat --image_lidar D:/dataset/RS/multimodal/Houston2013/LiDAR.mat --n
# _classes 16