resnet源码pytorch_pytorch yolov3训练自己的数据集

resnet源码pytorch_pytorch yolov3训练自己的数据集书上的,很抽象fromtorchimportnnfromtorch.nnimportfunctionalasFimportosimporttorchvisionclassResidualBlock(nn.Module):def__init__(self,inchannel,outchannel,stride=1,shortcut=None…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

书上的,很抽象

from torch import nn
from torch.nn import functional as F
import os
import torchvision


class ResidualBlock(nn.Module):
    def __init__(self, inchannel, outchannel, stride=1, shortcut=None):
        super(ResidualBlock, self).__init__()
        self.left = nn.Sequential(
            nn.Conv2d(inchannel, outchannel, 3, stride=stride, padding=1, bias=False),
            nn.BatchNorm2d(outchannel),
            nn.ReLU(inplace=True),
            nn.Conv2d(outchannel, outchannel, 3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(outchannel)
        )
        self.right = shortcut

    def forward(self, x):
        out = self.left(x)
        residual = x if self.right is None else self.right(x)
        out += residual
        return F.relu(out)


class ResNetmy(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNetmy, self).__init__()

        self.pre = nn.Sequential(
            nn.Conv2d(3, 64, 7, 2, 3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(3, 2, 1))
        self.layer1 = self._make_layer(64, 128, 3)
        self.layer2 = self._make_layer(128, 256, 4, stride=2)
        self.layer3 = self._make_layer(256, 512, 6, stride=2)
        self.layer4 = self._make_layer(512, 512, 3, stride=2)
        self.fc = nn.Linear(512, num_classes)

    def _make_layer(self, inchannel, outchannel, block_num, stride=1):
        shortcut = nn.Sequential(
            nn.Conv2d(inchannel, outchannel, 1, stride, bias=False),
            nn.BatchNorm2d(outchannel)
        )

        layers = []
        layers.append(ResidualBlock(inchannel, outchannel, stride, shortcut))

        for i in range(1, block_num):
            layers.append(ResidualBlock(outchannel, outchannel))
        return nn.Sequential(*layers)

    def forward(self, x):
        print('pre:',x.size())
        x = self.pre(x)
        print(x.size())
        x = self.layer1(x)
        print(x.size())
        x = self.layer2(x)
        print(x.size())
        x = self.layer3(x)
        print(x.size())
        x = self.layer4(x)
        print(x.size())
        x = F.avg_pool2d(x, 7)
        print(x.size())
        x = x.view(x.size(0), -1)
        return self.fc(x)


if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = "3"
    model = ResNetmy()
    print(model)
    resnet34 = torchvision.models.resnet34(pretrained=False)
    print(resnet34)

自己搭的很复杂的一个

from torch import nn
from torch.nn import functional as F
import os
import torchvision
from torchvision.transforms import ToTensor

totensor = ToTensor()


class myRes34(nn.Module):
    def __init__(self):
        super(myRes34, self).__init__()
        self.pre = nn.Sequential(
            nn.Conv2d(3, 64, 7, 2, 3, bias=False),
            nn.MaxPool2d(2)
        )
        self.f64conv_1 = nn.Sequential(
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.f64conv_2 = nn.Sequential(
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.f64conv_3 = nn.Sequential(
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.residual64_128 = nn.Sequential(
            nn.Conv2d(64, 128, 1, 2, bias=False),
            nn.BatchNorm2d(128)
        )
        self.f128conv_1 = nn.Sequential(
            nn.Conv2d(64, 128, 3, 2, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128)
        )
        self.f128conv_2 = nn.Sequential(
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )
        self.f128conv_3 = nn.Sequential(
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )
        self.f128conv_4 = nn.Sequential(
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )
        self.residual128_256 = nn.Sequential(
            nn.Conv2d(128, 256, 1, 2, bias=False),
            nn.BatchNorm2d(256)
        )
        self.f256conv_1 = nn.Sequential(
            nn.Conv2d(128, 256, 3, 2, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256)
        )
        self.f256conv_2 = nn.Sequential(
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.f256conv_3 = nn.Sequential(
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.f256conv_4 = nn.Sequential(
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.f256conv_5 = nn.Sequential(
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.f256conv_6 = nn.Sequential(
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.residual256_512 = nn.Sequential(
            nn.Conv2d(256, 512, 1, 2, bias=False),
            nn.BatchNorm2d(512)
        )
        self.f512conv_1 = nn.Sequential(
            nn.Conv2d(256, 512, 3, 2, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512)
        )
        self.f512conv_2 = nn.Sequential(
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU()
        )
        self.f512conv_3 = nn.Sequential(
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU()
        )
        self.fc = nn.Linear(512, 1000)
        self.myrelu1 = nn.ReLU()
        self.myrelu2 = nn.ReLU()
        self.myrelu3 = nn.ReLU()

    def forward(self, x):
        x = self.pre(x)
        x = self.f64conv_1(x)
        x = self.f64conv_2(x)
        x = self.f64conv_3(x)
        print(type(x))
        x = self.myrelu1(self.f128conv_1(x) + self.residual64_128(x))
        x = self.f128conv_2(x)
        x = self.f128conv_3(x)
        x = self.f128conv_4(x)
        x = self.myrelu2(self.f256conv_1(x) + self.residual128_256(x))
        x = self.f256conv_2(x)
        x = self.f256conv_3(x)
        x = self.f256conv_4(x)
        x = self.f256conv_5(x)
        x = self.f256conv_6(x)
        x = self.myrelu3(self.f512conv_1(x) + self.residual256_512(x))
        x = self.f512conv_2(x)
        x = self.f512conv_3(x)
        x = F.avg_pool2d(x, 7)
        x = x.view(x.size(0), -1)
        return self.fc(x)


if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = "3"
    model = myRes34()
    print(model)
    resnet34 = torchvision.models.resnet34(pretrained=False)
    print(resnet34)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185515.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java遍历map集合的4中方式

    方法一通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。Map<Integer,Integer>map=newHashMap<Integer,Integer>();for(Map.Entry&l…

    2022年4月8日
    52
  • Struts2中属性驱动与模型驱动

    Struts2中属性驱动与模型驱动

    2021年11月16日
    39
  • java文件上传下载接口_java 文件上传下载

    java文件上传下载接口_java 文件上传下载翻新十年前的老项目,文件上传改为调用接口方式,记录一下子~~~java后台代码://取配置文件中的上传目录@Value(“${uploadPath}”)Stringpath;//文件上传接口@RequestMapping(value=”upload”)@ResponseBodypublicStringgetMobileAuthCode(HttpServletRequestreques…

    2022年5月15日
    42
  • dingo「建议收藏」

    dingo「建议收藏」dingo

    2022年4月24日
    42
  • emwin实体按键_qt指示灯控件

    emwin实体按键_qt指示灯控件分享一个emWin软键盘控件[复制链接]本帖最后由glcd于2016-6-2716:30编辑花了两天时间做了个emWin软键盘控件,并命名为ButtonSKB控件:image001.png(20.29KB,下载次数:0)2016-6-2716:19上传前言:(1)ButtonSKB已经是1个控件,即可以像使用Button控件一样使用ButtonSKB。(2)ButtonSKB…

    2022年10月14日
    3
  • IC基础(一):异步FIFO[通俗易懂]

    IC基础(一):异步FIFO[通俗易懂]今天看别人的博客研究了一天的异步FIFO,中遇到了很多问题。很多人可能有过这样的经历,当你研究一个东西,可能你当时很清楚你是怎么想的,但是过后就忘记了当时的思路了。因此我写博客的主要目的就是为了回头查阅方便。IC基础可能会写很多篇,本篇异步FIFO就是此系列的第一篇。…

    2022年8月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号