Abdul Rafay commited on
Commit
0f90f9e
·
1 Parent(s): d4b6872

updated model

Browse files
Files changed (2) hide show
  1. main.py +4 -1
  2. model.py +26 -9
main.py CHANGED
@@ -7,9 +7,12 @@ from PIL import Image
7
  from model import Model
8
  from huggingface_hub import hf_hub_download
9
 
 
10
  transform = transforms.Compose([
 
11
  transforms.Resize((28, 28)),
12
- transforms.ToTensor()
 
13
  ])
14
 
15
  # -----------------------
 
7
  from model import Model
8
  from huggingface_hub import hf_hub_download
9
 
10
+ # Preprocess image
11
  transform = transforms.Compose([
12
+ transforms.Grayscale(),
13
  transforms.Resize((28, 28)),
14
+ transforms.ToTensor(),
15
+ transforms.Normalize((0.1307,), (0.3081,))
16
  ])
17
 
18
  # -----------------------
model.py CHANGED
@@ -6,24 +6,41 @@ class Model(nn.Module):
6
  super().__init__()
7
 
8
  self.conv_layers = nn.Sequential(
9
- nn.Conv2d(1, 64, kernel_size=3, padding=1),
 
 
10
  nn.ReLU(),
11
  nn.MaxPool2d(2),
 
12
 
 
 
 
 
 
 
 
 
13
  nn.Conv2d(64, 128, kernel_size=3, padding=1),
 
14
  nn.ReLU(),
15
- nn.MaxPool2d(2)
 
 
 
 
 
 
 
 
16
  )
17
 
18
  self.fc_layers = nn.Sequential(
19
- nn.Flatten(),
20
- nn.Linear(128 * 7 * 7, 84),
21
- nn.ReLU(),
22
- nn.Linear(84, 56),
23
- nn.ReLU(),
24
- nn.Linear(56, 32),
25
  nn.ReLU(),
26
- nn.Linear(32, 10)
 
27
  )
28
 
29
  def forward(self, x):
 
6
  super().__init__()
7
 
8
  self.conv_layers = nn.Sequential(
9
+ # Block 1: 1 -> 32 channels, 28x28 -> 14x14
10
+ nn.Conv2d(1, 32, kernel_size=3, padding=1),
11
+ nn.BatchNorm2d(32),
12
  nn.ReLU(),
13
  nn.MaxPool2d(2),
14
+ nn.Dropout2d(0.25),
15
 
16
+ # Block 2: 32 -> 64 channels, 14x14 -> 7x7
17
+ nn.Conv2d(32, 64, kernel_size=3, padding=1),
18
+ nn.BatchNorm2d(64),
19
+ nn.ReLU(),
20
+ nn.MaxPool2d(2),
21
+ nn.Dropout2d(0.25),
22
+
23
+ # Block 3: 64 -> 128 channels, 7x7 -> 3x3
24
  nn.Conv2d(64, 128, kernel_size=3, padding=1),
25
+ nn.BatchNorm2d(128),
26
  nn.ReLU(),
27
+ nn.MaxPool2d(2),
28
+ nn.Dropout2d(0.25),
29
+
30
+ # Block 3: 128 -> 256 channels, 3x3 -> 1x1
31
+ nn.Conv2d(128, 256, kernel_size=1),
32
+ nn.BatchNorm2d(256),
33
+ nn.ReLU(),
34
+ nn.MaxPool2d(2),
35
+ nn.Dropout2d(0.25),
36
  )
37
 
38
  self.fc_layers = nn.Sequential(
39
+ nn.Flatten(), # 256 * 1 * 1 = 256
40
+ nn.Linear(256 * 1 * 1, 128),
 
 
 
 
41
  nn.ReLU(),
42
+ nn.Dropout(0.25),
43
+ nn.Linear(128, 10)
44
  )
45
 
46
  def forward(self, x):