-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdat.py
More file actions
670 lines (607 loc) · 21.6 KB
/
fdat.py
File metadata and controls
670 lines (607 loc) · 21.6 KB
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
# fdat_arch.py
#
# A standalone, well-documented implementation of the FDAT architecture
# for image super-resolution.
"""
Standalone FDAT (FastDAT) Architecture
DAT Inspired Lightweight Attention Network for Real World Image Super Resolution
External dependencies required:
- torch
- numpy
- spandrel (for DySample and DropPath)
- pip install spandrel
"""
import collections.abc
import math
from collections import OrderedDict
from itertools import repeat
from typing import Literal
import numpy as np
import torch
import torch.nn.functional as F
from torch import Tensor, nn
from torch.nn import init
from torch.nn.init import trunc_normal_
from torch.nn.modules.batchnorm import _BatchNorm
# External dependencies from spandrel
try:
from spandrel.architectures.__arch_helpers.dysample import DySample
from spandrel.util.timm import DropPath
except ImportError as e:
raise ImportError(
"Missing spandrel dependency. Install with: pip install spandrel"
) from e
# --------------------------------------------
# Utility functions
# --------------------------------------------
def _ntuple(n):
def parse(x):
if isinstance(x, collections.abc.Iterable):
return x
return tuple(repeat(x, n))
return parse
to_2tuple = _ntuple(2)
@torch.no_grad()
def default_init_weights(module_list, scale=1, bias_fill=0, **kwargs) -> None:
"""Initialize weights using Kaiming Normal initialization."""
if not isinstance(module_list, list):
module_list = [module_list]
for module in module_list:
for m in module.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight, **kwargs)
m.weight.data *= scale
if m.bias is not None:
m.bias.data.fill_(bias_fill)
elif isinstance(m, nn.Linear):
init.kaiming_normal_(m.weight, **kwargs)
m.weight.data *= scale
if m.bias is not None:
m.bias.data.fill_(bias_fill)
elif isinstance(m, _BatchNorm):
init.constant_(m.weight, 1)
if m.bias is not None:
m.bias.data.fill_(bias_fill)
# Type definitions
SampleMods = Literal[
"conv",
"pixelshuffledirect",
"pixelshuffle",
"nearest+conv",
"dysample",
]
SampleMods3 = Literal[SampleMods, "transpose+conv"]
class UniUpsampleV3(nn.Sequential):
"""Unified upsampling module with multiple upsampling methods."""
def __init__(
self,
upsample: SampleMods3,
scale: int = 2,
in_dim: int = 64,
out_dim: int = 3,
mid_dim: int = 64, # Only pixelshuffle and DySample
group: int = 4, # Only DySample
) -> None:
m = []
if scale == 1 or upsample == "conv":
m.append(nn.Conv2d(in_dim, out_dim, 3, 1, 1))
elif upsample == "pixelshuffledirect":
m.extend(
[nn.Conv2d(in_dim, out_dim * scale**2, 3, 1, 1), nn.PixelShuffle(scale)]
)
elif upsample == "pixelshuffle":
m.extend([nn.Conv2d(in_dim, mid_dim, 3, 1, 1), nn.LeakyReLU(inplace=True)])
if (scale & (scale - 1)) == 0: # scale = 2^n
for _ in range(int(math.log2(scale))):
m.extend(
[nn.Conv2d(mid_dim, 4 * mid_dim, 3, 1, 1), nn.PixelShuffle(2)]
)
elif scale == 3:
m.extend([nn.Conv2d(mid_dim, 9 * mid_dim, 3, 1, 1), nn.PixelShuffle(3)])
else:
raise ValueError(
f"scale {scale} is not supported. Supported scales: 2^n and 3."
)
m.append(nn.Conv2d(mid_dim, out_dim, 3, 1, 1))
elif upsample == "nearest+conv":
if (scale & (scale - 1)) == 0:
for _ in range(int(math.log2(scale))):
m.extend(
(
nn.Conv2d(in_dim, in_dim, 3, 1, 1),
nn.Upsample(scale_factor=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
)
)
m.extend(
(
nn.Conv2d(in_dim, in_dim, 3, 1, 1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
)
)
elif scale == 3:
m.extend(
(
nn.Conv2d(in_dim, in_dim, 3, 1, 1),
nn.Upsample(scale_factor=scale),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(in_dim, in_dim, 3, 1, 1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
)
)
else:
raise ValueError(
f"scale {scale} is not supported. Supported scales: 2^n and 3."
)
m.append(nn.Conv2d(in_dim, out_dim, 3, 1, 1))
elif upsample == "dysample":
if mid_dim != in_dim:
m.extend(
[nn.Conv2d(in_dim, mid_dim, 3, 1, 1), nn.LeakyReLU(inplace=True)]
)
dys_dim = mid_dim
else:
dys_dim = in_dim
m.append(DySample(dys_dim, out_dim, scale, group))
elif upsample == "transpose+conv":
if scale == 2:
m.append(nn.ConvTranspose2d(in_dim, out_dim, 4, 2, 1))
elif scale == 3:
m.append(nn.ConvTranspose2d(in_dim, out_dim, 3, 3, 0))
elif scale == 4:
m.extend(
[
nn.ConvTranspose2d(in_dim, in_dim, 4, 2, 1),
nn.GELU(),
nn.ConvTranspose2d(in_dim, out_dim, 4, 2, 1),
]
)
else:
raise ValueError(
f"scale {scale} is not supported. Supported scales: 2, 3, 4"
)
m.append(nn.Conv2d(out_dim, out_dim, 3, 1, 1))
else:
raise ValueError(
f"An invalid Upsample was selected. Please choose one of {SampleMods3.__args__}"
)
super().__init__(*m)
self.register_buffer(
"MetaUpsample",
torch.tensor(
[
3, # Block version
list(SampleMods3.__args__).index(upsample), # UpSample method index
scale,
in_dim,
out_dim,
mid_dim,
group,
],
dtype=torch.uint8,
),
)
# --------------------------------------------
# FDAT Components
# --------------------------------------------
class FastSpatialWindowAttention(nn.Module):
def __init__(self, dim, window_size=8, num_heads=4, qkv_bias=False) -> None:
super().__init__()
self.dim, self.ws, self.nh = dim, window_size, num_heads
self.scale = (dim // num_heads) ** -0.5
self.qkv, self.proj = (
nn.Linear(dim, dim * 3, bias=qkv_bias),
nn.Linear(dim, dim),
)
self.bias = nn.Parameter(
torch.zeros(num_heads, window_size * window_size, window_size * window_size)
)
trunc_normal_(self.bias, std=0.02)
def forward(self, x, H, W):
B, L, C = x.shape
pad_r, pad_b = (
(self.ws - W % self.ws) % self.ws,
(self.ws - H % self.ws) % self.ws,
)
if pad_r > 0 or pad_b > 0:
x = F.pad(x.view(B, H, W, C), (0, 0, 0, pad_r, 0, pad_b)).view(B, -1, C)
H_pad, W_pad = H + pad_b, W + pad_r
x = (
x.view(B, H_pad // self.ws, self.ws, W_pad // self.ws, self.ws, C)
.permute(0, 1, 3, 2, 4, 5)
.contiguous()
.view(-1, self.ws * self.ws, C)
)
qkv = (
self.qkv(x)
.view(-1, self.ws * self.ws, 3, self.nh, C // self.nh)
.permute(2, 0, 3, 1, 4)
)
q, k, v = qkv.unbind(0)
attn = (q * self.scale @ k.transpose(-2, -1)) + self.bias
x = (
(F.softmax(attn, dim=-1) @ v)
.transpose(1, 2)
.reshape(-1, self.ws * self.ws, C)
)
x = (
self.proj(x)
.view(B, H_pad // self.ws, W_pad // self.ws, self.ws, self.ws, C)
.permute(0, 1, 3, 2, 4, 5)
.contiguous()
.view(B, H_pad, W_pad, C)
)
if pad_r > 0 or pad_b > 0:
x = x[:, :H, :W, :].contiguous()
return x.view(B, L, C)
class FastChannelAttention(nn.Module):
def __init__(self, dim, num_heads=4, qkv_bias=False) -> None:
super().__init__()
self.nh = num_heads
self.temp = nn.Parameter(torch.ones(num_heads, 1, 1))
self.qkv, self.proj = (
nn.Linear(dim, dim * 3, bias=qkv_bias),
nn.Linear(dim, dim),
)
def forward(self, x, H, W): # H, W are unused but kept for API consistency
B, N, C = x.shape
qkv = self.qkv(x).view(B, N, 3, self.nh, C // self.nh).permute(2, 0, 3, 1, 4)
q, k, v = qkv.unbind(0)
q, k = (
F.normalize(q.transpose(-2, -1), dim=-1),
F.normalize(k.transpose(-2, -1), dim=-1),
)
attn = F.softmax((q @ k.transpose(-2, -1)) * self.temp, dim=-1)
return self.proj(
(attn @ v.transpose(-2, -1)).permute(0, 3, 1, 2).reshape(B, N, C)
)
class SimplifiedAIM(nn.Module):
def __init__(self, dim, reduction_ratio=8) -> None:
super().__init__()
self.sg = nn.Sequential(nn.Conv2d(dim, 1, 1, bias=False), nn.Sigmoid())
self.cg = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(dim, dim // reduction_ratio, 1, bias=False),
nn.GELU(),
nn.Conv2d(dim // reduction_ratio, dim, 1, bias=False),
nn.Sigmoid(),
)
def forward(self, attn_feat, conv_feat, interaction_type, H, W):
B, L, C = attn_feat.shape
if interaction_type == "spatial_modulates_channel":
sm = (
self.sg(attn_feat.transpose(1, 2).view(B, C, H, W))
.view(B, 1, L)
.transpose(1, 2)
)
return attn_feat + (conv_feat * sm)
else:
cm = (
self.cg(conv_feat.transpose(1, 2).view(B, C, H, W))
.view(B, C, 1)
.transpose(1, 2)
)
return (attn_feat * cm) + conv_feat
class SimplifiedFFN(nn.Module):
def __init__(self, dim, expansion_ratio=2.0, drop=0.0) -> None:
super().__init__()
hd = int(dim * expansion_ratio)
self.fc1, self.act, self.fc2 = (
nn.Linear(dim, hd, False),
nn.GELU(),
nn.Linear(hd, dim, False),
)
self.drop = nn.Dropout(drop)
self.smix = nn.Conv2d(hd, hd, 3, 1, 1, groups=hd, bias=False)
def forward(self, x, H, W):
B, L, C = x.shape
x = self.drop(self.act(self.fc1(x)))
x_s = (
self.smix(x.transpose(1, 2).view(B, x.shape[-1], H, W))
.view(B, x.shape[-1], L)
.transpose(1, 2)
)
return self.drop(self.fc2(x_s))
class SimplifiedDATBlock(nn.Module):
def __init__(self, dim, nh, ws, ffn_exp, aim_re, btype, dp, qkv_b=False) -> None:
super().__init__()
self.btype = btype
self.n1, self.n2 = nn.LayerNorm(dim), nn.LayerNorm(dim)
self.attn = (
FastSpatialWindowAttention(dim, ws, nh, qkv_b)
if btype == "spatial"
else FastChannelAttention(dim, nh, qkv_b)
)
self.conv = nn.Sequential(
nn.Conv2d(dim, dim, 3, 1, 1, groups=dim, bias=False), nn.GELU()
)
self.inter = SimplifiedAIM(dim, aim_re)
self.dp = DropPath(dp) if dp > 0.0 else nn.Identity()
self.ffn = SimplifiedFFN(dim, ffn_exp)
def _conv_fwd(self, x, H, W):
B, L, C = x.shape
return (
self.conv(x.transpose(1, 2).view(B, C, H, W)).view(B, C, L).transpose(1, 2)
)
def forward(self, x, H, W):
n1 = self.n1(x)
itype = (
"channel_modulates_spatial"
if self.btype == "spatial"
else "spatial_modulates_channel"
)
fused = self.inter(self.attn(n1, H, W), self._conv_fwd(n1, H, W), itype, H, W)
x = x + self.dp(fused)
x = x + self.dp(self.ffn(self.n2(x), H, W))
return x
class SimplifiedResidualGroup(nn.Module):
def __init__(self, dim, depth, nh, ws, ffn_exp, aim_re, pattern, dp_rates) -> None:
super().__init__()
self.blocks = nn.ModuleList(
[
SimplifiedDATBlock(
dim, nh, ws, ffn_exp, aim_re, pattern[i % len(pattern)], dp_rates[i]
)
for i in range(depth)
]
)
self.conv = nn.Conv2d(dim, dim, 3, 1, 1, bias=False)
def forward(self, x: Tensor) -> Tensor:
B, C, H, W = x.shape
x_seq = x.view(B, C, H * W).transpose(1, 2).contiguous()
for block in self.blocks:
x_seq = block(x_seq, H, W)
return self.conv(x_seq.transpose(1, 2).view(B, C, H, W)) + x
# --------------------------------------------
# Main FDAT Architecture
# --------------------------------------------
class FDAT(nn.Module):
"""Fast Dual Attention Transformer for Super-Resolution."""
def __init__(
self,
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 120,
num_groups: int = 4,
depth_per_group: int = 3,
num_heads: int = 4,
window_size: int = 8,
ffn_expansion_ratio: float = 2.0,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.1,
mid_dim: int = 64,
upsampler_type: SampleMods3 = "pixelshuffle",
img_range: float = 1.0,
) -> None:
if group_block_pattern is None:
group_block_pattern = ["spatial", "channel"]
super().__init__()
self.img_range, self.upscale = img_range, scale
self.mean = torch.zeros(1, 1, 1, 1)
self.conv_first = nn.Conv2d(num_in_ch, embed_dim, 3, 1, 1, bias=True)
ad = depth_per_group * len(group_block_pattern)
td = num_groups * ad
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, td)]
self.groups = nn.Sequential(
*[
SimplifiedResidualGroup(
embed_dim,
ad,
num_heads,
window_size,
ffn_expansion_ratio,
aim_reduction_ratio,
group_block_pattern,
dpr[i * ad : (i + 1) * ad],
)
for i in range(num_groups)
]
)
self.conv_after = nn.Conv2d(embed_dim, embed_dim, 3, 1, 1, bias=False)
self.upsampler = UniUpsampleV3(
upsampler_type, scale, embed_dim, num_out_ch, mid_dim, 4
)
self.apply(self._init_weights)
def _init_weights(self, m: nn.Module) -> None:
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=0.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Conv2d):
trunc_normal_(m.weight, std=0.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm | nn.GroupNorm):
if hasattr(m, "bias") and m.bias is not None:
nn.init.constant_(m.bias, 0)
if hasattr(m, "weight") and m.weight is not None:
nn.init.constant_(m.weight, 1.0)
def forward(self, x: Tensor) -> Tensor:
x_shallow = self.conv_first(x)
x_deep = self.groups(x_shallow)
x_deep = self.conv_after(x_deep)
x_out = self.upsampler(x_deep + x_shallow)
return x_out
# --------------------------------------------
# Predefined model configurations
# --------------------------------------------
def fdat_tiny(
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 96,
num_groups: int = 2,
depth_per_group: int = 2,
num_heads: int = 3,
window_size: int = 8,
ffn_expansion_ratio: float = 1.5,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.05,
upsampler_type: SampleMods3 = "pixelshuffle",
img_range: float = 1.0,
) -> FDAT:
"""FDAT Tiny configuration."""
return FDAT(
num_in_ch=num_in_ch,
num_out_ch=num_out_ch,
scale=scale,
embed_dim=embed_dim,
num_groups=num_groups,
depth_per_group=depth_per_group,
num_heads=num_heads,
window_size=window_size,
ffn_expansion_ratio=ffn_expansion_ratio,
aim_reduction_ratio=aim_reduction_ratio,
group_block_pattern=group_block_pattern,
drop_path_rate=drop_path_rate,
upsampler_type=upsampler_type,
img_range=img_range,
)
def fdat_light(
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 108,
num_groups: int = 3,
depth_per_group: int = 2,
num_heads: int = 4,
window_size: int = 8,
ffn_expansion_ratio: float = 2.0,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.08,
upsampler_type: SampleMods3 = "pixelshuffle",
img_range: float = 1.0,
) -> FDAT:
"""FDAT Light configuration."""
return FDAT(
num_in_ch=num_in_ch,
num_out_ch=num_out_ch,
scale=scale,
embed_dim=embed_dim,
num_groups=num_groups,
depth_per_group=depth_per_group,
num_heads=num_heads,
window_size=window_size,
ffn_expansion_ratio=ffn_expansion_ratio,
aim_reduction_ratio=aim_reduction_ratio,
group_block_pattern=group_block_pattern,
drop_path_rate=drop_path_rate,
upsampler_type=upsampler_type,
img_range=img_range,
)
def fdat_medium(
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 120,
num_groups: int = 4,
depth_per_group: int = 3,
num_heads: int = 4,
window_size: int = 8,
ffn_expansion_ratio: float = 2.0,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.1,
upsampler_type: SampleMods3 = "transpose+conv",
img_range: float = 1.0,
) -> FDAT:
"""FDAT Medium configuration."""
return FDAT(
num_in_ch=num_in_ch,
num_out_ch=num_out_ch,
scale=scale,
embed_dim=embed_dim,
num_groups=num_groups,
depth_per_group=depth_per_group,
num_heads=num_heads,
window_size=window_size,
ffn_expansion_ratio=ffn_expansion_ratio,
aim_reduction_ratio=aim_reduction_ratio,
group_block_pattern=group_block_pattern,
drop_path_rate=drop_path_rate,
upsampler_type=upsampler_type,
img_range=img_range,
)
def fdat_large(
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 180,
num_groups: int = 4,
depth_per_group: int = 4,
num_heads: int = 6,
window_size: int = 8,
ffn_expansion_ratio: float = 2.0,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.1,
upsampler_type: SampleMods3 = "transpose+conv",
img_range: float = 1.0,
) -> FDAT:
"""FDAT Large configuration."""
return FDAT(
num_in_ch=num_in_ch,
num_out_ch=num_out_ch,
scale=scale,
embed_dim=embed_dim,
num_groups=num_groups,
depth_per_group=depth_per_group,
num_heads=num_heads,
window_size=window_size,
ffn_expansion_ratio=ffn_expansion_ratio,
aim_reduction_ratio=aim_reduction_ratio,
group_block_pattern=group_block_pattern,
drop_path_rate=drop_path_rate,
upsampler_type=upsampler_type,
img_range=img_range,
)
def fdat_xl(
num_in_ch: int = 3,
num_out_ch: int = 3,
scale: int = 4,
embed_dim: int = 180,
num_groups: int = 6,
depth_per_group: int = 6,
num_heads: int = 6,
window_size: int = 8,
ffn_expansion_ratio: float = 2.0,
aim_reduction_ratio: int = 8,
group_block_pattern: list[str] | None = None,
drop_path_rate: float = 0.1,
upsampler_type: SampleMods3 = "transpose+conv",
img_range: float = 1.0,
) -> FDAT:
"""FDAT XL configuration."""
return FDAT(
num_in_ch=num_in_ch,
num_out_ch=num_out_ch,
scale=scale,
embed_dim=embed_dim,
num_groups=num_groups,
depth_per_group=depth_per_group,
num_heads=num_heads,
window_size=window_size,
ffn_expansion_ratio=ffn_expansion_ratio,
aim_reduction_ratio=aim_reduction_ratio,
group_block_pattern=group_block_pattern,
drop_path_rate=drop_path_rate,
upsampler_type=upsampler_type,
img_range=img_range,
)
# Example usage:
if __name__ == "__main__":
# Create a model
model = fdat_tiny()
# Test with dummy input
x = torch.randn(1, 3, 64, 64) # Batch=1, Channels=3, Height=64, Width=64
with torch.no_grad():
output = model(x)
print(f"Input shape: {x.shape}")
print(f"Output shape: {output.shape}")
# Count parameters
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Total trainable parameters: {total_params:,}")