Spaces:
Running on Zero
Running on Zero
File size: 5,826 Bytes
03022ee | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import torch
import torch.nn as nn
import torch.nn.functional as F
class ResNetLayer(nn.Module):
"""
A ResNet layer used to build the ResNet network.
Architecture:
--> conv-bn-relu -> conv -> + -> bn-relu -> conv-bn-relu -> conv -> + -> bn-relu -->
| | | |
-----> downsample ------> ------------------------------------->
"""
def __init__(self, inplanes, outplanes, stride):
super(ResNetLayer, self).__init__()
self.conv1a = nn.Conv2d(inplanes, outplanes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1a = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001)
self.conv2a = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False)
self.downsample = nn.Sequential()
if stride != 1:
self.downsample = nn.Conv2d(inplanes, outplanes, kernel_size=(1,1), stride=stride, bias=False)
self.outbna = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001)
self.conv1b = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1b = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001)
self.conv2b = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False)
self.outbnb = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001)
def forward(self, inputBatch):
batch = F.relu(self.bn1a(self.conv1a(inputBatch)))
batch = self.conv2a(batch)
residualBatch = self.downsample(inputBatch)
batch = batch + residualBatch
intermediateBatch = batch
batch = F.relu(self.outbna(batch))
batch = F.relu(self.bn1b(self.conv1b(batch)))
batch = self.conv2b(batch)
residualBatch = intermediateBatch
batch = batch + residualBatch
outputBatch = F.relu(self.outbnb(batch))
return outputBatch
class ResNet(nn.Module):
"""
An 18-layer ResNet architecture.
"""
def __init__(self):
super(ResNet, self).__init__()
self.layer1 = ResNetLayer(64, 64, stride=1)
self.layer2 = ResNetLayer(64, 128, stride=2)
self.layer3 = ResNetLayer(128, 256, stride=2)
self.layer4 = ResNetLayer(256, 512, stride=2)
self.avgpool = nn.AvgPool2d(kernel_size=(4,4), stride=(1,1))
return
def forward(self, inputBatch):
batch = self.layer1(inputBatch)
batch = self.layer2(batch)
batch = self.layer3(batch)
batch = self.layer4(batch)
outputBatch = self.avgpool(batch)
return outputBatch
class GlobalLayerNorm(nn.Module):
def __init__(self, channel_size):
super(GlobalLayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.Tensor(1, channel_size, 1)) # [1, N, 1]
self.beta = nn.Parameter(torch.Tensor(1, channel_size, 1)) # [1, N, 1]
self.reset_parameters()
def reset_parameters(self):
self.gamma.data.fill_(1)
self.beta.data.zero_()
def forward(self, y):
mean = y.mean(dim=1, keepdim=True).mean(dim=2, keepdim=True) #[M, 1, 1]
var = (torch.pow(y-mean, 2)).mean(dim=1, keepdim=True).mean(dim=2, keepdim=True)
gLN_y = self.gamma * (y - mean) / torch.pow(var + 1e-8, 0.5) + self.beta
return gLN_y
class visualFrontend(nn.Module):
"""
A visual feature extraction module. Generates a 512-dim feature vector per video frame.
Architecture: A 3D convolution block followed by an 18-layer ResNet.
"""
def __init__(self):
super(visualFrontend, self).__init__()
self.frontend3D = nn.Sequential(
nn.Conv3d(1, 64, kernel_size=(5,7,7), stride=(1,2,2), padding=(2,3,3), bias=False),
nn.BatchNorm3d(64, momentum=0.01, eps=0.001),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(1,3,3), stride=(1,2,2), padding=(0,1,1))
)
self.resnet = ResNet()
return
def forward(self, inputBatch):
inputBatch = inputBatch.transpose(0, 1).transpose(1, 2)
batchsize = inputBatch.shape[0]
batch = self.frontend3D(inputBatch)
batch = batch.transpose(1, 2)
batch = batch.reshape(batch.shape[0]*batch.shape[1], batch.shape[2], batch.shape[3], batch.shape[4])
outputBatch = self.resnet(batch)
outputBatch = outputBatch.reshape(batchsize, -1, 512)
outputBatch = outputBatch.transpose(1 ,2)
outputBatch = outputBatch.transpose(1, 2).transpose(0, 1)
return outputBatch
class DSConv1d(nn.Module):
def __init__(self):
super(DSConv1d, self).__init__()
self.net = nn.Sequential(
nn.ReLU(),
nn.BatchNorm1d(512),
nn.Conv1d(512, 512, 3, stride=1, padding=1,dilation=1, groups=512, bias=False),
nn.PReLU(),
GlobalLayerNorm(512),
nn.Conv1d(512, 512, 1, bias=False),
)
def forward(self, x):
out = self.net(x)
return out + x
class visualTCN(nn.Module):
def __init__(self):
super(visualTCN, self).__init__()
stacks = []
for x in range(5):
stacks += [DSConv1d()]
self.net = nn.Sequential(*stacks) # Visual Temporal Network V-TCN
def forward(self, x):
out = self.net(x)
return out
class visualConv1D(nn.Module):
def __init__(self):
super(visualConv1D, self).__init__()
self.net = nn.Sequential(
nn.Conv1d(512, 256, 5, stride=1, padding=2),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Conv1d(256, 128, 1),
)
def forward(self, x):
out = self.net(x)
return out
|