-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Dmodels.py
More file actions
133 lines (111 loc) · 3.99 KB
/
3Dmodels.py
File metadata and controls
133 lines (111 loc) · 3.99 KB
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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, Conv2d, MaxPool2d,MaxUnpool2d, Module, Softmax, BatchNorm2d, Dropout
from torch.optim import Adam, SGD
from skimage import util
from torchvision import transforms, models
from torch import optim
import numpy as np
import random
import torchvision
from collections import namedtuple, deque
import math
class DQNSolver(nn.Module):
"""
Convolutional Neural Net with 3 conv3D layers and two linear layers
"""
def __init__(self, input_shape, n_actions):
super(DQNSolver, self).__init__()
self.conv = nn.Sequential(
nn.Conv3d(input_shape[0], 32, kernel_size=(5,5,5), stride=2),
nn.LeakyReLU(),
nn.MaxPool3d((2, 2, 2)),
nn.Conv3d(32, 64, kernel_size=(5,5,5), stride=1),
nn.LeakyReLU(),
nn.MaxPool3d((1, 2, 2)),
nn.Conv3d(64, 64, kernel_size=(3,3,3), stride=2),
nn.LeakyReLU(),
nn.MaxPool3d((2, 2, 2)),
)
conv_out_size = self._get_conv_out(input_shape)
self.fc = nn.Sequential(
nn.Linear(conv_out_size, 256),
nn.ReLU(),
nn.Linear(256, n_actions)
)
def _get_conv_out(self, shape):
o = self.conv(torch.zeros(1, *shape))
return int(np.prod(o.size()))
def forward(self, x):
conv_out = self.conv(x).view(x.size()[0], -1)
return self.fc(conv_out)
policy_model = DQNSolver((1,50,256,256), 7)
target_model = DQNSolver((1,50,256,256), 7)
class Classifier(nn.Module):
"""
Convolutional Neural Net with 3 conv3D layers and two linear layers
"""
def __init__(self, input_shape, num_features):
super(Classifier, self).__init__()
self.conv = nn.Sequential(
nn.Conv3d(input_shape[0], 32, kernel_size=(5,5,5), stride=2),
nn.LeakyReLU(),
nn.MaxPool3d((2, 2, 2)),
nn.Conv3d(32, 64, kernel_size=(5,5,5), stride=1),
nn.LeakyReLU(),
nn.MaxPool3d((1, 2, 2)),
nn.Conv3d(64, 64, kernel_size=(3,3,3), stride=2),
nn.LeakyReLU(),
nn.MaxPool3d((2, 2, 2)),
)
conv_out_size = self._get_conv_out(input_shape)
self.fc = nn.Sequential(
nn.Linear(conv_out_size, 256),
nn.ReLU(),
nn.Linear(256, num_features),
nn.ReLU()
)
def _get_conv_out(self, shape):
o = self.conv(torch.zeros(1, *shape))
return int(np.prod(o.size()))
def forward(self, x):
conv_out = self.conv(x).view(x.size()[0], -1)
return self.fc(conv_out)
classifier_hippocampus = Classifier((1,50,256,256), 512)
classifier_global = Classifier((1,145,256,256), 1024)
class concat_classifier(nn.Module):
def __init__(self, input_shape1,input_shape2, classes):
super(concat_classifier, self).__init__()
input_shape = input_shape1[1] + input_shape2[1]
self.fc = nn.Sequential(
nn.Linear(input_shape, 512),
nn.ReLU(),
nn.Linear(512, classes),
nn.Sigmoid()
)
def forward(self,x1, x2):
cat_out = torch.cat((x1,x2), dim = 1)
output = self.fc(cat_out)
return output
final_model = concat_classifier((1,512), (1, 1024), 2)
class combine_model(nn.Module):
def __init__(self, model1, model2, model3):
super(combine_model, self).__init__()
self.model1 = model1
self.model2 = model2
self.model3 = model3
def forward(self, x1, x2):
input1 = x1
input2 = x2
out1 = self.model1(input1)
out2 = self.model2(input2)
pred = self.model3(out1, out2)
return pred
class_model = combine_model(classifier_hippocampus, classifier_global, final_model)