mmsegmentation/tests/test_apis/test_rs_inferencer.py
zoulinxin 72e20a8854
[Feature] remote sensing inference (#3131)
## Motivation

Supports inference for ultra-large-scale remote sensing images.

## Modification

Add RSImageInference.py in demo.

## Use cases

Taking the inference of Vaihingen dataset images using PSPNet as an
example, the following settings are required:

**img**: Specify the path of the image.
**model**: Provide the configuration file for the model.
**checkpoint**: Specify the weight file for the model.
**out**: Set the output path for the results.
**batch_size**: Determine the batch size used during inference.
**win_size**: Specify the width and height(512x512) of the sliding
window.
**stride**: Set the stride(400x400) for sliding the window.
**thread(default: 1)**: Specify the number of threads to be used for
inference.
**Inference device (default: cuda:0)**: Specify the device for inference
(e.g., cuda:0 for CPU).

```shell
python demo/rs_image_inference.py demo/demo.png projects/pp_mobileseg/configs/pp_mobileseg/pp_mobileseg_mobilenetv3_2x16_80k_ade20k_512x512_tiny.py pp_mobileseg_mobilenetv3_2xb16_3rdparty-tiny_512x512-ade20k-a351ebf5.pth --batch-size 8 --device cpu --thread 2
```

---------

Co-authored-by: xiexinch <xiexinch@outlook.com>
2023-08-31 12:44:46 +08:00

74 lines
2.4 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
from unittest import TestCase
import numpy as np
from mmengine import ConfigDict, init_default_scope
from utils import * # noqa: F401, F403
from mmseg.apis import RSImage, RSInferencer
from mmseg.registry import MODELS
class TestRSImage(TestCase):
def test_read_whole_image(self):
init_default_scope('mmseg')
img_path = osp.join(
osp.dirname(__file__),
'../data/pseudo_loveda_dataset/img_dir/0.png')
rs_image = RSImage(img_path)
window_size = (16, 16)
rs_image.create_grids(window_size)
image_data = rs_image.read(rs_image.grids[0])
self.assertIsNotNone(image_data)
def test_write_image_data(self):
init_default_scope('mmseg')
img_path = osp.join(
osp.dirname(__file__),
'../data/pseudo_loveda_dataset/img_dir/0.png')
rs_image = RSImage(img_path)
window_size = (16, 16)
rs_image.create_grids(window_size)
data = np.random.random((16, 16)).astype(np.int8)
rs_image.write(data, rs_image.grids[0])
class TestRSInferencer(TestCase):
def test_read_and_inference(self):
init_default_scope('mmseg')
cfg_dict = dict(
model=dict(
type='InferExampleModel',
data_preprocessor=dict(type='SegDataPreProcessor'),
backbone=dict(type='InferExampleBackbone'),
decode_head=dict(type='InferExampleHead'),
test_cfg=dict(mode='whole')),
test_dataloader=dict(
dataset=dict(
type='ExampleDataset',
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations'),
dict(type='PackSegInputs')
])),
test_pipeline=[
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations'),
dict(type='PackSegInputs')
])
cfg = ConfigDict(cfg_dict)
model = MODELS.build(cfg.model)
model.cfg = cfg
inferencer = RSInferencer.from_model(model)
img_path = osp.join(
osp.dirname(__file__),
'../data/pseudo_loveda_dataset/img_dir/0.png')
rs_image = RSImage(img_path)
window_size = (16, 16)
stride = (16, 16)
inferencer.run(rs_image, window_size, stride)