forked from MrNeRF/gaussian-splatting-cuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
46 lines (38 loc) · 1.7 KB
/
convert.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
import torch
import numpy as np
from torchvision import transforms
from PIL import Image
tensor_specs = {
"image": {"dims": 3, "shape": [3, 546, 979], "type": float},
}
def load_tensor(filename, tensor_spec):
with open(filename, 'rb') as f:
dims = int.from_bytes(f.read(4), 'little')
shape = tuple(int.from_bytes(f.read(8), 'little') for _ in range(dims))
assert dims == tensor_spec["dims"], f"Expected dims {tensor_spec['dims']} for {filename}, got {dims}"
assert shape == tuple(tensor_spec["shape"]), f"Expected shape {tensor_spec['shape']} for {filename}, got {shape}"
data_type = tensor_spec["type"]
if data_type == bool:
data = np.fromfile(f, dtype=np.bool_).astype(np.bool_)
elif data_type == float:
data = np.fromfile(f, dtype=np.float32).astype(np.float32)
elif data_type == int:
data = np.fromfile(f, dtype=np.int32).astype(np.int32)
else:
data = np.fromfile(f, dtype=np.int64).astype(np.int64)
# Reshape the data based on tensor specification, unless it's a scalar
if tensor_spec["dims"] != 0:
print(f"Filename: {filename}")
print(f"Total size of loaded data: {data.size}")
print(f"Expected shape from tensor_spec: {tensor_spec['shape']}")
data = data.reshape(tensor_spec["shape"])
return torch.from_numpy(data)
for i in range(251):
tensor = load_tensor(f"{i}.tensor", tensor_specs["image"])
tensor_spec = {
"dims": len(tensor.shape),
"shape": tuple(tensor.shape),
"type": float
}
img = transforms.ToPILImage()(tensor)
img.save(f"{i}_img.png")