Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- model class of CNN in PyToch
- ```python
- # with batch normalization, dropout layer and 3 convolutional layers
- import torch.nn as nn
- import torch.nn.functional as F
- # define the CNN architecture
- class Net(nn.Module):
- def __init__(self):
- super(Net, self).__init__()
- ## Define layers of a CNN
- self.conv1 = nn.Conv2d(3,16,3, padding=1)
- self.conv2 = nn.Conv2d(16,32,3, padding=1)
- self.conv3 = nn.Conv2d(32,64,3, padding=1)
- #self.conv4 = nn.Conv2d(64,128,3, padding=1)
- #self.conv5 = nn.Conv2d(128,256,3, padding=1)
- self.pool = nn.MaxPool2d(2,2)
- # 224x224 size images will go through 5 maxpooling layer of 2,2 => 224/2/2/2/2/2 = 7.
- # final image size is 7x7.
- # the number of parameters will be 7*7*number of output features 256
- #self.fc1 = nn.Linear(7*7*256,500)
- self.fc1 = nn.Linear(28*28*64,500)
- self.fc2 = nn.Linear(500,133)
- self.dropout = nn.Dropout(0.25)
- self.batch_norm = nn.BatchNorm1d(num_features=500)
- def forward(self, x):
- ## Define forward behavior
- x = self.pool(F.relu(self.conv1(x)))
- x = self.pool(F.relu(self.conv2(x)))
- x = self.pool(F.relu(self.conv3(x)))
- #x = self.pool(F.relu(self.conv4(x)))
- #x = self.pool(F.relu(self.conv5(x)))
- # flatten to a vector
- #x = x.view(-1, 7*7*256)
- x = x.view(-1, 28*28*64)
- x = self.dropout(x)
- x = F.relu(self.batch_norm(self.fc1(x)))
- #x = F.relu(self.fc1(x))
- x = self.dropout(x)
- x = self.fc2(x)
- return x
- # check if CUDA is available
- use_cuda = torch.cuda.is_available()
- # instantiate the CNN
- model_scratch = Net()
- # move tensors to GPU if CUDA is available
- if use_cuda:
- model_scratch.cuda()
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement