import torch.nn as nn

class MyCNN(nn.Module):
    def __init__(self):
        super(MyCNN, self).__init__()
        
        self.conv1 = nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2)
        self.relu1 = nn.ReLU(inplace=True)
        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)
        
        self.conv2 = nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)
        self.relu2 = nn.ReLU(inplace=True)
        self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)
        
        self.conv3 = nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1)
        self.relu3 = nn.ReLU(inplace=True)
        
        self.conv4 = nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1)
        self.relu4 = nn.ReLU(inplace=True)
        
        self.conv5 = nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1)
        self.relu5 = nn.ReLU(inplace=True)
        self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        
        self.fc1 = nn.Linear(6*6*256, 4096)
        self.relu6 = nn.ReLU(inplace=True)
        self.fc2 = nn.Linear(4096, 1000)
        
    def forward(self, x):
        x = self.conv1(x)
        print('Conv2d output shape: 	', x.shape)
        x = self.relu1(x)
        print('ReLU output shape: 	', x.shape)
        x = self.pool1(x)
        print('MaxPool2d output shape: 	', x.shape)
        
        x = self.conv2(x)
        print('Conv2d output shape: 	', x.shape)
        x = self.relu2(x)
        print('ReLU output shape: 	', x.shape)
        x = self.pool2(x)
        print('MaxPool2d output shape: 	', x.shape)
        
        x = self.conv3(x)
        print('Conv2d output shape: 	', x.shape)
        x = self.relu3(x)
        print('ReLU output shape: 	', x.shape)
        
        x = self.conv4(x)
        print('Conv2d output shape: 	', x.shape)
        x = self.relu4(x)
        print('ReLU output shape: 	', x.shape)
        
        x = self.conv5(x)
        print('Conv2d output shape: 	', x.shape)
        x = self.relu5(x)
        print('ReLU output shape: 	', x.shape)
        x = self.pool3(x)
        print('MaxPool2d output shape: 	', x.shape)
        
        x = x.view(x.size(0), -1)
        print('Flatten output shape: 	', x.shape)
        x = self.fc1(x)
        print('Linear output shape: 	', x.shape)
        x = self.relu6(x)
        print('ReLU output shape: 	', x.shape)
        x = self.fc2(x)
        print('Linear output shape: 	', x.shape)
        
        return x

说明:

  • 该 CNN 模型由多个卷积层、池化层和全连接层构成,使用 ReLU 作为激活函数。
  • 输入图像大小为 3x224x224,经过各层的卷积和池化操作后,最终输出为 1x1000 的向量,代表着 1000 个类别上的概率值。
  • 代码中添加了 print 语句,以便在训练过程中打印各层输出的形状,便于观察网络的结构和数据流。
  • 由于输入图像大小为 224x224,经过第一层卷积核大小为 11x11,步长为 4,填充为 2 的卷积后,输出为 96x55x55,而非题目中给出的 96x56x56。

注意:

  • 这只是一个简单的 CNN 模型,可以根据具体的图像分类任务进行调整。
  • 可以使用其他激活函数,例如 sigmoid、tanh 等。
  • 可以添加更多层,例如 dropout 层、BatchNorm 层等。
  • 可以在训练过程中使用数据增强技术来提高模型的泛化能力。
  • 可以使用不同的优化算法,例如 SGD、Adam 等。
  • 可以使用不同的损失函数,例如交叉熵损失函数等。
  • 可以使用不同的评价指标,例如准确率、召回率、F1 分数等。
  • 可以使用不同的数据集,例如 ImageNet、CIFAR-10 等。
  • 可以使用不同的框架,例如 TensorFlow、Keras 等。

希望这个示例能够帮助您更好地理解 CNN 的设计与实现。

卷积神经网络 (CNN) 设计与实现:图像分类任务示例

原文地址: https://www.cveoy.top/t/topic/nYYl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录