evlib 0.8.7

Event Camera Data Processing Library
Documentation
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
#!/usr/bin/env python3
"""
Enhanced PyTorch to ONNX converter for event-to-video reconstruction models.

This script loads actual PyTorch checkpoints and converts them to optimized ONNX format.
"""

import argparse
import json
import os
from pathlib import Path

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F


class ConvLSTMCell(nn.Module):
    """ConvLSTM cell implementation for E2VID."""

    def __init__(self, input_dim, hidden_dim, kernel_size):
        super().__init__()
        self.hidden_dim = hidden_dim
        self.padding = kernel_size // 2

        # Gates: input, forget, cell, output
        self.gates = nn.Conv2d(
            input_dim + hidden_dim, 4 * hidden_dim, kernel_size, padding=self.padding
        )

    def forward(self, x, hidden_state):
        h, c = hidden_state
        combined = torch.cat([x, h], dim=1)

        gates = self.gates(combined)
        i, f, g, o = torch.split(gates, self.hidden_dim, dim=1)

        i = torch.sigmoid(i)
        f = torch.sigmoid(f)
        g = torch.tanh(g)
        o = torch.sigmoid(o)

        c = f * c + i * g
        h = o * torch.tanh(c)

        return h, (h, c)


class ConvLayer(nn.Module):
    """Convolutional layer with optional normalization."""

    def __init__(
        self, in_channels, out_channels, kernel_size, stride=1, padding=1, norm="BN"
    ):
        super().__init__()
        self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)

        if norm == "BN":
            self.norm_layer = nn.BatchNorm2d(out_channels)
        elif norm == "IN":
            self.norm_layer = nn.InstanceNorm2d(out_channels)
        else:
            self.norm_layer = None

    def forward(self, x):
        x = self.conv2d(x)
        if self.norm_layer is not None:
            x = self.norm_layer(x)
        return F.relu(x)


class ResidualBlock(nn.Module):
    """Residual block for E2VID."""

    def __init__(self, channels, norm="BN"):
        super().__init__()
        self.conv1 = ConvLayer(channels, channels, 3, norm=norm)
        self.conv2 = ConvLayer(channels, channels, 3, norm=norm)

    def forward(self, x):
        residual = x
        x = self.conv1(x)
        x = self.conv2(x)
        return x + residual


