一维振动信号输入,将一维振动信号变为时频图,利用一维resnet和二维resnet18提取特征,并进行特征融合的pytorch代码
时间: 2024-05-03 22:17:07 浏览: 146
利用pytorch实现图像分类的一个完整的代码,训练,预测,TTA,模型融合,模型部署,cnn提取特征,svm或者随机森林等进行
下面是将一维振动信号变为时频图,利用一维resnet和二维resnet18提取特征,并进行特征融合的PyTorch代码:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import librosa
import matplotlib.pyplot as plt
# 一维ResNet
class ResNet1D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super(ResNet1D, self).__init__()
self.conv1 = nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, bias=False)
self.bn1 = nn.BatchNorm1d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv1d(out_channels, out_channels, kernel_size, stride, padding, bias=False)
self.bn2 = nn.BatchNorm1d(out_channels)
self.downsample = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(out_channels)
)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# 二维ResNet18
class ResNet18(nn.Module):
def __init__(self, num_classes=10):
super(ResNet18, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(64, 2)
self.layer2 = self._make_layer(128, 2, stride=2)
self.layer3 = self._make_layer(256, 2, stride=2)
self.layer4 = self._make_layer(512, 2, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def _make_layer(self, planes, blocks, stride=1):
downsample = None
if stride != 1 or planes != 64:
downsample = nn.Sequential(
nn.Conv2d(64, planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes)
)
layers = []
layers.append(BasicBlock(64, planes, stride, downsample))
for i in range(1, blocks):
layers.append(BasicBlock(planes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# 二维ResNet基本块
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# 一维ResNet模型
class ResNet1DModel(nn.Module):
def __init__(self):
super(ResNet1DModel, self).__init__()
self.layer1 = ResNet1D(1, 64)
self.layer2 = ResNet1D(64, 128)
self.layer3 = ResNet1D(128, 256)
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.fc = nn.Linear(256, 10)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# 时频图模型
class SpectrogramModel(nn.Module):
def __init__(self):
super(SpectrogramModel, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(128)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv4 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
self.bn4 = nn.BatchNorm2d(512)
self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = F.relu(x)
x = self.pool3(x)
x = self.conv4(x)
x = self.bn4(x)
x = F.relu(x)
x = self.pool4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# 特征融合模型
class FusionModel(nn.Module):
def __init__(self):
super(FusionModel, self).__init__()
self.resnet1d = ResNet1DModel()
self.resnet18 = ResNet18()
self.fc = nn.Linear(20, 10)
def forward(self, x):
x1 = self.resnet1d(x)
x2 = x.squeeze(1)
x2 = x2.unsqueeze(1)
x2 = librosa.feature.melspectrogram(x2.numpy()[0], sr=16000, n_mels=128, hop_length=160, n_fft=480, fmax=8000)
x2 = librosa.power_to_db(x2, ref=np.max)
x2 = x2[np.newaxis, np.newaxis, :]
x2 = torch.from_numpy(x2).float()
x2 = self.resnet18(x2)
x = torch.cat((x1, x2), dim=1)
x = self.fc(x)
return x
# 示例代码
if __name__ == '__main__':
# 生成示例数据
x = np.sin(2*np.pi*500*np.linspace(0, 1, 16000))
x = x[np.newaxis, np.newaxis, :]
x = torch.from_numpy(x).float()
# 时频图
x_spectrogram = x.squeeze(1)
x_spectrogram = x_spectrogram.unsqueeze(1)
x_spectrogram = librosa.feature.melspectrogram(x_spectrogram.numpy()[0], sr=16000, n_mels=128, hop_length=160, n_fft=480, fmax=8000)
x_spectrogram = librosa.power_to_db(x_spectrogram, ref=np.max)
x_spectrogram = x_spectrogram[np.newaxis, np.newaxis, :]
x_spectrogram = torch.from_numpy(x_spectrogram).float()
# 模型
model = FusionModel()
y = model(x)
print(y)
# 绘制时频图
plt.figure(figsize=(10, 4))
librosa.display.specshow(x_spectrogram.numpy()[0, 0, :, :], sr=16000, hop_length=160, y_axis='mel', fmax=8000, x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Mel spectrogram')
plt.tight_layout()
plt.show()
```
上述代码实现了一维振动信号输入,将一维振动信号变为时频图,利用一维ResNet和二维ResNet18提取特征,并进行特征融合的过程。其中,`ResNet1D`和`ResNet18`分别为一维ResNet和二维ResNet18的实现,`ResNet1DModel`为一维ResNet模型,`SpectrogramModel`为时频图模型,`FusionModel`为特征融合模型。在示例代码中,我们生成了一个示例数据,使用模型进行推理,并绘制了时频图。
阅读全文