-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptics.py
48 lines (37 loc) · 1.12 KB
/
optics.py
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
import torch
import torch.nn as nn
import numpy as np
def hadamard(n):
if n == 1:
return np.array([[1]])
else:
h = hadamard(n // 2)
return np.block([[h, h], [h, -h]])
class OpticsSPC(nn.Module):
def __init__(self, input_size: tuple, snapshots, matrix):
super(OpticsSPC, self).__init__()
_, self.M, self.N = input_size
self.snapshots = snapshots
ca = torch.tensor(matrix).float()
ca = ca.view(self.snapshots, self.M, self.N)
self.cas = ca
def forward(self, x):
y = self.forward_pass(x)
x = self.transpose_pass(y)
return x
def forward_pass(self, x):
ca = self.get_coded_aperture().to(x.device)
y = x * ca
y = torch.sum(y, dim=(-2, -1))
y = y.unsqueeze(-1).unsqueeze(-1)
return y
def transpose_pass(self, y):
ca = self.get_coded_aperture().to(y.device)
x = y * ca
x = torch.sum(x, dim=1)
x = x.unsqueeze(1)
x = x / torch.max(x)
return x
def get_coded_aperture(self):
ca = self.cas.unsqueeze(0)
return ca