/
fourier_on_images.py
251 lines (196 loc) · 8.36 KB
/
fourier_on_images.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
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
import matplotlib.pyplot as plt
import operator
from functools import reduce
from functools import partial
from timeit import default_timer
from utilities3 import *
import torchvision
import torchvision.transforms as transforms
torch.manual_seed(0)
np.random.seed(0)
#Complex multiplication
def compl_mul2d(a, b):
op = partial(torch.einsum, "bctq,dctq->bdtq")
return torch.stack([
op(a[..., 0], b[..., 0]) - op(a[..., 1], b[..., 1]),
op(a[..., 1], b[..., 0]) + op(a[..., 0], b[..., 1])
], dim=-1)
class SpectralConv2d(nn.Module):
def __init__(self, in_channels, out_channels, modes1, modes2):
super(SpectralConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.modes1 = modes1 #Number of Fourier modes to multiply, at most floor(N/2) + 1
self.modes2 = modes2
self.scale = (1 / (in_channels * out_channels))
self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, 2))
self.weights2 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, 2))
def forward(self, x):
batchsize = x.shape[0]
#Compute Fourier coeffcients up to factor of e^(- something constant)
x_ft = torch.rfft(x, 2, normalized=True, onesided=True)
# Multiply relevant Fourier modes
out_ft = torch.zeros(batchsize, self.in_channels, x.size(-2), x.size(-1)//2 + 1, 2, device=x.device)
out_ft[:, :, :self.modes1, :self.modes2] = \
compl_mul2d(x_ft[:, :, :self.modes1, :self.modes2], self.weights1)
out_ft[:, :, -self.modes1:, :self.modes2] = \
compl_mul2d(x_ft[:, :, -self.modes1:, :self.modes2], self.weights2)
#Return to physical space
x = torch.irfft(out_ft, 2, normalized=True, onesided=True, signal_sizes=( x.size(-2), x.size(-1)))
return x
class SimpleBlock2d(nn.Module):
def __init__(self, modes):
super(SimpleBlock2d, self).__init__()
self.conv1 = SpectralConv2d(1, 16, modes=5)
self.conv2 = SpectralConv2d(16, 32, modes=5)
self.conv3 = SpectralConv2d(32, 64, modes=5)
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(64 * 14 * 14, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = self.conv3(x)
x = self.pool(x)
x = x.view(-1, 64 * 14 * 14)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class Net2d(nn.Module):
def __init__(self):
super(Net2d, self).__init__()
self.conv = SimpleBlock2d(10)
def forward(self, x):
x = self.conv(x)
return x.squeeze(-1)
def count_params(self):
c = 0
for p in self.parameters():
c += reduce(operator.mul, list(p.size()))
return c
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, modes=10):
super(BasicBlock, self).__init__()
self.conv1 = SpectralConv2d(in_planes, planes, modes=modes)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = SpectralConv2d(planes, planes, modes=modes)
self.bn2 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
SpectralConv2d(in_planes, self.expansion*planes, modes=modes),
nn.BatchNorm2d(self.expansion*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=10):
super(ResNet, self).__init__()
self.in_planes = 32
self.conv1 = SpectralConv2d(3, 32, modes=10)
self.bn1 = nn.BatchNorm2d(32)
self.layer1 = self._make_layer(block, 32, num_blocks[0], stride=1, modes=3)
self.layer2 = self._make_layer(block, 32, num_blocks[1], stride=1, modes=3)
self.layer3 = self._make_layer(block, 32, num_blocks[2], stride=1, modes=3)
self.layer4 = self._make_layer(block, 32, num_blocks[3], stride=1, modes=3)
self.linear1 = nn.Linear(32*64*block.expansion, num_classes)
# self.linear2 = nn.Linear(100, num_classes)
def _make_layer(self, block, planes, num_blocks, stride, modes=10):
strides = [stride] + [1]*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride, modes))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = F.relu(out)
out = self.layer1(out)
# out = F.avg_pool2d(out, 2)
out = self.layer2(out)
# out = F.avg_pool2d(out, 2)
out = self.layer3(out)
# out = F.avg_pool2d(out, 2)
out = self.layer4(out)
out = F.avg_pool2d(out, 4)
# print(out.shape)
out = out.view(out.size(0), -1)
out = self.linear1(out)
# out = F.relu(out)
# out = self.linear2(out)
return out
def ResNet18():
return ResNet(BasicBlock, [3, 4, 23, 3])
## Mnist
# transform = transforms.Compose([transforms.ToTensor(),
# transforms.Normalize((0.5,), (0.5,)),
# ])
# trainset = torchvision.datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=True, transform=transform)
# testset = torchvision.datasets.MNIST('PATH_TO_STORE_TESTSET', download=True, train=False, transform=transform)
# trainloader = torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=True)
# testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=True)
## Cifar10
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=100,
shuffle=True, num_workers=4)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=100,
shuffle=False, num_workers=4)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# model = Net2d().cuda()
model = ResNet18().cuda()
# model = torch.load('results/fourier_on_images')
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.75)
for epoch in range(50): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data[0].cuda(), data[1].cuda()
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 100 == 99: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data[0].cuda(), data[1].cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %f %%' % (
100 * correct / total))
torch.save(model, 'results/fourier_on_images_mnist_100')