0% found this document useful (0 votes)
17 views3 pages

NN Conv2d

This document describes a convolutional neural network (CNN) classifier model in PyTorch. The CNN consists of alternating convolutional and max pooling layers, followed by linear layers. The input shape to the convolutional layers should be a 4D tensor of [batch_size, channels, height, width]. The model takes an input tensor of shape [2, 3, 128, 128], passes it through the CNN and linear layers, and outputs predictions of shape [2, 5].
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

NN Conv2d

This document describes a convolutional neural network (CNN) classifier model in PyTorch. The CNN consists of alternating convolutional and max pooling layers, followed by linear layers. The input shape to the convolutional layers should be a 4D tensor of [batch_size, channels, height, width]. The model takes an input tensor of shape [2, 3, 128, 128], passes it through the CNN and linear layers, and outputs predictions of shape [2, 5].
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Conv2d 的輸入形狀應該是四維張量,即 [batch_size, channels, height, width]

import torch
import torch.nn as nn

class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
# torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
# torch.nn.MaxPool2d(kernel_size, stride, padding)
# input 維度 [3, 128, 128]
self.cnn = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1), # [64, 128, 128] [2, 64, 128, 128]
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [64, 64, 64] [2, 64, 64, 64]

nn.Conv2d(64, 128, 3, 1, 1), # [128, 64, 64] [2, 128, 64, 64]
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [128, 32, 32] [2, 128, 32, 32]

nn.Conv2d(128, 256, 3, 1, 1), # [256, 32, 32] [2, 256, 32, 32]
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [256, 16, 16] [2, 256, 16, 16

nn.Conv2d(256, 512, 3, 1, 1), # [512, 16, 16] [2, 512, 16, 16]
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [512, 8, 8] [2, 512, 8, 8]

nn.Conv2d(512, 512, 3, 1, 1), # [512, 8, 8] [2, 512, 8, 8]


nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [512, 4, 4] [2, 512, 4, 4]
)
self.fc = nn.Sequential(
nn.Linear(512*4*4, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 5)
)

def forward(self, x):


out = self.cnn(x)
print(out.shape) # [2, 512, 4, 4]
out = out.view(out.size()[0], -1) # [2, 512*4*4]
return self.fc(out)

a = torch.rand(3, 128, 128) # [1, 3, 128, 128]


random_tensor = torch.stack((a,a),0)
print(random_tensor.shape)
model = Classifier()
output = model(random_tensor)
print(output)

You might also like