File size: 32,583 Bytes
d62394f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e732b53
 
 
 
d62394f
e4c4055
 
 
d62394f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab70ec
 
 
d62394f
090544c
6f11a19
e732b53
 
 
d62394f
 
 
6f11a19
de1ddad
d62394f
 
 
 
 
e4c4055
 
de1ddad
 
d62394f
 
 
e4c4055
d62394f
 
 
 
 
 
 
 
e4c4055
d62394f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4c4055
d62394f
 
090544c
6f11a19
e732b53
 
 
d62394f
 
 
de1ddad
d62394f
 
 
 
 
e4c4055
 
de1ddad
 
d62394f
 
 
e4c4055
d62394f
 
 
e4c4055
d62394f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import logging
import math
import os
import threading
from time import perf_counter

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from fused_ssim import fused_ssim
from torchvision.transforms.functional import gaussian_blur
from PIL import Image

from gsplat import (
    project_gaussians_2d_scale_rot,
    rasterize_gaussians_no_tiles,
    rasterize_gaussians_sum,
)
from utils.image_utils import (
    compute_image_gradients,
    get_grid,
    get_psnr,
    load_images,
    to_output_format,
)
from utils.misc_utils import set_random_seed
from utils.quantization_utils import ste_quantize
from utils.saliency_utils import get_smap


class StreamingResults:
    """Container for streaming training results"""

    def __init__(self):
        self.step = 0
        self.total_steps = 0
        self.current_render = None
        self.current_gaussian_id = None
        self.initialization_map = None  # Single map for current initialization mode
        self.final_render = None
        self.final_checkpoint_path = None
        self.training_logs = []
        self.metrics = {
            "psnr": 0.0,
            "ssim": 0.0,
            "loss": 0.0,
            "render_time": 0.0,
            "total_time": 0.0,
        }
        self.is_complete = False
        # Store all step results for interactive browsing
        self.step_renders = {}  # {step: PIL_Image}
        self.step_gaussian_ids = {}  # {step: PIL_Image}
        # For async visualization generation
        self.vis_lock = threading.Lock()


class GradioStreamingHandler(logging.Handler):
    """Custom logging handler that captures logs for Gradio streaming"""

    def __init__(self, results_container: StreamingResults):
        super().__init__()
        self.results = results_container

    def emit(self, record):
        log_entry = self.format(record)
        self.results.training_logs.append(log_entry)
        # Keep only last 100 log entries to avoid memory issues
        if len(self.results.training_logs) > 100:
            self.results.training_logs = self.results.training_logs[-100:]


