两种调用mmseg中模型的简单代码

from mmseg.apis import MMSegInferencer


inferencer = MMSegInferencer(model='pspnet_r101b-d8_4xb2-80k_cityscapes-512x1024')
name = "/home//img/IMG_1360.jpg"
# Inference
inferencer(name, show=True)
from mmengine.config import Config
from mmengine.registry import MODELS
from mmengine.registry import init_default_scope
from mmengine.runner.checkpoint import (_load_checkpoint,
                                        _load_checkpoint_to_model)

import cv2
import torch
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt


cfg = Config.fromfile("/home/mmsegmentation/configs/pspnet/pspnet_r101b-d8_4xb2-80k_cityscapes-512x1024.py")
weights = "/home/mmsegmentation/my_checkpoints/pspnet/my_train_temp.pth"
device = "cuda"

init_default_scope('mmseg')
model = MODELS.build(cfg.model)

model.cfg = cfg
checkpoint = _load_checkpoint(weights, map_location='cpu')
_load_checkpoint_to_model(model, checkpoint)
model.to(device)
model.eval()

name = "/home/img/IMG_1360.jpg"
width = 640
height = 480
img0 = cv2.imread(name)
img0 = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
img0 = cv2.resize(img0, (width, height))

mean = np.array([0.485, 0.456, 0.406]) * 255
std = np.array([0.229, 0.224, 0.225]) * 255
# 对图像进行归一化
img = (img0 - mean) / std

img = torch.from_numpy(img).unsqueeze(0).to(torch.float32).to(device)
img = img.permute(0, 3, 1, 2)

output = model(img)

resized_tensor = F.interpolate(output, size=(height, width), mode='bilinear', align_corners=False)

max_indices = torch.argmax(resized_tensor, dim=1)
result = torch.squeeze(max_indices, dim=1)
result = result.cpu().numpy()
result = result.astype(np.uint8)
result = result.squeeze()


plt.subplot(121)
plt.imshow(img0)
plt.subplot(122)
plt.imshow(result, cmap='viridis')
plt.title('output'), plt.xticks([]), plt.yticks([])
plt.show()

要修改mmsegmentation模型,您可以按照以下步骤进行操作: 1. 打开mmseg/core/optimizer/__init__.py文件,并在其添加您的新模块。例如,如果您的新模块名为MyOptimizer,可以添加以下代码:`from .my_optimizer import MyOptimizer`。这样,注册器(registry)将会发现这个新的模块并添加它。\[1\] 2. 在配置文件找到optimizer域,并根据您的需求进行修改。例如,如果您想使用ADAM优化器,可以将optimizer字段修改为`optimizer = dict(type='Adam', lr=0.0003, weight_decay=0.0001)`。请注意,使用ADAM优化器可能会导致数值表现下降。\[2\] 3. 如果您想修改特定模型(如DANet),可以找到对应的配置文件。例如,如果您想修改danet_r50-d8_512x512_20k_voc12aug.py配置文件,可以打开该文件并进行修改。 - 在文件找到_base_字段,并根据您的需求修改其的路径。例如,您可以修改骨架模型的路径、数据集的路径等。\[3\] - 在model字段,根据您的需求修改decode_head和auxiliary_head的num_classes参数,以适应您的分类类别数。\[3\] 请根据您的具体需求进行相应的修改。希望对您有所帮助! #### 引用[.reference_title] - *1* *2* [MMsegmentation教程 4: 自定义模型](https://blog.csdn.net/yanqianglifei/article/details/122598010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [mmsegmentation教程1:自定义数据集、config文件修改、训练教程](https://blog.csdn.net/weixin_42748439/article/details/122845505)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值