-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifiedResnetLayered.py
More file actions
86 lines (67 loc) · 3.02 KB
/
Copy pathModifiedResnetLayered.py
File metadata and controls
86 lines (67 loc) · 3.02 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
import torch
import torch.nn as nn
from torchvision.models import resnet18, ResNet18_Weights
from CustomModules import LayerFusion
from CustomBlocks import MultiHeadAttentionBlock
class ModifiedResNetLayered(nn.Module):
def __init__(self, num_classes=100):
super(ModifiedResNetLayered, self).__init__()
self.name = 'ModifiedResNetLayered'
self.inplanes = 64
base = resnet18(weights=None)
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = base.bn1
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.Identity()
self.layer1 = base.layer1
#Branch1 - convolutional branch
self.Convolution_layer2 = base.layer2
self.Convolution_layer3 = base.layer3
#Branch2 - Attention Heavy Branch
self.attention_layer2 = self._make_layer(MultiHeadAttentionBlock, 128, 1, attention_heads=4, stride=2)
self.attention_layer3 = self._make_layer(MultiHeadAttentionBlock, 256, 1, attention_heads=4, stride=2)
self.fusion = LayerFusion(256)
self.layer4 = base.layer4
self.avgpool = base.avgpool
self.fc = nn.Linear(512, num_classes)
def _make_layer(self, block, planes, blocks, attention_heads, stride=1):
'''
:param block: The name of the block that the model uses to generate the layer
:param planes: The amount of neurons per layer - input
:param blocks: The amount of blocks(How many times is it repeated?)
:param attention_heads: Multi-head attention heads specific param
:param stride: Stride of the blocks - enables or disables downsampling
Note: Standard ResNet18 has [2, 2, 2, 2] blocks per layer.
Increasing blocks adds depth but increases computation time.
'''
downsample = None
if stride != 1 or self.inplanes != planes:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, attention_heads=attention_heads))
self.inplanes = planes
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, attention_heads=attention_heads))
return nn.Sequential(*layers)
def forward(self, x):
'''RESNET MODULE 1'''
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
'''Resnet Branch 1'''
x_conv_branch = self.Convolution_layer2(x)
x_conv_branch = self.Convolution_layer3(x_conv_branch)
'''Resnet Branch 2'''
x_attention_branch = self.attention_layer2(x)
x_attention_branch = self.attention_layer3(x_attention_branch)
x = self.fusion(x_conv_branch, x_attention_branch)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x