class GradioGaussianSplatting2D(nn.Module):
    """Gradio-optimized version of GaussianSplatting2D with streaming capabilities"""

    def __init__(self, args, results_container: StreamingResults):
        super(GradioGaussianSplatting2D, self).__init__()
        self.results = results_container
        self.evaluate = args.eval
        set_random_seed(seed=args.seed)

        # Device setup
        if torch.cuda.is_available():
            torch.cuda.set_device(0)
            self.device = torch.device("cuda:0")
        else:
            self.device = torch.device("cpu")
        self.dtype = torch.float32

        # Initialize components
        self._init_logging(args)
        self._init_target(args)
        self._init_bit_precision(args)
        self._init_gaussians(args)
        self._init_loss(args)
        self._init_optimization(args)

        # Initialization
        if self.evaluate:
            self.ckpt_file = args.ckpt_file
            self._load_model()
        else:
            self._init_pos_scale_feat(args)

    def _init_logging(self, args):
        self.log_dir = getattr(args, "log_dir", "temp_gradio_logs")
        self.vis_gaussians = args.vis_gaussians
        self.save_image_steps = args.save_image_steps
        self.eval_steps = args.eval_steps

        # Set up streaming logger
        self.worklog = logging.getLogger("GradioImageGS")
        self.worklog.handlers.clear()  # Remove existing handlers

        # Add our streaming handler
        stream_handler = GradioStreamingHandler(self.results)
        stream_handler.setFormatter(
            logging.Formatter(fmt="[{asctime}] {message}", style="{")
        )
        self.worklog.addHandler(stream_handler)
        self.worklog.setLevel(logging.INFO)

        self.worklog.info(
            f"Start optimizing {args.num_gaussians:d} Gaussians for '{args.input_path}'"
        )

    def _init_target(self, args):
        self.gamma = args.gamma
        self.downsample = args.downsample
        if self.downsample:
            self.downsample_ratio = float(args.downsample_ratio)

        self.block_h, self.block_w = 16, 16
        self._load_target_images(path=os.path.join(args.data_root, args.input_path))

        if self.downsample:
            self.gt_images_upsampled = self.gt_images
            self.img_h_upsampled, self.img_w_upsampled = self.img_h, self.img_w
            self.tile_bounds_upsampled = self.tile_bounds
            self._load_target_images(
                path=os.path.join(args.data_root, args.input_path),
                downsample_ratio=self.downsample_ratio,
            )

        self.num_pixels = self.img_h * self.img_w

    def _load_target_images(self, path, downsample_ratio=None):
        self.gt_images, self.input_channels, self.image_fnames = load_images(
            load_path=path, downsample_ratio=downsample_ratio, gamma=self.gamma
        )
        self.gt_images = torch.from_numpy(self.gt_images).to(
            dtype=self.dtype, device=self.device
        )
        self.img_h, self.img_w = (
            int(self.gt_images.shape[1]),
            int(self.gt_images.shape[2]),
        )
        self.tile_bounds = (
            int((self.img_w + self.block_w - 1) // self.block_w),
            int((self.img_h + self.block_h - 1) // self.block_h),
            int(1),
        )

    def _init_bit_precision(self, args):
        self.quantize = args.quantize
        self.pos_bits = args.pos_bits
        self.scale_bits = args.scale_bits
        self.rot_bits = args.rot_bits
        self.feat_bits = args.feat_bits

    def _init_gaussians(self, args):
        self.num_gaussians = args.num_gaussians
        self.total_num_gaussians = args.num_gaussians
        self.disable_prog_optim = args.disable_prog_optim

        if not self.disable_prog_optim and not self.evaluate:
            self.initial_ratio = args.initial_ratio
            self.add_times = args.add_times
            self.add_steps = args.add_steps
            self.num_gaussians = math.ceil(
                self.initial_ratio * self.total_num_gaussians
            )
            self.max_add_num = math.ceil(
                float(self.total_num_gaussians - self.num_gaussians) / self.add_times
            )
            min_steps = self.add_steps * self.add_times + args.post_min_steps
            if args.max_steps < min_steps:
                self.worklog.info(
                    f"Max steps ({args.max_steps:d}) is too small for progressive optimization. Resetting to {min_steps:d}"
                )
                args.max_steps = min_steps

        self.topk = args.topk
        self.eps = 1e-7 if args.disable_tiles else 1e-4
        self.init_scale = args.init_scale
        self.disable_topk_norm = args.disable_topk_norm
        self.disable_inverse_scale = args.disable_inverse_scale
        self.disable_color_init = args.disable_color_init

        # Initialize parameters
        self.xy = nn.Parameter(
            torch.rand(self.num_gaussians, 2, dtype=self.dtype, device=self.device),
            requires_grad=True,
        )
        self.scale = nn.Parameter(
            torch.ones(self.num_gaussians, 2, dtype=self.dtype, device=self.device),
            requires_grad=True,
        )
        self.rot = nn.Parameter(
            torch.zeros(self.num_gaussians, 1, dtype=self.dtype, device=self.device),
            requires_grad=True,
        )
        self.feat_dim = sum(self.input_channels)
        self.feat = nn.Parameter(
            torch.rand(
                self.num_gaussians, self.feat_dim, dtype=self.dtype, device=self.device
            ),
            requires_grad=True,
        )
        self.vis_feat = nn.Parameter(torch.rand_like(self.feat), requires_grad=False)

        self._log_compression_rate()

    def _log_compression_rate(self):
        bytes_uncompressed = float(self.gt_images.numel())
        bpp_uncompressed = float(8 * self.feat_dim)
        self.worklog.info(
            f"Uncompressed: {bytes_uncompressed / 1e3:.2f} KB | {bpp_uncompressed:.3f} bpp | 8.0 bppc"
        )
        bits_compressed = (
            2 * self.pos_bits
            + 2 * self.scale_bits
            + self.rot_bits
            + self.feat_dim * self.feat_bits
        ) * self.total_num_gaussians
        bytes_compressed = bits_compressed / 8.0
        bpp_compressed = float(bits_compressed) / self.num_pixels
        bppc_compressed = bpp_compressed / self.feat_dim
        self.num_bytes = bytes_compressed
        self.worklog.info(
            f"Compressed: {bytes_compressed / 1e3:.2f} KB | {bpp_compressed:.3f} bpp | {bppc_compressed:.3f} bppc"
        )
        self.worklog.info(
            f"Compression rate: {bpp_uncompressed / bpp_compressed:.2f}x | {100.0 * bpp_compressed / bpp_uncompressed:.2f}%"
        )

    def _init_loss(self, args):
        self.l1_loss_ratio = args.l1_loss_ratio
        self.l2_loss_ratio = args.l2_loss_ratio
        self.ssim_loss_ratio = args.ssim_loss_ratio

    def _init_optimization(self, args):
        self.disable_tiles = args.disable_tiles
        self.start_step = 1
        self.max_steps = args.max_steps
        self.results.total_steps = (
            args.max_steps
        )  # Set total steps for streaming progress
        self.pos_lr = args.pos_lr
        self.scale_lr = args.scale_lr
        self.rot_lr = args.rot_lr
        self.feat_lr = args.feat_lr

        self.optimizer = torch.optim.Adam(
            [
                {"params": self.xy, "lr": self.pos_lr},
                {"params": self.scale, "lr": self.scale_lr},
                {"params": self.rot, "lr": self.rot_lr},
                {"params": self.feat, "lr": self.feat_lr},
            ]
        )

        self.disable_lr_schedule = args.disable_lr_schedule
        if not self.disable_lr_schedule:
            self.decay_ratio = args.decay_ratio
            self.check_decay_steps = args.check_decay_steps
            self.max_decay_times = args.max_decay_times
            self.decay_threshold = args.decay_threshold

    def _init_pos_scale_feat(self, args):
        self.init_mode = args.init_mode
        self.init_random_ratio = args.init_random_ratio
        self.pixel_xy = (
            get_grid(h=self.img_h, w=self.img_w)
            .to(dtype=self.dtype, device=self.device)
            .reshape(-1, 2)
        )

        with torch.no_grad():
            # Position initialization
            if self.init_mode == "gradient":
                self._compute_gmap()
                self.xy.copy_(self._sample_pos(prob=self.image_gradients))
            elif self.init_mode == "saliency":
                self.smap_filter_size = args.smap_filter_size
                self._compute_smap()
                self.xy.copy_(self._sample_pos(prob=self.saliency))
            else:  # random mode
                selected = np.random.choice(
                    self.num_pixels, self.num_gaussians, replace=False, p=None
                )
                self.xy.copy_(self.pixel_xy.detach().clone()[selected])
                # For random mode, create a simple random noise pattern
                if self.init_mode == "random":
                    random_pattern = np.random.rand(self.img_h, self.img_w)
                    self.results.initialization_map = Image.fromarray(
                        (random_pattern * 255).astype(np.uint8)
                    )

            # Scale initialization
            self.scale.fill_(
                self.init_scale if self.disable_inverse_scale else 1.0 / self.init_scale
            )

            # Feature initialization
            if not self.disable_color_init:
                self.feat.copy_(
                    self._get_target_features(positions=self.xy).detach().clone()
                )

    def _sample_pos(self, prob):
        num_random = round(self.init_random_ratio * self.num_gaussians)
        selected_random = np.random.choice(
            self.num_pixels, num_random, replace=False, p=None
        )
        selected_other = np.random.choice(
            self.num_pixels, self.num_gaussians - num_random, replace=False, p=prob
        )
        return torch.cat(
            [
                self.pixel_xy.detach().clone()[selected_random],
                self.pixel_xy.detach().clone()[selected_other],
            ],
            dim=0,
        )

    def _compute_gmap(self):
        gy, gx = compute_image_gradients(
            np.power(self.gt_images.detach().cpu().clone().numpy(), 1.0 / self.gamma)
        )
        g_norm = np.hypot(gy, gx).astype(np.float32)
        g_norm = g_norm / g_norm.max()

        # Store gradient map for streaming (only if this is the selected initialization mode)
        if self.init_mode == "gradient":
            self.results.initialization_map = Image.fromarray(
                (g_norm * 255).astype(np.uint8)
            )

        g_norm = np.power(g_norm.reshape(-1), 2.0)
        self.image_gradients = g_norm / g_norm.sum()
        self.worklog.info("Image gradient map computed")

    def _compute_smap(self):
        smap = get_smap(
            torch.pow(self.gt_images.detach().clone(), 1.0 / self.gamma),
            "models",
            self.smap_filter_size,
        )

        # Store saliency map for streaming (only if this is the selected initialization mode)
        if self.init_mode == "saliency":
            self.results.initialization_map = Image.fromarray(
                (smap * 255).astype(np.uint8)
            )

        self.saliency = (smap / smap.sum()).reshape(-1)
        self.worklog.info("Saliency map computed")

    def _get_target_features(self, positions):
        with torch.no_grad():
            target_features = F.grid_sample(
                self.gt_images.unsqueeze(0),
                positions[None, None, ...] * 2.0 - 1.0,
                align_corners=False,
            )
            target_features = target_features[0, :, 0, :].permute(1, 0)
        return target_features

    def forward(self, img_h, img_w, tile_bounds, upsample_ratio=None, benchmark=False):
        scale = self._get_scale(upsample_ratio=upsample_ratio)
        xy, rot, feat = self.xy, self.rot, self.feat

        if self.quantize:
            xy, scale, rot, feat = (
                ste_quantize(xy, self.pos_bits),
                ste_quantize(scale, self.scale_bits),
                ste_quantize(rot, self.rot_bits),
                ste_quantize(feat, self.feat_bits),
            )

        begin = perf_counter()
        tmp = project_gaussians_2d_scale_rot(
            xy, scale, rot, int(img_h), int(img_w), tile_bounds
        )
        xy, radii, conics, num_tiles_hit = tmp

        # Ensure correct tensor types for CUDA backend
        # Note: The custom gsplat CUDA code expects int32 for both radii and num_tiles_hit
        num_tiles_hit = num_tiles_hit.to(dtype=torch.int32)
        radii = radii.to(dtype=torch.int32)

        if not self.disable_tiles:
            enable_topk_norm = not self.disable_topk_norm

            out_image = rasterize_gaussians_sum(
                xy,
                radii,
                conics,
                num_tiles_hit,
                feat,
                int(img_h),
                int(img_w),
                int(self.block_h),
                int(self.block_w),
                enable_topk_norm,
            )
        else:
            tmp = xy, conics, feat, int(img_h), int(img_w)
            out_image = rasterize_gaussians_no_tiles(*tmp)

        render_time = perf_counter() - begin

        if benchmark:
            return render_time

        out_image = (
            out_image.view(-1, int(img_h), int(img_w), self.feat_dim)
            .permute(0, 3, 1, 2)
            .contiguous()
        )
        return out_image.squeeze(dim=0), render_time

    def _get_scale(self, upsample_ratio=None):
        scale = self.scale
        if not self.disable_inverse_scale:
            scale = 1.0 / scale
        if upsample_ratio is not None:
            scale = upsample_ratio * scale
        return scale

    def _tensor_to_pil_image(self, tensor_image):
        """Convert tensor image to PIL Image for streaming"""
        if tensor_image is None:
            return None

        # Convert to numpy and apply gamma correction
        image_np = (
            torch.pow(torch.clamp(tensor_image, 0.0, 1.0), 1.0 / self.gamma)
            .detach()
            .cpu()
            .numpy()
        )

        # Convert to uint8 format
        image_formatted = to_output_format(image_np, gamma=None)
        return Image.fromarray(image_formatted)

    def _create_gaussian_id_visualization(self):
        """Create Gaussian ID visualization as PIL Image using rasterization with vis_feat"""
        if not self.vis_gaussians:
            return None

        try:
            # Use vis_feat for ID visualization (this creates unique colors per Gaussian)
            feat = self.vis_feat * self.feat.norm(dim=-1, keepdim=True)

            # Render with ID features
            scale = self._get_scale()
            xy, rot = self.xy, self.rot

            if self.quantize:
                xy, scale, rot, feat = (
                    ste_quantize(xy, self.pos_bits),
                    ste_quantize(scale, self.scale_bits),
                    ste_quantize(rot, self.rot_bits),
                    ste_quantize(feat, self.feat_bits),
                )

            tmp = project_gaussians_2d_scale_rot(
                xy, scale, rot, int(self.img_h), int(self.img_w), self.tile_bounds
            )
            xy, radii, conics, num_tiles_hit = tmp

            # Ensure correct tensor types for CUDA backend
            # Note: The custom gsplat CUDA code expects int32 for both radii and num_tiles_hit
            num_tiles_hit = num_tiles_hit.to(dtype=torch.int32)
            radii = radii.to(dtype=torch.int32)

            if not self.disable_tiles:
                enable_topk_norm = not self.disable_topk_norm
                out_image = rasterize_gaussians_sum(
                    xy,
                    radii,
                    conics,
                    num_tiles_hit,
                    feat,
                    int(self.img_h),
                    int(self.img_w),
                    int(self.block_h),
                    int(self.block_w),
                    enable_topk_norm,
                )
            else:
                tmp = xy, conics, feat, int(self.img_h), int(self.img_w)
                out_image = rasterize_gaussians_no_tiles(*tmp)

            out_image = (
                out_image.view(-1, int(self.img_h), int(self.img_w), self.feat_dim)
                .permute(0, 3, 1, 2)
                .contiguous()
            ).squeeze(dim=0)

            return self._tensor_to_pil_image(out_image)

        except Exception as e:
            self.worklog.error(f"Error creating Gaussian ID visualization: {e}")
            return None

    def optimize(self):
        """Main optimization loop with streaming updates"""
        self.psnr_curr, self.ssim_curr = 0.0, 0.0
        self.best_psnr, self.best_ssim = 0.0, 0.0
        self.decay_times, self.no_improvement_steps = 0, 0
        self.render_time_accum, self.total_time_accum = 0.0, 0.0

        # Initialize attributes needed for evaluation
        self.l1_loss = None
        self.l2_loss = None
        self.ssim_loss = None
        self.stop_requested = False

        # Initial render and update
        with torch.no_grad():
            images, _ = self.forward(self.img_h, self.img_w, self.tile_bounds)
            self.results.current_render = self._tensor_to_pil_image(images)
            if self.vis_gaussians:
                try:
                    self.results.current_gaussian_id = (
                        self._create_gaussian_id_visualization()
                    )
                    self.worklog.info(
                        f"Initial visualizations created - Render: {'βœ“' if self.results.current_render else 'βœ—'}, ID: {'βœ“' if self.results.current_gaussian_id else 'βœ—'}"
                    )
                except Exception as e:
                    self.worklog.error(f"Error creating initial visualizations: {e}")
                    self.results.current_gaussian_id = None

            # Store initial results (step 0)
            self.results.step_renders[0] = self.results.current_render
            if self.vis_gaussians:
                self.results.step_gaussian_ids[0] = self.results.current_gaussian_id

        for step in range(self.start_step, self.max_steps + 1):
            self.step = step
            self.results.step = step

            self.optimizer.zero_grad()

            # Forward pass
            images, render_time = self.forward(self.img_h, self.img_w, self.tile_bounds)
            self.render_time_accum += render_time

            # Compute loss
            begin = perf_counter()
            self._get_total_loss(images)
            self.total_loss.backward()
            self.optimizer.step()
            self.total_time_accum += perf_counter() - begin + render_time

            # Update streaming results
            with torch.no_grad():
                if step % self.eval_steps == 0:
                    self._evaluate_and_update_stream(images)

                # Update render image more frequently, but visualizations less frequently
                render_update_freq = max(
                    50, self.save_image_steps // 2
                )  # Render updates every 50 steps
                vis_update_freq = max(
                    200, self.save_image_steps
                )  # Visualizations every 200 steps

                if step % render_update_freq == 0:
                    render_img = self._tensor_to_pil_image(images)
                    self.results.current_render = render_img

                # Only update Gaussian ID visualization less frequently
                if step % vis_update_freq == 0 and self.vis_gaussians:
                    # Generate Gaussian ID visualization asynchronously
                    def generate_gaussian_id_async():
                        try:
                            with self.results.vis_lock:
                                gaussian_id_vis = (
                                    self._create_gaussian_id_visualization()
                                )
                                self.results.current_gaussian_id = gaussian_id_vis

                        except Exception as e:
                            self.worklog.error(
                                f"Error creating Gaussian ID visualization at step {step}: {e}"
                            )
                            with self.results.vis_lock:
                                self.results.current_gaussian_id = None

                    # Start async visualization generation
                    vis_thread = threading.Thread(target=generate_gaussian_id_async)
                    vis_thread.daemon = True
                    vis_thread.start()

                # Store results for interactive browsing only at save_image_steps intervals
                if step % self.save_image_steps == 0:
                    # Store the current render for browsing
                    if self.results.current_render:
                        self.results.step_renders[step] = self.results.current_render

                    # Store Gaussian ID visualization for browsing
                    if self.vis_gaussians and self.results.current_gaussian_id:
                        self.results.step_gaussian_ids[step] = (
                            self.results.current_gaussian_id
                        )

                # Progressive optimization
                if (
                    not self.disable_prog_optim
                    and step % self.add_steps == 0
                    and self.num_gaussians < self.total_num_gaussians
                ):
                    self._add_gaussians(self.max_add_num)

                # Learning rate schedule
                terminate = False
                if (
                    not self.disable_lr_schedule
                    and self.num_gaussians == self.total_num_gaussians
                    and step % self.eval_steps == 0
                ):
                    terminate = self._lr_schedule()

                if terminate or self.stop_requested:
                    if self.stop_requested:
                        self.worklog.info("Training stopped by user request")
                    break

        # Final updates
        with torch.no_grad():
            images, _ = self.forward(self.img_h, self.img_w, self.tile_bounds)
            self.results.final_render = self._tensor_to_pil_image(images)

            # Save final checkpoint and store path
            self._save_final_checkpoint()

            self.results.is_complete = True
            self.worklog.info("Optimization completed")

    def _get_total_loss(self, images):
        self.total_loss = 0

        if self.l1_loss_ratio > 1e-7:
            self.l1_loss = self.l1_loss_ratio * F.l1_loss(images, self.gt_images)
            self.total_loss += self.l1_loss
        else:
            self.l1_loss = None

        if self.l2_loss_ratio > 1e-7:
            self.l2_loss = self.l2_loss_ratio * F.mse_loss(images, self.gt_images)
            self.total_loss += self.l2_loss
        else:
            self.l2_loss = None

        if self.ssim_loss_ratio > 1e-7:
            self.ssim_loss = self.ssim_loss_ratio * (
                1 - fused_ssim(images.unsqueeze(0), self.gt_images.unsqueeze(0))
            )
            self.total_loss += self.ssim_loss
        else:
            self.ssim_loss = None

    def _evaluate_and_update_stream(self, images):
        """Evaluate current state and update streaming results"""
        gamma_corrected_images = torch.pow(
            torch.clamp(images, 0.0, 1.0), 1.0 / self.gamma
        )
        gamma_corrected_gt = torch.pow(self.gt_images, 1.0 / self.gamma)

        psnr = get_psnr(gamma_corrected_images, gamma_corrected_gt).item()
        ssim = fused_ssim(
            gamma_corrected_images.unsqueeze(0), gamma_corrected_gt.unsqueeze(0)
        ).item()

        self.psnr_curr, self.ssim_curr = psnr, ssim

        # Update metrics
        self.results.metrics.update(
            {
                "psnr": psnr,
                "ssim": ssim,
                "loss": self.total_loss.item(),
                "render_time": self.render_time_accum,
                "total_time": self.total_time_accum,
            }
        )

        # Log progress
        loss_results = f"Loss: {self.total_loss.item():.4f}"
        if self.l1_loss is not None:
            loss_results += f", L1: {self.l1_loss.item():.4f}"
        if self.l2_loss is not None:
            loss_results += f", L2: {self.l2_loss.item():.4f}"
        if self.ssim_loss is not None:
            loss_results += f", SSIM: {self.ssim_loss.item():.4f}"

        time_results = f"Total: {self.total_time_accum:.2f} s | Render: {self.render_time_accum:.2f} s"

        self.worklog.info(
            f"Step: {self.step:d} | {time_results} | {loss_results} | PSNR: {psnr:.2f} | SSIM: {ssim:.4f}"
        )

    def _save_final_checkpoint(self):
        """Save final checkpoint and store the path"""
        if self.quantize:
            with torch.no_grad():
                self.xy.copy_(ste_quantize(self.xy, self.pos_bits))
                self.scale.copy_(ste_quantize(self.scale, self.scale_bits))
                self.rot.copy_(ste_quantize(self.rot, self.rot_bits))
                self.feat.copy_(ste_quantize(self.feat, self.feat_bits))

        # Create checkpoint directory
        ckpt_dir = os.path.join(self.log_dir, "checkpoints")
        os.makedirs(ckpt_dir, exist_ok=True)

        psnr = self.results.metrics.get("psnr", 0.0)
        ssim = self.results.metrics.get("ssim", 0.0)

        ckpt_data = {
            "step": self.step,
            "psnr": psnr,
            "ssim": ssim,
            "bytes": getattr(self, "num_bytes", 0),
            "time": self.total_time_accum,
            "state_dict": self.state_dict(),
            "optim_state_dict": self.optimizer.state_dict(),
        }

        ckpt_path = os.path.join(ckpt_dir, f"ckpt_step-{self.step:d}.pt")
        torch.save(ckpt_data, ckpt_path)
        self.results.final_checkpoint_path = ckpt_path

        self.worklog.info(f"Final checkpoint saved: {ckpt_path}")

    def _lr_schedule(self):
        """Learning rate scheduling logic"""
        if (
            self.psnr_curr <= self.best_psnr + 100 * self.decay_threshold
            or self.ssim_curr <= self.best_ssim + self.decay_threshold
        ):
            self.no_improvement_steps += self.eval_steps
            if self.no_improvement_steps >= self.check_decay_steps:
                self.no_improvement_steps = 0
                self.decay_times += 1
                if self.decay_times > self.max_decay_times:
                    return True
                for param_group in self.optimizer.param_groups:
                    param_group["lr"] /= self.decay_ratio
                self.worklog.info(f"Learning rate decayed by {self.decay_ratio:.1f}")
            return False
        else:
            self.best_psnr = self.psnr_curr
            self.best_ssim = self.ssim_curr
            self.no_improvement_steps = 0
            return False

    def _add_gaussians(self, add_num):
        """Add Gaussians during progressive optimization"""
        add_num = min(
            add_num, self.max_add_num, self.total_num_gaussians - self.num_gaussians
        )
        if add_num <= 0:
            return

        # Compute error map for new Gaussian placement
        raw_images, _ = self.forward(self.img_h, self.img_w, self.tile_bounds)
        images = torch.pow(torch.clamp(raw_images, 0.0, 1.0), 1.0 / self.gamma)
        gt_images = torch.pow(self.gt_images, 1.0 / self.gamma)

        kernel_size = round(np.sqrt(self.img_h * self.img_w) // 400)
        if kernel_size >= 1:
            kernel_size = max(3, kernel_size)
            kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size
            gt_images = gaussian_blur(img=gt_images, kernel_size=kernel_size)

        diff_map = (gt_images - images).detach().clone()
        error_map = torch.pow(torch.abs(diff_map).mean(dim=0).reshape(-1), 2.0)
        sample_prob = (error_map / error_map.sum()).cpu().numpy()
        selected = np.random.choice(
            self.num_pixels, add_num, replace=False, p=sample_prob
        )

        # Create new Gaussians
        new_xy = self.pixel_xy.detach().clone()[selected]
        new_scale = torch.ones(add_num, 2, dtype=self.dtype, device=self.device)
        init_scale = self.init_scale
        new_scale.fill_(init_scale if self.disable_inverse_scale else 1.0 / init_scale)
        new_rot = torch.zeros(add_num, 1, dtype=self.dtype, device=self.device)
        new_feat = diff_map.permute(1, 2, 0).reshape(-1, self.feat_dim)[selected]
        new_vis_feat = torch.rand_like(new_feat)

        # Update parameters
        old_xy = self.xy.detach().clone()
        old_scale = self.scale.detach().clone()
        old_rot = self.rot.detach().clone()
        old_feat = self.feat.detach().clone()
        old_vis_feat = self.vis_feat.detach().clone()

        self.num_gaussians += add_num
        all_xy = torch.cat([old_xy, new_xy], dim=0)
        all_scale = torch.cat([old_scale, new_scale], dim=0)
        all_rot = torch.cat([old_rot, new_rot], dim=0)
        all_feat = torch.cat([old_feat, new_feat], dim=0)
        all_vis_feat = torch.cat([old_vis_feat, new_vis_feat], dim=0)

        self.xy = nn.Parameter(all_xy, requires_grad=True)
        self.scale = nn.Parameter(all_scale, requires_grad=True)
        self.rot = nn.Parameter(all_rot, requires_grad=True)
        self.feat = nn.Parameter(all_feat, requires_grad=True)
        self.vis_feat = nn.Parameter(all_vis_feat, requires_grad=False)

        # Update optimizer
        self.optimizer = torch.optim.Adam(
            [
                {"params": self.xy, "lr": self.pos_lr},
                {"params": self.scale, "lr": self.scale_lr},
                {"params": self.rot, "lr": self.rot_lr},
                {"params": self.feat, "lr": self.feat_lr},
            ]
        )

        self.worklog.info(
            f"Step: {self.step:d} | Adding {add_num:d} Gaussians ({self.num_gaussians - add_num:d} -> {self.num_gaussians:d})"
        )