From d16e8164b02f5a61e000c31cb685da6b96973675 Mon Sep 17 00:00:00 2001 From: Bayles Date: Fri, 31 Mar 2023 15:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ASFF.py | 119 ++++++++++++++++++++++++++++++++++++++++ CenterNet.py | 16 ++++++ CornerNet.py | 104 +++++++++++++++++++++++++++++++++++ FCOS.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++ FisheyeMODNet.py | 129 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 506 insertions(+) create mode 100644 ASFF.py create mode 100644 CenterNet.py create mode 100644 CornerNet.py create mode 100644 FCOS.py create mode 100644 FisheyeMODNet.py diff --git a/ASFF.py b/ASFF.py new file mode 100644 index 0000000..30b4504 --- /dev/null +++ b/ASFF.py @@ -0,0 +1,119 @@ +import torch +import torch.nn as nn +import torchvision + +def Conv1x1BnRelu(in_channels,out_channels): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, bias=False), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True), + ) + +def upSampling1(in_channels,out_channels): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels,out_channels=out_channels,kernel_size=1,stride=1,padding=0,bias=False), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True), + nn.Upsample(scale_factor=2, mode='nearest') + ) + +def upSampling2(in_channels,out_channels): + return nn.Sequential( + upSampling1(in_channels,out_channels), + nn.Upsample(scale_factor=2, mode='nearest'), + ) + +def downSampling1(in_channels,out_channels): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=2, padding=1, bias=False), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True), + ) + +def downSampling2(in_channels,out_channels): + return nn.Sequential( + nn.MaxPool2d(kernel_size=3, stride=2,padding=1), + downSampling1(in_channels=in_channels, out_channels=out_channels), + ) + +class ASFF(nn.Module): + def __init__(self, level, channel1, channel2, channel3, out_channel): + super(ASFF, self).__init__() + self.level = level + funsed_channel = 8 + + if self.level == 1: + # level = 1: + self.level2_1 = downSampling1(channel2,channel1) + self.level3_1 = downSampling2(channel3,channel1) + + self.weight1 = Conv1x1BnRelu(channel1, funsed_channel) + self.weight2 = Conv1x1BnRelu(channel1, funsed_channel) + self.weight3 = Conv1x1BnRelu(channel1, funsed_channel) + + self.expand_conv = Conv1x1BnRelu(channel1,out_channel) + + if self.level == 2: + # level = 2: + self.level1_2 = upSampling1(channel1,channel2) + self.level3_2 = downSampling1(channel3,channel2) + + self.weight1 = Conv1x1BnRelu(channel2, funsed_channel) + self.weight2 = Conv1x1BnRelu(channel2, funsed_channel) + self.weight3 = Conv1x1BnRelu(channel2, funsed_channel) + + self.expand_conv = Conv1x1BnRelu(channel2, out_channel) + + if self.level == 3: + # level = 3: + self.level1_3 = upSampling2(channel1,channel3) + self.level2_3 = upSampling1(channel2,channel3) + + self.weight1 = Conv1x1BnRelu(channel3, funsed_channel) + self.weight2 = Conv1x1BnRelu(channel3, funsed_channel) + self.weight3 = Conv1x1BnRelu(channel3, funsed_channel) + + self.expand_conv = Conv1x1BnRelu(channel3, out_channel) + + self.weight_level = nn.Conv2d(funsed_channel * 3, 3, kernel_size=1, stride=1, padding=0) + + self.softmax = nn.Softmax(dim=1) + + + def forward(self, x, y, z): + if self.level == 1: + level_x = x + level_y = self.level2_1(y) + level_z = self.level3_1(z) + + if self.level == 2: + level_x = self.level1_2(x) + level_y = y + level_z = self.level3_2(z) + + if self.level == 3: + level_x = self.level1_3(x) + level_y = self.level2_3(y) + level_z = z + + weight1 = self.weight1(level_x) + weight2 = self.weight2(level_y) + weight3 = self.weight3(level_z) + + level_weight = torch.cat((weight1, weight2, weight3), 1) + weight_level = self.weight_level(level_weight) + weight_level = self.softmax(weight_level) + + fused_level = level_x * weight_level[:,0,:,:] + level_y * weight_level[:,1,:,:] + level_z * weight_level[:,2,:,:] + out = self.expand_conv(fused_level) + return out + +if __name__ == '__main__': + model = ASFF(level=3, channel1=512, channel2=256, channel3=128, out_channel=128) + print(model) + + x = torch.randn(1, 512, 16, 16) + y = torch.randn(1, 256, 32, 32) + z = torch.randn(1, 128, 64, 64) + out = model(x,y,z) + print(out.shape) \ No newline at end of file diff --git a/CenterNet.py b/CenterNet.py new file mode 100644 index 0000000..6e94dbf --- /dev/null +++ b/CenterNet.py @@ -0,0 +1,16 @@ +import torch +import torch.nn as nn +import torchvision + + + + + + +if __name__ == '__main__': + model = YOLO() + print(model) + + data = torch.randn(1,3,448,448) + output = model(data) + print(output.shape) \ No newline at end of file diff --git a/CornerNet.py b/CornerNet.py new file mode 100644 index 0000000..5d75c48 --- /dev/null +++ b/CornerNet.py @@ -0,0 +1,104 @@ +import torch +import torch.nn as nn + +def ConvBNReLU(in_channels,out_channels,kernel_size,stride,padding=1): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=kernel_size//2), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True) + ) + +class ResidualBlock(nn.Module): + def __init__(self, in_channels, out_channels): + super(ResidualBlock, self).__init__() + mid_channels = out_channels//2 + + self.bottleneck = nn.Sequential( + ConvBNReLU(in_channels=in_channels, out_channels=mid_channels, kernel_size=1, stride=1), + ConvBNReLU(in_channels=mid_channels, out_channels=mid_channels, kernel_size=3, stride=1, padding=1), + ConvBNReLU(in_channels=mid_channels, out_channels=out_channels, kernel_size=1, stride=1), + ) + self.shortcut = ConvBNReLU(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1) + + def forward(self, x): + out = self.bottleneck(x) + return out+self.shortcut(x) + + +class left_pool(torch.autograd.Function): + def forward(self, input_): + self.save_for_backward(input_.clone()) + output = torch.zeros_like(input_) + batch = input_.size(0) + width = input_.size(3) + + input_tmp = input_.select(3, width - 1) + output.select(3, width - 1).copy_(input_tmp) + + for idx in range(1, width): + input_tmp = input_.select(3, width - idx - 1) + output_tmp = output.select(3, width - idx) + cmp_tmp = torch.cat((input_tmp.view(batch, 1, -1), output_tmp.view(batch, 1, -1)), 1).max(1)[0] + output.select(3, width - idx - 1).copy_(cmp_tmp.view_as(input_tmp)) + + return output + + def backward(self, grad_output): + input_, = self.saved_tensors + output = torch.zeros_like(input_) + + grad_output = grad_output.clone() + res = torch.zeros_like(grad_output) + + w = input_.size(3) + batch = input_.size(0) + + output_tmp = res.select(3, w - 1) + grad_output_tmp = grad_output.select(3, w - 1) + output_tmp.copy_(grad_output_tmp) + + input_tmp = input_.select(3, w - 1) + output.select(3, w - 1).copy_(input_tmp) + + for idx in range(1, w): + input_tmp = input_.select(3, w - idx - 1) + output_tmp = output.select(3, w - idx) + cmp_tmp = torch.cat((input_tmp.view(batch, 1, -1), output_tmp.view(batch, 1, -1)), 1).max(1)[0] + output.select(3, w - idx - 1).copy_(cmp_tmp.view_as(input_tmp)) + + grad_output_tmp = grad_output.select(3, w - idx - 1) + res_tmp = res.select(3, w - idx) + com_tmp = comp(input_tmp, output_tmp, grad_output_tmp, res_tmp) + res.select(3, w - idx - 1).copy_(com_tmp) + return res + +class HourglassNetwork(nn.Module): + def __init__(self): + super(HourglassNetwork, self).__init__() + + def forward(self, x): + return out + +class PredictionModule(nn.Module): + def __init__(self): + super(PredictionModule, self).__init__() + + def forward(self, x): + return out + + +class CornerNet(nn.Module): + def __init__(self): + super(CornerNet, self).__init__() + + def forward(self, x): + return out + + +if __name__ == '__main__': + model = CornerNet() + print(model) + + data = torch.randn(1,3,511,511) + output = model(data) + print(output.shape) \ No newline at end of file diff --git a/FCOS.py b/FCOS.py new file mode 100644 index 0000000..522dabd --- /dev/null +++ b/FCOS.py @@ -0,0 +1,138 @@ +import torch +import torch.nn as nn +import torchvision + +def Conv3x3ReLU(in_channels,out_channels): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels,out_channels=out_channels,kernel_size=3,stride=1,padding=1), + nn.ReLU6(inplace=True) + ) + +def locLayer(in_channels,out_channels): + return nn.Sequential( + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1), + ) + +def conf_centernessLayer(in_channels,out_channels): + return nn.Sequential( + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + Conv3x3ReLU(in_channels=in_channels, out_channels=in_channels), + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1), + ) + +class FCOS(nn.Module): + def __init__(self, num_classes=21): + super(FCOS, self).__init__() + self.num_classes = num_classes + resnet = torchvision.models.resnet50() + layers = list(resnet.children()) + + self.layer1 = nn.Sequential(*layers[:5]) + self.layer2 = nn.Sequential(*layers[5]) + self.layer3 = nn.Sequential(*layers[6]) + self.layer4 = nn.Sequential(*layers[7]) + + self.lateral5 = nn.Conv2d(in_channels=2048, out_channels=256, kernel_size=1) + self.lateral4 = nn.Conv2d(in_channels=1024, out_channels=256, kernel_size=1) + self.lateral3 = nn.Conv2d(in_channels=512, out_channels=256, kernel_size=1) + + self.upsample4 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=4, stride=2, padding=1) + self.upsample3 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=4, stride=2, padding=1) + + self.downsample6 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1) + self.downsample5 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1) + + self.loc_layer3 = locLayer(in_channels=256,out_channels=4) + self.conf_centerness_layer3 = conf_centernessLayer(in_channels=256,out_channels=self.num_classes+1) + + self.loc_layer4 = locLayer(in_channels=256, out_channels=4) + self.conf_centerness_layer4 = conf_centernessLayer(in_channels=256, out_channels=self.num_classes + 1) + + self.loc_layer5 = locLayer(in_channels=256, out_channels=4) + self.conf_centerness_layer5 = conf_centernessLayer(in_channels=256, out_channels=self.num_classes + 1) + + self.loc_layer6 = locLayer(in_channels=256, out_channels=4) + self.conf_centerness_layer6 = conf_centernessLayer(in_channels=256, out_channels=self.num_classes + 1) + + self.loc_layer7 = locLayer(in_channels=256, out_channels=4) + self.conf_centerness_layer7 = conf_centernessLayer(in_channels=256, out_channels=self.num_classes + 1) + + self.init_params() + + def init_params(self): + for m in self.modules(): + if isinstance(m, nn.Conv2d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.BatchNorm2d): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + + def forward(self, x): + x = self.layer1(x) + c3 =x = self.layer2(x) + c4 =x = self.layer3(x) + c5 = x = self.layer4(x) + + p5 = self.lateral5(c5) + p4 = self.upsample4(p5) + self.lateral4(c4) + p3 = self.upsample3(p4) + self.lateral3(c3) + + p6 = self.downsample5(p5) + p7 = self.downsample6(p6) + + loc3 = self.loc_layer3(p3) + conf_centerness3 = self.conf_centerness_layer3(p3) + conf3, centerness3 = conf_centerness3.split([self.num_classes, 1], dim=1) + + loc4 = self.loc_layer4(p4) + conf_centerness4 = self.conf_centerness_layer4(p4) + conf4, centerness4 = conf_centerness4.split([self.num_classes, 1], dim=1) + + loc5 = self.loc_layer5(p5) + conf_centerness5 = self.conf_centerness_layer5(p5) + conf5, centerness5 = conf_centerness5.split([self.num_classes, 1], dim=1) + + loc6 = self.loc_layer6(p6) + conf_centerness6 = self.conf_centerness_layer6(p6) + conf6, centerness6 = conf_centerness6.split([self.num_classes, 1], dim=1) + + loc7 = self.loc_layer7(p7) + conf_centerness7 = self.conf_centerness_layer7(p7) + conf7, centerness7 = conf_centerness7.split([self.num_classes, 1], dim=1) + + locs = torch.cat([loc3.permute(0, 2, 3, 1).contiguous().view(loc3.size(0), -1), + loc4.permute(0, 2, 3, 1).contiguous().view(loc4.size(0), -1), + loc5.permute(0, 2, 3, 1).contiguous().view(loc5.size(0), -1), + loc6.permute(0, 2, 3, 1).contiguous().view(loc6.size(0), -1), + loc7.permute(0, 2, 3, 1).contiguous().view(loc7.size(0), -1)],dim=1) + + confs = torch.cat([conf3.permute(0, 2, 3, 1).contiguous().view(conf3.size(0), -1), + conf4.permute(0, 2, 3, 1).contiguous().view(conf4.size(0), -1), + conf5.permute(0, 2, 3, 1).contiguous().view(conf5.size(0), -1), + conf6.permute(0, 2, 3, 1).contiguous().view(conf6.size(0), -1), + conf7.permute(0, 2, 3, 1).contiguous().view(conf7.size(0), -1),], dim=1) + + centernesses = torch.cat([centerness3.permute(0, 2, 3, 1).contiguous().view(centerness3.size(0), -1), + centerness4.permute(0, 2, 3, 1).contiguous().view(centerness4.size(0), -1), + centerness5.permute(0, 2, 3, 1).contiguous().view(centerness5.size(0), -1), + centerness6.permute(0, 2, 3, 1).contiguous().view(centerness6.size(0), -1), + centerness7.permute(0, 2, 3, 1).contiguous().view(centerness7.size(0), -1), ], dim=1) + + out = (locs, confs, centernesses) + return out + +if __name__ == '__main__': + model = FCOS() + print(model) + + input = torch.randn(1, 3, 800, 1024) + out = model(input) + print(out[0].shape) + print(out[1].shape) + print(out[2].shape) \ No newline at end of file diff --git a/FisheyeMODNet.py b/FisheyeMODNet.py new file mode 100644 index 0000000..1c02320 --- /dev/null +++ b/FisheyeMODNet.py @@ -0,0 +1,129 @@ +import torch +import torch.nn as nn + +def Conv3x3BNReLU(in_channels,out_channels,stride,groups): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=1,groups=groups), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True) + ) + +def Conv1x1BNReLU(in_channels,out_channels,groups): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1,groups=groups), + nn.BatchNorm2d(out_channels), + nn.ReLU6(inplace=True) + ) + +def Conv1x1BN(in_channels,out_channels,groups): + return nn.Sequential( + nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1,groups=groups), + nn.BatchNorm2d(out_channels) + ) + +class ChannelShuffle(nn.Module): + def __init__(self, groups): + super(ChannelShuffle, self).__init__() + self.groups = groups + + def forward(self, x): + '''Channel shuffle: [N,C,H,W] -> [N,g,C/g,H,W] -> [N,C/g,g,H,w] -> [N,C,H,W]''' + N, C, H, W = x.size() + g = self.groups + return x.view(N, g, int(C / g), H, W).permute(0, 2, 1, 3, 4).contiguous().view(N, C, H, W) + + +class ShuffleNetUnits(nn.Module): + def __init__(self, in_channels, out_channels, stride, groups): + super(ShuffleNetUnits, self).__init__() + self.stride = stride + out_channels = out_channels - in_channels if self.stride>1 else out_channels + mid_channels = out_channels // 4 + + self.bottleneck = nn.Sequential( + Conv1x1BNReLU(in_channels, mid_channels,groups), + ChannelShuffle(groups), + Conv3x3BNReLU(mid_channels, mid_channels, stride,groups), + Conv1x1BN(mid_channels, out_channels,groups) + ) + if self.stride>1: + self.shortcut = nn.MaxPool2d(kernel_size=3,stride=2,padding=1) + + self.relu = nn.ReLU6(inplace=True) + + def forward(self, x): + out = self.bottleneck(x) + out = torch.cat([self.shortcut(x), out], dim=1) if self.stride > 1 else (out + x) + return self.relu(out) + +class FisheyeMODNet(nn.Module): + def __init__(self, groups=1, num_classes=2): + super(FisheyeMODNet, self).__init__() + layers = [4, 8, 4] + + self.stage1a = nn.Sequential( + nn.Conv2d(in_channels=3, out_channels=24, kernel_size=3,stride=2, padding=1), + nn.MaxPool2d(kernel_size=2,stride=2), + ) + self.stage2a = self._make_layer(24, 120, groups, layers[0]) + + self.stage1b = nn.Sequential( + nn.Conv2d(in_channels=3, out_channels=24, kernel_size=3, stride=2, padding=1), + nn.MaxPool2d(kernel_size=2, stride=2), + ) + self.stage2b = self._make_layer(24, 120, groups, layers[0]) + + self.stage3 = self._make_layer(240, 480, groups, layers[1]) + self.stage4 = self._make_layer(480, 960, groups, layers[2]) + + self.adapt_conv3 = nn.Conv2d(960, num_classes, kernel_size=1) + self.adapt_conv2 = nn.Conv2d(480, num_classes, kernel_size=1) + self.adapt_conv1 = nn.Conv2d(240, num_classes, kernel_size=1) + + self.up_sampling3 = nn.ConvTranspose2d(in_channels=num_classes, out_channels=num_classes, kernel_size=4, stride=2, padding=1) + self.up_sampling2 = nn.ConvTranspose2d(in_channels=num_classes, out_channels=num_classes, kernel_size=4, stride=2, padding=1) + self.up_sampling1 = nn.ConvTranspose2d(in_channels=num_classes, out_channels=num_classes, kernel_size=16, stride=8, padding=4) + + self.softmax = nn.Softmax(dim=1) + + self.init_params() + + def _make_layer(self, in_channels, out_channels, groups, block_num): + layers = [] + layers.append(ShuffleNetUnits(in_channels=in_channels, out_channels=out_channels, stride=2, groups=groups)) + for idx in range(1, block_num): + layers.append(ShuffleNetUnits(in_channels=out_channels, out_channels=out_channels, stride=1, groups=groups)) + return nn.Sequential(*layers) + + def init_params(self): + for m in self.modules(): + if isinstance(m, nn.Conv2d): + nn.init.kaiming_normal_(m.weight) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.Linear): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + + def forward(self, x, y): + x = self.stage2a(self.stage1a(x)) + y = self.stage2b(self.stage1b(y)) + feature1 = torch.cat([x, y], dim=1) + feature2 = self.stage3(feature1) + feature3 = self.stage4(feature2) + + out3 = self.up_sampling3(self.adapt_conv3(feature3)) + out2 = self.up_sampling2(self.adapt_conv2(feature2) + out3) + out1 = self.up_sampling1(self.adapt_conv1(feature1) + out2) + + out = self.softmax(out1) + return out + + +if __name__ == '__main__': + model = FisheyeMODNet() + + input1 = torch.randn(1, 3, 640, 640) + input2 = torch.randn(1, 3, 640, 640) + + out = model(input1, input2) + print(out.shape) \ No newline at end of file -- 2.34.1