class E2VIDRecurrent(nn.Module):
    """E2VID UNet with ConvLSTM architecture matching the actual checkpoint."""

    def __init__(
        self,
        num_bins=5,
        base_channels=32,
        num_encoders=3,
        num_residual_blocks=2,
        norm="BN",
        use_upsample_conv=True,
    ):
        super().__init__()

        # Head
        self.head = ConvLayer(num_bins, base_channels, 5, padding=2, norm=None)

        # Encoders with ConvLSTM
        self.encoders = nn.ModuleList()
        in_ch = base_channels
        # Channel progression: 32 -> 64 -> 128 -> 256
        encoder_channels = [base_channels * (2 ** (i + 1)) for i in range(num_encoders)]

        for i, out_ch in enumerate(encoder_channels):
            encoder = nn.ModuleDict(
                {
                    "conv": ConvLayer(in_ch, out_ch, 5, stride=2, padding=2, norm=norm),
                    "recurrent_block": ConvLSTMCell(out_ch, out_ch, 3),
                }
            )
            self.encoders.append(encoder)
            in_ch = out_ch

        # Residual blocks
        self.resblocks = nn.ModuleList(
            [ResidualBlock(in_ch, norm=norm) for _ in range(num_residual_blocks)]
        )

        # Decoders
        self.decoders = nn.ModuleList()
        decoder_channels = list(reversed(encoder_channels[:-1])) + [base_channels]

        for i, out_ch in enumerate(decoder_channels):
            if use_upsample_conv:
                decoder = nn.ModuleDict(
                    {
                        "upsample": nn.ConvTranspose2d(
                            in_ch, out_ch, 4, stride=2, padding=1
                        ),
                        "conv": ConvLayer(out_ch * 2, out_ch, 5, padding=2, norm=norm),
                    }
                )
            else:
                decoder = nn.ModuleDict(
                    {"conv": ConvLayer(in_ch * 2, out_ch, 5, padding=2, norm=norm)}
                )
            self.decoders.append(decoder)
            in_ch = out_ch

        # Prediction
        self.prediction = nn.Conv2d(base_channels, 1, 1)

    def forward(self, x, states=None):
        # Head
        x = self.head(x)

        # Encode
        skip_connections = []
        new_states = []

        for i, encoder in enumerate(self.encoders):
            x = encoder["conv"](x)
            skip_connections.append(x)

            # Initialize state if not provided
            if states is None or i >= len(states):
                b, c, h, w = x.shape
                h_state = torch.zeros(b, c, h, w, device=x.device)
                c_state = torch.zeros(b, c, h, w, device=x.device)
                state = (h_state, c_state)
            else:
                state = states[i]

            # ConvLSTM
            h, new_state = encoder["recurrent_block"](x, state)
            x = h
            new_states.append(new_state)

        # Residual blocks
        for resblock in self.resblocks:
            x = resblock(x)

        # Decode
        for i, decoder in enumerate(self.decoders):
            if "upsample" in decoder:
                x = decoder["upsample"](x)
            else:
                # Upsample using interpolation
                x = F.interpolate(
                    x, scale_factor=2, mode="bilinear", align_corners=False
                )

            # Skip connection - resize x to match skip dimensions
            skip = skip_connections[-(i + 1)]
            # Always resize to ensure dimensions match (ONNX-friendly)
            x = F.interpolate(
                x,
                size=(skip.shape[2], skip.shape[3]),
                mode="bilinear",
                align_corners=False,
            )

            x = torch.cat([x, skip], dim=1)
            x = decoder["conv"](x)

        # Prediction
        x = self.prediction(x)
        x = torch.sigmoid(x)

        return x, new_states


def load_e2vid_from_checkpoint(checkpoint_path):
    """Load E2VID model from PyTorch checkpoint."""
    checkpoint = torch.load(checkpoint_path, map_location="cpu")

    # Extract model configuration
    if "model" in checkpoint:
        config = checkpoint["model"]
        model = E2VIDRecurrent(
            num_bins=config.get("num_bins", 5),
            base_channels=config.get("base_num_channels", 32),
            num_encoders=config.get("num_encoders", 3),
            num_residual_blocks=config.get("num_residual_blocks", 2),
            norm=config.get("norm", "BN"),
            use_upsample_conv=config.get("use_upsample_conv", True),
        )
    else:
        # Default configuration
        model = E2VIDRecurrent()

    # Load weights
    if "state_dict" in checkpoint:
        # Map checkpoint keys to model keys
        state_dict = {}
        for key, value in checkpoint["state_dict"].items():
            # Remove 'unetrecurrent.' prefix if present
            if key.startswith("unetrecurrent."):
                key = key[14:]  # Remove 'unetrecurrent.'
            state_dict[key] = value

        model.load_state_dict(state_dict, strict=False)
        print("Loaded E2VID weights from checkpoint")

    return model


def optimize_onnx_model(onnx_path):
    """Apply optimization passes to ONNX model."""
    try:
        import onnx
        from onnx import optimizer

        # Load model
        model = onnx.load(onnx_path)

        # Available optimization passes
        passes = [
            "eliminate_identity",
            "eliminate_nop_dropout",
            "eliminate_nop_pad",
            "eliminate_unused_initializer",
            "fuse_bn_into_conv",
            "fuse_consecutive_squeezes",
            "fuse_consecutive_transposes",
            "fuse_pad_into_conv",
        ]

        # Optimize
        optimized_model = optimizer.optimize(model, passes)

        # Save optimized model
        opt_path = onnx_path.replace(".onnx", "_optimized.onnx")
        onnx.save(optimized_model, opt_path)

        print(f"Saved optimized model to {opt_path}")
        return opt_path
    except ImportError:
        print("ONNX optimizer not available. Install with: pip install onnx")
        return onnx_path


