forked from wajht7553/DIPCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegnet_utils.py
executable file
·94 lines (74 loc) · 3.94 KB
/
segnet_utils.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
#
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# 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.
#
from jetson_utils import cudaAllocMapped, cudaToNumpy
import numpy as np
class SegmentationBuffers:
def __init__(self, net, args):
self.net = net
self.mask = None
self.overlay = None
self.composite = None
self.class_mask = None
self.use_stats = args.stats
self.use_mask = "mask" in args.visualize
self.use_overlay = "overlay" in args.visualize
self.use_composite = self.use_mask and self.use_overlay
if not self.use_overlay and not self.use_mask:
raise Exception("invalid visualize flags - valid values are 'overlay' 'mask' 'overlay,mask'")
self.grid_width, self.grid_height = net.GetGridSize()
self.num_classes = net.GetNumClasses()
@property
def output(self):
if self.use_overlay and self.use_mask:
return self.composite
elif self.use_overlay:
return self.overlay
elif self.use_mask:
return self.mask
def Alloc(self, shape, format):
if self.overlay is not None and self.overlay.height == shape[0] and self.overlay.width == shape[1]:
return
if self.use_overlay:
self.overlay = cudaAllocMapped(width=shape[1], height=shape[0], format=format)
if self.use_mask:
mask_downsample = 2 if self.use_overlay else 1
self.mask = cudaAllocMapped(width=shape[1]/mask_downsample, height=shape[0]/mask_downsample, format=format)
if self.use_composite:
self.composite = cudaAllocMapped(width=self.overlay.width+self.mask.width, height=self.overlay.height, format=format)
if self.use_stats:
self.class_mask = cudaAllocMapped(width=self.grid_width, height=self.grid_height, format="gray8")
self.class_mask_np = cudaToNumpy(self.class_mask)
def ComputeStats(self):
if not self.use_stats:
return
# get the class mask (each pixel contains the classID for that grid cell)
self.net.Mask(self.class_mask, self.grid_width, self.grid_height)
# compute the number of times each class occurs in the mask
class_histogram, _ = np.histogram(self.class_mask_np, bins=self.num_classes, range=(0, self.num_classes-1))
print('grid size: {:d}x{:d}'.format(self.grid_width, self.grid_height))
print('num classes: {:d}'.format(self.num_classes))
print('-----------------------------------------')
print(' ID class name count %')
print('-----------------------------------------')
for n in range(self.num_classes):
percentage = float(class_histogram[n]) / float(self.grid_width * self.grid_height)
print(' {:>2d} {:<18s} {:>3d} {:f}'.format(n, self.net.GetClassDesc(n), class_histogram[n], percentage))