forked from Kahsolt/bearing-fault-diagnosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
116 lines (91 loc) · 4.33 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
# Author: Armit
# Create Time: 2024/02/01
from utils import *
# ref: https://github.com/xiaosongshine/bearing_detection_by_conv1d
class NaiveConv1d(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
# x1024 downsample: [1, 6000] => [512, 94]
self.features = nn.Sequential(
nn.Conv1d( 1, 16, kernel_size=8, stride=2), nn.ReLU(inplace=True),
nn.Conv1d( 16, 16, kernel_size=8, stride=2, padding=3), nn.ReLU(inplace=True), nn.MaxPool1d(kernel_size=2),
nn.Conv1d( 16, 64, kernel_size=4, stride=2, padding=2), nn.ReLU(inplace=True),
nn.Conv1d( 64, 64, kernel_size=4, stride=2, padding=2), nn.ReLU(inplace=True), nn.MaxPool1d(kernel_size=2),
nn.Conv1d( 64, 256, kernel_size=4, stride=2, padding=2), nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=4, stride=2, padding=2), nn.ReLU(inplace=True), nn.MaxPool1d(kernel_size=2),
nn.Conv1d(256, 512, kernel_size=2, stride=1, padding=1), nn.ReLU(inplace=True),
nn.Conv1d(512, 512, kernel_size=2, stride=1, padding=1), nn.ReLU(inplace=True), nn.MaxPool1d(kernel_size=2),
)
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.dropout = nn.Dropout(0.3)
self.fc = nn.Linear(512, num_classes)
def forward(self, x:Tensor) -> Tensor:
x = self.features(x) # [B, C=512, L=7]
x = self.avgpool(x) # [B, C=512, L=1]
x = x.view(x.size(0), -1)
x = self.dropout(x) # [B, C=512]
return self.fc(x) # [B, NC]
class Naive4Conv1d(nn.Module):
base_cls = NaiveConv1d
def __init__(self, num_classes=4):
super().__init__()
self.base = NaiveConv1d()
self.fc = nn.Sequential(
nn.Linear(10, 16),
nn.ReLU(),
nn.Linear(16, num_classes),
)
def load_weights(self, state_dict:Dict[str, Tensor]):
self.base.load_state_dict(state_dict)
self.base.requires_grad_(False)
def forward(self, x:Tensor) -> Tensor:
x = self.base(x) # [B, D=10]
return self.fc(x) # [B, NC=4]
class SimpleConv1d(nn.Module):
def __init__(self, num_classes=4):
super().__init__()
# x1024 downsample: [1, 4096] => [512, 4]
self.features = nn.Sequential(
nn.Conv1d( 1, 16, kernel_size=16, stride=4, padding=8), nn.BatchNorm1d(16), nn.SiLU(inplace=True), nn.MaxPool1d(kernel_size=2, stride=2),
nn.Conv1d( 16, 64, kernel_size=8, stride=4, padding=4), nn.BatchNorm1d(64), nn.SiLU(inplace=True), nn.MaxPool1d(kernel_size=2, stride=2),
nn.Conv1d( 64, 256, kernel_size=5, stride=2, padding=2), nn.BatchNorm1d(256), nn.SiLU(inplace=True), nn.MaxPool1d(kernel_size=2, stride=2),
nn.Conv1d(256, 512, kernel_size=3, stride=2, padding=1), nn.BatchNorm1d(512), nn.SiLU(inplace=True), nn.MaxPool1d(kernel_size=2, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.dropout = nn.Dropout(0.75)
self.fc = nn.Linear(512, num_classes)
def forward(self, x:Tensor) -> Tensor:
x = self.features(x) # [B, C=512, L=4]
x = self.avgpool(x) # [B, C=512, L=1]
x = x.view(x.size(0), -1)
x = self.dropout(x) # [B, C=512]
return self.fc(x) # [B, NC]
class SimpleConv2d(nn.Module):
def __init__(self, num_classes=4):
super().__init__()
# x64 downsample: [F=129, L=256] => [F=2, L=4]
self.features = nn.Sequential(
nn.Conv2d( 1, 16, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(16), nn.SiLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d( 16, 64, kernel_size=5, stride=2, padding=2), nn.BatchNorm2d(64), nn.SiLU(inplace=True),
nn.Conv2d( 64, 256, kernel_size=5, stride=2, padding=2), nn.BatchNorm2d(256), nn.SiLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(512), nn.SiLU(inplace=True),
)
self.avgpool = nn.AdaptiveMaxPool2d((1, 1))
self.dropout = nn.Dropout(0.75)
self.fc = nn.Linear(512, num_classes)
def forward(self, x:Tensor) -> Tensor:
x = self.features(x) # [B, C=512, F=2, L=4]
x = self.avgpool(x) # [B, C=512, F=1, L=1]
x = x.view(x.size(0), -1)
x = self.dropout(x) # [B, C=512]
return self.fc(x) # [B, NC]
if __name__ == '__main__':
#model = SimpleConv1d()
#X = torch.randn(1, 1, 4096)
model = NaiveConv1d()
X = torch.randn(1, 1, 6000)
logits = model(X)
print(model)
print('X.shape:', X.shape)
print('logits.shape:', logits.shape)