140 lines
4.4 KiB
Python
140 lines
4.4 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: UTF-8 -*-
|
|
"""
|
|
@author:andrew
|
|
@file:Hybrid_Net014.py
|
|
@email:admin@marques22.com
|
|
@email:2021022362@m.scnu.edu.cn
|
|
@time:2022/10/14
|
|
"""
|
|
|
|
import os
|
|
|
|
import torch
|
|
from torch import nn
|
|
from torchinfo import summary
|
|
from torch import cat
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
|
|
# 修改激活函数
|
|
# 提高呼吸采样率
|
|
|
|
# 输入时长
|
|
WHOLE_SEGMENT_SECOND = 30
|
|
|
|
# 呼吸采样率
|
|
RESPIRATORY_FRE = 10
|
|
|
|
# BCG 时频图大小
|
|
BCG_GRAPH_SIZE = (26, 121)
|
|
|
|
|
|
class BasicBlock_1d(nn.Module):
|
|
expansion = 1
|
|
|
|
def __init__(self, input_channel, output_channel, stride=1):
|
|
super(BasicBlock_1d, self).__init__()
|
|
self.left = nn.Sequential(
|
|
nn.Conv1d(in_channels=input_channel, out_channels=output_channel,
|
|
kernel_size=9, stride=stride, padding=4, bias=False),
|
|
nn.BatchNorm1d(output_channel),
|
|
nn.GELU(),
|
|
nn.Conv1d(in_channels=output_channel, out_channels=output_channel,
|
|
kernel_size=9, stride=1, padding=4, bias=False),
|
|
nn.BatchNorm1d(output_channel)
|
|
)
|
|
self.right = nn.Sequential()
|
|
|
|
if stride != 1 or input_channel != self.expansion * output_channel:
|
|
self.right = nn.Sequential(
|
|
nn.Conv1d(in_channels=input_channel, out_channels=output_channel * self.expansion,
|
|
kernel_size=1, stride=stride, bias=False),
|
|
nn.BatchNorm1d(self.expansion * output_channel)
|
|
)
|
|
|
|
self.relu = nn.GELU()
|
|
|
|
def forward(self, x):
|
|
out = self.left(x)
|
|
residual = self.right(x)
|
|
out += residual
|
|
out = self.relu(out)
|
|
return out
|
|
|
|
|
|
class ResNet_1d(nn.Module):
|
|
def __init__(self, block, number_block, num_classes=2, init_weights=True):
|
|
super(ResNet_1d, self).__init__()
|
|
|
|
self.in_channel = 64
|
|
|
|
self.conv1 = nn.Conv1d(in_channels=1, out_channels=self.in_channel,
|
|
kernel_size=7, stride=2, padding=3, bias=False)
|
|
self.bn1 = nn.BatchNorm1d(64)
|
|
self.relu = nn.GELU()
|
|
self.pool1 = nn.AvgPool1d(kernel_size=3, stride=2, padding=1)
|
|
|
|
self.layer1 = self._make_layer(block=block, out_channel=64, num_block=number_block[0], stride=1)
|
|
self.layer2 = self._make_layer(block=block, out_channel=128, num_block=number_block[1], stride=2)
|
|
self.layer3 = self._make_layer(block=block, out_channel=256, num_block=number_block[2], stride=2)
|
|
self.layer4 = self._make_layer(block=block, out_channel=512, num_block=number_block[3], stride=2)
|
|
|
|
self.pool2 = nn.MaxPool1d(4)
|
|
|
|
self.linear = nn.Linear(512, num_classes)
|
|
self.features = nn.Sequential(
|
|
# nn.Linear(in_features=1024, out_features=nc),
|
|
nn.Flatten(),
|
|
# nn.Linear(in_features=512 * 2, out_features=512),
|
|
nn.Linear(in_features=512 * 2, out_features=num_classes)
|
|
|
|
# nn.Softmax()
|
|
# nn.Sigmoid()
|
|
)
|
|
# self.linear = nn.Linear(512 * block.expansion, num_classes)
|
|
if init_weights:
|
|
self.initialize_weights()
|
|
|
|
def _make_layer(self, block, out_channel, num_block, stride):
|
|
strides = [stride] + [1] * (num_block - 1)
|
|
layers = []
|
|
for stride in strides:
|
|
layers.append(block(self.in_channel, out_channel, stride))
|
|
self.in_channel = out_channel * block.expansion
|
|
return nn.Sequential(*layers)
|
|
|
|
def initialize_weights(self):
|
|
for m in self.modules():
|
|
if isinstance(m, (nn.Conv2d, nn.Conv1d)):
|
|
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') # 何教授方法
|
|
if m.bias is not None:
|
|
nn.init.constant_(m.bias, 0)
|
|
elif isinstance(m, nn.Linear):
|
|
nn.init.normal_(m.weight, 0, 0.01) # 正态分布赋值
|
|
nn.init.constant_(m.bias, 0)
|
|
|
|
def forward(self, x):
|
|
x = self.conv1(x)
|
|
x = self.bn1(x)
|
|
x = self.relu(x)
|
|
x = self.pool1(x)
|
|
x = self.layer1(x)
|
|
x = self.layer2(x)
|
|
x = self.layer3(x)
|
|
x = self.layer4(x)
|
|
x = self.pool2(x)
|
|
x = x.view(x.size(0), -1)
|
|
|
|
x = self.features(x)
|
|
return x
|
|
|
|
|
|
def HYBRIDNET018():
|
|
return ResNet_1d(BasicBlock_1d, [2, 2, 2, 2])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
model = HYBRIDNET018().cuda()
|
|
summary(model, [(32, 1, 300)])
|