如何使用 libtorch 实现 VGG16 网络?

参考地址:https://ethereon.github.io/netscope/#/preset/vgg-16

按照上面的图来写即可。

论文地址:https://arxiv.org/pdf/1409.1556.pdf

// Define a new Module.
struct Net : torch::nn::Module {
    Net() {
        conv1_1 = torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 64, { 3,3 }).padding(1));
        conv1_2 = torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 64, { 3,3 }).padding(1));
        conv2_1 = torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 128, { 3,3 }).padding(1));
        conv2_2 = torch::nn::Conv2d(torch::nn::Conv2dOptions(128, 128, { 3,3 }).padding(1));
        conv3_1 = torch::nn::Conv2d(torch::nn::Conv2dOptions(128, 256, { 3,3 }).padding(1));
        conv3_2 = torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, { 3,3 }).padding(1));
        conv3_3 = torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, { 3,3 }).padding(1));
        conv4_1 = torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 512, { 3,3 }).padding(1));
        conv4_2 = torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, { 3,3 }).padding(1));
        conv4_3 = torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, { 3,3 }).padding(1));
        conv5_1 = torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, { 3,3 }).padding(1));
        conv5_2 = torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, { 3,3 }).padding(1));
        conv5_3 = torch::nn::Conv2d(torch::nn::Conv2dOptions(512, 512, { 3,3 }).padding(1));

        fc1 = torch::nn::Linear(512*7*7,4096);
        fc2 = torch::nn::Linear(4096, 4096);
        fc3 = torch::nn::Linear(4096, 1000);
    }

    // Implement the Net's algorithm.
    torch::Tensor forward(torch::Tensor x) {
        x = conv1_1->forward(x);
        x = torch::relu(x);
        x = conv1_2->forward(x);
        x = torch::relu(x);
        x = torch::max_pool2d(x, { 2,2 }, { 2,2 });

        x = conv2_1->forward(x);
        x = torch::relu(x);
        x = conv2_2->forward(x);
        x = torch::relu(x);
        x = torch::max_pool2d(x, { 2,2 }, { 2,2 });

        x = conv3_1->forward(x);
        x = torch::relu(x);
        x = conv3_2->forward(x);
        x = torch::relu(x);
        x = conv3_3->forward(x);
        x = torch::relu(x);
        x = torch::max_pool2d(x, { 2,2 }, { 2,2 });

        x = conv4_1->forward(x);
        x = torch::relu(x);
        x = conv4_2->forward(x);
        x = torch::relu(x);
        x = conv4_3->forward(x);
        x = torch::relu(x);
        x = torch::max_pool2d(x, { 2,2 }, { 2,2 });

        x = conv5_1->forward(x);
        x = torch::relu(x);
        x = conv5_2->forward(x);
        x = torch::relu(x);
        x = conv5_3->forward(x);
        x = torch::relu(x);
        x = torch::max_pool2d(x, { 2,2 }, { 2,2 });

        x = x.view({ x.size(0), -1 });//512x7x7 = 25088

        x = fc1->forward(x);
        x = torch::relu(x);
        x = torch::dropout(x, 0.5, is_training());

        x = fc2->forward(x);
        x = torch::relu(x);
        x = torch::dropout(x, 0.5, is_training());

        x = fc3->forward(x);

        x = torch::log_softmax(x, 1);

        return x;
    }

    // Use one of many "standard library" modules.
    torch::nn::Conv2d conv1_1{ nullptr };
    torch::nn::Conv2d conv1_2{ nullptr };
    torch::nn::Conv2d conv2_1{ nullptr };
    torch::nn::Conv2d conv2_2{ nullptr };
    torch::nn::Conv2d conv3_1{ nullptr };
    torch::nn::Conv2d conv3_2{ nullptr };
    torch::nn::Conv2d conv3_3{ nullptr };
    torch::nn::Conv2d conv4_1{ nullptr };
    torch::nn::Conv2d conv4_2{ nullptr };
    torch::nn::Conv2d conv4_3{ nullptr };
    torch::nn::Conv2d conv5_1{ nullptr };
    torch::nn::Conv2d conv5_2{ nullptr };
    torch::nn::Conv2d conv5_3{ nullptr };
    torch::nn::Linear fc1{ nullptr };
    torch::nn::Linear fc2{ nullptr };
    torch::nn::Linear fc3{ nullptr };
};

转载于:https://www.cnblogs.com/cheungxiongwei/p/10714974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值