Moremoholo2 commited on
Commit
3d2256a
·
verified ·
1 Parent(s): 6097489

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +13 -6
model.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import torch
3
  import torch.nn as nn
4
  import torch.nn.functional as F
@@ -6,13 +5,20 @@ import torch.nn.functional as F
6
  class AudioCNN(nn.Module):
7
  def __init__(self, num_classes, input_shape):
8
  super(AudioCNN, self).__init__()
9
- self.conv1 = nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
 
10
  self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
11
- self.conv2 = nn.Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
 
 
 
 
 
 
 
12
 
13
- pooled_height = input_shape[1] // (2 * 2)
14
- pooled_width = input_shape[2] // (2 * 2)
15
- self.fc1 = nn.Linear(64 * pooled_height * pooled_width, 128)
16
  self.fc2 = nn.Linear(128, num_classes)
17
 
18
  def forward(self, x):
@@ -22,3 +28,4 @@ class AudioCNN(nn.Module):
22
  x = F.relu(self.fc1(x))
23
  x = self.fc2(x)
24
  return x
 
 
 
1
  import torch
2
  import torch.nn as nn
3
  import torch.nn.functional as F
 
5
  class AudioCNN(nn.Module):
6
  def __init__(self, num_classes, input_shape):
7
  super(AudioCNN, self).__init__()
8
+ # Convolution layers
9
+ self.conv1 = nn.Conv2d(1, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
10
  self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
11
+ self.conv2 = nn.Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
12
+
13
+ # Dynamically compute the flattened size after conv + pooling
14
+ with torch.no_grad():
15
+ dummy_input = torch.zeros(1, *input_shape) # batch=1
16
+ x = self.pool(F.relu(self.conv1(dummy_input)))
17
+ x = self.pool(F.relu(self.conv2(x)))
18
+ flattened_size = x.numel() // x.size(0)
19
 
20
+ # Fully connected layers
21
+ self.fc1 = nn.Linear(flattened_size, 128)
 
22
  self.fc2 = nn.Linear(128, num_classes)
23
 
24
  def forward(self, x):
 
28
  x = F.relu(self.fc1(x))
29
  x = self.fc2(x)
30
  return x
31
+