Upload convnets.py
Browse files- convnets.py +79 -0
convnets.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CNN models for binary and multi-class classifications
|
| 3 |
+
"""
|
| 4 |
+
import torch
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Convnet(nn.Module):
|
| 9 |
+
"""
|
| 10 |
+
Convolutional Neural Network for binary classification
|
| 11 |
+
|
| 12 |
+
input args: n_classes (int) --> number of classes
|
| 13 |
+
|
| 14 |
+
Input shape: [1, 60, 60]
|
| 15 |
+
|
| 16 |
+
Matrix shape (Conv layer):
|
| 17 |
+
|
| 18 |
+
Input shape: [N, C_in, H, W]
|
| 19 |
+
- N: batch_size
|
| 20 |
+
- C_in: number of input channels
|
| 21 |
+
- H: height of input planes
|
| 22 |
+
- W: width of input planes
|
| 23 |
+
|
| 24 |
+
- Conv2d(1, 64, (5, 3), 1) --> [64, 56, 58]
|
| 25 |
+
- MaxPool2d(kernel_size=(2, 1)) --> [64, 28, 58]
|
| 26 |
+
- Conv2d(64, 128, (5, 3), 1) --> [128, 24, 56]
|
| 27 |
+
- MaxPool2d(kernel_size=(2, 1)) --> [128, 12, 56]
|
| 28 |
+
- Conv2d(128, 256, (5, 3), 1) --> [256, 8, 54]
|
| 29 |
+
- MaxPool2d(kernel_size=(2, 1)) --> [256, 4, 54]
|
| 30 |
+
|
| 31 |
+
Matrix shape (Fully connected layer):
|
| 32 |
+
- Linear(256 * 4 * 54, 1024) --> [1024]
|
| 33 |
+
- Linear(1024, 512) --> [512]
|
| 34 |
+
- Linear(512, 128) --> [128]
|
| 35 |
+
- Linear(128, 64) --> [64]
|
| 36 |
+
- Linear(64, n_classes) --> [n_classes]
|
| 37 |
+
|
| 38 |
+
Softmax() --> to probability
|
| 39 |
+
"""
|
| 40 |
+
def __init__(self, n_classes: int) -> None:
|
| 41 |
+
super().__init__()
|
| 42 |
+
self.cnn = nn.Sequential(
|
| 43 |
+
nn.Conv2d(in_channels=1, out_channels=64, kernel_size=(5, 3), stride=1),
|
| 44 |
+
nn.BatchNorm2d(64),
|
| 45 |
+
nn.LeakyReLU(negative_slope=0.01),
|
| 46 |
+
nn.MaxPool2d(kernel_size=(2, 1)),
|
| 47 |
+
nn.Conv2d(64, 128, (5, 3), 1),
|
| 48 |
+
nn.BatchNorm2d(128),
|
| 49 |
+
nn.LeakyReLU(negative_slope=0.01),
|
| 50 |
+
nn.MaxPool2d(kernel_size=(2, 1)),
|
| 51 |
+
nn.Conv2d(128, 256, (5, 3), 1),
|
| 52 |
+
nn.BatchNorm2d(256),
|
| 53 |
+
nn.LeakyReLU(negative_slope=0.01),
|
| 54 |
+
nn.MaxPool2d(kernel_size=(2, 1)),
|
| 55 |
+
)
|
| 56 |
+
self.dropout = nn.Sequential(nn.Dropout(0.5))
|
| 57 |
+
self.fc = nn.Sequential(
|
| 58 |
+
nn.Linear(256 * 4 * 54, 1024),
|
| 59 |
+
nn.Linear(1024, 512),
|
| 60 |
+
nn.Linear(512, 128),
|
| 61 |
+
nn.Linear(128, 64),
|
| 62 |
+
nn.Linear(64, n_classes),
|
| 63 |
+
nn.Softmax()
|
| 64 |
+
)
|
| 65 |
+
for layer in self.cnn:
|
| 66 |
+
if isinstance(layer, nn.Conv2d):
|
| 67 |
+
nn.init.xavier_normal_(layer.weight)
|
| 68 |
+
nn.init.constant_(layer.bias, 0.0)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 72 |
+
"""
|
| 73 |
+
forward prop
|
| 74 |
+
"""
|
| 75 |
+
x = self.cnn(x)
|
| 76 |
+
x = self.dropout(x)
|
| 77 |
+
x = x.view(x.size(0), -1)
|
| 78 |
+
x = self.fc(x)
|
| 79 |
+
return x
|