forked from ISHITA1234/AD_Classification_App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
74 lines (65 loc) · 2.7 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
import torch
import torch.nn as nn
class VoxCNN(nn.Module):
"""
VoxCNN is a convolutional neural network for 3D image classification.
Args:
num_classes (int): The number of classes in the classification problem. Defaults to 4.
"""
def __init__(self, num_classes=4):
"""
Initializes a new instance of the VoxCNN class.
Args:
num_classes (int): The number of classes in the classification problem. Defaults to 4.
"""
super(VoxCNN, self).__init__()
self.num_classes = num_classes
# Define the convolutional layers.
self.features = nn.Sequential(
nn.Conv3d(in_channels=1, out_channels=8, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=8, out_channels=8, kernel_size=3, padding='same'),
nn.ReLU(),
nn.MaxPool3d(kernel_size=2),
nn.Conv3d(in_channels=8, out_channels=16, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=16, out_channels=16, kernel_size=3, padding='same'),
nn.ReLU(),
nn.MaxPool3d(kernel_size=2),
nn.Conv3d(in_channels=16, out_channels=32, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=32, out_channels=32, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=32, out_channels=32, kernel_size=3, padding='same'),
nn.ReLU(),
nn.MaxPool3d(kernel_size=2),
nn.Conv3d(in_channels=32, out_channels=64, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=64, out_channels=64, kernel_size=3, padding='same'),
nn.ReLU(),
nn.Conv3d(in_channels=64, out_channels=64, kernel_size=3, padding='same'),
nn.ReLU(),
nn.MaxPool3d(kernel_size=2)
)
# Define the fully connected layers.
self.classifier = nn.Sequential(
# in_features = channel x depth x width x height
nn.Linear(in_features=64*8*8*8, out_features=128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Linear(in_features=128, out_features=64),
nn.ReLU(),
nn.Linear(in_features=64, out_features=self.num_classes)
)
def forward(self, x):
"""
Forward pass of the VoxCNN.
Args:
x (torch.Tensor): The input tensor of shape (batch_size, 1, depth, height, width).
Returns:
torch.Tensor: The output tensor of shape (batch_size, num_classes).
"""
x = self.features(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x