File size: 1,819 Bytes
11e63e4
 
 
 
084900a
11e63e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import collections
import io
import tempfile
import zipfile

import random

has_debugpy = True
try:
    import debugpy
except ImportError:
    has_debugpy = False
import pycolmap
import torch
import numpy as np


def parse_colmap_reconstruction(colmap_data: bytes) -> pycolmap.Reconstruction:
    """Parses a COLMAP reconstruction from a zip file.

    Args:
        colmap_data (bytes): The COLMAP reconstruction data as a zip file.
    Returns:
        pycolmap.Reconstruction: The parsed COLMAP reconstruction.
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        with zipfile.ZipFile(io.BytesIO(colmap_data), "r") as zf:
            zf.extractall(tmpdir)
        return pycolmap.Reconstruction(tmpdir)



def map_tensor(input: any, func: callable) -> any:
    if isinstance(input, str):
        return input
    elif isinstance(input, collections.abc.Mapping):
        return {k: map_tensor(sample, func) for k, sample in input.items()}
    elif isinstance(input, collections.abc.Sequence):
        return [map_tensor(sample, func) for sample in input]
    else:
        return func(input)


def batch_to_device(batch: any, device: str, non_blocking: bool = True):
    return map_tensor(batch, lambda x: x.to(device=device, non_blocking=non_blocking))



def start_debug():
    if not has_debugpy:
        raise ImportError("debugpy library is required for debugging.")
    debugpy.listen(5678)
    print("Wait for debugger!")
    debugpy.wait_for_client()
    print("Attached!")


def set_random_seed(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
    if hasattr(torch.backends, "cudnn"):
        torch.backends.cudnn.benchmark = False
        torch.backends.cudnn.deterministic = True