-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
111 lines (88 loc) · 3.58 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
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
import torch
from torch_geometric.nn import GCNConv
import torch.nn.functional as F
from torch_geometric.data import Data, Batch
import torch.nn as nn
class FCN(nn.Module):
"""
Proposed Fully Convolutional Network
This function/module uses fully convolutional blocks to extract pixel-wise image features.
Tested on 1024*1024, 512*512 resolution; RGB, Immunohistochemical color channels
Keyword arguments:
input_dim -- input channel, 3 for RGB images (default)
"""
def __init__(self,input_dim, output_classes, p_mode = 'replicate'):
super(FCN, self).__init__()
#self.Dropout = nn.Dropout(p=0.05)
self.conv1 = nn.Conv2d(input_dim, 32, kernel_size=3, stride=1, padding=1 ,padding_mode=p_mode)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1, padding_mode=p_mode)
self.bn2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1, padding_mode=p_mode)
self.bn3 = nn.BatchNorm2d(64)
self.conv4 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, padding_mode=p_mode)
self.bn4 = nn.BatchNorm2d(64)
self.conv5 = nn.Conv2d(64, output_classes, kernel_size=1, stride=1, padding=0)
#self.Dropout = nn.Dropout(p=0.3)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.bn1(x)
#x = self.Dropout(x)
x = self.conv2(x)
x = F.relu(x)
x = self.bn2(x)
#x = self.Dropout(x)
x = self.conv3(x)
x = F.relu(x)
x = self.bn3(x)
#x = self.Dropout(x)
x = self.conv4(x)
x = F.relu(x)
x = self.bn4(x)
x = self.conv5(x)
return x
class GCN(torch.nn.Module):
"""
Proposed Graph Convolutional Network
This function/module uses classic GCN layers to generate superpixels(nodes) classification.
--"Semi-Supervised Classification with Graph Convolutional Networks",
--Thomas N. Kipf, Max Welling, ICLR2017
Keyword arguments:
input_dim -- input channel, aligns with output channel from FCN
output_classes --output channel, default 1 for our proposed loss function
"""
def __init__(self, input_dim, output_classes):
super(GCN, self).__init__()
self.conv1 = GCNConv(input_dim, 64)
self.conv2 = GCNConv(64, 128)
self.conv3 = GCNConv(128, 256)
self.conv4 = GCNConv(256, 64)
self.conv5 = GCNConv(64, output_classes)
#self.Dropout = nn.Dropout(p=0.5)
# self.bn1 = nn.BatchNorm1d(64)
# self.bn2 = nn.BatchNorm1d(128)
# self.bn3 = nn.BatchNorm1d(256)
# self.bn4 = nn.BatchNorm1d(64)
#
# self.lin1 = Linear(64, 256)
# self.lin2 = Linear(256, 128)
# self.lin3 = Linear(128, output_classes)
def forward(self, data):
x = self.conv1(data.x, edge_index = data.edge_index, edge_weight = data.edge_weight)
x = F.relu(x)
#x = self.Dropout(x)
#x = self.bn1(x)
x = self.conv2(x, edge_index = data.edge_index, edge_weight = data.edge_weight)
x = F.relu(x)
#x = self.Dropout(x)
#x = self.bn2(x)
x = self.conv3(x, edge_index = data.edge_index, edge_weight = data.edge_weight)
x = F.relu(x)
#x = self.Dropout(x)
#x = self.bn3(x)
x = self.conv4(x, edge_index = data.edge_index, edge_weight = data.edge_weight)
x = F.relu(x)
#x = self.bn4(x)
x = self.conv5(x, edge_index = data.edge_index, edge_weight = data.edge_weight)
return torch.tanh(x)