Ubuntu18环境下的 Swin-Transformer-Semantic-Segmentation(MMsegmentation)安装过程

windows 安装真的兼容性问题很大,换用Ubuntu后几分钟解决,严格安装按照以下版本一般都没问题

由于我没有ubuntu系统,所以我在矩池云上租了一个服务器,环境选择得是Cuda10.1作为基础环境

1、创建虚拟环境(租用服务器的就不用创建啦)

conda create -n open-mmlab python=3.7
conda activate open-mmlab

2、安装pytorch

pip install -U torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

3、安装mmcv-full

pip install mmcv-full==1.3.0+torch1.6.0+cu101 -f https://download.openmmlab.com/mmcv/dist/index.html

4、下载Swin-Transformer-Semantic-Segmentation

git clone https://github.com/SwinTransformer/Swin-Transformer-Semantic-Segmentation
cd Swin-Transformer-Semantic-Segmentation 
pip install -e .

5、检查一下你装的环境

# Check Pytorch installation
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())

# Check MMSegmentation installation
import mmseg
print(mmseg.__version__)

如果输出以下内容就大功告成了

1.6.0+cu101 True
0.11.0

6、一个简单测试 Swin-Transformer 是否能用的代码

测试之前,下载模型

终端执行:

!mkdir checkpoints
!wget https://github.com/SwinTransformer/storage/releases/download/v1.0.1/upernet_swin_tiny_patch4_window7_512x512.pth -P checkpoints

测试图片:

Ubuntu18环境下的 Swin-Transformer-Semantic-Segmentation(MMsegmentation)安装过程_第1张图片

测试代码:

# Check Pytorch installation
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())

# Check MMSegmentation installation
import mmseg
print(mmseg.__version__)

from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot
from mmseg.core.evaluation import get_palette

config_file = r'configs/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k.py'
checkpoint_file = r'checkpoints/upernet_swin_tiny_patch4_window7_512x512.pth'

model = init_segmentor(config_file, checkpoint_file, device='cuda:0')
print(model)

from scipy.io import loadmat
import csv
from PIL import Image

colors = loadmat('color150.mat')['colors']
# print(colors)
def create_visual_anno(anno):
    """"""

    # visualize
    visual_anno = np.zeros((anno.shape[0], anno.shape[1], 3), dtype=np.uint8)
    for i in range(visual_anno.shape[0]):  # i for h
        for j in range(visual_anno.shape[1]):
            color = colors[anno[i, j]]
            visual_anno[i, j, 0] = color[0]
            visual_anno[i, j, 1] = color[1]
            visual_anno[i, j, 2] = color[2]

    return visual_anno
img = 'demo/img.jpg'
result = inference_segmentor(model, img)
import numpy as np
result = torch.tensor(np.array(result))
result.squeeze_()
result = result.numpy()
im1 = create_visual_anno(result)
im1 = Image.fromarray(im1)
im1.save("your_file.jpeg")

执行完毕后项目文件夹下会有一张your_file.jpeg,打开后是以下内容:

Ubuntu18环境下的 Swin-Transformer-Semantic-Segmentation(MMsegmentation)安装过程_第2张图片

得到结果,这就说明我们Swin-Transformer语义分割环境配置成功了,之后就可以用于训练自己的数据和推理了。

特别注意得是使用矩池云配置环境得童鞋一定要记得保存环境,否则下次还得重新配置!!!

由于下载的模型是由ADE20K数据集,该数据集有150个物体。在显示时,为了方便将预测的label值转成不同的颜色,用到了color150.mat文件,里面定义的是不同实例对应得颜色值,需要得可以在以下链接下载:

https://download.csdn.net/download/qq_41964545/16781203

 

你可能感兴趣的:(Pytorch学习,python,深度学习,pytorch,图像处理)