Skip to content

Common

A suite of common utility functions.

This module includes general utility functions used by the sub-packages of the CVTkit library.

This module contains the following functions:

  • non_zero_std(maps, device, dim, keepdim) - Computes the standard deviation of all non-zero values in an input Tensor along the given dimension.
  • print_gpu_mem() - Prints the current unallocated memory of the GPU.
  • round_nearest(num, decimal=0) - Rounds a floating point number to the nearest decimal place.
  • scale_image(image, scale, interpolation) - Scales an input pixel grid.

non_zero_std(maps, device, dim=1, keepdim=False)

Computes the standard deviation of all non-zero values in an input Tensor along the given dimension.

Parameters:

Name Type Description Default
maps torch.Tensor required
device str required
keepdim bool False

Returns:

Type Description
torch.Tensor

The standard deviation of the non-zero elements of the input map.

Source code in cvt/common.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def non_zero_std(maps: torch.Tensor, device: str, dim: int = 1, keepdim: bool = False) -> torch.Tensor:
    """Computes the standard deviation of all non-zero values in an input Tensor along the given dimension.

    Parameters:
        maps:
        device:
        keepdim:

    Returns:
        The standard deviation of the non-zero elements of the input map.
    """
    batch_size, views, height, width = maps.shape
    valid_map = torch.ne(maps, 0.0).to(torch.float32).to(device)
    valid_count = torch.sum(valid_map, dim=1, keepdim=keepdim)+1e-7
    mean = torch.div(torch.sum(maps,dim=1, keepdim=keepdim), valid_count).reshape(batch_size, 1, height, width).repeat(1,views,1,1)
    mean = torch.mul(valid_map, mean)

    std = torch.sub(maps, mean)
    std = torch.square(std)
    std = torch.sum(std, dim=1, keepdim=keepdim)
    std = torch.div(std, valid_count)
    std = torch.sqrt(std)

    return std

print_gpu_mem()

Prints the current unallocated memory of the GPU.

Source code in cvt/common.py
133
134
135
136
137
138
139
140
def print_gpu_mem() -> None:
    """Prints the current unallocated memory of the GPU.
    """
    t = torch.cuda.get_device_properties(0).total_memory
    r = torch.cuda.memory_reserved(0)
    a = torch.cuda.memory_allocated(0)
    f = t- (a+r)
    print("Free: {:0.4f} GB".format(f/(1024*1024*1024)))

round_nearest(num, decimal=0)

Rounds a floating point number to the nearest decimal place.

Parameters:

Name Type Description Default
num float

Float to be rounded.

required
decimal int

Decimal place to round to.

0

Returns:

Type Description
int

The given number rounded to the nearest decimal place.

Examples:

>>> round_nearest(11.1)
11
>>> round_nearest(15.7)
16
>>> round_nearest(2.5)
2
>>> round_nearest(3.5)
3
>>> round_nearest(14.156, 1)
14.2
>>> round_nearest(15.156, 1)
15.2
>>> round_nearest(15.156, 2)
15.16
Source code in cvt/common.py
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
def round_nearest(num: float, decimal: int = 0) -> int:
    """Rounds a floating point number to the nearest decimal place.

    Args:
        num: Float to be rounded.
        decimal: Decimal place to round to.

    Returns:
        The given number rounded to the nearest decimal place.

    Examples:
        >>> round_nearest(11.1)
        11
        >>> round_nearest(15.7)
        16
        >>> round_nearest(2.5)
        2
        >>> round_nearest(3.5)
        3
        >>> round_nearest(14.156, 1)
        14.2
        >>> round_nearest(15.156, 1)
        15.2
        >>> round_nearest(15.156, 2)
        15.16
    """

    return round(num+10**(-len(str(num))-1), decimal)

scale_camera(cam, scale=1.0)

Scales a camera intrinsic parameters.

Parameters:

Name Type Description Default
cam np.ndarray

Input camera to be scaled.

required
scale float

