P274书上的代码,这里是从ipynb文件中按顺序复制来的:
使用到的图片如下:
代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import PIL
import torch
from torchvision import transforms
import torchvision
model = torchvision.models.segmentation.fcn_resnet101(pretrained=True)
model.eval()
## 读取照片
image = PIL.Image.open("Zdata/img1.jpg")
# 显示图像
plt.imshow(image)
plt.show()
## 图像预处理,转为0-1之间,标准化处理
image_transf = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean = [0.485, 0.456,0.406],
std = [0.229, 0.224, 0.225])
])
image_tensor = image_transf(image).unsqueeze(0)
# 将图像丢入模型进行处理
output = model(image_tensor)["out"]
# 查看输出张量的形状
print("Output shape:", output.shape)
## 将输出转化为二维图像
outputarg = torch.argmax(output.squeeze(),dim=0).numpy()
outputarg
#对得到的输出结果进行编码
def decode_segmaps(image,label_colors,nc=21):
"""函数将输出的2D图像,会将不同的类编码为不同的颜色"""
r = np.zeros_like(image).astype(np.uint8)
g = np.zeros_like(image).astype(np.uint8)
b = np.zeros_like(image).astype(np.uint8)
for cla in range(0,nc):
idx = image ==cla
r[idx] = label_colors[cla ,0]
g[idx] = label_colors[cla ,1]
b[idx] = label_colors[cla ,2]
rgbimage = np.stack([r,g,b],axis=2)
return rgbimage
label_colors = np.array([(0,0,0),
(128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128),
(0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0),
(192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128),
(0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)])
outputrgb = decode_segmaps(outputarg,label_colors)
plt.figure(figsize=(20,8))
plt.subplot(1,2,1)
plt.imshow(image)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(outputrgb)
plt.axis("off")
plt.subplots_adjust(wspace=0.5)
plt.show()
output_probs = torch.tensor([[0.1, 0.6, 0.3],
[0.4, 0.2, 0.7]])
# 获取每个样本预测的类别索引
predicted_classes = torch.argmax(output_probs, dim=1)
print(predicted_classes)
tensor([1, 2])
注:
此处使用torch.argmax()
得到的是21个类别