使用PyTorch预训练模型

使用PyTorch torch.utils.model_zoo提供预先训练过的模型。可以通过传递pretrained=True

import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)
alexnet = models.alexnet(pretrained=True)
squeezenet = models.squeezenet1_0(pretrained=True)
vgg16 = models.vgg16(pretrained=True)
densenet = models.densenet161(pretrained=True)
inception = models.inception_v3(pretrained=True)
googlenet = models.googlenet(pretrained=True)
shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
mobilenet_v2 = models.mobilenet_v2(pretrained=True)
mobilenet_v3_large = models.mobilenet_v3_large(pretrained=True)
mobilenet_v3_small = models.mobilenet_v3_small(pretrained=True)
resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
mnasnet = models.mnasnet1_0(pretrained=True)

实例化一个预训练模型会把它的权重下载到一个缓存目录中。可以使用TORCH_MODEL_ZOO环境变量设置此目录。详情参阅torch.utils.model_zoo.load_url()

warnings.warn('TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME instead')

一些模型使用的模块具有不同的训练和评估行为,例如批处理规范化(batch normalization)。要在这些模式之间切换,请使用model.train()或model.eval()。

All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224. The images have to be loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225]. You can use the following transform to normalize:

所有的预训练模型都期望输入图像以相同的方式归一化,即shape为(3 x H x W)的3通道RGB图像的mini-batches,其中H和W至少为224。图像必须归一化到[0,1]范围内,然后使用mean =[0.485, 0.456, 0.406]和std =[0.229, 0.224, 0.225]进行标准化。你可以使用下面的转换来标准化:

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

归一化再标准化:

_transforms = transforms.Compose([
    transforms.ToTensor(),  # [0, 255] -> [0.0, 1.0]
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))  # [0.0, 1.0] -> [-1.0, 1.0]
    ]
)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ayiya_Oese

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值