-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
64 lines (50 loc) · 2.06 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import get_rot_mat, rotate_torch
from unet import UNet
class Filter_Func(nn.Module):
def __init__(self, in_channels, out_channels,kernel_size=(1,3)):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=(0, 1))
def forward(self,x):
return self.conv(x)
class Tune_Func(nn.Module):
def __init__(self):
super().__init__()
self.pool = nn.AvgPool3d(kernel_size=(2),stride=1, padding=(1, 1, 1))
self.norm = nn.BatchNorm3d(num_features=1)
def forward(self,x):
poolL = self.pool(x)
poolL = poolL[:,1:,1:,1:]
shift = self.norm(poolL.reshape(1,poolL.shape[0],poolL.shape[1],poolL.shape[2],poolL.shape[3]))
return shift
def linear_reconstruction(minibatch, device, out_size=(1, 155, 284, 284), num_Projections=12):
''' minibatch: 12 projections, width, height
out: the 3D reconstrauction of the 12 projections
out (155, 284, 284)
'''
max_rotation = num_Projections
degrees = 180.0 // max_rotation
d = out_size[1] #depth of output tensor
out = torch.zeros(out_size, dtype=torch.float).to(device)
for i in range(max_rotation):
proj = minibatch[i].repeat([d, 1, 1]).to(device)
proj = proj.reshape(1, 1, *proj.shape)
rot_mat = get_rot_mat(-i*degrees)
out += rotate_torch(proj, rot_mat, device).squeeze()
return out
class ColorProjUNet(nn.Module):
def __init__(self, n_classes, device, bilinear=True):
super().__init__()
self.device = device
self.unet = UNet(3, n_classes, bilinear)
self.filtration_func = Filter_Func(n_classes, 1, (1,3))
self.finetune_func = Tune_Func()
def forward(self, x):
composed_img = x.squeeze()
logits = self.unet(composed_img)
filtered = self.filtration_func(logits)
reconstructed = linear_reconstruction(filtered, self.device, num_Projections=12)
out = self.finetune_func(reconstructed)
return out