Skip to content

Instantly share code, notes, and snippets.

@hengck23
Created September 30, 2021 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengck23/8739ac7537ad5a47d382b8c23b61253f to your computer and use it in GitHub Desktop.
Save hengck23/8739ac7537ad5a47d382b8c23b61253f to your computer and use it in GitHub Desktop.
1dCNN
class ConvBn1d(nn.Module):
def __init__(self, in_dim, out_dim, kernel_size, padding, stride=1, pool=None):
super().__init__()
self.conv = nn.Conv1d(in_dim, out_dim, kernel_size=kernel_size, padding=padding, stride=stride)
self.bn = nn.BatchNorm1d(out_dim)
self.relu = nn.SiLU(inplace=True)
if pool is None:
self.pool=nn.Identity()
else:
self.pool=nn.MaxPool1d(kernel_size=pool)
#self.pool=nn.AvgPool1d(kernel_size=pool)
def forward(self, x):
x = self.conv(x)
x = self.pool(x)
x = self.bn(x)
x = self.relu(x)
return x
# see: https://arxiv.org/abs/1703.01789
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Sequential(
ConvBn1d( 3, 64, kernel_size=64, padding=32, ),
ConvBn1d( 64, 64, kernel_size=64, padding=32, ),
ConvBn1d( 64, 64, kernel_size=32, padding=16, pool=8),
)
self.conv2 = nn.Sequential(
ConvBn1d( 64, 128, kernel_size=32, padding=16, ),
ConvBn1d(128, 128, kernel_size=32, padding=16, ),
ConvBn1d(128, 128, kernel_size=32, padding=16, pool=4),
)
self.conv3 = nn.Sequential(
ConvBn1d(128, 256, kernel_size=16, padding=8, ),
ConvBn1d(256, 256, kernel_size=16, padding=8, ),
ConvBn1d(256, 256, kernel_size=16, padding=8, pool=4),
)
self.conv4 = nn.Sequential(
ConvBn1d(256, 512, kernel_size=8, padding=4, ),
ConvBn1d(512, 512, kernel_size=8, padding=4, ),
ConvBn1d(512, 512, kernel_size=8, padding=4, pool=2),
)
self.conv5 = nn.Sequential(
ConvBn1d( 512, 1024, kernel_size=4, padding=2, ),
ConvBn1d(1024, 1024, kernel_size=4, padding=2, pool=2),
)
self.conv6 = nn.Sequential(
ConvBn1d(1024, 2048, kernel_size=4, padding=2, ),
ConvBn1d(2048, 2048, kernel_size=4, padding=2, pool=2),
)
self.signal = nn.Linear(2048, 1)
def forward(self, x):
x = self.conv1(x) #; print(x.shape)
x = self.conv2(x) #; print(x.shape)
x = self.conv3(x) #; print(x.shape)
x = self.conv4(x) #; print(x.shape)
x = self.conv5(x) #; print(x.shape)
x = self.conv6(x) #; print(x.shape)
x = F.dropout(x,p=0.5,training=self.training)
x = F.adaptive_avg_pool1d(x,1)
x = x.flatten(1)
signal = self.signal(x)
signal = signal.reshape(-1)
return signal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment