{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# !pip install install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html\n\n# !pip install install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.12.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:23:44.459068Z","iopub.execute_input":"2023-06-14T07:23:44.460053Z","iopub.status.idle":"2023-06-14T07:24:26.316136Z","shell.execute_reply.started":"2023-06-14T07:23:44.460015Z","shell.execute_reply":"2023-06-14T07:24:26.315000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchaudio\nimport torchvision\nprint(torch.__version__)\nprint(torchaudio.__version__)\nprint(torchvision.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:25:36.051712Z","iopub.execute_input":"2023-06-14T07:25:36.052424Z","iopub.status.idle":"2023-06-14T07:25:36.237724Z","shell.execute_reply.started":"2023-06-14T07:25:36.052388Z","shell.execute_reply":"2023-06-14T07:25:36.236707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U openmim\n!mim install mmengine\n!mim install mmcv","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:27:53.636001Z","iopub.execute_input":"2023-06-14T07:27:53.636390Z","iopub.status.idle":"2023-06-14T07:28:40.320636Z","shell.execute_reply.started":"2023-06-14T07:27:53.636352Z","shell.execute_reply":"2023-06-14T07:28:40.319457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install opencv-python pillow matplotlib seaborn tqdm pytorch-lightning 'mmdet>=3.0.0rc1'","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:28:53.554647Z","iopub.execute_input":"2023-06-14T07:28:53.555067Z","iopub.status.idle":"2023-06-14T07:29:30.846366Z","shell.execute_reply.started":"2023-06-14T07:28:53.555031Z","shell.execute_reply":"2023-06-14T07:29:30.845226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 删掉原有的 mmsegmentation 文件夹（如有）\n!rm -rf mmsegmentation","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:29:38.447773Z","iopub.execute_input":"2023-06-14T07:29:38.448184Z","iopub.status.idle":"2023-06-14T07:29:39.412102Z","shell.execute_reply.started":"2023-06-14T07:29:38.448153Z","shell.execute_reply":"2023-06-14T07:29:39.410734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 从 github 上下载最新的 mmsegmentation 源代码\n!git clone https://github.com/open-mmlab/mmsegmentation.git -b dev-1.x","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:29:41.676003Z","iopub.execute_input":"2023-06-14T07:29:41.676438Z","iopub.status.idle":"2023-06-14T07:29:45.243446Z","shell.execute_reply.started":"2023-06-14T07:29:41.676403Z","shell.execute_reply":"2023-06-14T07:29:45.242272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 进入主目录\nimport os\nos.chdir('mmsegmentation')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:29:48.643551Z","iopub.execute_input":"2023-06-14T07:29:48.643973Z","iopub.status.idle":"2023-06-14T07:29:48.650145Z","shell.execute_reply.started":"2023-06-14T07:29:48.643936Z","shell.execute_reply":"2023-06-14T07:29:48.648763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -v -e .","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:29:51.890233Z","iopub.execute_input":"2023-06-14T07:29:51.891150Z","iopub.status.idle":"2023-06-14T07:30:07.311590Z","shell.execute_reply.started":"2023-06-14T07:29:51.891106Z","shell.execute_reply":"2023-06-14T07:30:07.310267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# 创建 checkpoint 文件夹，用于存放预训练模型权重文件\nos.mkdir('checkpoint')\n\n# 创建 outputs 文件夹，用于存放预测结果\nos.mkdir('outputs')\n\n# 创建 data 文件夹，用于存放图片和视频素材\nos.mkdir('data')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:32:48.081930Z","iopub.execute_input":"2023-06-14T07:32:48.082345Z","iopub.status.idle":"2023-06-14T07:32:48.089007Z","shell.execute_reply.started":"2023-06-14T07:32:48.082313Z","shell.execute_reply":"2023-06-14T07:32:48.087797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 检查 Pytorch\nimport torch, torchvision\nprint('Pytorch 版本', torch.__version__)\nprint('CUDA 是否可用',torch.cuda.is_available())","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:32:50.326704Z","iopub.execute_input":"2023-06-14T07:32:50.327692Z","iopub.status.idle":"2023-06-14T07:32:50.333767Z","shell.execute_reply.started":"2023-06-14T07:32:50.327658Z","shell.execute_reply":"2023-06-14T07:32:50.332580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 检查 mmcv\nimport mmcv\n# from mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint('MMCV版本', mmcv.__version__)\n# print('CUDA版本', get_compiling_cuda_version())\n# print('编译器版本', get_compiler_version())","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:33:41.920408Z","iopub.execute_input":"2023-06-14T07:33:41.920783Z","iopub.status.idle":"2023-06-14T07:33:41.926469Z","shell.execute_reply.started":"2023-06-14T07:33:41.920753Z","shell.execute_reply":"2023-06-14T07:33:41.925452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 检查 mmsegmentation\nimport mmseg\n# from mmseg.utils import register_all_modules\n# from mmseg.apis import inference_model, init_model\nprint('mmsegmentation版本', mmseg.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:34:56.415731Z","iopub.execute_input":"2023-06-14T07:34:56.416446Z","iopub.status.idle":"2023-06-14T07:34:56.421617Z","shell.execute_reply.started":"2023-06-14T07:34:56.416413Z","shell.execute_reply":"2023-06-14T07:34:56.420447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -V","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:34:59.555416Z","iopub.execute_input":"2023-06-14T07:34:59.555769Z","iopub.status.idle":"2023-06-14T07:35:00.515375Z","shell.execute_reply.started":"2023-06-14T07:34:59.555741Z","shell.execute_reply":"2023-06-14T07:35:00.513741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmcv.ops import get_compiling_cuda_version, get_compiler_version\n\n# print('CUDA版本', get_compiling_cuda_version())\n# print('编译器版本', get_compiler_version())\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:35:36.599602Z","iopub.execute_input":"2023-06-14T07:35:36.600053Z","iopub.status.idle":"2023-06-14T07:35:36.729244Z","shell.execute_reply.started":"2023-06-14T07:35:36.600015Z","shell.execute_reply":"2023-06-14T07:35:36.727830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmseg.utils import register_all_modules\nfrom mmseg.apis import inference_model, init_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:35:40.758024Z","iopub.execute_input":"2023-06-14T07:35:40.758394Z","iopub.status.idle":"2023-06-14T07:35:41.215576Z","shell.execute_reply.started":"2023-06-14T07:35:40.758360Z","shell.execute_reply":"2023-06-14T07:35:41.213792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 伦敦街景图片\n!wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20220713-mmdetection/images/street_uk.jpeg -P data","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:55:36.348338Z","iopub.execute_input":"2023-06-14T07:55:36.348730Z","iopub.status.idle":"2023-06-14T07:55:40.987421Z","shell.execute_reply.started":"2023-06-14T07:55:36.348700Z","shell.execute_reply":"2023-06-14T07:55:40.985888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nImage.open('data/street_uk.jpeg')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:56:08.942753Z","iopub.execute_input":"2023-06-14T07:56:08.943618Z","iopub.status.idle":"2023-06-14T07:56:10.611258Z","shell.execute_reply.started":"2023-06-14T07:56:08.943580Z","shell.execute_reply":"2023-06-14T07:56:10.608945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python demo/image_demo.py \\\n        data/street_uk.jpeg \\\n        configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py \\\n        https://download.openmmlab.com/mmsegmentation/v0.5/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth \\\n        --out-file outputs/B1_uk_pspnet.jpg \\\n        --device cuda:0 \\\n        --opacity 0.5","metadata":{"execution":{"iopub.status.busy":"2023-06-14T07:57:56.604705Z","iopub.execute_input":"2023-06-14T07:57:56.605733Z","iopub.status.idle":"2023-06-14T07:58:00.683055Z","shell.execute_reply.started":"2023-06-14T07:57:56.605697Z","shell.execute_reply":"2023-06-14T07:58:00.681859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}