self.deconv3 = deconv(self.batchNorm, 512, 256)
self.deconv2 = deconv(self.batchNorm, 512, 128)
self.deconv1 = deconv(self.batchNorm, 960, 960)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
variance1 = math.sqrt(3.0 / n) # use 3 for dt1 and 2 for dt4
m.weight.data.normal_(0, variance1)
if m.bias is not None:
constant_(m.bias, 0)
elif isinstance(m, nn.ConvTranspose2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
variance1 = math.sqrt(3.0 / n) # use 3 for dt1 and 2 for dt4
m.weight.data.normal_(0, variance1)
if m.bias is not None:
constant_(m.bias, 0)
self.fusion_feature_map = nn.Parameter(torch.randn(1, 16, 60, 60))
kaiming_init(self.fusion_feature_map, mode='fan_out')
self.fu_concat_1 = conv_s(self.batchNorm, 256, 16, kernel_size=3, stride=1)
self.fu_concat_2 = nn.Conv2d(in_channels=16,out_channels=256, kernel_size=5, stride=3,padding=1)
## DCN-v2: dynamic convolutional network module
self.dcnfeat_layers = nn.Sequential(OrderedDict([
('conv1', nn.Sequential(nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.GroupNorm(32, 32),
nn.MaxPool2d(kernel_size=3, stride=2))),
('dcn_conv2', nn.Sequential(DCN(32, 32, kernel_size=3, stride=1, padding=1, dilation=1, deformable_groups=1),
nn.ReLU(inplace=True),
nn.GroupNorm(32, 32),
nn.MaxPool2d(kernel_size=3, stride=2)
)),
('dcn_conv3', nn.Sequential(DCN(32, 32, kernel_size=3, stride=1, padding=1, dilation=1, deformable_groups=1),
nn.ReLU(inplace=True),
nn.GroupNorm(32, 32),
)),
('dcn_conv4', nn.Sequential(DCN(32, 32, kernel_size=3, stride=1, padding=1, dilation=1, deformable_groups=1),
nn.ReLU(inplace=True),
nn.GroupNorm(32, 32))),
('dcn_conv5', nn.Sequential(DCN(32, 32, kernel_size=3, stride=1, padding=1, dilation=1, deformable_groups=1),
nn.ReLU(inplace=True)))
]))
self.former_out = nn.Sequential(
# nn.Linear(768, 32),
nn.Linear(512, 32),
nn.ReLU(inplace=True),
)
self.snn_former_fc = nn.Sequential(
nn.Linear(7232, 4096),
nn.ReLU(inplace=True),
nn.Dropout3d(0.5)
)
self.init_values =init_values
self.batch_size = batch_size
self.clip_len =clip_len
self.GRU_Transformer = GRU_Transformer(clip_n =clip_n, dim=dim,num_heads=num_heads,qkv_bias=False,drop_rate=0.0,init_values=self.init_values)
self.pretrained = pretrained
self.init_std = init_std
self.pool= nn.MaxPool2d(2, 2)
self.batchNorm1 = nn.BatchNorm2d(64)
self.batchNorm2 = nn.BatchNorm2d(128)
self.batchNorm3 = nn.BatchNorm2d(256)
self.batchNorm4 = nn.BatchNorm2d(512)
beta1 = 0.5
self.snn_lif1 = snn.Lapicque(beta=beta1,threshold=threshold)
self.snn_lif2 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif3 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif4 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif5 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif6 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif7 = snn.Lapicque(beta=beta1, threshold=threshold)
self.snn_lif8 = snn.Lapicque(beta=beta1, threshold=threshold)
self.resnet18_feature_extractor = torchvision_resnet.resnet18(pretrained = True)
self.conv2d_query = nn.Conv2d(in_channels=512,out_channels=4096, kernel_size=1, stride=1,padding=0)
self.conv2d_support = nn.Conv2d(in_channels=512,out_channels=64, kernel_size=1, stride=1,padding=0)
def init_weights(self):
"""Initiate the parameters either from existing checkpoint or from
scratch."""
if isinstance(self.pretrained, str):
logger = MMLogger.get_current_instance()
logger.info(f'load model from: {self.pretrained}')
load_checkpoint(self, self.pretrained, strict=False, logger=logger)
elif self.pretrained is None:
for m in self.modules():
if isinstance(m, nn.Conv3d):
kaiming_init(m)
elif isinstance(m, nn.Linear):
normal_init(m, std=self.init_std)
elif isinstance(m, _BatchNorm):
constant_init(m, 1)
else:
raise TypeError('pretrained must be a str or None')
def _add_output_and_check(self, name, x, outputs, output_layers):
if name in output_layers:
outputs[name] = x
return len(output_layers) == len(outputs)
def forward(self, input_, output_layers=None, image_resize=240, sp_threshold=0.75):
rgb_input = input_[0]
input = input_[1]
threshold = sp_threshold
mem_11 = torch.zeros(input.size(0), 64, int(math.ceil(image_resize)), int(math.ceil(image_resize))).cuda()
mem_12 = torch.zeros(input.size(0), 64, int(math.ceil(image_resize/2)), int(math.ceil(image_resize/2))).cuda()
mem_21 = torch.zeros(input.size(0), 128, int(math.ceil(image_resize/2)), int(math.ceil(image_resize/2))).cuda()
mem_22 = torch.zeros(input.size(0), 128, int(math.ceil(image_resize/4)), int(math.ceil(image_resize/4))).cuda()
mem_31 = torch.zeros(input.size(0), 256, int(math.ceil(image_resize/4)), int(math.ceil(image_resize/4))).cuda()
mem_32 = torch.zeros(input.size(0), 256, int(math.ceil(image_resize/8)), int(math.ceil(image_resize/8))).cuda()
mem_41 = torch.zeros(input.size(0), 512, int(math.ceil(image_resize/8)), int(math.ceil(image_resize/8))).cuda()
mem_42 = torch.zeros(input.size(0), 512, int(math.ceil(image_resize/16)), int(math.ceil(image_resize/16))).cuda()
mem_1_total = torch.zeros(input.size(0), 64, int(math.ceil(image_resize/2)), int(math.ceil(image_resize/2))).cuda()
mem_2_total = torch.zeros(input.size(0), 128, int(math.ceil(image_resize/4)), int(math.ceil(image_resize/4))).cuda()
mem_3_total = torch.zeros(input.size(0), 256, int(math.ceil(image_resize/8)), int(math.ceil(image_resize/8))).cuda()
mem_4_total = torch.zeros(input.size(0), 512, int(math.ceil(image_resize/16)), int(math.ceil(image_resize/16))).cuda()
input = input.permute(0,1,4,2,3)
for i in range(input.shape[1]): ## from 1 to 16
current_input = input[:, i, :, :, :]
current_input = F.interpolate(current_input, [image_resize, image_resize])
#----------------------------------------------------------------------------
current_11 = self.conv1(current_input)
mem_11 = mem_11 + current_11
mem_11,current_1 =self.snn_lif1(current_11,mem_11)
current_1 = self.conv2(current_1)
current_1 = self.pool(current_1)
current_12 = self.batchNorm1(current_1)
mem_12 = mem_12 + current_12
mem_1_total = mem_1_total + current_12
mem_12,out_conv1 =self.snn_lif2(current_12,mem_12)
#----------------------------------------------------------------------------
current_21 = self.conv3(out_conv1)
mem_21 = mem_21 + current_21
mem_21,current_2 =self.snn_lif3(current_21,mem_21)
current_2 = self.conv4(current_2)
current_2 = self.pool(current_2)
current_22 = self.batchNorm2(current_2)
mem_22 = mem_22 + current_22
mem_2_total = mem_2_total + current_22
mem_22,out_conv2 =self.snn_lif4(current_22,mem_22)
#----------------------------------------------------------------------------
current_31 = self.conv5(out_conv2)
mem_31 = mem_31 + current_31
mem_31,current_3 =self.snn_lif5(current_31, mem_31)
current_3 = self.conv6(current_3)
current_3 = self.pool(current_3)
current_32 = self.batchNorm3(current_3)
mem_32 = mem_32 + current_32
mem_3_total = mem_3_total + current_32
mem_32,out_conv3 =self.snn_lif6(current_32, mem_32)
#----------------------------------------------------------------------------
current_41 = self.conv7(out_conv3)
mem_41 = mem_41 + current_41
mem_41, current_4 =self.snn_lif7(current_41,mem_41)
current_4 = self.conv8(current_4)
current_4 = self.pool(current_4)
current_42 = self.batchNorm4(current_4)
mem_42 = mem_42 + current_42
mem_4_total = mem_4_total + current_42
mem_42, out_conv4 =self.snn_lif8(current_42, mem_42)
#----------------------------------------------------------------------------
out_conv4 = mem_4_total
out_conv3 = mem_3_total
out_conv2 = mem_2_total
out_deconv3 = self.deconv3(out_conv4)
concat3 = torch.cat((out_conv3, out_deconv3),1)
out_deconv2 = self.deconv2(concat3)
concat2 = torch.cat((out_conv2, out_deconv2),1)
############################################################################################################
concat_fu = self.fu_concat_1(concat2)
fusion_feature_map = self.fusion_feature_map.expand_as(concat_fu)
fusion_feature_map_agg = torch.cat((fusion_feature_map,concat_fu),1)
############################################################################################################
fu_eventFeats = self.dcnfeat_layers(fusion_feature_map_agg)
fu_fea_map = fu_eventFeats[:,0:16,:,:] #torch.Size([6, 16, 14, 14])
scnn_out = fu_eventFeats[:,16:32,:,:] #torch.Size([6, 16, 14, 14])
scnn_out_Feats = torch.flatten(scnn_out, start_dim=1, end_dim=3)
bottleneck_fea = self.fu_concat_2(fu_fea_map).reshape(fu_fea_map.shape[0],-1)
################################################################################################
B,C,N,H,W =rgb_input.shape
rgb_input_res = rgb_input.reshape(B*N,C,H,W)
rgb_res_out = self.resnet18_feature_extractor(rgb_input_res).reshape( B,N,512,8,8)
# rgb_res_out = self.resnet50_feature_extractor(rgb_input_res).reshape( B,N,2048,8,8)
if self.clip_len==16:
res_query = torch.cat((rgb_res_out[:,3:4,:,:,:],rgb_res_out[:,7:8,:,:,:],rgb_res_out[:,11:12,:,:,:],rgb_res_out[:,15:16,:,:,:]),1).reshape(B*4,512,8,8)
res_support = torch.cat((rgb_res_out[:,0:3,:,:,:],rgb_res_out[:,4:7,:,:,:],rgb_res_out[:,8:11,:,:,:],rgb_res_out[:,12:15,:,:,:]),1).reshape(B*(N-4),512,8,8)
former_query = self.conv2d_query(res_query).reshape(B,4,64,4096)
former_support = self.conv2d_support(res_support).reshape(B,N-4,-1)
former_out = self.GRU_Transformer(former_support, former_query, bottleneck_fea)
################################################################################################
out_ = torch.cat((former_out,scnn_out_Feats),1)
predict = self.snn_former_fc(out_)
# print('self.proj : ',self.snn_former_fc[0].weight)
return predict
def weight_parameters(self):
return [param for name, param in self.named_parameters() if 'weight' in name]
def bias_parameters(self):
return [param for name, param in self.named_parameters() if 'bias' in name]