Scale factor.

1.0

Returns:

Type Description
np.ndarray

The scaled camera.

Source code in cvt/common.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def scale_camera(cam: np.ndarray, scale: float = 1.0) -> np.ndarray:
    """Scales a camera intrinsic parameters.

    Parameters:
        cam: Input camera to be scaled.
        scale: Scale factor.

    Returns:
        The scaled camera.
    """
    new_cam = np.copy(cam)
    new_cam[1][0][0] = cam[1][0][0] * scale
    new_cam[1][1][1] = cam[1][1][1] * scale
    new_cam[1][0][2] = cam[1][0][2] * scale
    new_cam[1][1][2] = cam[1][1][2] * scale
    return new_cam

scale_image(image, scale=1.0, interpolation='linear')

Scales an input pixel grid.

Parameters:

Name Type Description Default
image np.ndarray

Input image to be scaled.

required
scale float

Scale factor.

1.0
interpolation str

Interpolation technique to be used.

'linear'

Returns:

Type Description
np.ndarray

The scaled image.

Source code in cvt/common.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
def scale_image(image: np.ndarray, scale: float = 1.0, interpolation: str = "linear") -> np.ndarray:
    """Scales an input pixel grid.

    Parameters:
        image: Input image to be scaled.
        scale: Scale factor.
        interpolation: Interpolation technique to be used.

    Returns:
        The scaled image.
    """
    if interpolation == 'linear':
        return cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
    if interpolation == 'nearest':
        return cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_NEAREST)

scale_mvs_data(depths, confs, cams, scale=1.0, interpolation='linear')

Scales input depth maps, confidence maps, and cameras.

Parameters:

Name Type Description Default
depths np.ndarray

Input depth maps to be scaled.

required
confs np.ndarray

Input confidence maps to be scaled.

required
cams np.ndarray

Input cameras to be scaled

required
scale float

Scale factor.

1.0
interpolation str

Interpolation technique.

'linear'

Returns:

Name Type Description
scaled_depths np.ndarray

The scaled depth maps.

scaled_confs np.ndarray

The scaled confidence maps.

cams np.ndarray

The scaled cameras.

Source code in cvt/common.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
def scale_mvs_data(depths: np.ndarray, confs: np.ndarray, cams: np.ndarray, scale: float = 1.0, interpolation: str = "linear") -> Tuple[np.ndarray,np.ndarray,np.ndarray]:
    """Scales input depth maps, confidence maps, and cameras.

    Parameters:
        depths: Input depth maps to be scaled.
        confs: Input confidence maps to be scaled.
        cams: Input cameras to be scaled
        scale: Scale factor.
        interpolation: Interpolation technique.

    Returns:
        scaled_depths: The scaled depth maps.
        scaled_confs: The scaled confidence maps.
        cams: The scaled cameras.
    """
    views, height, width = depths.shape

    scaled_depths = []
    scaled_confs = []

    for view in range(views):
        scaled_depths.append(scale_image(depths[view], scale=scale, interpolation=interpolation))
        scaled_confs.append(scale_image(confs[view], scale=scale, interpolation=interpolation))
        cams[view] = scale_camera(cams[view], scale=scale)

    return np.asarray(scaled_depths), np.asarray(scaled_confs), cams

to_gpu(data, device)

Loads a dictionary of elements onto the GPU device.

Parameters:

Name Type Description Default
data dict

Dictionary to be loaded.

required
device string

GPU device identifier.

required
Source code in cvt/common.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def to_gpu(data: dict, device: string) -> None:
    """Loads a dictionary of elements onto the GPU device.

    Parameters:
        data: Dictionary to be loaded.
        device: GPU device identifier.
    """
    no_gpu_list = ["index", "filenames", "num_frame"]

    for key,val in data.items():
        if (key not in no_gpu_list):
            data[key] = val.cuda(device, non_blocking=True)
        else:
            print(key)
            print(val)
            print(type(val))
    sys.exit()
    return