def verify_onnx_model(pytorch_model, onnx_path, input_shape):
    """Verify ONNX model produces same output as PyTorch model."""
    try:
        import onnxruntime as ort

        # Create test input
        test_input = torch.randn(*input_shape)

        # PyTorch inference
        pytorch_model.eval()
        with torch.no_grad():
            pytorch_output, _ = pytorch_model(test_input)

        # ONNX inference
        ort_session = ort.InferenceSession(onnx_path)
        input_name = ort_session.get_inputs()[0].name
        onnx_output = ort_session.run(None, {input_name: test_input.numpy()})[0]

        # Compare outputs
        diff = np.abs(pytorch_output.numpy() - onnx_output)
        max_diff = np.max(diff)
        mean_diff = np.mean(diff)

        print("Model verification:")
        print(f"  Max difference: {max_diff:.6f}")
        print(f"  Mean difference: {mean_diff:.6f}")

        if max_diff < 1e-5:
            print("  SUCCESS: Model outputs match!")
            return True
        else:
            print("  ERROR: Model outputs differ significantly")
            return False

    except ImportError:
        print("ONNX Runtime not available. Install with: pip install onnxruntime")
        return None


def convert_model_to_onnx(model, model_name, output_dir, input_shape=(1, 5, 256, 256)):
    """Convert PyTorch model to ONNX with optimization and verification."""
    model.eval()

    # Create dummy input
    dummy_input = torch.randn(*input_shape)

    # Export to ONNX
    onnx_path = os.path.join(output_dir, f"{model_name}.onnx")

    print(f"Converting {model_name} to ONNX...")
    torch.onnx.export(
        model,
        (dummy_input,),
        onnx_path,
        export_params=True,
        opset_version=11,
        do_constant_folding=True,
        input_names=["input"],
        output_names=["output", "states"],
        dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}},
        verbose=False,
    )

    print(f"Saved to {onnx_path}")

    # Optimize
    opt_path = optimize_onnx_model(onnx_path)

    # Verify
    verify_onnx_model(model, opt_path, input_shape)

    # Save model info
    info = {
        "model_name": model_name,
        "input_shape": list(input_shape),
        "onnx_path": os.path.basename(onnx_path),
        "optimized_path": os.path.basename(opt_path),
        "file_size": os.path.getsize(opt_path),
        "opset_version": 11,
    }

    info_path = os.path.join(output_dir, f"{model_name}_info.json")
    with open(info_path, "w") as f:
        json.dump(info, f, indent=2)

    return opt_path


def main():
    parser = argparse.ArgumentParser(
        description="Enhanced PyTorch to ONNX converter for event-based models"
    )
    parser.add_argument(
        "--checkpoint",
        type=str,
        required=True,
        help="Path to PyTorch checkpoint (.pth or .pth.tar)",
    )
    parser.add_argument(
        "--model-name", type=str, default="e2vid", help="Name for the output model"
    )
    parser.add_argument(
        "--output-dir",
        type=str,
        default="models/onnx",
        help="Output directory for ONNX models",
    )
    parser.add_argument("--height", type=int, default=256, help="Input height")
    parser.add_argument("--width", type=int, default=256, help="Input width")
    parser.add_argument(
        "--num-bins", type=int, default=5, help="Number of time bins in voxel grid"
    )

    args = parser.parse_args()

    # Create output directory
    output_dir = Path(args.output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    # Load model
    if "e2vid" in args.model_name.lower():
        model = load_e2vid_from_checkpoint(args.checkpoint)
    else:
        print(f"Unknown model type: {args.model_name}")
        return

    # Convert to ONNX
    input_shape = (1, args.num_bins, args.height, args.width)
    convert_model_to_onnx(model, args.model_name, str(output_dir), input_shape)


if __name__ == "__main__":
    main()