由于笔记本CUDA版本过低,如果要装pytorch 1.5.1只能以cpuonly安装:
安装v1.1.0从而利用笔记本上的GPU
pytorch下载速度过慢,如无法科学上网,可借助清华镜像源来下载相应的包
下载的包放到下面的路径
根据下载的文件修改anaconda的下载列表
再次执行conda install命令
或者直接本地安装:conda install --use-local pytorch-1.1.0-py3.7_cuda90_cudnn7_1.tar.bz2
预训练的网络使用的是ImageNet数据集,所有图片都有来自WordNet的标签。
The pretrained network we’ll explore here was trained on a subset of the ImageNet dataset (http://imagenet.stanford.edu). ImageNet is a very large dataset of over 14 million images maintained by Stanford University. All of the images are labeled with a hierarchy of nouns that come from the WordNet dataset (http://wordnet.princeton.edu), which is in turn a large lexical database of the English language.
输入图像经过处理后存入多维矩阵类torch. Tensor。图像是RGB,有长度和宽度。tensor有三个维度: 三色通道,两个指定大小的空间图像维度。
The input image will first be preprocessed into an instance of the multidimensional array class torch.Tensor. It is an RGB image with height and width, so this tensor will have three dimensions: the three color channels, and two spatial image dimensions of a specific size.
Our model will take that processed input image and pass it into the pretrained network to obtain scores for each class. The highest score corresponds to the most likely class according to the weights. Each class is then mapped one-to-one onto a class label. That output is contained in a torch.Tensor with 1,000 elements, each representing the score associated with that class.
TorchVision project (https://github.com/pytorch/vision) 包含一些计算机视觉的神经网络架构,比如AlexNet、ResNet和Inception v3。
which contains a few of the best-performing neural network architectures for computer vision, such as AlexNet (http://mng.bz/lo6z), ResNet (https://arxiv.org/pdf/1512.03385.pdf), and Inception v3 (https://arxiv.org/pdf/1512.00567.pdf).
The predefined models can be found in torchvision.models
In[1]:
from torchvision import models
In[2]:
dir(models)
Out[2]:
['AlexNet',
'DenseNet',
'Inception3',
'ResNet',
'SqueezeNet',
'VGG',
...
'alexnet',
'densenet',
'densenet121',
...
'resnet',
'resnet101',
'resnet152',
...
]
Python模块问题:ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘
TorchVision project中大写字母开头的是模型类,而小写字母开头的是工厂函数。比如:
创建AlexNet类实例
In[3]:
alexnet = models.AlexNet()
创建的实例是未配置未训练的。the network is uninitialized: its weights, the numbers by which inputs are added and multiplied, have not been trained on anything—the network itself is a blank (or rather, random) slate
创建ResNet类实例
We’ll pass an argument that will instruct the function to download the weights of resnet101 trained on the ImageNet
dataset, with 1.2 million images and 1,000 categories: 加载了权重的101层卷积神经网络的resnet实例
In[4]:
resnet = models.resnet101(pretrained=True)
In[5]:
resnet
Out[5]:
ResNet(
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3),
bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True,
track_running_stats=True)
(relu): ReLU(inplace)
(maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1,
ceil_mode=False)
(layer1): Sequential(
(0): Bottleneck(
...
)
)
(avgpool): AvgPool2d(kernel_size=7, stride=1, padding=0)
(fc): Linear(in_features=2048, out_features=1000, bias=True)
)
对图像需要进行预处理,比如大小和值的范围要合法。torchvision模块提供了转换,用于基础预处理的函数。
Before we can do that, however, we have to preprocess the input images so
they are the right size and so that their values (colors) sit roughly in the same numerical range. In order to do that, the torchvision module provides transforms, which
allow us to quickly define pipelines of basic preprocessing functions:
In[6]:
from torchvision import transforms
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)])