forked from gegewen/ufno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufno.py
217 lines (178 loc) · 9.36 KB
/
ufno.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import operator
from functools import reduce
from functools import partial
torch.manual_seed(0)
class SpectralConv3d(nn.Module):
def __init__(self, in_channels, out_channels, modes1, modes2, modes3):
super(SpectralConv3d, self).__init__()
"""
3D Fourier layer. It does FFT, linear transform, and Inverse FFT.
"""
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.modes3 = modes3
self.scale = (1 / (in_channels * out_channels))
self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3, dtype=torch.cfloat))
self.weights2 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3, dtype=torch.cfloat))
self.weights3 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3, dtype=torch.cfloat))
self.weights4 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3, dtype=torch.cfloat))
# Complex multiplication
def compl_mul3d(self, input, weights):
# (batch, in_channel, x,y,t ), (in_channel, out_channel, x,y,t) -> (batch, out_channel, x,y,t)
return torch.einsum("bixyz,ioxyz->boxyz", input, weights)
def forward(self, x):
batchsize = x.shape[0]
#Compute Fourier coeffcients up to factor of e^(- something constant)
x_ft = torch.fft.rfftn(x, dim=[-3,-2,-1])
# Multiply relevant Fourier modes
out_ft = torch.zeros(batchsize, self.out_channels, x.size(-3), x.size(-2), x.size(-1)//2 + 1, dtype=torch.cfloat, device=x.device)
out_ft[:, :, :self.modes1, :self.modes2, :self.modes3] = \
self.compl_mul3d(x_ft[:, :, :self.modes1, :self.modes2, :self.modes3], self.weights1)
out_ft[:, :, -self.modes1:, :self.modes2, :self.modes3] = \
self.compl_mul3d(x_ft[:, :, -self.modes1:, :self.modes2, :self.modes3], self.weights2)
out_ft[:, :, :self.modes1, -self.modes2:, :self.modes3] = \
self.compl_mul3d(x_ft[:, :, :self.modes1, -self.modes2:, :self.modes3], self.weights3)
out_ft[:, :, -self.modes1:, -self.modes2:, :self.modes3] = \
self.compl_mul3d(x_ft[:, :, -self.modes1:, -self.modes2:, :self.modes3], self.weights4)
#Return to physical space
x = torch.fft.irfftn(out_ft, s=(x.size(-3), x.size(-2), x.size(-1)))
return x
class U_net(nn.Module):
def __init__(self, input_channels, output_channels, kernel_size, dropout_rate):
super(U_net, self).__init__()
self.input_channels = input_channels
self.conv1 = self.conv(input_channels, output_channels, kernel_size=kernel_size, stride=2, dropout_rate = dropout_rate)
self.conv2 = self.conv(input_channels, output_channels, kernel_size=kernel_size, stride=2, dropout_rate = dropout_rate)
self.conv2_1 = self.conv(input_channels, output_channels, kernel_size=kernel_size, stride=1, dropout_rate = dropout_rate)
self.conv3 = self.conv(input_channels, output_channels, kernel_size=kernel_size, stride=2, dropout_rate = dropout_rate)
self.conv3_1 = self.conv(input_channels, output_channels, kernel_size=kernel_size, stride=1, dropout_rate = dropout_rate)
self.deconv2 = self.deconv(input_channels, output_channels)
self.deconv1 = self.deconv(input_channels*2, output_channels)
self.deconv0 = self.deconv(input_channels*2, output_channels)
self.output_layer = self.output(input_channels*2, output_channels,
kernel_size=kernel_size, stride=1, dropout_rate = dropout_rate)
def forward(self, x):
out_conv1 = self.conv1(x)
out_conv2 = self.conv2_1(self.conv2(out_conv1))
out_conv3 = self.conv3_1(self.conv3(out_conv2))
out_deconv2 = self.deconv2(out_conv3)
concat2 = torch.cat((out_conv2, out_deconv2), 1)
out_deconv1 = self.deconv1(concat2)
concat1 = torch.cat((out_conv1, out_deconv1), 1)
out_deconv0 = self.deconv0(concat1)
concat0 = torch.cat((x, out_deconv0), 1)
out = self.output_layer(concat0)
return out
def conv(self, in_planes, output_channels, kernel_size, stride, dropout_rate):
return nn.Sequential(
nn.Conv3d(in_planes, output_channels, kernel_size=kernel_size,
stride=stride, padding=(kernel_size - 1) // 2, bias = False),
nn.BatchNorm3d(output_channels),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(dropout_rate)
)
def deconv(self, input_channels, output_channels):
return nn.Sequential(
nn.ConvTranspose3d(input_channels, output_channels, kernel_size=4,
stride=2, padding=1),
nn.LeakyReLU(0.1, inplace=True)
)
def output(self, input_channels, output_channels, kernel_size, stride, dropout_rate):
return nn.Conv3d(input_channels, output_channels, kernel_size=kernel_size,
stride=stride, padding=(kernel_size - 1) // 2)
class SimpleBlock3d(nn.Module):
def __init__(self, modes1, modes2, modes3, width):
super(SimpleBlock3d, self).__init__()
"""
U-FNO contains 3 Fourier layers and 3 U-Fourier layers.
input shape: (batchsize, x=200, y=96, t=24, c=12)
output shape: (batchsize, x=200, y=96, t=24, c=1)
"""
self.modes1 = modes1
self.modes2 = modes2
self.modes3 = modes3
self.width = width
self.fc0 = nn.Linear(12, self.width)
"""
12 channels for [kr, kz, porosity, inj_loc, inj_rate,
pressure, temperature, Swi, Lam,
grid_x, grid_y, grid_t]
"""
self.conv0 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.conv1 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.conv2 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.conv3 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.conv4 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.conv5 = SpectralConv3d(self.width, self.width, self.modes1, self.modes2, self.modes3)
self.w0 = nn.Conv1d(self.width, self.width, 1)
self.w1 = nn.Conv1d(self.width, self.width, 1)
self.w2 = nn.Conv1d(self.width, self.width, 1)
self.w3 = nn.Conv1d(self.width, self.width, 1)
self.w4 = nn.Conv1d(self.width, self.width, 1)
self.w5 = nn.Conv1d(self.width, self.width, 1)
self.unet3 = U_net(self.width, self.width, 3, 0)
self.unet4 = U_net(self.width, self.width, 3, 0)
self.unet5 = U_net(self.width, self.width, 3, 0)
self.fc1 = nn.Linear(self.width, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, x):
batchsize = x.shape[0]
size_x, size_y, size_z = x.shape[1], x.shape[2], x.shape[3]
x = self.fc0(x)
x = x.permute(0, 4, 1, 2, 3)
x1 = self.conv0(x)
x2 = self.w0(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x = x1 + x2
x = F.relu(x)
x1 = self.conv1(x)
x2 = self.w1(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x = x1 + x2
x = F.relu(x)
x1 = self.conv2(x)
x2 = self.w2(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x = x1 + x2
x = F.relu(x)
x1 = self.conv3(x)
x2 = self.w3(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x3 = self.unet3(x)
x = x1 + x2 + x3
x = F.relu(x)
x1 = self.conv4(x)
x2 = self.w4(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x3 = self.unet4(x)
x = x1 + x2 + x3
x = F.relu(x)
x1 = self.conv5(x)
x2 = self.w5(x.view(batchsize, self.width, -1)).view(batchsize, self.width, size_x, size_y, size_z)
x3 = self.unet5(x)
x = x1 + x2 + x3
x = F.relu(x)
x = x.permute(0, 2, 3, 4, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return x
class Net3d(nn.Module):
def __init__(self, modes1, modes2, modes3, width):
super(Net3d, self).__init__()
"""
A wrapper function
"""
self.conv1 = SimpleBlock3d(modes1, modes2, modes3, width)
def forward(self, x):
batchsize = x.shape[0]
size_x, size_y, size_z = x.shape[1], x.shape[2], x.shape[3]
x = F.pad(F.pad(x, (0,0,0,8,0,8), "replicate"), (0,0,0,0,0,0,0,8), 'constant', 0)
x = self.conv1(x)
x = x.view(batchsize, size_x+8, size_y+8, size_z+8, 1)[..., :-8,:-8,:-8, :]
return x.squeeze()
def count_params(self):
c = 0
for p in self.parameters():
c += reduce(operator.mul, list(p.size()))
return c