├── AlexNet.py ├── DenseNet.py ├── EfficientNet.py ├── GoogLeNet.py ├── LeNet.py ├── Mnasnet.py ├── MobileNetV1.py ├── MobileNetV2.py ├── MobileNetV3.py ├── README.md ├── ResNet50.py ├── SqueezeNet.py ├── VGG19.py ├── Xception.py ├── ZFNet.py ├── shuffuleNetV1.py └── shuffuleNetV2.py /AlexNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class AlexNet(torch.nn.Module): 4 | def __init__(self,in_channels,classes,input_sample_points): 5 | super(AlexNet, self).__init__() 6 | 7 | self.input_channels = in_channels 8 | self.input_sample_points = input_sample_points 9 | 10 | self.features = torch.nn.Sequential( 11 | 12 | torch.nn.Conv1d(in_channels,64,kernel_size=11,stride=4,padding=2), 13 | torch.nn.BatchNorm1d(64), 14 | torch.nn.ReLU(inplace=True), 15 | #torch.nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75, k=2), 16 | torch.nn.MaxPool1d(kernel_size=3,stride=2), 17 | 18 | torch.nn.Conv1d(64, 192, kernel_size=5, padding=2), 19 | torch.nn.BatchNorm1d(192), 20 | torch.nn.ReLU(inplace=True), 21 | #torch.nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75, k=2), 22 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 23 | 24 | torch.nn.Conv1d(192, 384, kernel_size=3, padding=1), 25 | torch.nn.BatchNorm1d(384), 26 | torch.nn.ReLU(inplace=True), 27 | torch.nn.Conv1d(384, 256, kernel_size=3, padding=1), 28 | torch.nn.ReLU(inplace=True), 29 | torch.nn.BatchNorm1d(256), 30 | torch.nn.Conv1d(256, 256, kernel_size=3, padding=1), 31 | torch.nn.BatchNorm1d(256), 32 | torch.nn.ReLU(inplace=True), 33 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 34 | #自适应平均池化不管输入多少输出一定为6 35 | torch.nn.AdaptiveAvgPool1d(6), 36 | ) 37 | 38 | self.classifier = torch.nn.Sequential( 39 | 40 | torch.nn.Dropout(0.5), 41 | torch.nn.Linear(1536,1024), 42 | torch.nn.ReLU(inplace=True), 43 | 44 | torch.nn.Dropout(0.5), 45 | torch.nn.Linear(1024, 1024), 46 | torch.nn.ReLU(inplace=True), 47 | torch.nn.Linear(1024,classes), 48 | 49 | ) 50 | 51 | def forward(self,x): 52 | 53 | x = self.features(x) 54 | x = x.view(-1,1536) 55 | x = self.classifier(x) 56 | return x 57 | 58 | 59 | if __name__ == '__main__': 60 | model = AlexNet(in_channels=1, input_sample_points=224, classes=5) 61 | input = torch.randn(size=(1, 1, 224)) 62 | output = model(input) 63 | print(output.shape) 64 | -------------------------------------------------------------------------------- /DenseNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class DenseLayer(torch.nn.Module): 4 | def __init__(self,in_channels,middle_channels=128,out_channels=32): 5 | super(DenseLayer, self).__init__() 6 | self.layer = torch.nn.Sequential( 7 | torch.nn.BatchNorm1d(in_channels), 8 | torch.nn.ReLU(inplace=True), 9 | torch.nn.Conv1d(in_channels,middle_channels,1), 10 | torch.nn.BatchNorm1d(middle_channels), 11 | torch.nn.ReLU(inplace=True), 12 | torch.nn.Conv1d(middle_channels,out_channels,3,padding=1) 13 | ) 14 | def forward(self,x): 15 | return torch.cat([x,self.layer(x)],dim=1) 16 | 17 | 18 | class DenseBlock(torch.nn.Sequential): 19 | def __init__(self,layer_num,growth_rate,in_channels,middele_channels=128): 20 | super(DenseBlock, self).__init__() 21 | for i in range(layer_num): 22 | layer = DenseLayer(in_channels+i*growth_rate,middele_channels,growth_rate) 23 | self.add_module('denselayer%d'%(i),layer) 24 | 25 | class Transition(torch.nn.Sequential): 26 | def __init__(self,channels): 27 | super(Transition, self).__init__() 28 | self.add_module('norm',torch.nn.BatchNorm1d(channels)) 29 | self.add_module('relu',torch.nn.ReLU(inplace=True)) 30 | self.add_module('conv',torch.nn.Conv1d(channels,channels//2,3,padding=1)) 31 | self.add_module('Avgpool',torch.nn.AvgPool1d(2)) 32 | 33 | 34 | class DenseNet(torch.nn.Module): 35 | def __init__(self,layer_num=(6,12,24,16),growth_rate=32,init_features=64,in_channels=1,middele_channels=128,classes=5): 36 | super(DenseNet, self).__init__() 37 | self.feature_channel_num=init_features 38 | self.conv=torch.nn.Conv1d(in_channels,self.feature_channel_num,7,2,3) 39 | self.norm=torch.nn.BatchNorm1d(self.feature_channel_num) 40 | self.relu=torch.nn.ReLU() 41 | self.maxpool=torch.nn.MaxPool1d(3,2,1) 42 | 43 | self.DenseBlock1=DenseBlock(layer_num[0],growth_rate,self.feature_channel_num,middele_channels) 44 | self.feature_channel_num=self.feature_channel_num+layer_num[0]*growth_rate 45 | self.Transition1=Transition(self.feature_channel_num) 46 | 47 | self.DenseBlock2=DenseBlock(layer_num[1],growth_rate,self.feature_channel_num//2,middele_channels) 48 | self.feature_channel_num=self.feature_channel_num//2+layer_num[1]*growth_rate 49 | self.Transition2 = Transition(self.feature_channel_num) 50 | 51 | self.DenseBlock3 = DenseBlock(layer_num[2],growth_rate,self.feature_channel_num//2,middele_channels) 52 | self.feature_channel_num=self.feature_channel_num//2+layer_num[2]*growth_rate 53 | self.Transition3 = Transition(self.feature_channel_num) 54 | 55 | self.DenseBlock4 = DenseBlock(layer_num[3],growth_rate,self.feature_channel_num//2,middele_channels) 56 | self.feature_channel_num=self.feature_channel_num//2+layer_num[3]*growth_rate 57 | 58 | self.avgpool=torch.nn.AdaptiveAvgPool1d(1) 59 | 60 | self.classifer = torch.nn.Sequential( 61 | torch.nn.Linear(self.feature_channel_num, self.feature_channel_num//2), 62 | torch.nn.ReLU(), 63 | torch.nn.Dropout(0.5), 64 | torch.nn.Linear(self.feature_channel_num//2, classes), 65 | 66 | ) 67 | 68 | 69 | def forward(self,x): 70 | x = self.conv(x) 71 | x = self.norm(x) 72 | x = self.relu(x) 73 | x = self.maxpool(x) 74 | 75 | x = self.DenseBlock1(x) 76 | x = self.Transition1(x) 77 | 78 | x = self.DenseBlock2(x) 79 | x = self.Transition2(x) 80 | 81 | x = self.DenseBlock3(x) 82 | x = self.Transition3(x) 83 | 84 | x = self.DenseBlock4(x) 85 | x = self.avgpool(x) 86 | x = x.view(-1,self.feature_channel_num) 87 | x = self.classifer(x) 88 | 89 | return x 90 | 91 | 92 | 93 | if __name__ == '__main__': 94 | input = torch.randn(size=(1,1,224)) 95 | model = DenseNet(layer_num=(6,12,24,16),growth_rate=32,in_channels=1,classes=5) 96 | output = model(input) 97 | print(output.shape) 98 | 99 | 100 | -------------------------------------------------------------------------------- /EfficientNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from thop import profile 3 | 4 | class SEModule(torch.nn.Module): 5 | def __init__(self,in_channel,ratio=4): 6 | super(SEModule, self).__init__() 7 | self.avepool = torch.nn.AdaptiveAvgPool1d(1) 8 | self.linear1 = torch.nn.Linear(in_channel,in_channel//ratio) 9 | self.linear2 = torch.nn.Linear(in_channel//ratio,in_channel) 10 | self.Hardsigmoid = torch.nn.Hardsigmoid(inplace=True) 11 | self.Relu = torch.nn.ReLU(inplace=True) 12 | 13 | def forward(self,input): 14 | b,c,_ = input.shape 15 | x = self.avepool(input) 16 | x = x.view([b,c]) 17 | x = self.linear1(x) 18 | x = self.Relu(x) 19 | x = self.linear2(x) 20 | x = self.Hardsigmoid(x) 21 | x = x.view([b,c,1]) 22 | 23 | return input*x 24 | 25 | 26 | class MBConvBlock(torch.nn.Module): 27 | def __init__(self, in_channels, out_channels, expand_ratio, kernel_size, stride, se_ratio=4): 28 | super(MBConvBlock, self).__init__() 29 | # Expansion phase 30 | expanded_channels = int(in_channels * expand_ratio) 31 | self.expand_conv = torch.nn.Conv1d(in_channels, expanded_channels, kernel_size=1, stride=1, padding=0, bias=False) 32 | self.bn1 = torch.nn.BatchNorm1d(expanded_channels) 33 | # Depthwise convolution 34 | self.depthwise_conv = torch.nn.Conv1d(expanded_channels, expanded_channels, kernel_size=kernel_size, stride=stride, 35 | padding=kernel_size // 2, groups=expanded_channels, bias=False) 36 | self.bn2 = torch.nn.BatchNorm1d(expanded_channels) 37 | # Squeeze and Excitation (SE) phase 38 | self.se = SEModule(expanded_channels, se_ratio) 39 | # Linear Bottleneck 40 | self.linear_bottleneck = torch.nn.Conv1d(expanded_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False) 41 | self.bn3 = torch.nn.BatchNorm1d(out_channels) 42 | # Skip connection if input and output channels are the same and stride is 1 43 | self.use_skip_connection = (stride == 1) and (in_channels == out_channels) 44 | self.leakyrelu = torch.nn.LeakyReLU(0.02) 45 | 46 | def forward(self, x): 47 | identity = x 48 | # Expansion phase 49 | x = self.leakyrelu(self.bn1(self.expand_conv(x))) 50 | # Depthwise convolution phase 51 | x = self.leakyrelu(self.bn2(self.depthwise_conv(x))) 52 | # Squeeze and Excitation phase 53 | x = self.se(x) 54 | # Linear Bottleneck phase 55 | x = self.bn3(self.linear_bottleneck(x)) 56 | 57 | # Skip connection 58 | if self.use_skip_connection: 59 | x = identity + x 60 | 61 | return x 62 | 63 | 64 | class EfficientNetB0(torch.nn.Module): 65 | def __init__(self, in_channels=3,classes=1000): 66 | super(EfficientNetB0, self).__init__() 67 | 68 | # Initial stem convolution 69 | self.stem = torch.nn.Sequential( 70 | torch.nn.Conv1d(in_channels, 32, kernel_size=3, stride=2, padding=1, bias=False), 71 | torch.nn.BatchNorm1d(32), 72 | torch.nn.LeakyReLU(0.02) 73 | ) 74 | 75 | # Building blocks 76 | self.blocks = torch.nn.Sequential( 77 | MBConvBlock(32, 16, 1, 3, 1), 78 | 79 | MBConvBlock(16, 24, 6, 3, 2), 80 | MBConvBlock(24, 24, 6, 3, 1), 81 | 82 | MBConvBlock(24, 40, 6, 5, 2), 83 | MBConvBlock(40, 40, 6, 5, 1), 84 | 85 | MBConvBlock(40, 80, 6, 3, 2), 86 | MBConvBlock(80, 80, 6, 3, 1), 87 | MBConvBlock(80, 80, 6, 3, 1), 88 | 89 | 90 | MBConvBlock(80, 112, 6, 5, 1), 91 | MBConvBlock(112, 112, 6, 5, 1), 92 | MBConvBlock(112, 112, 6, 5, 1), 93 | 94 | MBConvBlock(112, 192, 6, 5, 2), 95 | MBConvBlock(192, 192, 6, 5, 1), 96 | MBConvBlock(192, 192, 6, 5, 1), 97 | MBConvBlock(192, 192, 6, 5, 1), 98 | 99 | MBConvBlock(192, 320, 6, 3, 1), 100 | ) 101 | 102 | # Head 103 | self.head = torch.nn.Sequential( 104 | torch.nn.Conv1d(320, 1280, kernel_size=1, stride=1, padding=0, bias=False), 105 | torch.nn.BatchNorm1d(1280), 106 | torch.nn.LeakyReLU(0.02) 107 | ) 108 | 109 | # Global average pooling and classifier 110 | self.avg_pool = torch.nn.AdaptiveAvgPool1d(1) 111 | self.fc = torch.nn.Linear(1280, classes) 112 | 113 | def forward(self, x): 114 | x = self.stem(x) 115 | x = self.blocks(x) 116 | x = self.head(x) 117 | x = self.avg_pool(x) 118 | x = x.view(x.size(0), -1) 119 | x = self.fc(x) 120 | return x 121 | 122 | 123 | if __name__ == '__main__': 124 | # input = torch.randn((1,1,224)) 125 | model = EfficientNetB0(2,200) 126 | 127 | input = torch.randn(1, 2, 200) 128 | flops, params = profile(model, inputs=(input,)) 129 | 130 | print("FLOPs=", str(flops / 1e6) + '{}'.format("M")) 131 | print("params=", str(params / 1e6) + '{}'.format("M")) 132 | -------------------------------------------------------------------------------- /GoogLeNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | class Inception(torch.nn.Module): 5 | def __init__(self, in_channels=56, ch1=64, ch3_reduce=96, ch3=128, ch5_reduce=16, ch5=32, pool_proj=32): 6 | super(Inception, self).__init__() 7 | 8 | self.branch1 = torch.nn.Sequential( 9 | torch.nn.Conv1d(in_channels, ch1, kernel_size=1), 10 | torch.nn.BatchNorm1d(ch1) 11 | ) 12 | 13 | self.branch3 = torch.nn.Sequential( 14 | torch.nn.Conv1d(in_channels, ch3_reduce, kernel_size=1), 15 | torch.nn.BatchNorm1d(ch3_reduce), 16 | torch.nn.Conv1d(ch3_reduce, ch3, kernel_size=3, padding=1), 17 | torch.nn.BatchNorm1d(ch3), 18 | ) 19 | 20 | self.branch5 = torch.nn.Sequential( 21 | torch.nn.Conv1d(in_channels, ch5_reduce, kernel_size=1), 22 | torch.nn.BatchNorm1d(ch5_reduce), 23 | torch.nn.Conv1d(ch5_reduce, ch5, kernel_size=5, padding=2), 24 | torch.nn.BatchNorm1d(ch5), 25 | ) 26 | 27 | self.branch_pool = torch.nn.Sequential( 28 | torch.nn.MaxPool1d(kernel_size=3, stride=1, padding=1), 29 | torch.nn.Conv1d(in_channels, pool_proj, kernel_size=1) 30 | ) 31 | 32 | def forward(self, x): 33 | return torch.cat([self.branch1(x), self.branch3(x), self.branch5(x), self.branch_pool(x)], 1) 34 | 35 | 36 | class GoogLeNet(torch.nn.Module): 37 | def __init__(self, in_channels=2, classes=5, in_sample_points=224): 38 | super(GoogLeNet, self).__init__() 39 | 40 | self.features = torch.nn.Sequential( 41 | torch.nn.Linear(in_sample_points, 224), 42 | torch.nn.Conv1d(in_channels, 64, kernel_size=7, 43 | stride=2, padding=3), 44 | torch.nn.MaxPool1d(3, 2, padding=1), 45 | torch.nn.Conv1d(64, 192, 3, padding=1), 46 | torch.nn.MaxPool1d(3, 2, padding=1), 47 | Inception(192, 64, 96, 128, 16, 32, 32), 48 | Inception(256, 128, 128, 192, 32, 96, 64), 49 | torch.nn.MaxPool1d(3, 2, padding=1), 50 | Inception(480, 192, 96, 208, 16, 48, 64), 51 | ) 52 | 53 | self.classifer_max_pool = torch.nn.MaxPool1d(5, 3) 54 | 55 | self.classifer = torch.nn.Sequential( 56 | torch.nn.Linear(2048, 1024), 57 | torch.nn.Dropout(0.5), 58 | torch.nn.ReLU(), 59 | torch.nn.Linear(1024, 512), 60 | torch.nn.Dropout(0.5), 61 | torch.nn.ReLU(), 62 | torch.nn.Linear(512, classes), 63 | ) 64 | 65 | self.Inception_4b = Inception(512, 160, 112, 224, 24, 64, 64) 66 | self.Inception_4c = Inception(512, 128, 128, 256, 24, 64, 64) 67 | self.Inception_4d = Inception(512, 112, 144, 288, 32, 64, 64) 68 | 69 | self.classifer1 = torch.nn.Sequential( 70 | torch.nn.Linear(2112, 1056), 71 | torch.nn.Dropout(0.5), 72 | torch.nn.ReLU(), 73 | torch.nn.Linear(1056, 528), 74 | torch.nn.Dropout(0.5), 75 | torch.nn.ReLU(), 76 | torch.nn.Linear(528, classes), 77 | ) 78 | 79 | self.Inception_4e = Inception(528, 256, 160, 320, 32, 128, 128) 80 | self.max_pool = torch.nn.MaxPool1d(3, 2, 1) 81 | 82 | self.Inception_5a = Inception(832, 256, 160, 320, 32, 128, 128) 83 | self.Inception_5b = Inception(832, 384, 192, 384, 48, 128, 128) 84 | 85 | self.avg_pool = torch.nn.AvgPool1d(7, stride=1) 86 | self.dropout = torch.nn.Dropout(0.4) 87 | self.classifer2 = torch.nn.Sequential( 88 | torch.nn.Linear(1024, 512), 89 | torch.nn.Dropout(0.5), 90 | torch.nn.ReLU(), 91 | torch.nn.Linear(512, classes), 92 | ) 93 | 94 | def forward(self, x): 95 | x = self.features(x) 96 | 97 | y = self.classifer(self.classifer_max_pool(x).view(-1, 2048)) 98 | 99 | x = self.Inception_4b(x) 100 | x = self.Inception_4c(x) 101 | x = self.Inception_4d(x) 102 | 103 | y1 = self.classifer1(self.classifer_max_pool(x).view(-1, 2112)) 104 | 105 | x = self.Inception_4e(x) 106 | x = self.max_pool(x) 107 | x = self.Inception_5a(x) 108 | x = self.Inception_5b(x) 109 | x = self.avg_pool(x) 110 | x = self.dropout(x) 111 | x = x.view(-1, 1024) 112 | x = self.classifer2(x) 113 | 114 | return x, y, y1 115 | 116 | 117 | class GoogLeNetLoss(torch.nn.Module): 118 | def __init__(self): 119 | super(GoogLeNetLoss, self).__init__() 120 | self.CrossEntropyLoss = torch.nn.CrossEntropyLoss() 121 | 122 | def forward(self, data, label): 123 | c2_loss = self.CrossEntropyLoss(data[0], label) 124 | c0_loss = self.CrossEntropyLoss(data[1], label) 125 | c1_loss = self.CrossEntropyLoss(data[2], label) 126 | 127 | loss = c2_loss + 0.3*(c0_loss+c1_loss) 128 | 129 | return loss 130 | 131 | 132 | if __name__ == '__main__': 133 | model = GoogLeNet(in_channels=2, classes=2) 134 | input = torch.randn(size=(2, 2, 224)) 135 | # [c2,c0,c1] = model(input) 136 | output = model(input) 137 | criterion = GoogLeNetLoss() 138 | label = torch.tensor([1, 0]) 139 | print(f"Loss:{criterion(output,label)}") 140 | print(f"result:{output}") 141 | -------------------------------------------------------------------------------- /LeNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class LeNet(torch.nn.Module): 4 | def __init__(self, input_channels, input_sample_points, classes): 5 | super(LeNet, self).__init__() 6 | 7 | self.input_channels = input_channels 8 | self.input_sample_points = input_sample_points 9 | 10 | self.features = torch.nn.Sequential( 11 | torch.nn.Conv1d(input_channels, 20, kernel_size=5), 12 | torch.nn.BatchNorm1d(20), 13 | torch.nn.MaxPool1d(2), 14 | torch.nn.Conv1d(20, 50, kernel_size=5), 15 | torch.nn.BatchNorm1d(50), 16 | torch.nn.MaxPool1d(2), 17 | ) 18 | 19 | self.After_features_channels = 50 20 | # 根据公式计算出通过所有的卷积层和池化层后输出的通道中样本点的数量 21 | # self.After_features_sample_points = ((input_sample_points - 5 + 1) // 2 - 5 + 1) // 2 22 | self.After_features_sample_points = ((input_sample_points-4)//2-4) // 2 23 | 24 | 25 | self.classifier = torch.nn.Sequential( 26 | torch.nn.Linear(self.After_features_channels * self.After_features_sample_points, 512), 27 | torch.nn.ReLU(), 28 | torch.nn.Linear(512, classes), 29 | torch.nn.ReLU() 30 | ) 31 | 32 | def forward(self, x): 33 | # check the dimensionality 34 | # 检查输入样本维度是否有错误 35 | if x.size(1) != self.input_channels or x.size(2) != self.input_sample_points: 36 | raise Exception( 37 | 'Input dimensionality is wrong,Input dimensionality should be [Batch_size,{},{}],Actually is {}'.format(self.input_channels, self.input_sample_points,x.size())) 38 | 39 | x = self.features(x) 40 | x = x.view(-1, self.After_features_channels * self.After_features_sample_points) 41 | x = self.classifier(x) 42 | return x 43 | 44 | 45 | if __name__ == '__main__': 46 | model = LeNet(input_channels=1, input_sample_points=224, classes=5) 47 | input = torch.randn(size=(1, 1, 224)) 48 | output = model(input) 49 | print(output.shape) 50 | 51 | -------------------------------------------------------------------------------- /Mnasnet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class SE_block(torch.nn.Module): 4 | def __init__(self,in_channel,ratio=1): 5 | super(SE_block, self).__init__() 6 | self.avepool = torch.nn.AdaptiveAvgPool1d(1) 7 | self.linear1 = torch.nn.Linear(in_channel,in_channel//ratio) 8 | self.linear2 = torch.nn.Linear(in_channel//ratio,in_channel) 9 | self.Hardsigmoid = torch.nn.Hardsigmoid(inplace=True) 10 | self.Relu = torch.nn.ReLU(inplace=True) 11 | 12 | def forward(self,input): 13 | b,c,_ = input.shape 14 | x = self.avepool(input) 15 | x = x.view([b,c]) 16 | x = self.linear1(x) 17 | x = self.Relu(x) 18 | x = self.linear2(x) 19 | x = self.Hardsigmoid(x) 20 | x = x.view([b,c,1]) 21 | 22 | return input*x 23 | 24 | 25 | 26 | 27 | class conv(torch.nn.Module): 28 | def __init__(self, in_channels, out_channels, keral,stride=1, groups=1,use_activation=True): 29 | super().__init__() 30 | self.use_activation = use_activation 31 | padding = keral//2 32 | self.conv = torch.nn.Conv1d(in_channels, out_channels, keral, stride,padding, groups=groups) 33 | self.bath = torch.nn.BatchNorm1d(out_channels) 34 | self.activation = torch.nn.ReLU(inplace=True) 35 | 36 | def forward(self,x): 37 | x = self.conv(x) 38 | if x.size()[-1] != 1: 39 | x = self.bath(x) 40 | if self.use_activation: 41 | x = self.activation(x) 42 | return x 43 | 44 | 45 | 46 | 47 | class SepConv(torch.nn.Module): 48 | def __init__(self,in_channels,out_channels,stride): 49 | super().__init__() 50 | self.conv = conv(in_channels,in_channels,3,stride,out_channels,True) 51 | self.conv1 = conv(in_channels,out_channels,1,use_activation=False) 52 | 53 | def forward(self,x): 54 | x = self.conv(x) 55 | x = self.conv1(x) 56 | return x 57 | 58 | 59 | 60 | 61 | class MBConv(torch.nn.Module): 62 | def __init__(self,in_channels,out_channels,keral,stride,t=3,use_attention = False): 63 | super().__init__() 64 | self.use_attention = use_attention 65 | self.conv = conv(in_channels,in_channels*t,1) 66 | self.conv1 = conv(in_channels*t,in_channels*t,keral,stride=stride,groups=in_channels*t) 67 | self.attention = SE_block(in_channels*t) 68 | self.conv2 = conv(in_channels*t,out_channels,1,use_activation=False) 69 | 70 | def forward(self,x): 71 | x = self.conv(x) 72 | x = self.conv1(x) 73 | if self.use_attention: 74 | x = self.attention(x) 75 | x = self.conv2(x) 76 | 77 | return x 78 | 79 | class MnasNetA1(torch.nn.Module): 80 | def __init__(self,in_channels,classes): 81 | super().__init__() 82 | 83 | self.fearures = torch.nn.Sequential( 84 | conv(in_channels,32,3,stride=2,use_activation=False), 85 | SepConv(32,16,1), 86 | MBConv(16,16,3,2,6), 87 | MBConv(16,24,3,1,6), 88 | MBConv(24,24,5,2,3,True), 89 | MBConv(24,24,5,2,3,True), 90 | MBConv(24,40,5,2,3,True), 91 | MBConv(40, 40, 3, 2,6), 92 | MBConv(40, 40, 3, 1,6 ), 93 | MBConv(40, 40, 3, 1,6), 94 | MBConv(40, 80, 3, 1,6), 95 | MBConv(80, 80, 3, 1,6,True), 96 | MBConv(80, 112, 3, 1,6,True), 97 | MBConv(112, 112, 5, 2,6,True), 98 | MBConv(112, 112, 5, 1,6,True), 99 | MBConv(112, 160, 5, 1,6,True), 100 | MBConv(160, 160, 3, 2,6), 101 | torch.nn.AdaptiveAvgPool1d(1) 102 | ) 103 | self.classifier = torch.nn.Sequential( 104 | torch.nn.Flatten(), 105 | torch.nn.Linear(160,80), 106 | torch.nn.ReLU(inplace=True), 107 | torch.nn.Linear(80, classes), 108 | ) 109 | def forward(self,x): 110 | x = self.fearures(x) 111 | x = self.classifier(x) 112 | return x 113 | 114 | 115 | if __name__ == "__main__": 116 | model = MnasNetA1(3,5) 117 | input = torch.randn(1,3,224) 118 | output = model(input) 119 | print(output.size()) 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /MobileNetV1.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | class DepthwiseSeparableConv(nn.Module): 5 | def __init__(self, in_channels, out_channels, stride): 6 | super(DepthwiseSeparableConv, self).__init__() 7 | self.depthwise = nn.Conv1d(in_channels, in_channels, kernel_size=3, stride=stride, padding=1, groups=in_channels) 8 | self.pointwise = nn.Conv1d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) 9 | self.relu = nn.ReLU(inplace=True) 10 | 11 | def forward(self, x): 12 | x = self.depthwise(x) 13 | x = self.pointwise(x) 14 | x = self.relu(x) 15 | return x 16 | 17 | class MobileNetV1(nn.Module): 18 | def __init__(self, in_channels = 3,classes=1000): 19 | super(MobileNetV1, self).__init__() 20 | self.model = nn.Sequential( 21 | nn.Conv1d(in_channels, 32, kernel_size=3, stride=2, padding=1), 22 | nn.ReLU(inplace=True), 23 | DepthwiseSeparableConv(32, 64, stride=1), 24 | DepthwiseSeparableConv(64, 128, stride=2), 25 | DepthwiseSeparableConv(128, 128, stride=1), 26 | DepthwiseSeparableConv(128, 256, stride=2), 27 | DepthwiseSeparableConv(256, 256, stride=1), 28 | DepthwiseSeparableConv(256, 512, stride=2), 29 | DepthwiseSeparableConv(512, 512, stride=1), 30 | DepthwiseSeparableConv(512, 512, stride=1), 31 | DepthwiseSeparableConv(512, 512, stride=1), 32 | DepthwiseSeparableConv(512, 512, stride=1), 33 | DepthwiseSeparableConv(512, 1024, stride=2), 34 | DepthwiseSeparableConv(1024, 1024, stride=1), 35 | nn.AdaptiveAvgPool1d(1) 36 | ) 37 | self.classifier = nn.Linear(1024, classes) 38 | 39 | def forward(self, x): 40 | x = self.model(x) 41 | x = x.view(x.size(0), -1) 42 | x = self.classifier(x) 43 | return x 44 | 45 | 46 | if __name__ == "__main__": 47 | # 创建MobileNet V1模型实例 48 | model = MobileNetV1(in_channels=3,classes=5) 49 | # 打印模型结构 50 | input = torch.randn(1, 3, 224) 51 | output = model(input) 52 | print(output.shape) 53 | 54 | -------------------------------------------------------------------------------- /MobileNetV2.py: -------------------------------------------------------------------------------- 1 | # 全文注释 2 | import torch 3 | 4 | 5 | class conv(torch.nn.Module): 6 | def __init__(self, in_channels, out_channels, keral,stride=1, groups=1): 7 | super().__init__() 8 | padding = 0 if keral==1 else 1 9 | 10 | self.conv = torch.nn.Conv1d(in_channels, out_channels, keral, stride,padding, groups=groups) 11 | self.bath = torch.nn.BatchNorm1d(out_channels) 12 | self.relu6 = torch.nn.ReLU6() 13 | 14 | 15 | 16 | def forward(self,x): 17 | x = self.conv(x) 18 | if x.size()[-1] != 1: 19 | x = self.bath(x) 20 | x = self.relu6(x) 21 | return x 22 | 23 | 24 | class bottleneck(torch.nn.Module): 25 | def __init__(self,in_channels,out_channels,stride,t): 26 | super().__init__() 27 | self.conv = conv(in_channels,in_channels*t,1) 28 | self.conv1 = conv(in_channels*t,in_channels*t,3,stride=stride,groups=in_channels*t) 29 | self.conv2 = conv(in_channels*t,out_channels,1) 30 | 31 | self.stride = stride 32 | self.in_channels = in_channels 33 | self.out_channels = out_channels 34 | 35 | def forward(self,x): 36 | x1 = self.conv(x) 37 | x1 = self.conv1(x1) 38 | x1 = self.conv2(x1) 39 | 40 | if self.stride == 1 and self.in_channels == self.out_channels: 41 | x1 += x 42 | 43 | return x1 44 | 45 | 46 | 47 | class MobileNetV2(torch.nn.Module): 48 | def __init__(self,in_channels,classes): 49 | super().__init__() 50 | 51 | self.fearures = torch.nn.Sequential( 52 | conv(in_channels,32,keral=3,stride=2), 53 | bottleneck(32,16,stride=1,t=1), 54 | 55 | bottleneck(16,24,stride=2,t=6), 56 | bottleneck(24,24,stride=1,t=6), 57 | 58 | bottleneck(24, 32,stride=2, t=6), 59 | bottleneck(32, 32,stride=1, t=6), 60 | bottleneck(32, 32,stride=1, t=6), 61 | 62 | bottleneck(32, 64,stride=2, t=6), 63 | bottleneck(64, 64,stride=1, t=6), 64 | bottleneck(64, 64,stride=1, t=6), 65 | bottleneck(64, 64,stride=1, t=6), 66 | 67 | bottleneck(64, 96,stride=1, t=6), 68 | bottleneck(96, 96,stride=1, t=6), 69 | bottleneck(96, 96,stride=1, t=6), 70 | 71 | bottleneck(96, 160,stride=2, t=6), 72 | bottleneck(160, 160,stride=1, t=6), 73 | bottleneck(160, 160,stride=1, t=6), 74 | 75 | bottleneck(160, 320,stride=1, t=6), 76 | conv(320,1280,1,stride=1), 77 | torch.nn.AdaptiveAvgPool1d(1) 78 | 79 | ) 80 | 81 | self.classifier = torch.nn.Sequential( 82 | conv(1280,out_channels=classes,keral=1), 83 | torch.nn.Flatten() 84 | 85 | ) 86 | 87 | def forward(self,x): 88 | x = self.fearures(x) 89 | x = self.classifier(x) 90 | return x 91 | 92 | 93 | 94 | if __name__ == "__main__": 95 | # model = conv(3,20,1) 96 | # model = bottleneck(32,32,1) 97 | model = MobileNetV2(32,5) 98 | input = torch.randn((1,32,224)) 99 | output = model(input) 100 | print(output.size()) 101 | -------------------------------------------------------------------------------- /MobileNetV3.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | class conv(torch.nn.Module): 5 | def __init__(self, in_channels, out_channels, keral,stride=1, groups=1,activation = None): 6 | super().__init__() 7 | 8 | padding = keral//2 9 | self.use_activation = activation 10 | self.conv = torch.nn.Conv1d(in_channels, out_channels, keral, stride,padding, groups=groups) 11 | self.bath = torch.nn.BatchNorm1d(out_channels) 12 | if self.use_activation == 'Relu': 13 | self.activation = torch.nn.ReLU6() 14 | elif self.use_activation == 'H_swish': 15 | self.activation = torch.nn.Hardswish() 16 | 17 | def forward(self,x): 18 | x = self.conv(x) 19 | if x.size()[-1] != 1: 20 | x = self.bath(x) 21 | if self.use_activation != None: 22 | x = self.activation(x) 23 | return x 24 | 25 | 26 | class bottleneck(torch.nn.Module): 27 | def __init__(self,in_channels,keral_size,expansion_size,out_channels,use_attenton = False,activation = 'Relu',stride=1): 28 | super().__init__() 29 | 30 | self.stride = stride 31 | self.in_channels = in_channels 32 | self.out_channels = out_channels 33 | self.use_attenton = use_attenton 34 | 35 | self.conv = conv(in_channels,expansion_size,1,activation=activation) 36 | self.conv1 = conv(expansion_size,expansion_size,keral_size,stride=stride,groups=expansion_size,activation=activation) 37 | 38 | if self.use_attenton: 39 | self.attenton = SE_block(expansion_size) 40 | 41 | self.conv2 = conv(expansion_size,out_channels,1,activation=activation) 42 | 43 | def forward(self,x): 44 | 45 | x1 = self.conv(x) 46 | x1 = self.conv1(x1) 47 | if self.use_attenton: 48 | x1 = self.attenton(x1) 49 | x1 = self.conv2(x1) 50 | 51 | if self.stride == 1 and self.in_channels == self.out_channels: 52 | x1 += x 53 | 54 | return x1 55 | 56 | class SE_block(torch.nn.Module): 57 | def __init__(self,in_channel,ratio=1): 58 | super(SE_block, self).__init__() 59 | self.avepool = torch.nn.AdaptiveAvgPool1d(1) 60 | self.linear1 = torch.nn.Linear(in_channel,in_channel//ratio) 61 | self.linear2 = torch.nn.Linear(in_channel//ratio,in_channel) 62 | self.Hardsigmoid = torch.nn.Hardsigmoid(inplace=True) 63 | self.Relu = torch.nn.ReLU(inplace=True) 64 | 65 | def forward(self,input): 66 | b,c,_ = input.shape 67 | x = self.avepool(input) 68 | x = x.view([b,c]) 69 | x = self.linear1(x) 70 | x = self.Relu(x) 71 | x = self.linear2(x) 72 | x = self.Hardsigmoid(x) 73 | x = x.view([b,c,1]) 74 | 75 | return input*x 76 | 77 | 78 | class MobileNetV3_small(torch.nn.Module): 79 | def __init__(self,in_channels,classes): 80 | super().__init__() 81 | self.fearures = torch.nn.Sequential( 82 | conv(in_channels,16,3,2,activation='H_swish'), 83 | bottleneck(16,3,16,16,True,'Relu',2), 84 | bottleneck(16,3,72,24,False,'Relu',2), 85 | bottleneck(24,3,88,24,False,'Relu',1), 86 | bottleneck(24,5,96,40,False,'H_swish',2), 87 | bottleneck(40,5,240,40,True,'H_swish',1), 88 | bottleneck(40,5,240,40,True,'H_swish',1), 89 | bottleneck(40,5,120,48,True,'H_swish',1), 90 | bottleneck(48,5,144,48,True,'H_swish',1), 91 | bottleneck(48,5,288,96,True,'H_swish',2), 92 | bottleneck(96,5,576,96,True,'H_swish',1), 93 | bottleneck(96,5,576,96,True,'H_swish',1), 94 | conv(96, 576, 1, 1, activation='H_swish'), 95 | torch.nn.AdaptiveAvgPool1d(1), 96 | ) 97 | self.classifier = torch.nn.Sequential( 98 | conv(576, 1024, 1, 1, activation='H_swish'), 99 | conv(1024, classes, 1, 1, activation='H_swish'), 100 | torch.nn.Flatten() 101 | 102 | ) 103 | def forward(self,x): 104 | x = self.fearures(x) 105 | x = self.classifier(x) 106 | 107 | return x 108 | 109 | class MobileNetV3_large(torch.nn.Module): 110 | def __init__(self,in_channels,classes): 111 | super().__init__() 112 | self.fearures = torch.nn.Sequential( 113 | conv(in_channels,16,3,2,activation='H_swish'), 114 | bottleneck(16,3,16,16,False,'Relu',1), 115 | bottleneck(16,3,64,24,False,'Relu',2), 116 | bottleneck(24,3,72,24,False,'Relu',1), 117 | bottleneck(24,5,72,40,True,'Relu',2), 118 | bottleneck(40,5,120,40,True,'Relu',1), 119 | bottleneck(40,5,120,40,True,'Relu',1), 120 | bottleneck(40,3,240,80,False,'H_swish',2), 121 | bottleneck(80,3,200,80,False,'H_swish',1), 122 | bottleneck(80,3,184,80,False,'H_swish',1), 123 | bottleneck(80,3,184,80,False,'H_swish',1), 124 | bottleneck(80,3,480,112,True,'H_swish',1), 125 | bottleneck(112,3,672,112,True,'H_swish',1), 126 | bottleneck(112,5,672,160,True,'H_swish',2), 127 | bottleneck(160,5,960,160,True,'H_swish',2), 128 | bottleneck(160,5,960,160,True,'H_swish',2), 129 | 130 | 131 | 132 | conv(160, 960, 1, 1, activation='H_swish'), 133 | torch.nn.AdaptiveAvgPool1d(1), 134 | ) 135 | self.classifier = torch.nn.Sequential( 136 | conv(960, 1280, 1, 1, activation='H_swish'), 137 | conv(1280, classes, 1, 1, activation='H_swish'), 138 | torch.nn.Flatten() 139 | 140 | ) 141 | def forward(self,x): 142 | x = self.fearures(x) 143 | x = self.classifier(x) 144 | 145 | return x 146 | 147 | 148 | 149 | if __name__ == "__main__": 150 | input = torch.randn((1,112,224)) 151 | # model = MobileV3_small(in_channels=112, classes=5) 152 | model = MobileNetV3_large(in_channels=112, classes=5) 153 | output = model(input) 154 | print(output.shape) 155 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # One-demotion-deeplearning-model 2 | 3 | To do a deep learning project on ecg. I use pytorch to reproduce the traditional CNN models include LeNet AlexNet ZFNet VGG GoogLeNet ResNet DenseNet MonileNetV1-3 ShuffuleNet EfficientV0 with one demotion and more. 4 | In order to understand models easily, I',m not copy the Official routines,but reprodece the models by myself.Therefore it’s easy to carry out secondary developmet on codes. 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ResNet50.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class Bottlrneck(torch.nn.Module): 4 | def __init__(self,In_channel,Med_channel,Out_channel,downsample=False): 5 | super(Bottlrneck, self).__init__() 6 | self.stride = 1 7 | if downsample == True: 8 | self.stride = 2 9 | 10 | self.layer = torch.nn.Sequential( 11 | torch.nn.Conv1d(In_channel, Med_channel, 1, self.stride), 12 | torch.nn.BatchNorm1d(Med_channel), 13 | torch.nn.ReLU(), 14 | torch.nn.Conv1d(Med_channel, Med_channel, 3, padding=1), 15 | torch.nn.BatchNorm1d(Med_channel), 16 | torch.nn.ReLU(), 17 | torch.nn.Conv1d(Med_channel, Out_channel, 1), 18 | torch.nn.BatchNorm1d(Out_channel), 19 | torch.nn.ReLU(), 20 | ) 21 | 22 | if In_channel != Out_channel: 23 | self.res_layer = torch.nn.Conv1d(In_channel, Out_channel,1,self.stride) 24 | else: 25 | self.res_layer = None 26 | 27 | def forward(self,x): 28 | if self.res_layer is not None: 29 | residual = self.res_layer(x) 30 | else: 31 | residual = x 32 | return self.layer(x)+residual 33 | 34 | 35 | class ResNet50(torch.nn.Module): 36 | def __init__(self,in_channels=2,classes=125): 37 | super(ResNet50, self).__init__() 38 | self.features = torch.nn.Sequential( 39 | torch.nn.Conv1d(in_channels,64,kernel_size=7,stride=2,padding=3), 40 | torch.nn.MaxPool1d(3,2,1), 41 | 42 | Bottlrneck(64,64,256,False), 43 | Bottlrneck(256,64,256,False), 44 | Bottlrneck(256,64,256,False), 45 | # 46 | Bottlrneck(256,128,512, True), 47 | Bottlrneck(512,128,512, False), 48 | Bottlrneck(512,128,512, False), 49 | Bottlrneck(512,128,512, False), 50 | # 51 | Bottlrneck(512,256,1024, True), 52 | Bottlrneck(1024,256,1024, False), 53 | Bottlrneck(1024,256,1024, False), 54 | Bottlrneck(1024,256,1024, False), 55 | Bottlrneck(1024,256,1024, False), 56 | Bottlrneck(1024,256,1024, False), 57 | # 58 | Bottlrneck(1024,512,2048, True), 59 | Bottlrneck(2048,512,2048, False), 60 | Bottlrneck(2048,512,2048, False), 61 | 62 | torch.nn.AdaptiveAvgPool1d(1) 63 | ) 64 | self.classifer = torch.nn.Sequential( 65 | torch.nn.Linear(2048,classes) 66 | ) 67 | 68 | def forward(self,x): 69 | x = self.features(x) 70 | x = x.view(-1,2048) 71 | x = self.classifer(x) 72 | return x 73 | 74 | if __name__ == '__main__': 75 | x = torch.randn(size=(1,1,224)) 76 | # x = torch.randn(size=(1,64,224)) 77 | # model = Bottlrneck(64,64,256,True) 78 | model = ResNet50(in_channels=1,classes=5) 79 | 80 | output = model(x) 81 | print(output.shape) 82 | 83 | 84 | -------------------------------------------------------------------------------- /SqueezeNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchsummary import summary 3 | class FireModule(torch.nn.Module): 4 | def __init__(self, in_channels, squeeze_channels, expand1x1_channels, expand1x3_channels): 5 | super(FireModule, self).__init__() 6 | self.squeeze = torch.nn.Conv1d(in_channels, squeeze_channels, kernel_size=1) 7 | self.relu = torch.nn.ReLU(inplace=True) 8 | self.expand1x1 = torch.nn.Conv1d(squeeze_channels, expand1x1_channels, kernel_size=1) 9 | self.expand1x3 = torch.nn.Conv1d(squeeze_channels, expand1x3_channels, kernel_size=3, padding=1) 10 | 11 | def forward(self, x): 12 | x = self.squeeze(x) 13 | x = self.relu(x) 14 | out1x1 = self.expand1x1(x) 15 | out1x3 = self.expand1x3(x) 16 | out = torch.cat([out1x1, out1x3], dim=1) 17 | return self.relu(out) 18 | 19 | 20 | class SqueezeNet(torch.nn.Module): 21 | def __init__(self,in_channels,classes): 22 | super(SqueezeNet, self).__init__() 23 | self.features = torch.nn.Sequential( 24 | # conv1 25 | torch.nn.Conv1d(in_channels, 96, kernel_size=7, stride=2), 26 | torch.nn.ReLU(inplace=True), 27 | # maxpool1 28 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 29 | # Fire2 30 | FireModule(96, 16, 64, 64), 31 | # Fire3 32 | FireModule(128, 16, 64, 64), 33 | # Fire4 34 | FireModule(128, 32, 128, 128), 35 | # maxpool4 36 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 37 | # Fire5 38 | FireModule(256, 32, 128, 128), 39 | # Fire6 40 | FireModule(256, 48, 192, 192), 41 | # Fire7 42 | FireModule(384, 48, 192, 192), 43 | # Fire8 44 | FireModule(384, 64, 256, 256), 45 | # maxpool8 46 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 47 | # Fire9 48 | FireModule(512, 64, 256, 256) 49 | ) 50 | self.classifier = torch.nn.Sequential( 51 | # conv10 52 | torch.nn.Conv1d(512, classes, kernel_size=1), 53 | torch.nn.ReLU(inplace=True), 54 | # avgpool10 55 | torch.nn.AdaptiveAvgPool1d((1)) 56 | ) 57 | 58 | def forward(self, x): 59 | x = self.features(x) 60 | x = self.classifier(x) 61 | x = torch.flatten(x, 1) 62 | return x 63 | 64 | 65 | 66 | 67 | if __name__ == "__main__": 68 | # 创建一个SqueezeNet实例 69 | model = SqueezeNet(in_channels=3,classes=10) 70 | # model = FireModule(96,16,64,64) 71 | # 打印模型结构 72 | input = torch.randn(1,3,224) 73 | output = model(input) 74 | print(output.shape) 75 | -------------------------------------------------------------------------------- /VGG19.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class VGG19(torch.nn.Module): 4 | def __init__(self,in_channels=1,classes=5): 5 | super(VGG19, self).__init__() 6 | self.feature = torch.nn.Sequential( 7 | 8 | torch.nn.Conv1d(in_channels, 64, kernel_size=3, padding=1), 9 | torch.nn.BatchNorm1d(64), 10 | torch.nn.ReLU(), 11 | torch.nn.Conv1d(64, 64, kernel_size=3, padding=1), 12 | torch.nn.BatchNorm1d(64), 13 | torch.nn.ReLU(), 14 | torch.nn.MaxPool1d(2), 15 | 16 | torch.nn.Conv1d(64, 128, kernel_size=3, padding=1), 17 | torch.nn.BatchNorm1d(128), 18 | torch.nn.ReLU(), 19 | torch.nn.Conv1d(128, 128, kernel_size=3, padding=1), 20 | torch.nn.BatchNorm1d(128), 21 | torch.nn.ReLU(), 22 | torch.nn.MaxPool1d(2), 23 | 24 | torch.nn.Conv1d(128, 256, kernel_size=3, padding=1), 25 | torch.nn.BatchNorm1d(256), 26 | torch.nn.ReLU(), 27 | torch.nn.Conv1d(256, 256, kernel_size=3, padding=1), 28 | torch.nn.BatchNorm1d(256), 29 | torch.nn.ReLU(), 30 | torch.nn.Conv1d(256, 256, kernel_size=3, padding=1), 31 | torch.nn.BatchNorm1d(256), 32 | torch.nn.ReLU(), 33 | torch.nn.Conv1d(256, 256, kernel_size=3, padding=1), 34 | torch.nn.BatchNorm1d(256), 35 | torch.nn.ReLU(), 36 | torch.nn.MaxPool1d(2), 37 | 38 | torch.nn.Conv1d(256, 512, kernel_size=3, padding=1), 39 | torch.nn.BatchNorm1d(512), 40 | torch.nn.ReLU(), 41 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 42 | torch.nn.BatchNorm1d(512), 43 | torch.nn.ReLU(), 44 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 45 | torch.nn.BatchNorm1d(512), 46 | torch.nn.ReLU(), 47 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 48 | torch.nn.BatchNorm1d(512), 49 | torch.nn.ReLU(), 50 | torch.nn.MaxPool1d(2), 51 | 52 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 53 | torch.nn.BatchNorm1d(512), 54 | torch.nn.ReLU(), 55 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 56 | torch.nn.BatchNorm1d(512), 57 | torch.nn.ReLU(), 58 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 59 | torch.nn.BatchNorm1d(512), 60 | torch.nn.ReLU(), 61 | torch.nn.Conv1d(512, 512, kernel_size=3, padding=1), 62 | torch.nn.BatchNorm1d(512), 63 | torch.nn.ReLU(), 64 | torch.nn.MaxPool1d(2), 65 | 66 | torch.nn.AdaptiveAvgPool1d(7) 67 | ) 68 | self.classifer = torch.nn.Sequential( 69 | torch.nn.Linear(3584,1024), 70 | torch.nn.ReLU(), 71 | torch.nn.Dropout(0.5), 72 | torch.nn.Linear(1024,1024), 73 | torch.nn.ReLU(), 74 | torch.nn.Dropout(0.5), 75 | torch.nn.Linear(1024, 512), 76 | torch.nn.ReLU(), 77 | torch.nn.Linear(512, classes), 78 | ) 79 | 80 | def forward(self, x): 81 | x = self.feature(x) 82 | x = x.view(-1, 3584) 83 | x = self.classifer(x) 84 | return x 85 | 86 | 87 | if __name__ == '__main__': 88 | model = VGG19(in_channels=1,classes=5) 89 | input = torch.randn(size=(1,1,224)) 90 | output = model(input) 91 | print(output.shape) 92 | 93 | -------------------------------------------------------------------------------- /Xception.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | class SeparableConv1d(torch.nn.Module): 5 | def __init__(self, in_channels, out_channels, kernel_size, stride, padding): 6 | super(SeparableConv1d, self).__init__() 7 | 8 | # 深度卷积 9 | self.depthwise = torch.nn.Conv1d(in_channels, in_channels, kernel_size, stride, padding, groups=in_channels) 10 | # 逐点卷积 11 | self.pointwise = torch.nn.Conv1d(in_channels, out_channels, kernel_size=1) 12 | 13 | def forward(self, x): 14 | x = self.depthwise(x) 15 | x = self.pointwise(x) 16 | return x 17 | 18 | 19 | class Entry(torch.nn.Module): 20 | def __init__(self,in_channels): 21 | super().__init__() 22 | self.beforeresidual = torch.nn.Sequential( 23 | torch.nn.Conv1d(in_channels,32,3,2,1), 24 | torch.nn.ReLU(), 25 | torch.nn.Conv1d(32, 64, 3, 2, 1), 26 | torch.nn.ReLU() 27 | ) 28 | 29 | self.residual_branch1 = torch.nn.Conv1d(64, 128, 1, 2) 30 | self.residual_model1 = torch.nn.Sequential( 31 | SeparableConv1d(64,128,3,1,1), 32 | torch.nn.ReLU(), 33 | SeparableConv1d(128, 128, 3, 1, 1), 34 | torch.nn.MaxPool1d(3,2,1) 35 | ) 36 | 37 | self.residual_branch2 = torch.nn.Conv1d(256, 256, 1, 2) 38 | self.residual_model2 = torch.nn.Sequential( 39 | torch.nn.ReLU(), 40 | SeparableConv1d(256,256,3,1,1), 41 | torch.nn.ReLU(), 42 | SeparableConv1d(256, 256, 3, 1, 1), 43 | torch.nn.MaxPool1d(3,2,1) 44 | ) 45 | 46 | self.residual_branch3 = torch.nn.Conv1d(512, 728, 1, 2) 47 | self.residual_model3 = torch.nn.Sequential( 48 | torch.nn.ReLU(), 49 | SeparableConv1d(512,728,3,1,1), 50 | torch.nn.ReLU(), 51 | SeparableConv1d(728, 728, 3, 1, 1), 52 | torch.nn.MaxPool1d(3,2,1) 53 | ) 54 | 55 | 56 | def forward(self,x): 57 | x = self.beforeresidual(x) 58 | 59 | x1 = self.residual_branch1(x) 60 | x = self.residual_model1(x) 61 | x = torch.cat([x,x1],dim=1) 62 | 63 | x1 = self.residual_branch2(x) 64 | x = self.residual_model2(x) 65 | x = torch.cat([x,x1],dim=1) 66 | 67 | x1 = self.residual_branch3(x) 68 | x = self.residual_model3(x) 69 | # x = torch.cat([x,x1],dim=1) 70 | x = x+x1 71 | 72 | return x 73 | 74 | 75 | class Middleflow(torch.nn.Module): 76 | def __init__(self): 77 | super().__init__() 78 | self.layers = torch.nn.Sequential( 79 | torch.nn.ReLU(), 80 | SeparableConv1d(728,728,3,1,1), 81 | torch.nn.ReLU(), 82 | SeparableConv1d(728, 728, 3, 1, 1), 83 | torch.nn.ReLU(), 84 | SeparableConv1d(728, 728, 3, 1, 1), 85 | ) 86 | 87 | def forward(self,x): 88 | return x + self.layers(x) 89 | 90 | 91 | class Exitflow(torch.nn.Module): 92 | def __init__(self,classes): 93 | super().__init__() 94 | 95 | self.residual = torch.nn.Conv1d(728,1024,1,2) 96 | self.residual_model = torch.nn.Sequential( 97 | torch.nn.ReLU(), 98 | SeparableConv1d(728,728,3,1,1), 99 | torch.nn.ReLU(), 100 | SeparableConv1d(728, 1024, 3, 1, 1), 101 | torch.nn.MaxPool1d(3,2,1) 102 | ) 103 | self.last_layer = torch.nn.Sequential( 104 | SeparableConv1d(1024,1536,3,1,1), 105 | torch.nn.ReLU(), 106 | SeparableConv1d(1536, 2048, 3, 1, 1), 107 | torch.nn.ReLU(), 108 | torch.nn.AdaptiveAvgPool1d(1), 109 | torch.nn.Flatten(), 110 | torch.nn.Linear(2048,classes) 111 | ) 112 | 113 | 114 | def forward(self,x): 115 | x = self.residual_model(x) + self.residual(x) 116 | x = self.last_layer(x) 117 | 118 | return x 119 | 120 | 121 | 122 | class Xception(torch.nn.Module): 123 | def __init__(self,in_channels,classes): 124 | super().__init__() 125 | self.layers = torch.nn.Sequential( 126 | Entry(in_channels), 127 | Middleflow(), 128 | Middleflow(), 129 | Middleflow(), 130 | Middleflow(), 131 | Middleflow(), 132 | Middleflow(), 133 | Middleflow(), 134 | Middleflow(), 135 | Exitflow(classes) 136 | ) 137 | def forward(self,x): 138 | return self.layers(x) 139 | 140 | 141 | 142 | if __name__ == '__main__': 143 | input = torch.randn((1,3,224)) 144 | # model = SeparableConv1d(12,12,3,1,1) 145 | # model = Entry(12) 146 | # model = Middleflow() 147 | model = Xception(3,5) 148 | output = model(input) 149 | print(output.size()) 150 | 151 | -------------------------------------------------------------------------------- /ZFNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class ZFNet(torch.nn.Module): 4 | def __init__(self,input_channels,input_sample_points,classes): 5 | super(ZFNet, self).__init__() 6 | 7 | self.input_channels = input_channels 8 | self.input_sample_points = input_sample_points 9 | 10 | self.features = torch.nn.Sequential( 11 | torch.nn.Conv1d(input_channels,96,kernel_size=7,stride=2), 12 | torch.nn.BatchNorm1d(96), 13 | torch.nn.MaxPool1d(kernel_size=3,stride=2), 14 | torch.nn.Conv1d(96, 256, kernel_size=5, stride=2), 15 | torch.nn.BatchNorm1d(256), 16 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 17 | 18 | torch.nn.Conv1d(256, 384, kernel_size=3, padding=1), 19 | torch.nn.BatchNorm1d(384), 20 | torch.nn.Conv1d(384, 384, kernel_size=3, padding=1), 21 | torch.nn.BatchNorm1d(384), 22 | torch.nn.Conv1d(384, 256, kernel_size=3, padding=1), 23 | torch.nn.BatchNorm1d(256), 24 | torch.nn.MaxPool1d(kernel_size=3, stride=2), 25 | ) 26 | 27 | self.After_features_channels = 256 28 | self.After_features_sample_points = (((((((((input_sample_points-7)//2 + 1)-3)//2+1)-5)//2+1)-3)//2+1)-3)//2+1 29 | self.classifier = torch.nn.Sequential( 30 | 31 | torch.nn.Linear(self.After_features_channels*self.After_features_sample_points,1024), 32 | torch.nn.ReLU(inplace=True), 33 | torch.nn.Dropout(0.5), 34 | 35 | torch.nn.Linear(1024, 1024), 36 | torch.nn.ReLU(inplace=True), 37 | torch.nn.Dropout(0.5), 38 | 39 | torch.nn.Linear(1024,classes), 40 | ) 41 | 42 | def forward(self,x): 43 | if x.size(1)!=self.input_channels or x.size(2)!=self.input_sample_points: 44 | raise Exception('输入数据维度错误,输入维度应为[Batch_size,{},{}],实际输入维度为{}'.format(self.input_channels,self.input_sample_points,x.size())) 45 | 46 | x = self.features(x) 47 | x = x.view(-1,self.After_features_channels*self.After_features_sample_points) 48 | x = self.classifier(x) 49 | return x 50 | 51 | 52 | if __name__ == '__main__': 53 | model = ZFNet(input_channels=1, input_sample_points=224, classes=5) 54 | input = torch.randn(size=(1, 1, 224)) 55 | output = model(input) 56 | print(output.shape) 57 | -------------------------------------------------------------------------------- /shuffuleNetV1.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class channel_shuffle(torch.nn.Module): 4 | def __init__(self, groups): 5 | super().__init__() 6 | self.groups = groups 7 | 8 | def forward(self, x): 9 | b, c, l = x.size() 10 | group_channel = c // self.groups 11 | x = x.reshape(b, self.groups, group_channel, l) 12 | x = x.permute(0, 2, 1, 3).contiguous() 13 | x = x.reshape(b, c, l) 14 | return x 15 | 16 | class shuffuleBlock(torch.nn.Module): 17 | def __init__(self, In_channel, Med_channel, Out_channel, stride=2, group=3): 18 | super(shuffuleBlock, self).__init__() 19 | self.stride = stride # Added to store the stride value 20 | 21 | if stride == 2: 22 | self.res_layer = torch.nn.AvgPool1d(3, stride, 1) 23 | 24 | self.layer = torch.nn.Sequential( 25 | torch.nn.Conv1d(In_channel, Med_channel, 1), 26 | torch.nn.BatchNorm1d(Med_channel), 27 | torch.nn.ReLU(), 28 | channel_shuffle(groups=group), 29 | torch.nn.Conv1d(Med_channel, Med_channel, 3, stride, padding=1, groups=group), 30 | torch.nn.BatchNorm1d(Med_channel), 31 | torch.nn.ReLU(), 32 | torch.nn.Conv1d(Med_channel, Out_channel, 1), 33 | torch.nn.BatchNorm1d(Out_channel), 34 | torch.nn.ReLU(), 35 | ) 36 | 37 | def forward(self, x): 38 | if self.stride == 2: 39 | return torch.cat((self.res_layer(x), self.layer(x)), 1) 40 | else: 41 | return self.layer(x) 42 | 43 | 44 | 45 | class shufuleNetV1_G3(torch.nn.Module): 46 | def __init__(self,in_channels,classes): 47 | super().__init__() 48 | 49 | self.features = torch.nn.Sequential( 50 | torch.nn.Conv1d(in_channels,24,3,2,1), 51 | torch.nn.Conv1d(24,120,3,2,1), 52 | torch.nn.MaxPool1d(3,2,1), 53 | # 54 | shuffuleBlock(120,120//4,120,2,3), 55 | # 56 | shuffuleBlock(240,240//4,240,1,3), 57 | shuffuleBlock(240,240//4,240,1,3), 58 | shuffuleBlock(240,240//4,240,1,3), 59 | # 60 | shuffuleBlock(240, 240 // 4, 240, 2,3), 61 | # 62 | shuffuleBlock(480, 480 // 4, 480, 1,3), 63 | shuffuleBlock(480, 480 // 4, 480, 1,3), 64 | shuffuleBlock(480, 480 // 4, 480, 1,3), 65 | shuffuleBlock(480, 480 // 4, 480, 1,3), 66 | shuffuleBlock(480, 480 // 4, 480, 1,3), 67 | shuffuleBlock(480, 480 // 4, 480, 1,3), 68 | shuffuleBlock(480, 480 // 4, 480, 1,3), 69 | 70 | shuffuleBlock(480, 480 // 4, 480, 2,3), 71 | 72 | shuffuleBlock(960, 960 // 4, 960, 1,3), 73 | shuffuleBlock(960, 960 // 4, 960, 1,3), 74 | shuffuleBlock(960, 960 // 4, 960, 1,3), 75 | 76 | torch.nn.AdaptiveAvgPool1d(1) 77 | 78 | ) 79 | 80 | self.classifier = torch.nn.Sequential( 81 | torch.nn.Flatten(), 82 | torch.nn.Linear(960,classes) 83 | ) 84 | 85 | 86 | def forward(self,x): 87 | x = self.features(x) 88 | x = self.classifier(x) 89 | return x 90 | if __name__ == "__main__": 91 | x = torch.randn(1, 2, 200) 92 | # model = shuffuleBlock(300, 300 // 4, 300, 2, 3) 93 | model = shufuleNetV1_G3(2, 125) 94 | output = model(x) 95 | print(output.size()) 96 | -------------------------------------------------------------------------------- /shuffuleNetV2.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class channel_shuffle(torch.nn.Module): 4 | def __init__(self, groups): 5 | super().__init__() 6 | self.groups = groups 7 | 8 | def forward(self, x): 9 | b, c, l = x.size() 10 | group_channel = c // self.groups 11 | x = x.reshape(b, self.groups, group_channel, l) 12 | x = x.permute(0, 2, 1, 3).contiguous() 13 | x = x.reshape(b, c, l) 14 | return x 15 | 16 | class shuffuleV2Block(torch.nn.Module): 17 | def __init__(self, In_channel, Med_channel, Out_channel, stride=2): 18 | super(shuffuleV2Block, self).__init__() 19 | self.stride = stride # Added to store the stride value 20 | self.In_channel = In_channel 21 | self.Out_channel = Out_channel 22 | 23 | if self.stride == 2: 24 | self.left = torch.nn.Sequential( 25 | torch.nn.Conv1d(self.In_channel, self.In_channel, 3, self.stride, padding=1, groups=self.In_channel), 26 | torch.nn.BatchNorm1d(self.In_channel), 27 | torch.nn.ReLU(), 28 | torch.nn.Conv1d(self.In_channel, Out_channel, 1), 29 | torch.nn.BatchNorm1d(Out_channel), 30 | torch.nn.ReLU(), 31 | ) 32 | else: 33 | self.In_channel = self.In_channel//2 34 | self.Out_channel = self.Out_channel//2 35 | 36 | self.right = torch.nn.Sequential( 37 | torch.nn.Conv1d(self.In_channel, Med_channel, 1), 38 | torch.nn.BatchNorm1d(Med_channel), 39 | torch.nn.ReLU(), 40 | torch.nn.Conv1d(Med_channel, Med_channel, 3, self.stride, padding=1, groups=Med_channel), 41 | torch.nn.BatchNorm1d(Med_channel), 42 | torch.nn.ReLU(), 43 | torch.nn.Conv1d(Med_channel, self.Out_channel, 1), 44 | torch.nn.BatchNorm1d(self.Out_channel), 45 | torch.nn.ReLU(), 46 | ) 47 | self.shuffule = channel_shuffle(2) 48 | 49 | def forward(self, x): 50 | if self.stride == 2: 51 | xl = self.left(x) 52 | xr = self.right(x) 53 | x_out = torch.cat((xl, xr), 1) 54 | 55 | else: 56 | xl,xr = x.chunk(2,dim=1) 57 | xr = self.right(xr) 58 | x_out = torch.cat((xl, xr), 1) 59 | 60 | return self.shuffule(x_out) 61 | 62 | 63 | 64 | 65 | class shufuleNetV2(torch.nn.Module): 66 | def __init__(self,in_channels = 2, classes = 125): 67 | super().__init__() 68 | self.feature = torch.nn.Sequential( 69 | torch.nn.Conv1d(in_channels,24,3,2,1), 70 | torch.nn.MaxPool1d(3,2,1), 71 | shuffuleV2Block(24,24,58,2), 72 | 73 | shuffuleV2Block(116,116//4,116,1), 74 | shuffuleV2Block(116,116//4,116,1), 75 | shuffuleV2Block(116,116//4,116,1), 76 | 77 | shuffuleV2Block(116, 116, 116, 2), 78 | 79 | shuffuleV2Block(232, 232//4, 232, 1), 80 | shuffuleV2Block(232, 232//4, 232, 1), 81 | shuffuleV2Block(232, 232//4, 232, 1), 82 | shuffuleV2Block(232, 232//4, 232, 1), 83 | shuffuleV2Block(232, 232//4, 232, 1), 84 | shuffuleV2Block(232, 232//4, 232, 1), 85 | shuffuleV2Block(232, 232//4, 232, 1), 86 | 87 | shuffuleV2Block(232, 232, 232, 2), 88 | shuffuleV2Block(464, 464//4, 464, 1), 89 | shuffuleV2Block(464, 464//4, 464, 1), 90 | shuffuleV2Block(464, 464//4, 464, 1), 91 | 92 | torch.nn.Conv1d(464,1024,1), 93 | torch.nn.AdaptiveAvgPool1d(1) 94 | ) 95 | 96 | self.classifier = torch.nn.Sequential( 97 | torch.nn.Flatten(), 98 | torch.nn.Linear(1024,classes) 99 | 100 | ) 101 | 102 | def forward(self,x): 103 | x = self.feature(x) 104 | x = self.classifier(x) 105 | return x 106 | if __name__ == "__main__": 107 | x = torch.randn(1, 2, 200) 108 | # model = shuffuleV2Block(300, 300 // 4, 300, 1) 109 | model = shufuleNetV2(2, 125) 110 | output = model(x) 111 | print(output.size()) 112 | --------------------------------------------------------------------------------