-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_roughness.py
221 lines (200 loc) · 6.05 KB
/
sparse_roughness.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
import torch
import numpy as np
import rasterio as rio
from pathlib import Path
from rich.console import Console
from torchsparse import SparseTensor
from typing import Tuple, List, Union
from torchsparse.nn.functional import conv3d
def mean_conv(
sparse_tensor: SparseTensor,
kernel_size: Union[int, Tuple[int, int, int], List[int]] = 3
):
if isinstance(kernel_size, int):
kernel_size = [kernel_size, ] * 3
kernel_weight = torch.ones(
size=(torch.prod(torch.tensor(kernel_size)).item(), 1, 1),
dtype=torch.float,
device=sparse_tensor.F.device
)
sparse_index = SparseTensor(
coords=sparse_tensor.C,
feats=torch.ones_like(
input=sparse_tensor.F,
dtype=sparse_tensor.F.dtype,
device=sparse_tensor.F.device
)
)
cum = conv3d(
input=sparse_tensor,
weight=kernel_weight,
kernel_size=kernel_size,
bias=None,
stride=(1, 1, 1),
dilation=(1, 1, 1),
transposed=False
)
freq = conv3d(
input=sparse_index,
weight=kernel_weight,
kernel_size=kernel_size,
bias=None,
stride=(1, 1, 1),
dilation=(1, 1, 1),
transposed=False
)
cum.F = cum.F / freq.F
return cum.detach()
def calc_roughness(
src_path: Union[str, Path],
dst_path: Union[str, Path],
kernel_size: Union[int, Tuple[int, int], List[int]] = 3,
device: torch.device = torch.device('cpu')
):
try:
if isinstance(kernel_size, int):
kernel_size = kernel_size, kernel_size, 1
elif isinstance(kernel_size, (list, tuple)):
if len(kernel_size) == 2:
kernel_size = *kernel_size, 1
elif len(kernel_size) == 1:
kernel_size = *(kernel_size * 2), 1
else:
raise ValueError(
f"Incompatible 'kernel_size': {kernel_size}. " +
"Expected <int> or <int, int> "
)
else:
raise ValueError(f"Illegal 'kernel_size': {kernel_size}")
with rio.open(src_path, 'r') as src:
meta = src.meta.copy()
img = np.expand_dims(
a=np.moveaxis(
a=src.read(masked=True),
source=0,
destination=-1
),
axis=-2
) # X-Y-Z-B
indexes = np.moveaxis(
a=np.indices(
dimensions=img.shape,
dtype=np.int64,
sparse=False
),
source=0,
destination=-1
).reshape(-1, len(img.shape))
# noinspection PyUnresolvedReferences
indexes = indexes[np.logical_not(img.mask.ravel()), slice(None)]
# noinspection PyUnresolvedReferences
features = img.compressed().reshape(-1, 1)
assert indexes.shape[0] == features.shape[0]
indexes = torch.tensor(indexes, dtype=torch.int, device=device)
features = torch.tensor(features, dtype=torch.float, device=device)
st = SparseTensor(coords=indexes, feats=features)
mu = mean_conv(
sparse_tensor=st,
kernel_size=kernel_size
)
st.F = (st.F - mu.F) ** 2.0
st = mean_conv(
sparse_tensor=st,
kernel_size=kernel_size
)
idx_list = [
st.C[slice(None), i].to(dtype=torch.long)
for i in range(st.C.size(-1))
]
values = torch.squeeze(input=st.F)
out = torch.full(
size=img.shape,
fill_value=np.nan,
dtype=st.F.dtype,
device=st.F.device
)
out[idx_list] = values
out = torch.squeeze(input=out, dim=-2)
out = torch.permute(input=out, dims=(-1, 0, 1))
out = out.cpu().numpy()
meta['dtype'] = out.dtype
meta['nodata'] = np.nan
with rio.open(dst_path, "w", **meta) as dst:
dst.write(out)
return True
except Exception as err:
print(err)
return False
if __name__ == "__main__":
import argparse
from halo import Halo
parser = argparse.ArgumentParser(
description=(
'Calculate surface roughness / localized standard deviation ' +
'from sparse image'
)
)
parser.add_argument(
'-i', '--input',
metavar='Input file path',
action='store',
type=str,
required=True,
dest='import_path',
help='Specify input file'
)
parser.add_argument(
'-o', '--output',
metavar='Output file path',
action='store',
type=str,
required=True,
dest='export_path',
help='Specify output file'
)
parser.add_argument(
'-k', '--kernel',
metavar='Kernel size',
action='store',
type=int,
nargs='*',
required=False,
dest='ks',
default=(3, 3),
help='Specify kernel size. Hint: int or int int'
)
parser.add_argument(
'-d', '--device',
metavar='Target device',
action='store',
type=str,
default='auto',
required=False,
dest='dev',
help="Specify target device. Default: 'cpu'"
)
args = parser.parse_args()
# noinspection SpellCheckingInspection
srcpath = Path(args.import_path)
# noinspection SpellCheckingInspection
dstpath = Path(args.export_path)
if args.dev.lower() == 'auto':
if torch.cuda.is_available():
dev = torch.device('cuda')
else:
dev = torch.device('cpu')
else:
dev = torch.device(args.dev)
console = Console()
with console.status("[bold orange] Processing...") as spinner:
status = calc_roughness(
src_path=srcpath,
dst_path=dstpath,
kernel_size=args.ks,
device=dev
)
if status:
console.log(u"✅ Process completed successfully!")
else:
console.log(u"❌ Process Failed!")