torchvision 中的预训练模型及相关参数
发布日期:2021-05-04 21:02:10 浏览次数:19 分类:技术文章

本文共 2286 字,大约阅读时间需要 7 分钟。

可以用以下代码加载模型,注意,只需要加载一种即可
​​​​​​​import torchvision.models as modelsresnet18 = 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 

 

Instancing a pre-trained model will download its weights to a cache directory. This directory can be set using the TORCH_MODEL_ZOO environment variable. See torch.utils.model_zoo.load_url() for details.

Some models use modules which have different training and evaluation behavior, such as batch normalization. To switch between these modes, use model.train() or model.eval() as appropriate. See train() or eval() for details.

 

正则化方法:

注意:所以以上模型的正则化参数是一样的

均值 为:[0.485, 0.456, 0.406]

标准差为 [0.229, 0.224, 0.225]

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:

 

from torchvision import datasets, transforms as T
normalize = T.Normalize(mean=[0.485, 0.456, 0.406],                                 std=[0.229, 0.224, 0.225])

 

 

以下代码为获取 mean/std的详细过程,不需要使用,只是为了好理解:

import torchfrom torchvision import datasets, transforms as Ttransform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])dataset = datasets.ImageNet(".", split="train", transform=transform)means = []stds = []for img in subset(dataset):    means.append(torch.mean(img))    stds.append(torch.std(img))mean = torch.mean(torch.tensor(means))std = torch.mean(torch.tensor(stds))

 

上一篇:修改 pytorch中的model zoo下载后的模型的保存目录
下一篇:TVM 实战: 用llvm提高人工智能模型推理速度

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月14日 23时18分13秒