Use mmcv, not mmcv-full, to install the full version, because it took me so long that I canceled the installation.
MMCV contains C++ and CUDA extensions, thus depending on PyTorch in a complex way. MIM solves such dependencies automatically and makes the installation easier.However, it is not a must.
To install MMCV with pip instead of MIM, please follow MMCV installation guides. This requires manually specifying a find-url based on PyTorch version and its CUDA version.
There are two ways to install mmsegmentation:
Option(a): If you use mmsegmentation as a dependency or third-party package, install it with pip:
1
!pip install mmsegmentation
Option(b): If you develop and run mmseg directly, install it from source:
The downloading will take several seconds or more, depending on your network environment. When it is done, you will find two files in your current folder.
# build the model from a config file and a checkpoint file model = init_model(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results img = 'demo/005.jpg'# or img = mmcv.imread(img), which will only load it once result = inference_model(model, img) # visualize the results in a new window show_result_pyplot(model, img, result, show=True) # or save the visualization results to image files # you can change the opacity of the painted segmentation map in (0, 1]. show_result_pyplot(model, img, result, show=True, out_file='result.jpg', opacity=0.5)
You can modify the code above to test a single image or a video, both of these options can verify that the installation was successful.
Run Swin Transformer for Semantic Segmentation on local environment
I have to close my Clash to excute the following commands successfully. And it takes very long time to download the file from the internet without a proxy.
Maybe you could try to add -i https://mirrors.aliyun.com/pypi/simple/ behind the install command next time.
ERROR: Could not build wheels for mmcv, which is required to install pyproject.toml-based projects solution by onexiaophai
Use pip installation regardless of mim.
Step 2. Install MMSegmentation.
Case a: If you develop and run mmseg directly, install it from source:
1 2 3
# using proxy of Clash to download this from github git clone -b main https://github.com/open-mmlab/mmsegmentation.git cd mmsegmentation
I closed the proxy of Clash for installation, and I successfully make the downloading process quicker by adding the aliyun mirror image source behind the install command.
1 2 3 4 5
# (openmmlab)...\mmsegmentation> pip install -v -e . -i https://mirrors.aliyun.com/pypi/simple/ # '-v' means verbose, or more output # '-e' means installing a project in editable mode, # thus any local modifications made to the code will take effect without reinstallation.
I chose to install from the source, the case a, so I can check the source code of models and menthods easily.
Case b: If you use mmsegmentation as a dependency or third-party package, install it with pip:
The Potsdam dataset is for urban semantic segmentation used in the 2D Semantic Labeling Contest - Potsdam.
The dataset can be requested at the challenge homepage. You will get a file named utf-8' 'Potsdam.zip (size: 13.3GB), and unzip this file to get a folder named Potsdam which contains 10 files:
In the default setting of mmsegmentation, it will generate 3456 images for training and 2016 images for validation.
In the 2_Ortho_RGB.zip file, it contains 38 pictures of size 6000x6000:
And every picture have been seplited into 12x12=144 patches of size 512x512. There are 38x144=5472=3456+2016 patches in total, in which 3456 patches are used for training and 2016 for validation.
Masks are single-channel images, and the comparison table of their values and categories is as follows:
The original color map of the mmseg split code for potsdam
import argparse import glob import math import os import os.path as osp import tempfile import zipfile from tqdm import tqdm
from PIL import Image import numpy as np
defget_parser(): parser = argparse.ArgumentParser( description='Convert potsdam dataset to mmsegmentation format') parser.add_argument('dataset_path', help='potsdam folder path') parser.add_argument('--tmp_dir', help='path of the temporary directory') parser.add_argument('-o', '--out_dir', help='output path') parser.add_argument( '--clip_size', type=int, help='clipped size of image after preparation', default=512) parser.add_argument( '--stride_size', type=int, help='stride of clipping original images', default=256) # args = parser.parse_args(arg_list) return parser
defclip_big_image(image_path, clip_save_dir, args, to_label=False): # Original image of Potsdam dataset is very large, thus pre-processing # of them is adopted. Given fixed clip size and stride size to generate # clipped image, the intersection of width and height is determined. # For example, given one 5120 x 5120 original image, the clip size is # 512 and stride size is 256, thus it would generate 20x20 = 400 images # whose size are all 512x512. # image = PIL.Image.open(image_path) image = Image.open(image_path) image = np.array(image)
for box in boxes: start_x, start_y, end_x, end_y = box clipped_image = image[start_y:end_y, start_x:end_x] if to_label else image[ start_y:end_y, start_x:end_x, :] idx_i, idx_j = osp.basename(image_path).split('_')[2:4]
# it takes too much of time to save clipped images in this way. # mmcv.imwrite( # clipped_image.astype(np.uint8), # osp.join( # clip_save_dir, # f'{idx_i}_{idx_j}_{start_x}_{start_y}_{end_x}_{end_y}.png'))
for zipp in zipp_list: with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir: # tmp_dir changes in every loop zip_file = zipfile.ZipFile(zipp) zip_file.extractall(tmp_dir) # Check whether the *.tif files are unziped to current directory or a sub directory src_path_list = glob.glob(os.path.join(tmp_dir, '*.tif')) # if len(src_path_list)==0, it means *.tif are extracted to a sub directory rather than current directory directly ifnotlen(src_path_list): sub_tmp_dir = os.path.join(tmp_dir, os.listdir(tmp_dir)[0]) src_path_list = glob.glob(os.path.join(sub_tmp_dir, '*.tif'))
Note: You need to join the challenge firstly, then you will see Abdomen and Cervix in the Files directory, which are private and invisible if you have not joined the challenge.
Just downloading the RawData.zip (1.531GB) is enough.