-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDepthAnythingExt.py
151 lines (127 loc) · 5.48 KB
/
DepthAnythingExt.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
'''
License for https://github.com/IntentDev/TopArray
MIT License
Copyright (c) 2024 Keith Lostracco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import tensorrt as trt
import torch
import numpy as np
import torchvision.transforms as transforms
import webbrowser
class DepthAnythingExt:
def __init__(self, ownerComp):
self.ownerComp = ownerComp
self.ownerComp.par.Dimensions = ''
"""Initialize TensorRT plugins, engine and conetxt."""
self.trt_path = self.ownerComp.par.Enginefile.val
self.device = "cuda"
self.trt_logger = trt.Logger(trt.Logger.INFO)
try:
self.engine = self._load_engine()
self.get_dimensions(self.engine)
self.context = self.engine.create_execution_context()
self.stream = torch.cuda.current_stream(device=self.device)
except Exception as e:
debug(e)
self.source = op('null1')
self.trt_input = torch.zeros((self.source.height, self.source.width), device=self.device)
self.trt_output = torch.zeros((self.source.height, self.source.width), device=self.device)
self.to_tensor = TopArrayInterface(self.source)
self.normalize = transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225))
def get_dimensions(self, engine):
shapes = []
for binding in range(engine.num_bindings):
shape = engine.get_binding_shape(binding)
shapes.append(shape)
dimensions = shapes[0][2:]
self.ownerComp.par.Dimensions = f"{dimensions[1]}x{dimensions[0]}"
op('fit2').par.resolutionw = dimensions[1]
op('fit2').par.resolutionh = dimensions[0]
def _load_engine(self):
"""Load TensorRT engine."""
TRTbin = self.trt_path
with open(TRTbin, 'rb') as f, trt.Runtime(self.trt_logger) as runtime:
return runtime.deserialize_cuda_engine(f.read())
def infer(self, img, output):
"""Run inference on TensorRT engine."""
self.bindings = [img.data_ptr()] + [output.data_ptr()]
self.context.execute_async_v2(bindings=self.bindings, stream_handle=self.stream.cuda_stream)
self.stream.synchronize()
def run(self, scriptOp):
if self.ownerComp.par.Enginefile.val != '' and self.ownerComp.par.Venvpath.val != '':
self.to_tensor.update(self.stream.cuda_stream)
self.trt_input = torch.as_tensor(self.to_tensor, device=self.device)
self.trt_input = self.normalize(self.trt_input[1:, :, :]).ravel()
self.infer(self.trt_input, self.trt_output)
if self.ownerComp.par.Normalize == 'normal':
tensor_min = self.trt_output.min()
tensor_max = self.trt_output.max()
self.trt_output = (self.trt_output - tensor_min) / (tensor_max - tensor_min)
output = TopCUDAInterface(
self.source.width,
self.source.height,
1,
np.float32
)
scriptOp.copyCUDAMemory(self.trt_output.data_ptr(), output.size, output.mem_shape)
def about(self, endpoint):
if endpoint == 'Urlg':
webbrowser.open('https://github.com/olegchomp/TDDepthAnything', new=2)
if endpoint == 'Urld':
webbrowser.open('https://discord.gg/wNW8xkEjrf', new=2)
if endpoint == 'Urlt':
webbrowser.open('https://www.youtube.com/vjschool', new=2)
if endpoint == 'Urla':
webbrowser.open('https://olegcho.mp/', new=2)
if endpoint == 'Urldonate':
webbrowser.open('https://boosty.to/vjschool/donate', new=2)
class TopCUDAInterface:
def __init__(self, width, height, num_comps, dtype):
self.mem_shape = CUDAMemoryShape()
self.mem_shape.width = width
self.mem_shape.height = height
self.mem_shape.numComps = num_comps
self.mem_shape.dataType = dtype
self.bytes_per_comp = np.dtype(dtype).itemsize
self.size = width * height * num_comps * self.bytes_per_comp
class TopArrayInterface:
def __init__(self, top, stream=0):
self.top = top
mem = top.cudaMemory(stream=stream)
self.w, self.h = mem.shape.width, mem.shape.height
self.num_comps = mem.shape.numComps
self.dtype = mem.shape.dataType
shape = (mem.shape.numComps, self.h, self.w)
dtype_info = {'descr': [('', '<f4')], 'num_bytes': 4}
dtype_descr = dtype_info['descr']
num_bytes = dtype_info['num_bytes']
num_bytes_px = num_bytes * mem.shape.numComps
self.__cuda_array_interface__ = {
"version": 3,
"shape": shape,
"typestr": dtype_descr[0][1],
"descr": dtype_descr,
"stream": stream,
"strides": (num_bytes, num_bytes_px * self.w, num_bytes_px),
"data": (mem.ptr, False),
}
def update(self, stream=0):
mem = self.top.cudaMemory(stream=stream)
self.__cuda_array_interface__['stream'] = stream
self.__cuda_array_interface__['data'] = (mem.ptr, False)
return