Unverified Commit 3e083be9 authored by Masahiro Ogawa's avatar Masahiro Ogawa Committed by GitHub
Browse files

Feature/seginference dir (#213)

* fix installation procedure explanation of segmentation. This is mainly cuda related issues.

* let it runnable segmentation inference for image directory too.

* update segmentation readme for image directory processing.

* add new line
parent 18cfeadb
...@@ -4,3 +4,5 @@ ...@@ -4,3 +4,5 @@
__pycache__/ __pycache__/
classification/convertor/ classification/convertor/
segmentation/convertor/ segmentation/convertor/
checkpoint_dir/
demo/
...@@ -26,23 +26,28 @@ conda activate internimage ...@@ -26,23 +26,28 @@ conda activate internimage
the [official installation instructions](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html) the [official installation instructions](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html)
- Install `PyTorch>=1.10.0` and `torchvision>=0.9.0` with `CUDA>=10.2`: - Install `PyTorch>=1.10.0` and `torchvision>=0.9.0` with `CUDA>=10.2`:
For examples, to install torch==1.11 with CUDA==11.3: For examples, to install torch==1.11 with CUDA==11.3 and nvcc:
```bash ```bash
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/torch_stable.html conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch -y
conda install -c conda-forge cudatoolkit-dev=11.3 -y # to install nvcc
``` ```
- Install `timm==0.6.11` and `mmcv-full==1.5.0`: - Install other requirements:
note: conda opencv will break torchvision as not to support GPU, so we need to install opencv using pip.
```bash ```bash
pip install -U openmim conda install -c conda-forge termcolor yacs pyyaml scipy pip -y
mim install mmcv-full==1.5.0 pip install opencv-python
pip install timm==0.6.11 mmdet==2.28.1
``` ```
- Install other requirements: - Install `timm` and `mmcv-full` and `mmsegmentation':
```bash ```bash
pip install opencv-python termcolor yacs pyyaml scipy pip install -U openmim
mim install mmcv-full==1.5.0
mim install mmsegmentation==0.27.0
pip install timm==0.6.11 mmdet==2.28.1
``` ```
- Compile CUDA operators - Compile CUDA operators
...@@ -67,6 +72,7 @@ To evaluate our `InternImage` on ADE20K val, run: ...@@ -67,6 +72,7 @@ To evaluate our `InternImage` on ADE20K val, run:
```bash ```bash
sh dist_test.sh <config-file> <checkpoint> <gpu-num> --eval mIoU sh dist_test.sh <config-file> <checkpoint> <gpu-num> --eval mIoU
``` ```
You can download checkpoint files from [here](https://huggingface.co/OpenGVLab/InternImage/tree/fc1e4e7e01c3e7a39a3875bdebb6577a7256ff91). Then place it to segmentation/checkpoint_dir/seg.
For example, to evaluate the `InternImage-T` with a single GPU: For example, to evaluate the `InternImage-T` with a single GPU:
...@@ -103,7 +109,8 @@ GPUS=8 sh slurm_train.sh <partition> <job-name> configs/ade20k/upernet_internima ...@@ -103,7 +109,8 @@ GPUS=8 sh slurm_train.sh <partition> <job-name> configs/ade20k/upernet_internima
``` ```
### Image Demo ### Image Demo
To inference a single image like this: To inference a single/multiple image like this.
If you specify image containing directory instead of a single image, it will process all the images in the directory.:
``` ```
CUDA_VISIBLE_DEVICES=0 python image_demo.py \ CUDA_VISIBLE_DEVICES=0 python image_demo.py \
data/ade/ADEChallengeData2016/images/validation/ADE_val_00000591.jpg \ data/ade/ADEChallengeData2016/images/validation/ADE_val_00000591.jpg \
......
...@@ -11,11 +11,29 @@ from mmcv.runner import load_checkpoint ...@@ -11,11 +11,29 @@ from mmcv.runner import load_checkpoint
from mmseg.core import get_classes from mmseg.core import get_classes
import cv2 import cv2
import os.path as osp import os.path as osp
import os
def test_single_image(model, img_name, out_dir, color_palette, opacity):
result = inference_segmentor(model, img_name)
# show the results
if hasattr(model, 'module'):
model = model.module
img = model.show_result(img_name, result,
palette=color_palette,
show=False, opacity=opacity)
# save the results
mmcv.mkdir_or_exist(out_dir)
out_path = osp.join(out_dir, osp.basename(img_name))
cv2.imwrite(out_path, img)
print(f"Result is save at {out_path}")
def main(): def main():
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument('img', help='Image file') parser.add_argument('img', help='Image file or a directory contains images')
parser.add_argument('config', help='Config file') parser.add_argument('config', help='Config file')
parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('checkpoint', help='Checkpoint file')
parser.add_argument('--out', type=str, default="demo", help='out dir') parser.add_argument('--out', type=str, default="demo", help='out dir')
...@@ -34,7 +52,6 @@ def main(): ...@@ -34,7 +52,6 @@ def main():
args = parser.parse_args() args = parser.parse_args()
# build the model from a config file and a checkpoint file # build the model from a config file and a checkpoint file
model = init_segmentor(args.config, checkpoint=None, device=args.device) model = init_segmentor(args.config, checkpoint=None, device=args.device)
checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
if 'CLASSES' in checkpoint.get('meta', {}): if 'CLASSES' in checkpoint.get('meta', {}):
...@@ -42,18 +59,12 @@ def main(): ...@@ -42,18 +59,12 @@ def main():
else: else:
model.CLASSES = get_classes(args.palette) model.CLASSES = get_classes(args.palette)
# test a single image # check arg.img is directory of a single image.
result = inference_segmentor(model, args.img) if osp.isdir(args.img):
# show the results for img in os.listdir(args.img):
if hasattr(model, 'module'): test_single_image(model, osp.join(args.img, img), args.out, get_palette(args.palette), args.opacity)
model = model.module else:
img = model.show_result(args.img, result, test_single_image(model, args.img, args.out, get_palette(args.palette), args.opacity)
palette=get_palette(args.palette),
show=False, opacity=args.opacity)
mmcv.mkdir_or_exist(args.out)
out_path = osp.join(args.out, osp.basename(args.img))
cv2.imwrite(out_path, img)
print(f"Result is save at {out_path}")
if __name__ == '__main__': if __name__ == '__main__':
main() main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment