-
Notifications
You must be signed in to change notification settings - Fork 55
/
model.py
118 lines (101 loc) · 3.89 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
112
113
114
115
116
117
118
'''---------------------------------------------------------------------------
IFCNN: A General Image Fusion Framework Based on Convolutional Neural Network
----------------------------------------------------------------------------'''
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
# My Convolution Block
class ConvBlock(nn.Module):
def __init__(self, inplane, outplane):
super(ConvBlock, self).__init__()
self.padding = (1, 1, 1, 1)
self.conv = nn.Conv2d(inplane, outplane, kernel_size=3, padding=0, stride=1, bias=False)
self.bn = nn.BatchNorm2d(outplane)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = F.pad(x, self.padding, 'replicate')
out = self.conv(out)
out = self.bn(out)
out = self.relu(out)
return out
class IFCNN(nn.Module):
def __init__(self, resnet, fuse_scheme=0):
super(IFCNN, self).__init__()
self.fuse_scheme = fuse_scheme # MAX, MEAN, SUM
self.conv2 = ConvBlock(64, 64)
self.conv3 = ConvBlock(64, 64)
self.conv4 = nn.Conv2d(64, 3, kernel_size=1, padding=0, stride=1, bias=True)
# Initialize parameters for other parameters
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
# Initialize conv1 with the pretrained resnet101 and freeze its parameters
for p in resnet.parameters():
p.requires_grad = False
self.conv1 = resnet.conv1
self.conv1.stride = 1
self.conv1.padding = (0, 0)
def tensor_max(self, tensors):
max_tensor = None
for i, tensor in enumerate(tensors):
if i == 0:
max_tensor = tensor
else:
max_tensor = torch.max(max_tensor, tensor)
return max_tensor
def tensor_sum(self, tensors):
sum_tensor = None
for i, tensor in enumerate(tensors):
if i == 0:
sum_tensor = tensor
else:
sum_tensor = sum_tensor + tensor
return sum_tensor
def tensor_mean(self, tensors):
sum_tensor = None
for i, tensor in enumerate(tensors):
if i == 0:
sum_tensor = tensor
else:
sum_tensor = sum_tensor + tensor
mean_tensor = sum_tensor / len(tensors)
return mean_tensor
def operate(self, operator, tensors):
out_tensors = []
for tensor in tensors:
out_tensor = operator(tensor)
out_tensors.append(out_tensor)
return out_tensors
def tensor_padding(self, tensors, padding=(1, 1, 1, 1), mode='constant', value=0):
out_tensors = []
for tensor in tensors:
out_tensor = F.pad(tensor, padding, mode=mode, value=value)
out_tensors.append(out_tensor)
return out_tensors
def forward(self, *tensors):
# Feature extraction
outs = self.tensor_padding(tensors=tensors, padding=(3, 3, 3, 3), mode='replicate')
outs = self.operate(self.conv1, outs)
outs = self.operate(self.conv2, outs)
# Feature fusion
if self.fuse_scheme == 0: # MAX
out = self.tensor_max(outs)
elif self.fuse_scheme == 1: # SUM
out = self.tensor_sum(outs)
elif self.fuse_scheme == 2: # MEAN
out = self.tensor_mean(outs)
else: # Default: MAX
out = self.tensor_max(outs)
# Feature reconstruction
out = self.conv3(out)
out = self.conv4(out)
return out
def myIFCNN(fuse_scheme=0):
# pretrained resnet101
resnet = models.resnet101(pretrained=True)
# our model
model = IFCNN(resnet, fuse_scheme=fuse_scheme)
return model