使用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]
    ]
)

你可能感兴趣的:(Deep,Learning,pytorch,预训练,深度学习,计算机视觉,迁移学习)