98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: UTF-8 -*-
|
|
"""
|
|
@author:andrew
|
|
@file:Hybrid_Net001.py
|
|
@email:admin@marques22.com
|
|
@email:2021022362@m.scnu.edu.cn
|
|
@time:2022/09/30
|
|
"""
|
|
|
|
import os
|
|
|
|
import torch
|
|
from torch import nn
|
|
from torchinfo import summary
|
|
from torch import cat
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
|
|
|
|
# 输入时长
|
|
WHOLE_SEGMENT_SECOND = 30
|
|
|
|
# 呼吸采样率
|
|
RESPIRATORY_FRE = 4
|
|
|
|
# BCG 时频图大小
|
|
BCG_GRAPH_SIZE = (26, 121)
|
|
|
|
|
|
class HYBRIDNET001(nn.Module):
|
|
def __init__(self, num_classes=2, init_weights=True):
|
|
super(HYBRIDNET001, self).__init__()
|
|
|
|
self.lstm = nn.LSTM(input_size=1,
|
|
hidden_size=16,
|
|
num_layers=1,
|
|
bidirectional=True,
|
|
batch_first=True)
|
|
|
|
self.right = nn.Sequential(
|
|
nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(3, 3),
|
|
stride=(1, 1), padding=(1, 1)),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=3, stride=(2, 2), padding=1),
|
|
nn.BatchNorm2d(16),
|
|
|
|
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(3, 3),
|
|
stride=(1, 1), padding=(1, 1)),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=3, stride=(2, 2), padding=1),
|
|
nn.BatchNorm2d(32),
|
|
|
|
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=(3, 3),
|
|
stride=(1, 1), padding=(1, 1)),
|
|
nn.ReLU(inplace=True),
|
|
nn.MaxPool2d(kernel_size=3, stride=(2, 2), padding=1),
|
|
nn.BatchNorm2d(32)
|
|
|
|
|
|
|
|
)
|
|
|
|
self.classifier = nn.Sequential(
|
|
# nn.Dropout(p=0.5),
|
|
nn.Linear((120 * 32 + 32 * 16 * 4), 512),
|
|
nn.ReLU(inplace=True),
|
|
nn.Linear(512, 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)
|
|
|
|
def forward(self, x1, x2):
|
|
x1, (_, _) = self.lstm(x1)
|
|
x2 = self.right(x2)
|
|
# print(x1.shape)
|
|
# print(x2.shape)
|
|
x1 = torch.flatten(x1, start_dim=1)
|
|
x2 = torch.flatten(x2, start_dim=1)
|
|
x = torch.cat((x1, x2), dim=1)
|
|
x = self.classifier(x)
|
|
return x
|
|
|
|
|
|
if __name__ == '__main__':
|
|
model = HYBRIDNET001(2).cuda()
|
|
summary(model, [(32, 120, 1), (32, 1, 121, 26)])
|