-
Notifications
You must be signed in to change notification settings - Fork 1
/
meshgraphnet.py
259 lines (246 loc) · 9.11 KB
/
meshgraphnet.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
252
253
254
255
256
import torch
from torch.nn import Linear, Sequential, ReLU, GroupNorm
from torch_scatter import scatter
#%%
class MLP(torch.nn.Module):
def __init__(self, in_dim, h_dim, out_dim, n_layers, layer_norm):
super().__init__()
self.in_dim=in_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.n_layers=n_layers
self.layer_norm=layer_norm
mlp=[]
mlp.append(Linear(in_dim, h_dim))
mlp.append(ReLU())
for n in range(0, n_layers):
mlp.append(Linear(h_dim, h_dim))
mlp.append(ReLU())
mlp.append(Linear(h_dim, out_dim))
if layer_norm==True:
mlp.append(GroupNorm(1, out_dim))
self.mlp=Sequential(*mlp)
def forward(self, x):
y=self.mlp(x)
return y
#%%
class EdgeConv(torch.nn.Module):
def __init__(self, in_dim, h_dim, out_dim, n_layers, layer_norm):
super().__init__()
self.in_dim=in_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.n_layers=n_layers
self.layer_norm=layer_norm
self.mlp=MLP(3*in_dim, h_dim, out_dim, n_layers, layer_norm)
def forward(self, x, edge_index, e):
# x has shape [N, in_dim]
# edge_index has shape [2, E]
# e has shape [E, in_dim]
#--------- message --------------
x_j = x[edge_index[0]] # Source node features [E, in_dim]
x_i = x[edge_index[1]] # Target node features [E, in_dim]
in_=torch.cat([x_i, x_j, e], dim=1)
msg=self.mlp(in_) # shape [E, out_dim]
#---------------------------------------
e_new=e+msg # residual connection
return e_new
#%%
class NodeConv(torch.nn.Module):
def __init__(self, in_dim, h_dim, out_dim, n_layers, layer_norm, aggr):
super().__init__()
self.in_dim=in_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.n_layers=n_layers
self.layer_norm=layer_norm
self.aggr=aggr
self.mlp=MLP(2*in_dim, h_dim, out_dim, n_layers, layer_norm)
def forward(self, x, edge_index, e):
# x has shape [N, in_dim]
# edge_index has shape [2, E]
# e has shape [E, in_dim]
#--------- message --------------
#msg_e shape [N, in_dim]
msg_e = scatter(e, edge_index[1], dim=0, dim_size=x.shape[0], reduce=self.aggr)
in_=torch.cat([x, msg_e], dim=1)
msg=self.mlp(in_) # shape [N, in_dim]
#---------------------------------------
x_new=x+msg # residual connection
return x_new
#%%
class GraphConv(torch.nn.Module):
def __init__(self, in_dim, h_dim, out_dim, n_layers, layer_norm, aggr):
super().__init__()
self.in_dim=in_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.n_layers=n_layers
self.layer_norm=layer_norm
self.aggr=aggr
self.edge_conv=EdgeConv(in_dim, h_dim, out_dim, n_layers, layer_norm)
self.node_conv=NodeConv(in_dim, h_dim, out_dim, n_layers, layer_norm, aggr)
def forward(self, x, edge_index, e):
e_new=self.edge_conv(x, edge_index, e)
x_new=self.node_conv(x, edge_index, e_new)
return x_new, e_new
#%%
class Processor(torch.nn.Module):
def __init__(self, n_blocks, in_dim, h_dim, out_dim, n_layers, layer_norm, aggr):
super().__init__()
self.in_dim=in_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.n_layers=n_layers
self.layer_norm=layer_norm
self.aggr=aggr
self.n_blocks=n_blocks
self.block=torch.nn.ModuleList()
for n in range(0, n_blocks):
self.block.append(GraphConv(in_dim, h_dim, out_dim, n_layers, layer_norm, aggr))
def forward(self, x, edge_index, e):
for n in range(0, self.n_blocks):
x, e=self.block[n](x, edge_index, e)
#print(x.shape, e.shape)
return x, e
#%%
class MeshGraphNet(torch.nn.Module):
def __init__(self, x_dim, e_dim, n_blocks, h_dim, out_dim, x_scale=1, n_layers=1, aggr='mean'):
super().__init__()
self.x_dim=x_dim
self.e_dim=e_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.x_scale=x_scale
self.n_layers=n_layers
self.aggr=aggr
self.encoder_x=MLP(x_dim, h_dim, h_dim, n_layers, True)
self.encoder_e=MLP(e_dim, h_dim, h_dim, n_layers, True)
self.processor=Processor(n_blocks, h_dim, h_dim, h_dim, n_layers, True, aggr)
self.decoder=MLP(h_dim, h_dim, out_dim, n_layers, False)
def forward(self, x, edge_index, e, x_ref=None):
if self.x_scale != 1:
x=x*self.x_scale
x=self.encoder_x(x)
e=self.encoder_e(e)
x, e=self.processor(x, edge_index, e)
y=self.decoder(x)
return y
#%%
from NNFEA_net_x_c import BaseNet0, Net1
#--------------------------------------------
class Encoder3(torch.nn.Module):
def __init__(self, BaseNet, x_dim, h_dim, n_layers, x_scale, alpha, gamma, y_dim):
super().__init__()
self.BaseNet=BaseNet
self.x_dim=x_dim
self.h_dim=h_dim
self.n_layers=n_layers
self.x_scale=x_scale
self.alpha=alpha
self.gamma=gamma
self.y_dim=y_dim
self.netA=eval(BaseNet+"("
+'x_dim=3'
+',h_dim='+str(h_dim)
+',n_layers='+str(n_layers)
+',x_scale='+str(x_scale)
+',alpha='+str(alpha)
+',gamma='+str(gamma)
+',y_dim='+str(x_dim*y_dim)
+")"
)
def forward(self, x, x_ref):
#x.shape (N, x_dim)
#x_ref.shape (N, 3)
y1=self.netA(x_ref)
#y.shape (N, y_dim*x1_dim)
y1=y1.view(x.shape[0], self.x_dim, self.y_dim)
x1=x.view(x.shape[0], self.x_dim, 1)
y=(y1*x1).mean(dim=(0,1))
y=y.view(1,-1)
y=y.expand(x1.shape[0], self.y_dim)
return y
#--------------------------------------------
class MeshGraphNet1(torch.nn.Module):
def __init__(self, x_dim, e_dim, n_blocks, h_dim, out_dim, x_scale=1, n_layers=1, aggr='mean'):
super().__init__()
self.x_dim=x_dim
self.e_dim=e_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.x_scale=x_scale
self.n_layers=n_layers
self.aggr=aggr
self.encoder_x=MLP(x_dim, h_dim, h_dim, n_layers, True)
self.encoder_e=MLP(e_dim, h_dim, h_dim, n_layers, True)
self.processor=Processor(n_blocks, h_dim, h_dim, h_dim, n_layers, True, aggr)
if out_dim == 3: #shape
self.encoder=eval("Encoder3('BaseNet0',h_dim,h_dim,2,1,1,1,3)")
self.decoder=eval("Net1('BaseNet5b',3,3,512,4,1,1,1,3,'sigmoid')")
elif out_dim == 9: #stress
self.encoder=eval("Encoder3('BaseNet0',h_dim,h_dim,2,1,1,1,3)")
self.decoder=eval("Net1('BaseNet5b',3,3,512,4,1,1,1,9,'sigmoid')")
def forward(self, x, edge_index, e, x_ref):
if self.x_scale != 1:
x=x*self.x_scale
x=self.encoder_x(x)
e=self.encoder_e(e)
x, e=self.processor(x, edge_index, e)
c=self.encoder(x, x_ref)
y=self.decoder(x_ref, c)
return y
#%%
class MeshGraphNet2(torch.nn.Module):
def __init__(self, x_dim, e_dim, n_blocks, h_dim, out_dim, x_scale=1, n_layers=1, aggr='mean'):
super().__init__()
self.x_dim=x_dim
self.e_dim=e_dim
self.h_dim=h_dim
self.out_dim=out_dim
self.x_scale=x_scale
self.n_layers=n_layers
self.aggr=aggr
self.encoder_x=MLP(x_dim, h_dim, h_dim, n_layers, True)
self.encoder_e=MLP(e_dim, h_dim, h_dim, n_layers, True)
self.encoder_c=MLP(h_dim, h_dim, h_dim, n_layers, True)
self.processor=Processor(n_blocks, h_dim, h_dim, h_dim, n_layers, True, aggr)
self.decoder=MLP(2*h_dim, h_dim, out_dim, n_layers, False)
def forward(self, x, edge_index, e, x_ref=None):
if self.x_scale != 1:
x=x*self.x_scale
x=self.encoder_x(x)
e=self.encoder_e(e)
x, e=self.processor(x, edge_index, e)
c=self.encoder_c(x.mean(dim=0, keepdim=True))
c=c.expand(x.shape[0], self.h_dim)
xc=torch.cat([x,c],dim=1)
y=self.decoder(xc)
return y
#%%
if __name__ == '__main__':
#%%
import sys
sys.path.append("../../../MLFEA/code/mesh")
import numpy as np
from IPython import display
import matplotlib.pyplot as plt
import torch
from QuadMesh import QuadMesh
from PolyhedronMesh import PolyhedronMesh
mesh_px=PolyhedronMesh()
mesh_px.load_from_torch("F:/MLFEA/TAA/data/343c1.5/p0_0_solid.pt")
mesh_px.build_adj_node_link()
#%%
edge_index=mesh_px.adj_node_link.t()
x=mesh_px.node.to(torch.float32)
e=torch.rand((edge_index.shape[1], 10), dtype=torch.float32)
#%%
net=MeshGraphNet(x_dim=3, e_dim=10, n_blocks=2, h_dim=128, out_dim=3)
y=net(x, edge_index, e)
#%%
net=MeshGraphNet1(x_dim=3, e_dim=10, n_blocks=2, h_dim=128, out_dim=3)
y=net(x, edge_index, e, x)
#%%
net=MeshGraphNet(x_dim=3, e_dim=10, n_blocks=2, h_dim=128, out_dim=3)
y=net(x, edge_index, e)