-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
78 lines (69 loc) · 2.82 KB
/
model.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
from torch import nn
from ops import MeshConv, MeshConv_transpose, ResBlock
import torch.nn.functional as F
import os
import torch
import pickle
class Up(nn.Module):
def __init__(self, in_ch, out_ch, level, mesh_folder, bias=True):
"""
use mesh_file for the mesh of one-level up
"""
super().__init__()
mesh_file = os.path.join(mesh_folder, "icosphere_{}.pkl".format(level))
half_in = int(in_ch/2)
self.up = MeshConv_transpose(half_in, half_in, mesh_file, stride=2)
self.conv = ResBlock(in_ch, out_ch, out_ch, level, False, mesh_folder)
def forward(self, x1, x2):
x1 = self.up(x1)
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class Down(nn.Module):
def __init__(self, in_ch, out_ch, level, mesh_folder, bias=True):
"""
use mesh_file for the mesh of one-level down
"""
super().__init__()
self.conv = ResBlock(in_ch, in_ch, out_ch, level+1, True, mesh_folder)
def forward(self, x):
x = self.conv(x)
return x
class SphericalUNet(nn.Module):
def __init__(self, mesh_folder, in_ch, out_ch, max_level=5, min_level=0, fdim=16):
super().__init__()
self.mesh_folder = mesh_folder
self.fdim = fdim
self.max_level = max_level
self.min_level = min_level
self.levels = max_level - min_level
self.down = []
self.up = []
self.in_conv = MeshConv(in_ch, fdim, self.__meshfile(max_level), stride=1)
self.out_conv = MeshConv(fdim, out_ch, self.__meshfile(max_level), stride=1)
# Downward path
for i in range(self.levels-1):
self.down.append(Down(fdim*(2**i), fdim*(2**(i+1)), max_level-i-1, mesh_folder))
self.down.append(Down(fdim*(2**(self.levels-1)), fdim*(2**(self.levels-1)), min_level, mesh_folder))
# Upward path
for i in range(self.levels-1):
self.up.append(Up(fdim*(2**(self.levels-i)), fdim*(2**(self.levels-i-2)), min_level+i+1, mesh_folder))
self.up.append(Up(fdim*2, fdim, max_level, mesh_folder))
self.down = nn.ModuleList(self.down)
self.up = nn.ModuleList(self.up)
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
#x_ = [self.dropout(self.in_conv(x))]
x_ = [self.in_conv(x)]
for i in range(self.levels):
#x_.append(self.dropout(self.down[i](x_[-1])))
x_.append(self.down[i](x_[-1]))
#x = self.dropout(self.up[0](x_[-1], x_[-2]))
x = self.up[0](x_[-1], x_[-2])
for i in range(self.levels-1):
#x = self.dropout(self.up[i+1](x, x_[-3-i]))
x = self.up[i+1](x, x_[-3-i])
x = self.out_conv(x)
return x
def __meshfile(self, i):
return os.path.join(self.mesh_folder, "icosphere_{}.pkl".format(i))