【代码】使用预训练的语义分割网络

本文介绍了如何使用PyTorch库中的fcn_resnet101模型对图像进行预处理、分类和可视化。作者展示了从IPYNB文件中读取图片,进行图像处理,最终输出类别预测的过程。
摘要由CSDN通过智能技术生成

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个类别

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超好的小白

没体验过打赏,能让我体验一次吗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值