models/mobilenet_v1.py (184 lines of code) (raw):

import torch import torch.nn import torch.functional import torch.nn.functional class MobileNet(torch.nn.Module): def __init__(self): super().__init__() self.model_0_0 = torch.nn.modules.conv.Conv2d(3, 32, 3, 2, 1, bias=False) self.model_0_1 = torch.nn.modules.batchnorm.BatchNorm2d(32) self.model_0_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_1_0 = torch.nn.modules.conv.Conv2d(32, 32, 3, 1, 1, groups=32, bias=False) self.model_1_1 = torch.nn.modules.batchnorm.BatchNorm2d(32) self.model_1_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_1_3 = torch.nn.modules.conv.Conv2d(32, 64, 1, 1, 0, bias=False) self.model_1_4 = torch.nn.modules.batchnorm.BatchNorm2d(64) self.model_1_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_2_0 = torch.nn.modules.conv.Conv2d(64, 64, 3, 2, 1, groups=64, bias=False) self.model_2_1 = torch.nn.modules.batchnorm.BatchNorm2d(64) self.model_2_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_2_3 = torch.nn.modules.conv.Conv2d(64, 128, 1, 1, 0, bias=False) self.model_2_4 = torch.nn.modules.batchnorm.BatchNorm2d(128) self.model_2_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_3_0 = torch.nn.modules.conv.Conv2d(128, 128, 3, 1, 1, groups=128, bias=False) self.model_3_1 = torch.nn.modules.batchnorm.BatchNorm2d(128) self.model_3_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_3_3 = torch.nn.modules.conv.Conv2d(128, 128, 1, 1, 0, bias=False) self.model_3_4 = torch.nn.modules.batchnorm.BatchNorm2d(128) self.model_3_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_4_0 = torch.nn.modules.conv.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False) self.model_4_1 = torch.nn.modules.batchnorm.BatchNorm2d(128) self.model_4_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_4_3 = torch.nn.modules.conv.Conv2d(128, 256, 1, 1, 0, bias=False) self.model_4_4 = torch.nn.modules.batchnorm.BatchNorm2d(256) self.model_4_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_5_0 = torch.nn.modules.conv.Conv2d(256, 256, 3, 1, 1, groups=256, bias=False) self.model_5_1 = torch.nn.modules.batchnorm.BatchNorm2d(256) self.model_5_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_5_3 = torch.nn.modules.conv.Conv2d(256, 256, 1, 1, 0, bias=False) self.model_5_4 = torch.nn.modules.batchnorm.BatchNorm2d(256) self.model_5_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_6_0 = torch.nn.modules.conv.Conv2d(256, 256, 3, 2, 1, groups=256, bias=False) self.model_6_1 = torch.nn.modules.batchnorm.BatchNorm2d(256) self.model_6_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_6_3 = torch.nn.modules.conv.Conv2d(256, 512, 1, 1, 0, bias=False) self.model_6_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_6_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_7_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 1, 1, groups=512, bias=False) self.model_7_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_7_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_7_3 = torch.nn.modules.conv.Conv2d(512, 512, 1, 1, 0, bias=False) self.model_7_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_7_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_8_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 1, 1, groups=512, bias=False) self.model_8_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_8_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_8_3 = torch.nn.modules.conv.Conv2d(512, 512, 1, 1, 0, bias=False) self.model_8_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_8_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_9_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 1, 1, groups=512, bias=False) self.model_9_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_9_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_9_3 = torch.nn.modules.conv.Conv2d(512, 512, 1, 1, 0, bias=False) self.model_9_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_9_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_10_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 1, 1, groups=512, bias=False) self.model_10_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_10_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_10_3 = torch.nn.modules.conv.Conv2d(512, 512, 1, 1, 0, bias=False) self.model_10_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_10_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_11_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 1, 1, groups=512, bias=False) self.model_11_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_11_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_11_3 = torch.nn.modules.conv.Conv2d(512, 512, 1, 1, 0, bias=False) self.model_11_4 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_11_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_12_0 = torch.nn.modules.conv.Conv2d(512, 512, 3, 2, 1, groups=512, bias=False) self.model_12_1 = torch.nn.modules.batchnorm.BatchNorm2d(512) self.model_12_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_12_3 = torch.nn.modules.conv.Conv2d(512, 1024, 1, 1, 0, bias=False) self.model_12_4 = torch.nn.modules.batchnorm.BatchNorm2d(1024) self.model_12_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_13_0 = torch.nn.modules.conv.Conv2d(1024, 1024, 3, 1, 1, groups=1024, bias=False) self.model_13_1 = torch.nn.modules.batchnorm.BatchNorm2d(1024) self.model_13_2 = torch.nn.modules.activation.ReLU(inplace=True) self.model_13_3 = torch.nn.modules.conv.Conv2d(1024, 1024, 1, 1, 0, bias=False) self.model_13_4 = torch.nn.modules.batchnorm.BatchNorm2d(1024) self.model_13_5 = torch.nn.modules.activation.ReLU(inplace=True) self.model_14 = torch.nn.modules.pooling.AvgPool2d(7) self.fc = torch.nn.modules.linear.Linear(1024, 1000) def forward(self, input_1): model_0_0 = self.model_0_0(input_1) model_0_1 = self.model_0_1(model_0_0) model_0_2 = self.model_0_2(model_0_1) model_1_0 = self.model_1_0(model_0_2) model_1_1 = self.model_1_1(model_1_0) model_1_2 = self.model_1_2(model_1_1) model_1_3 = self.model_1_3(model_1_2) model_1_4 = self.model_1_4(model_1_3) model_1_5 = self.model_1_5(model_1_4) model_2_0 = self.model_2_0(model_1_5) model_2_1 = self.model_2_1(model_2_0) model_2_2 = self.model_2_2(model_2_1) model_2_3 = self.model_2_3(model_2_2) model_2_4 = self.model_2_4(model_2_3) model_2_5 = self.model_2_5(model_2_4) model_3_0 = self.model_3_0(model_2_5) model_3_1 = self.model_3_1(model_3_0) model_3_2 = self.model_3_2(model_3_1) model_3_3 = self.model_3_3(model_3_2) model_3_4 = self.model_3_4(model_3_3) model_3_5 = self.model_3_5(model_3_4) model_4_0 = self.model_4_0(model_3_5) model_4_1 = self.model_4_1(model_4_0) model_4_2 = self.model_4_2(model_4_1) model_4_3 = self.model_4_3(model_4_2) model_4_4 = self.model_4_4(model_4_3) model_4_5 = self.model_4_5(model_4_4) model_5_0 = self.model_5_0(model_4_5) model_5_1 = self.model_5_1(model_5_0) model_5_2 = self.model_5_2(model_5_1) model_5_3 = self.model_5_3(model_5_2) model_5_4 = self.model_5_4(model_5_3) model_5_5 = self.model_5_5(model_5_4) model_6_0 = self.model_6_0(model_5_5) model_6_1 = self.model_6_1(model_6_0) model_6_2 = self.model_6_2(model_6_1) model_6_3 = self.model_6_3(model_6_2) model_6_4 = self.model_6_4(model_6_3) model_6_5 = self.model_6_5(model_6_4) model_7_0 = self.model_7_0(model_6_5) model_7_1 = self.model_7_1(model_7_0) model_7_2 = self.model_7_2(model_7_1) model_7_3 = self.model_7_3(model_7_2) model_7_4 = self.model_7_4(model_7_3) model_7_5 = self.model_7_5(model_7_4) model_8_0 = self.model_8_0(model_7_5) model_8_1 = self.model_8_1(model_8_0) model_8_2 = self.model_8_2(model_8_1) model_8_3 = self.model_8_3(model_8_2) model_8_4 = self.model_8_4(model_8_3) model_8_5 = self.model_8_5(model_8_4) model_9_0 = self.model_9_0(model_8_5) model_9_1 = self.model_9_1(model_9_0) model_9_2 = self.model_9_2(model_9_1) model_9_3 = self.model_9_3(model_9_2) model_9_4 = self.model_9_4(model_9_3) model_9_5 = self.model_9_5(model_9_4) model_10_0 = self.model_10_0(model_9_5) model_10_1 = self.model_10_1(model_10_0) model_10_2 = self.model_10_2(model_10_1) model_10_3 = self.model_10_3(model_10_2) model_10_4 = self.model_10_4(model_10_3) model_10_5 = self.model_10_5(model_10_4) model_11_0 = self.model_11_0(model_10_5) model_11_1 = self.model_11_1(model_11_0) model_11_2 = self.model_11_2(model_11_1) model_11_3 = self.model_11_3(model_11_2) model_11_4 = self.model_11_4(model_11_3) model_11_5 = self.model_11_5(model_11_4) model_12_0 = self.model_12_0(model_11_5) model_12_1 = self.model_12_1(model_12_0) model_12_2 = self.model_12_2(model_12_1) model_12_3 = self.model_12_3(model_12_2) model_12_4 = self.model_12_4(model_12_3) model_12_5 = self.model_12_5(model_12_4) model_13_0 = self.model_13_0(model_12_5) model_13_1 = self.model_13_1(model_13_0) model_13_2 = self.model_13_2(model_13_1) model_13_3 = self.model_13_3(model_13_2) model_13_4 = self.model_13_4(model_13_3) model_13_5 = self.model_13_5(model_13_4) model_14 = self.model_14(model_13_5) shape_1 = model_14.shape view_1 = model_14.view(shape_1[0], -1) fc = self.fc(view_1) return fc if __name__ == "__main__": model = MobileNet() model.eval() model.cpu() dummy_input_0 = torch.ones((1, 3, 224, 224), dtype=torch.float32) output = model(dummy_input_0) print(output)