import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet50', pretrained=True)
# or any of these variants
# model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_resnet101', pretrained=True)
# model = torch.hub.load('pytorch/vision:v0.10.0', 'deeplabv3_mobilenet_v3_large', pretrained=True)
model.eval()
所有预训练模型期望输入图像以相同方式进行归一化,即形状为 (N, 3, H, W)
的 3 通道 RGB 图像的小批量,其中 N
是图像数量,H
和 W
预期至少为 224
像素。图像必须加载到 [0, 1]
范围,然后使用均值 mean = [0.485, 0.456, 0.406]
和标准差 std = [0.229, 0.224, 0.225]
进行归一化。
模型返回一个 OrderedDict
,包含两个与输入 Tensor 具有相同高度和宽度但有 21 个类别的 Tensor。output['out']
包含语义掩码,而 output['aux']
包含每个像素的辅助损失值。在推理模式下,output['aux']
没有用。因此,output['out']
的形状为 (N, 21, H, W)
。更多文档可在此处找到。
# Download an example image from the pytorch website
import urllib
url, filename = ("https://github.com/pytorch/hub/raw/master/images/deeplab1.png", "deeplab1.png")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
input_image = input_image.convert("RGB")
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
# move the input and model to GPU for speed if available
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')
with torch.no_grad():
output = model(input_batch)['out'][0]
output_predictions = output.argmax(0)
这里的输出形状为 (21, H, W)
,在每个位置,都有对应于每个类别预测的未归一化概率。要获取每个类别的最大预测,然后将其用于下游任务,可以使用 output_predictions = output.argmax(0)
。
以下是一个绘制预测结果的小片段,其中每种颜色对应一个类别(参见左侧的可视化图像)。
# create a color pallette, selecting a color for each class
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
colors = torch.as_tensor([i for i in range(21)])[:, None] * palette
colors = (colors % 255).numpy().astype("uint8")
# plot the semantic segmentation predictions of 21 classes in each color
r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
r.putpalette(colors)
import matplotlib.pyplot as plt
plt.imshow(r)
# plt.show()
模型描述
Deeplabv3-ResNet 由使用 ResNet-50 或 ResNet-101 作为骨干网络的 Deeplabv3 模型构建而成。Deeplabv3-MobileNetV3-Large 由使用 MobileNetV3 large 作为骨干网络的 Deeplabv3 模型构建而成。预训练模型已在 COCO train2017 数据集的子集上进行训练,涵盖 Pascal VOC 数据集中存在的 20 个类别。
在 COCO val2017 数据集上评估的预训练模型的准确率如下所示。
模型结构 | 平均 IOU | 全局像素精度 |
---|---|---|
deeplabv3_resnet50 | 66.4 | 92.4 |
deeplabv3_resnet101 | 67.4 | 92.4 |
deeplabv3_mobilenet_v3_large | 60.3 | 91.2 |