File size: 5,526 Bytes
3718945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from diffusionGS.models.gsrenderer.gs_core import (
    GaussianModel,
    render_opencv_cam,
    render_turntable,
    RGB2SH,
    deferred_gaussian_render,
    deferred_gaussian_render_scene,
    imageseq2video,
)
import torch
import torch.nn as nn
import torch.nn.functional as F
from easydict import EasyDict as edict
'''
    调用时输入的是 Gaussian Model 的参数
    随后 init Gaussian model, 用 set_data 传参
    最后调用 render_opencv_cam 渲染
    返回的是渲染出来的 multi-view images
'''
class Renderer(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.config = config

        self.scaling_modifier = None #onfig.model.gaussians.get("scaling_modifier", None)
        self.gaussians_model = GaussianModel(
            config.gaussians_sh_degree, self.scaling_modifier
        )
        print(f"in Renderer, scaling_modifier: {self.scaling_modifier}")
        print(
            f"in Renderer, gaussians_model.scaling_modifier: {self.gaussians_model.scaling_modifier}"
        )

    @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
    def forward(
        self,
        xyz,
        features,
        scaling,
        rotation,
        opacity,
        height,
        width,
        C2W,
        fxfycxcy,
        deferred=True,
    ):
        """
        高斯常规的一些 attribute
        xyz: [b, n_gaussians, 3]
        features: [b, n_gaussians, (sh_degree+1)^2, 3]
        scaling: [b, n_gaussians, 3]
        rotation: [b, n_gaussians, 4]
        opacity: [b, n_gaussians, 1]

        height: int
        width: int
        C2W: [b, v, 4, 4]
        fxfycxcy: [b, v, 4]

        output: [b, v, 3, height, width]
        """

        if deferred:
            renderings = deferred_gaussian_render(
                xyz,
                features,
                scaling,
                rotation,
                opacity,
                height,
                width,
                C2W,
                fxfycxcy,
                self.scaling_modifier,
                self.config.use_gssplat,
            )
        else:
            b, v = C2W.size(0), C2W.size(1)
            renderings = torch.zeros(
                b, v, 3, height, width, dtype=torch.float32, device=xyz.device
            )

            for i in range(b):
                pc = self.gaussians_model.set_data(
                    xyz[i], features[i], scaling[i], rotation[i], opacity[i]
                )
                for j in range(v):
                    renderings[i, j] = render_opencv_cam(
                        pc, height, width, C2W[i, j], fxfycxcy[i, j]
                    )["render"]
        return renderings



class SceneRenderer(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.config = config
        #self.sh_degree = config.model.gaussians.sh_degree
        self.scaling_modifier = None #config.model.gaussians.get("scaling_modifier", None)
        self.gaussians_model = GaussianModel(
            config.gaussians_sh_degree, self.scaling_modifier
        )
        print(f"in Renderer, scaling_modifier: {self.scaling_modifier}")
        print(f"in Renderer, gaussians_model.scaling_modifier: {self.gaussians_model.scaling_modifier}")

    @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
    def forward(
        self,
        xyz,
        features,
        scaling,
        rotation,
        opacity,
        height,
        width,
        C2W,
        fxfycxcy,
        defer_render=False,
    ):
        """
        xyz: [b, n_gaussians, 3]
        features: [b, n_gaussians, (sh_degree+1)^2, 3]
        scaling: [b, n_gaussians, 3]
        rotation: [b, n_gaussians, 4]
        opacity: [b, n_gaussians, 1]

        height: int
        width: int
        C2W: [b, v, 4, 4]
        fxfycxcy: [b, v, 4]

        output: [b, v, 3, height, width]
        """

        if defer_render == True:
            # if False:
            renderings, depth, alpha = deferred_gaussian_render_scene(
                xyz, features, scaling, rotation, opacity, height, width, C2W, fxfycxcy, self.scaling_modifier
            )
        else:
            b, v = C2W.size(0), C2W.size(1)
            renderings = torch.zeros(
                b, v, 3, height, width, dtype=torch.float32, device=xyz.device
            )

            depth = torch.zeros(
                b, v, 1, height, width, dtype=torch.float32, device=xyz.device
            )
            alpha = torch.zeros(
                b, v, 1, height, width, dtype=torch.float32, device=xyz.device
            )

            for i in range(b):
                pc = self.gaussians_model.set_data(
                    xyz[i], features[i], scaling[i], rotation[i], opacity[i]
                )
                for j in range(v):
                    # renderings[i, j] = render_opencv_cam(
                    #     pc, height, width, C2W[i, j], fxfycxcy[i, j]
                    # )["render"]
                    buffers = render_opencv_cam(
                        pc, height, width, C2W[i, j], fxfycxcy[i, j]
                    )
                    renderings[i, j] = buffers["render"]
                    if "depth" in buffers and buffers["depth"] is not None: 
                        depth[i, j] = buffers["depth"]
                    if "alpha" in buffers and buffers["alpha"] is not None:
                        alpha[i, j] = buffers["alpha"]

        # return renderings
        return renderings #edict(render=renderings, depth=depth, alpha=alpha)