sleep_apnea_hybrid/exam/037/model/Hybrid_Net017.py
2022-10-17 21:15:03 +08:00

145 lines
4.6 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=3, stride=stride, padding=1, bias=False),
nn.BatchNorm1d(output_channel),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels=output_channel, out_channels=output_channel,
kernel_size=3, stride=1, padding=1, 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.ReLU(inplace=True)
def forward(self, x):
out = self.left(x)
residual = self.right(x)
out += residual
out = self.relu(out)
return out
class HYBRIDNET017(nn.Module):
def __init__(self, num_classes=2, init_weights=True):
super(HYBRIDNET017, self).__init__()
self.lstm = nn.LSTM(input_size=1,
hidden_size=8,
num_layers=3,
bidirectional=True,
batch_first=True)
self.classifier = nn.Sequential(
# nn.Dropout(p=0.5),
nn.Linear(4800, 128),
nn.GELU(),
nn.Linear(128, num_classes),
)
if init_weights:
self.initialize_weights()
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)
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.ReLU(inplace=True)
self.pool1 = nn.MaxPool1d(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.AvgPool1d(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 * 23, out_features=512),
nn.Linear(in_features=512, out_features=num_classes)
# nn.Softmax()
# nn.Sigmoid()
)
# self.linear = nn.Linear(512 * block.expansion, num_classes)
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 forward(self, x):
x, (_, _) = self.lstm(x)
# print(x.shape)
# x = x[:, -1]
x = torch.flatten(x, start_dim=1)
# print(x.shape)
x = self.classifier(x)
return x
if __name__ == '__main__':
model = HYBRIDNET017().cuda()
summary(model, [(32, 300, 1)])