-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
688 lines (541 loc) · 22.7 KB
/
Copy pathmodel.py
File metadata and controls
688 lines (541 loc) · 22.7 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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
"""
SPI (Single-Pixel Imaging) Model with Fixed Patterns and GAN-based Reconstruction
Key Changes from LED:
1. Fixed measurement patterns (Hadamard zigzag or Random binary)
2. Decoder-only trainable network (Generator)
3. Added Discriminator for GAN training
4. Fixed 1024 measurements with -1/+1 binarization
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def generate_hadamard_matrix(n):
"""
Generate Hadamard matrix of size n x n using Sylvester's construction.
n must be a power of 2.
Returns:
Hadamard matrix with values -1 and +1
"""
if n == 1:
return np.array([[1]])
# Find the largest power of 2 <= n
k = int(np.log2(n))
size = 2 ** k
H = np.array([[1]])
for _ in range(k):
H = np.block([[H, H], [H, -H]])
return H
def zigzag_order(n):
"""
Generate zigzag ordering indices for an n x n matrix.
Returns:
Array of indices in zigzag order
"""
indices = []
for s in range(2 * n - 1):
if s % 2 == 0:
# Even diagonal: go down-left
for i in range(min(s, n - 1), max(0, s - n + 1) - 1, -1):
j = s - i
indices.append(i * n + j)
else:
# Odd diagonal: go up-right
for i in range(max(0, s - n + 1), min(s, n - 1) + 1):
j = s - i
indices.append(i * n + j)
return np.array(indices)
def generate_hadamard_zigzag_patterns(img_size, n_measurements):
"""
Generate Hadamard patterns reordered by zigzag (low-frequency first).
Args:
img_size: Image size (H = W)
n_measurements: Number of measurements to select
Returns:
Pattern matrix of shape (n_measurements, img_size * img_size) with values -1/+1
"""
n_pixels = img_size * img_size
# Find the smallest power of 2 >= n_pixels
hadamard_size = 2 ** int(np.ceil(np.log2(n_pixels)))
# Generate full Hadamard matrix
H = generate_hadamard_matrix(hadamard_size)
# Truncate to n_pixels
H = H[:n_pixels, :n_pixels]
# Apply zigzag reordering to select low-frequency patterns first
zigzag_idx = zigzag_order(img_size)
# Reorder rows according to zigzag pattern
H_zigzag = H[zigzag_idx, :]
# Select first n_measurements patterns
patterns = H_zigzag[:n_measurements, :].astype(np.float32)
return patterns
def generate_random_binary_patterns(img_size, n_measurements, seed=42):
"""
Generate random binary patterns with values -1/+1.
Args:
img_size: Image size (H = W)
n_measurements: Number of measurements
seed: Random seed for reproducibility
Returns:
Pattern matrix of shape (n_measurements, img_size * img_size) with values -1/+1
"""
np.random.seed(seed)
n_pixels = img_size * img_size
# Generate random 0/1 matrix and convert to -1/+1
patterns = np.random.randint(0, 2, size=(n_measurements, n_pixels)).astype(np.float32)
patterns = patterns * 2 - 1 # Convert 0/1 to -1/+1
return patterns
class FixedPatternEncoder(nn.Module):
"""
Fixed pattern encoder for single-pixel imaging.
Uses pre-generated -1/+1 binary patterns (not learnable).
Supports Hadamard zigzag and Random binary patterns.
Args:
img_size: Image size (H = W, default: 128)
n_measurements: Number of measurements (default: 1024)
pattern_type: Type of patterns ('hadamard' or 'random')
seed: Random seed for random patterns
"""
def __init__(self, img_size=128, n_measurements=1024, pattern_type='hadamard', seed=42, learnable=False):
super(FixedPatternEncoder, self).__init__()
self.img_size = img_size
self.n_pixels = img_size * img_size
self.n_measurements = n_measurements
self.pattern_type = pattern_type
self.learnable = learnable
# Generate patterns
if pattern_type == 'hadamard':
patterns = generate_hadamard_zigzag_patterns(img_size, n_measurements)
elif pattern_type == 'random':
patterns = generate_random_binary_patterns(img_size, n_measurements, seed)
else:
raise ValueError(f"Unknown pattern type: {pattern_type}. Use 'hadamard' or 'random'.")
patterns = torch.from_numpy(patterns).float()
if learnable:
# Initialize learnable patterns near zero so sign can flip early
init = torch.randn_like(patterns) * 0.01
self.patterns = nn.Parameter(init)
else:
# Register as buffer (not trainable, but moves with model to device)
self.register_buffer('patterns', patterns)
print(f"FixedPatternEncoder initialized:")
print(f" Pattern type: {pattern_type}")
print(f" Shape: {self.patterns.shape}")
print(f" Values: {torch.unique(self.patterns).detach().cpu().numpy()}")
def forward(self, x):
"""
Compute measurements using fixed patterns.
Args:
x: Input image batch of shape (B, 1, H, W)
Returns:
measurements: Tensor of shape (B, m)
"""
batch_size = x.shape[0]
# Flatten image: (B, 1, H, W) -> (B, n)
x_flat = x.view(batch_size, -1)
# Compute measurements: y = E @ x, using F.linear with patterns
if self.learnable:
patterns = BinarizeSTE.apply(self.patterns)
else:
patterns = self.patterns
measurements = F.linear(x_flat, patterns)
return measurements
def get_patterns(self):
"""Return the fixed pattern matrix."""
if self.learnable:
return BinarizeSTE.apply(self.patterns)
return self.patterns
class BinarizeSTE(torch.autograd.Function):
"""
Straight-through estimator for binary patterns.
Forward: sign
Backward: pass-through gradient (optionally clipped).
"""
@staticmethod
def forward(ctx, w):
ctx.save_for_backward(w)
return w.sign()
@staticmethod
def backward(ctx, grad_output):
(w,) = ctx.saved_tensors
grad = grad_output.clone()
grad[w.abs() > 1] = 0
return grad
class DoubleConv(nn.Module):
"""Double convolution block for U-Net."""
def __init__(self, in_channels, out_channels, mid_channels=None):
super(DoubleConv, self).__init__()
if mid_channels is None:
mid_channels = out_channels
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(mid_channels),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(0.2, inplace=True)
)
def forward(self, x):
return self.double_conv(x)
class Down(nn.Module):
"""Downsampling block for U-Net encoder."""
def __init__(self, in_channels, out_channels):
super(Down, self).__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
class Up(nn.Module):
"""Upsampling block for U-Net decoder."""
def __init__(self, in_channels, out_channels, bilinear=True):
super(Up, self).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
# Handle size mismatch
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
# Concatenate skip connection
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
class UNet(nn.Module):
"""
U-Net architecture for image reconstruction refinement.
Args:
in_channels: Number of input channels (default: 1)
out_channels: Number of output channels (default: 1)
base_features: Number of features in first layer (default: 64)
bilinear: Use bilinear upsampling (default: True)
"""
def __init__(self, in_channels=1, out_channels=1, base_features=64, bilinear=True):
super(UNet, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.bilinear = bilinear
factor = 2 if bilinear else 1
# Encoder
self.inc = DoubleConv(in_channels, base_features)
self.down1 = Down(base_features, base_features * 2)
self.down2 = Down(base_features * 2, base_features * 4)
self.down3 = Down(base_features * 4, base_features * 8)
self.down4 = Down(base_features * 8, base_features * 16 // factor)
# Decoder
self.up1 = Up(base_features * 16, base_features * 8 // factor, bilinear)
self.up2 = Up(base_features * 8, base_features * 4 // factor, bilinear)
self.up3 = Up(base_features * 4, base_features * 2 // factor, bilinear)
self.up4 = Up(base_features * 2, base_features, bilinear)
# Output
self.outc = nn.Conv2d(base_features, out_channels, kernel_size=1)
def forward(self, x):
# Encoder
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
# Decoder with skip connections
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
# Output
logits = self.outc(x)
return logits
class Generator(nn.Module):
"""
Generator (Decoder) for reconstructing images from measurements.
Architecture:
1. Standardization: Instance Normalization (no affine parameters)
2. Initial Mapping: Linear + BatchNorm + LeakyReLU
3. Reshape to image
4. Refinement: U-Net
Args:
n_measurements: Number of measurements (m)
n_pixels: Number of pixels in the image (n = H * W)
img_size: Image size (H = W)
base_features: Base features for U-Net (default: 64)
"""
def __init__(self, n_measurements, n_pixels, img_size, base_features=64):
super(Generator, self).__init__()
self.n_measurements = n_measurements
self.n_pixels = n_pixels
self.img_size = img_size
# Standardization: Instance Normalization without affine parameters
self.standardization = nn.InstanceNorm1d(1, affine=False)
# Initial Mapping: m -> n
self.initial_mapping = nn.Sequential(
nn.Linear(n_measurements, n_pixels),
nn.BatchNorm1d(n_pixels),
nn.LeakyReLU(0.2, inplace=True)
)
# Refinement Network: U-Net
self.unet = UNet(in_channels=1, out_channels=1, base_features=base_features)
def forward(self, measurements):
"""
Forward pass: reconstruct image from measurements.
Args:
measurements: Tensor of shape (B, m)
Returns:
Reconstructed image of shape (B, 1, H, W)
"""
batch_size = measurements.shape[0]
# Standardization: (B, m) -> (B, 1, m) -> InstanceNorm -> (B, m)
x = measurements.unsqueeze(1)
x = self.standardization(x)
x = x.squeeze(1)
# Initial Mapping: (B, m) -> (B, n)
x = self.initial_mapping(x)
# Reshape to image: (B, n) -> (B, 1, H, W)
x = x.view(batch_size, 1, self.img_size, self.img_size)
# Refinement with U-Net
x = self.unet(x)
# Ensure output is in valid range [0, 1]
x = torch.sigmoid(x)
return x
class Discriminator(nn.Module):
"""
PatchGAN Discriminator for GAN-based training.
Uses a series of convolutions to classify patches as real or fake.
Output is a map of predictions for overlapping patches.
Args:
in_channels: Number of input channels (default: 1)
base_features: Number of features in first layer (default: 64)
n_layers: Number of discriminator layers (default: 3)
"""
def __init__(self, in_channels=1, base_features=64, n_layers=3):
super(Discriminator, self).__init__()
layers = []
# First layer (no normalization)
layers.append(nn.Conv2d(in_channels, base_features, kernel_size=4, stride=2, padding=1))
layers.append(nn.LeakyReLU(0.2, inplace=True))
nf_mult = 1
nf_mult_prev = 1
# Intermediate layers
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2 ** n, 8)
layers.append(nn.Conv2d(base_features * nf_mult_prev, base_features * nf_mult,
kernel_size=4, stride=2, padding=1, bias=False))
layers.append(nn.BatchNorm2d(base_features * nf_mult))
layers.append(nn.LeakyReLU(0.2, inplace=True))
# Second to last layer
nf_mult_prev = nf_mult
nf_mult = min(2 ** n_layers, 8)
layers.append(nn.Conv2d(base_features * nf_mult_prev, base_features * nf_mult,
kernel_size=4, stride=1, padding=1, bias=False))
layers.append(nn.BatchNorm2d(base_features * nf_mult))
layers.append(nn.LeakyReLU(0.2, inplace=True))
# Output layer (no sigmoid - use with BCEWithLogitsLoss or LSGAN loss)
layers.append(nn.Conv2d(base_features * nf_mult, 1, kernel_size=4, stride=1, padding=1))
self.model = nn.Sequential(*layers)
def forward(self, x):
"""
Forward pass.
Args:
x: Input image (B, 1, H, W)
Returns:
Discrimination map (B, 1, H', W')
"""
return self.model(x)
class SPIModel(nn.Module):
"""
SPI (Single-Pixel Imaging) model with fixed patterns.
Uses pre-generated patterns (Hadamard zigzag or Random binary) for measurement,
and a trainable Generator (Decoder) for reconstruction.
Args:
img_size: Image size (H = W, default: 128)
n_measurements: Number of measurements (default: 1024)
noise_std: Standard deviation of Gaussian noise (default: 0.05)
base_features: Base features for U-Net decoder (default: 64)
pattern_type: Type of patterns ('hadamard' or 'random')
seed: Random seed for random patterns
"""
def __init__(self, img_size=128, n_measurements=1024, noise_std=0.05,
base_features=64, pattern_type='hadamard', seed=42, learnable_patterns=False):
super(SPIModel, self).__init__()
self.img_size = img_size
self.n_pixels = img_size * img_size
self.n_measurements = n_measurements
self.noise_std = noise_std
self.pattern_type = pattern_type
# Pattern Encoder (optionally learnable)
self.encoder = FixedPatternEncoder(
img_size=img_size,
n_measurements=n_measurements,
pattern_type=pattern_type,
seed=seed,
learnable=learnable_patterns
)
# Generator (Decoder) - trainable
self.generator = Generator(
n_measurements=n_measurements,
n_pixels=self.n_pixels,
img_size=img_size,
base_features=base_features
)
def forward(self, x, add_noise=True):
"""
Forward pass through the SPI model.
Args:
x: Input image batch of shape (B, 1, H, W)
add_noise: Whether to add Gaussian noise to measurements
Returns:
Reconstructed image of shape (B, 1, H, W)
"""
# Encode: get measurements using fixed patterns
measurements = self.encoder(x)
# Add Gaussian noise (during training)
if add_noise and self.training and self.noise_std > 0:
noise = torch.randn_like(measurements) * self.noise_std
measurements = measurements + noise
# Decode: reconstruct image
reconstructed = self.generator(measurements)
return reconstructed
def encode(self, x, add_noise=False):
"""
Encode image to measurements.
Args:
x: Input image batch of shape (B, 1, H, W)
add_noise: Whether to add noise
Returns:
measurements: Tensor of shape (B, m)
"""
measurements = self.encoder(x)
if add_noise and self.noise_std > 0:
noise = torch.randn_like(measurements) * self.noise_std
measurements = measurements + noise
return measurements
def decode(self, measurements):
"""
Decode measurements to image.
Args:
measurements: Tensor of shape (B, m)
Returns:
Reconstructed image of shape (B, 1, H, W)
"""
return self.generator(measurements)
def get_patterns(self):
"""Return the fixed pattern matrix."""
return self.encoder.get_patterns()
def get_compression_ratio(self):
"""Calculate compression ratio: n_pixels / n_measurements."""
return self.n_pixels / self.n_measurements
def get_sampling_ratio(self):
"""Calculate sampling ratio: n_measurements / n_pixels."""
return self.n_measurements / self.n_pixels
def create_spi_model(img_size=128, n_measurements=1024, noise_std=0.05,
base_features=64, pattern_type='hadamard', seed=42,
learnable_patterns=False):
"""
Create SPI model with specified configuration.
Args:
img_size: Image size (H = W)
n_measurements: Number of measurements (default: 1024)
noise_std: Standard deviation of Gaussian noise (default: 0.05)
base_features: Base features for U-Net decoder
pattern_type: 'hadamard' or 'random'
seed: Random seed for random patterns
learnable_patterns: Whether encoder patterns are learnable (STE binarized)
Returns:
SPIModel instance
"""
n_pixels = img_size * img_size
sampling_ratio = n_measurements / n_pixels
model = SPIModel(
img_size=img_size,
n_measurements=n_measurements,
noise_std=noise_std,
base_features=base_features,
pattern_type=pattern_type,
seed=seed,
learnable_patterns=learnable_patterns
)
print(f"Created SPI model:")
print(f" Image size: {img_size}x{img_size}")
print(f" Pixels: {n_pixels}")
print(f" Measurements: {n_measurements}")
print(f" Sampling ratio: {sampling_ratio*100:.2f}%")
print(f" Compression ratio: {1/sampling_ratio:.2f}x")
print(f" Noise std: {noise_std}")
print(f" Pattern type: {pattern_type}")
return model
def create_discriminator(in_channels=1, base_features=64, n_layers=3):
"""
Create PatchGAN discriminator.
Args:
in_channels: Number of input channels
base_features: Base features
n_layers: Number of layers
Returns:
Discriminator instance
"""
return Discriminator(in_channels=in_channels, base_features=base_features, n_layers=n_layers)
# Backward compatibility aliases
Decoder = Generator
if __name__ == "__main__":
# Test the model
print("Testing SPI model components...")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
# Test parameters
img_size = 128
batch_size = 8
n_measurements = 1024
noise_std = 0.05
# Create test input
x = torch.rand(batch_size, 1, img_size, img_size).to(device)
print(f"\nInput shape: {x.shape}")
# Test Hadamard patterns
print("\n--- Testing Hadamard Zigzag Patterns ---")
encoder_hadamard = FixedPatternEncoder(img_size, n_measurements, 'hadamard').to(device)
measurements = encoder_hadamard(x)
print(f"Measurements shape: {measurements.shape}")
print(f"Pattern values: {torch.unique(encoder_hadamard.get_patterns())}")
# Test Random patterns
print("\n--- Testing Random Binary Patterns ---")
encoder_random = FixedPatternEncoder(img_size, n_measurements, 'random').to(device)
measurements_rand = encoder_random(x)
print(f"Measurements shape: {measurements_rand.shape}")
print(f"Pattern values: {torch.unique(encoder_random.get_patterns())}")
# Test Generator
print("\n--- Testing Generator ---")
generator = Generator(n_measurements, img_size * img_size, img_size).to(device)
reconstructed = generator(measurements)
print(f"Reconstructed shape: {reconstructed.shape}")
# Test Discriminator
print("\n--- Testing Discriminator ---")
discriminator = Discriminator(in_channels=1, base_features=64, n_layers=3).to(device)
disc_out = discriminator(x)
print(f"Discriminator output shape: {disc_out.shape}")
# Test full SPI model
print("\n--- Testing Full SPI Model ---")
model = SPIModel(img_size=img_size, n_measurements=n_measurements,
noise_std=noise_std, pattern_type='hadamard').to(device)
# Count parameters
total_params = sum(p.numel() for p in model.parameters())
encoder_params = sum(p.numel() for p in model.encoder.parameters())
generator_params = sum(p.numel() for p in model.generator.parameters())
disc_params = sum(p.numel() for p in discriminator.parameters())
print(f"Total model parameters: {total_params:,}")
print(f"Encoder parameters (fixed, non-trainable): {encoder_params:,}")
print(f"Generator parameters (trainable): {generator_params:,}")
print(f"Discriminator parameters: {disc_params:,}")
# Forward pass
model.train()
output = model(x, add_noise=True)
print(f"Output shape: {output.shape}")
print(f"Output range: [{output.min():.4f}, {output.max():.4f}]")
# Test with sampling ratio
print("\n--- Testing create_spi_model ---")
model2 = create_spi_model(img_size=128, n_measurements=1024, pattern_type='random')
print(f"Sampling ratio: {model2.get_sampling_ratio()*100:.2f}%")
print(f"Compression ratio: {model2.get_compression_ratio():.2f}x")
print("\nmodel.py: OK")