From 7b5065cc6922d15cf2aad14f3a506dc131036a79 Mon Sep 17 00:00:00 2001 From: KSkun Date: Wed, 18 May 2022 17:48:43 +0800 Subject: [PATCH 1/2] fix: fix typo, remove ending blank lines and outputs in notebook --- SparseInst.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SparseInst.ipynb b/SparseInst.ipynb index ddaa273..a9c576b 100644 --- a/SparseInst.ipynb +++ b/SparseInst.ipynb @@ -1 +1 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"SparseInst.ipynb","provenance":[],"collapsed_sections":["-RKGUL3zCvt_"],"mount_file_id":"1C5IoDr5FUIljYflQD67G9fxuDqCwcfy1","authorship_tag":"ABX9TyNsJUuDC3aUUGN30rZTquYI"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"markdown","source":[""],"metadata":{"id":"axCOSYen3ro5"}},{"cell_type":"markdown","source":["# mount drive"],"metadata":{"id":"mhca95Zr_kCm"}},{"cell_type":"markdown","source":[""],"metadata":{"id":"IhWCBdtL5wlk"}},{"cell_type":"code","source":["import os\n","import sys\n","from google.colab import drive\n","drive.mount('/content/drive')\n","path = \"/content/drive/My Drive\"\n","\n","os.chdir(path)\n","os.listdir(path)\n","sys.path.append(\"/content/drive/\")\n"],"metadata":{"id":"F01WX-ON4K2l"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# prepare SpareInst code\n","!pwd\n","os.chdir('/content/')\n","!git clone https://github.com/hustvl/SparseInst.git\n","\n","sys.path.append(\"/content/SparseInst/sparseinst\")"],"metadata":{"id":"owvzOlpN81QS"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# prepare model\n","# upload from your local or load from your google drive\n","!cp '/content/drive/My Drive/SparseInst_Colab/sparse_inst_r50vd_giam_aug_8bc5b3.pth' '/content/SparseInst'"],"metadata":{"id":"J7VQ-7NcMwBU"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# unzip coco val2017 dataset, this will cost a little time\n","# or you can use your own images\n","!pwd\n","!unzip -uq '/content/drive/My Drive/data/coco/val2017.zip' -d '/content'"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"2t94Cbj6CxbU","executionInfo":{"status":"ok","timestamp":1651148757167,"user_tz":-480,"elapsed":18037,"user":{"displayName":"范子蔚","userId":"15551234769924635327"}},"outputId":"8e841d41-4436-4200-fc64-5f55c18249ce"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content\n"]}]},{"cell_type":"markdown","source":["# 安装环境"],"metadata":{"id":"fbNKjLN85Y3t"}},{"cell_type":"code","source":["import os\n","# change to workdirectory\n","os.chdir('/content/SparseInst')\n","# %cd /content/drive/My Drive/AlexNet\n","!pwd\n","!ls"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NPfrLm71DXZW","executionInfo":{"status":"ok","timestamp":1651148787149,"user_tz":-480,"elapsed":404,"user":{"displayName":"范子蔚","userId":"15551234769924635327"}},"outputId":"ab8359a8-b099-442a-d73b-1d9f08de8f9e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/SparseInst\n","configs README.md sparse_inst_r50vd_giam_aug_8bc5b3.pth\n","demo.py resources test_net.py\n","LICENCE sparseinst train_net.py\n"]}]},{"cell_type":"code","source":["!pip install timm\n","!pip install pyyaml==5.1"],"metadata":{"id":"Z7UtV1h0HMo-"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Install detectron2--v0.3**"],"metadata":{"id":"zTZTr5e7Ab0Q"}},{"cell_type":"code","source":["!pwd\n","!git clone https://github.com/facebookresearch/detectron2.git"],"metadata":{"id":"smaim4McQHe5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["os.chdir('/content/SparseInst/detectron2')\n","!git checkout tags/v0.3"],"metadata":{"id":"oWwMeY0GJYEx"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# import detectron2\n","# print(detectron2.version)"],"metadata":{"id":"Z9HRqaSX5Re1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# ATTENTION HERE !!!\n","\n","# Before run the code, please modify a line of code in detetron2\n","# See issue#18 in husvvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# Then run setup.py, otherwise there will be error occured.\n","\n","# This bug will be fixed soon\n","\n","# build detectron2\n","os.chdir('/content/SparseInst/detectron2')\n","!python setup.py build develop\n","\n","sys.path.append(\"/content/SparseInst/detectron2/\")"],"metadata":{"id":"3Uf4BhxO2nkO"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# remove built detectron2\n","# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"],"metadata":{"id":"ipWjzaECZwPU"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Fix package bug"],"metadata":{"id":"5BqQzqI0SsaA"}},{"cell_type":"code","source":["# The following operartion may be much confusing, but\n","# there will be some \"import errors\" occur if not do this\n","\n","# 1. reinstall fvcore\n","!pip uninstall fvcore\n","!pip install fvcore"],"metadata":{"id":"hN1AJSBlIbHW"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# 2. reinstall iopath\n","!pip uninstall iopath\n","!pip install iopath"],"metadata":{"id":"Neo1NrnDSGZ6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# 3. reinstall portalocker\n","!pip uninstall portalocker\n","!pip install portalocker"],"metadata":{"id":"jfj6BsceSRXK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# 4. reinstall yacs\n","!pip uninstall yacs\n","!pip install yacs"],"metadata":{"id":"xQwX_PQhSdc-"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# !pip list fvcore\n","# os.chdir('/content/SparseInst')\n","import fvcore"],"metadata":{"id":"ZSmhRrLMDCrr"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"],"metadata":{"id":"4zESNpt-SAri"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# SparseInst Demo"],"metadata":{"id":"jGT7Uyhi590J"}},{"cell_type":"code","source":["!nvidia-smi"],"metadata":{"id":"cAyIRrECIEVn"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Run demo with command line format\n"],"metadata":{"id":"-RKGUL3zCvt_"}},{"cell_type":"code","source":["os.chdir('/content/SparseInst')\n","!python demo.py --config-file configs/sparse_inst_r50vd_giam_aug.yaml --input ../_val2017_part/* --output results --opt MODEL.WEIGHTS sparse_inst_r50vd_giam_aug_8bc5b3.pth INPUT.MIN_SIZE_TEST 512"],"metadata":{"id":"burRHlTZEBeq"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["os.environ[\"PATH\"] = '/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin'"],"metadata":{"id":"kjKlo4iO8qDk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["os.chdir('/content/SparseInst')\n","import fvcore"],"metadata":{"id":"Wpuht-T4E3iQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["print(sys.path)\n","import numpy as np"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rQWV2ZUOGtpT","executionInfo":{"status":"ok","timestamp":1651115674638,"user_tz":-480,"elapsed":5,"user":{"displayName":"范子蔚","userId":"15551234769924635327"}},"outputId":"61c789f4-c1cf-42ad-e179-f87f4b92466c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['', '/content', '/env/python', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.7/dist-packages/IPython/extensions', '/root/.ipython', '/content/SparseInst/sparseinst', '/content/SparseInst/detectron2/', '/content/drive/']\n"]}]},{"cell_type":"code","source":["# The packs below may raise 'import error'\n","# uninstall then install may solve the problem but I haven't found the reason\n","\n","# import fvcore\n","# import iopath\n","# import portalocker\n","# import yacs"],"metadata":{"id":"XArYSkLiJhH3"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### Run with cell"],"metadata":{"id":"rdGin15pC8lz"}},{"cell_type":"code","source":["os.chdir('/content/SparseInst')\n","\n","# import libs\n","import argparse\n","import glob\n","import multiprocessing as mp\n","import os\n","import time\n","import cv2\n","import tqdm\n","from skimage import io\n","\n","from detectron2.config import get_cfg\n","from detectron2.data.detection_utils import read_image\n","from detectron2.utils.logger import setup_logger\n","\n","from sparseinst import VisualizationDemo, add_sparse_inst_config\n","\n"],"metadata":{"id":"oFGOYtzU4Xqu"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["WINDOW_NAME = \"COCO detections -- val2017\"\n","def proc_single(image_path, args, cfg, demo):\n"," # cfg = setup_cfg(args)\n","\n"," # demo = VisualizationDemo(cfg)\n","\n"," image_path = './val2017/000000003934.jpg'\n"," args.input = glob.glob(os.path.expanduser(image_path))\n","\n"," start_time = time.time()\n"," img = read_image(path, format=\"RGB\")\n"," predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n"," print(\n"," \"{}: {} in {:.2f}s\".format(\n"," path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," )\n"," )\n","\n"," if args.output:\n"," if os.path.isdir(args.output):\n"," assert os.path.isdir(args.output), args.output\n"," out_filename = os.path.join(\n"," args.output, os.path.basename(path))\n"," else:\n"," assert len(\n"," args.output) > 0, \"Please specify a directory with args.output\"\n"," out_filename = args.output\n"," visualized_output.save(out_filename)\n"," else:\n"," cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)"],"metadata":{"id":"d737hslZ4105"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["class parser():\n"," def __init__(self, image_path, output_dir='/content/SparseInst/results/'):\n"," # reset to your own model-config-file path\n"," self.config_file = '/content/SparseInst/configs/sparse_inst_r50vd_giam_aug.yaml'\n","\n"," self.opts = [\"MODEL.WEIGHTS\", \n"," # reset to your own model.pth path\n"," \"/content/SparseInst/sparse_inst_r50vd_giam_aug_8bc5b3.pth\",\n"," \"INPUT.MIN_SIZE_TEST\", \n"," 512]\n","\n"," self.input = image_path\n"," if not os.path.exists(output_dir):\n"," os.makedirs(output_dir)\n"," self.output = output_dir\n","\n"," # reset to your desired threshold\n"," self.confidence_threshold = 0.3\n","\n","\n","def _setup_cfg(args):\n"," cfg = get_cfg()\n"," add_sparse_inst_config(cfg)\n","\n"," cfg.merge_from_file(args.config_file)\n"," cfg.merge_from_list(args.opts)\n","\n"," cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold\n"," cfg.freeze()\n"," return cfg\n","\n","args = parser('/content/val2017/000000003934.jpg')\n","cfg = _setup_cfg(args)\n","\n","\n","demo = VisualizationDemo(cfg)"],"metadata":{"id":"U0JutUnfMpMH"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# ATTENTION HERE!\n","# Before run the code, please modify 2 lines of code in detetron2\n","# See issue#18, issue#10, issue#13 in husvvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# This bug will be fixed soon"],"metadata":{"id":"cY_PXqhYT98U"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# run the cell\n","# change the image path to your own\n","image_path = \"/content/drive/MyDrive/SparseInst_Colab/569021651119418_.pic_hd.jpg\"\n","args.input = image_path\n","\n","img = read_image(image_path, format=\"RGB\")\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(image_path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(image_path))\n","print(out_filename)\n","visualized_output.save(out_filename)"],"metadata":{"id":"yqduAWwM5GHf"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[""],"metadata":{"id":"Va9ereBCXUCj"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# run the cell\n","# change the image url to your own\n","img_src = \"http://n1image.hjfile.cn/shetuan/2017-05-17-1495016837-986-732.jpg\"\n","url_img = io.imread(img_src)\n","\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," url_img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(img_src,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(img_src))\n","print(out_filename)\n","visualized_output.save(out_filename)"],"metadata":{"id":"pM3UHL405ZsZ"},"execution_count":null,"outputs":[]}]} \ No newline at end of file +{"cells":[{"cell_type":"markdown","metadata":{"id":"mhca95Zr_kCm"},"source":["# mount drive"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"F01WX-ON4K2l"},"outputs":[],"source":["import os\n","import sys\n","from google.colab import drive\n","drive.mount('/content/drive')\n","path = \"/content/drive/My Drive\"\n","\n","os.chdir(path)\n","os.listdir(path)\n","sys.path.append(\"/content/drive/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"owvzOlpN81QS"},"outputs":[],"source":["# prepare SpareInst code\n","!pwd\n","os.chdir('/content/')\n","!git clone https://github.com/hustvl/SparseInst.git\n","\n","sys.path.append(\"/content/SparseInst/sparseinst\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J7VQ-7NcMwBU"},"outputs":[],"source":["# prepare model\n","# upload from your local or load from your google drive\n","!cp '/content/drive/My Drive/SparseInst_Colab/sparse_inst_r50vd_giam_aug_8bc5b3.pth' '/content/SparseInst'"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":18037,"status":"ok","timestamp":1651148757167,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"2t94Cbj6CxbU","outputId":"8e841d41-4436-4200-fc64-5f55c18249ce"},"outputs":[],"source":["# unzip coco val2017 dataset, this will cost a little time\n","# or you can use your own images\n","!pwd\n","!unzip -uq '/content/drive/My Drive/data/coco/val2017.zip' -d '/content'"]},{"cell_type":"markdown","metadata":{"id":"fbNKjLN85Y3t"},"source":["# 安装环境"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":404,"status":"ok","timestamp":1651148787149,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"NPfrLm71DXZW","outputId":"ab8359a8-b099-442a-d73b-1d9f08de8f9e"},"outputs":[],"source":["import os\n","# change to workdirectory\n","os.chdir('/content/SparseInst')\n","# %cd /content/drive/My Drive/AlexNet\n","!pwd\n","!ls"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z7UtV1h0HMo-"},"outputs":[],"source":["!pip install timm\n","!pip install pyyaml==5.1"]},{"cell_type":"markdown","metadata":{"id":"zTZTr5e7Ab0Q"},"source":["**Install detectron2--v0.3**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"smaim4McQHe5"},"outputs":[],"source":["!pwd\n","!git clone https://github.com/facebookresearch/detectron2.git"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oWwMeY0GJYEx"},"outputs":[],"source":["os.chdir('/content/SparseInst/detectron2')\n","!git checkout tags/v0.3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z9HRqaSX5Re1"},"outputs":[],"source":["# import detectron2\n","# print(detectron2.version)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3Uf4BhxO2nkO"},"outputs":[],"source":["# ATTENTION HERE !!!\n","\n","# Before run the code, please modify a line of code in detetron2\n","# See issue#18 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# Then run setup.py, otherwise there will be error occured.\n","\n","# This bug will be fixed soon\n","\n","# build detectron2\n","os.chdir('/content/SparseInst/detectron2')\n","!python setup.py build develop\n","\n","sys.path.append(\"/content/SparseInst/detectron2/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ipWjzaECZwPU"},"outputs":[],"source":["# remove built detectron2\n","# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"5BqQzqI0SsaA"},"source":["# Fix package bug"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hN1AJSBlIbHW"},"outputs":[],"source":["# The following operartion may be much confusing, but\n","# there will be some \"import errors\" occur if not do this\n","\n","# 1. reinstall fvcore\n","!pip uninstall fvcore\n","!pip install fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Neo1NrnDSGZ6"},"outputs":[],"source":["# 2. reinstall iopath\n","!pip uninstall iopath\n","!pip install iopath"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jfj6BsceSRXK"},"outputs":[],"source":["# 3. reinstall portalocker\n","!pip uninstall portalocker\n","!pip install portalocker"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xQwX_PQhSdc-"},"outputs":[],"source":["# 4. reinstall yacs\n","!pip uninstall yacs\n","!pip install yacs"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZSmhRrLMDCrr"},"outputs":[],"source":["# !pip list fvcore\n","# os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4zESNpt-SAri"},"outputs":[],"source":["# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"jGT7Uyhi590J"},"source":["# SparseInst Demo"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cAyIRrECIEVn"},"outputs":[],"source":["!nvidia-smi"]},{"cell_type":"markdown","metadata":{"id":"-RKGUL3zCvt_"},"source":["### Run demo with command line format\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"burRHlTZEBeq"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","!python demo.py --config-file configs/sparse_inst_r50vd_giam_aug.yaml --input ../_val2017_part/* --output results --opt MODEL.WEIGHTS sparse_inst_r50vd_giam_aug_8bc5b3.pth INPUT.MIN_SIZE_TEST 512"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kjKlo4iO8qDk"},"outputs":[],"source":["os.environ[\"PATH\"] = '/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Wpuht-T4E3iQ"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":5,"status":"ok","timestamp":1651115674638,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"rQWV2ZUOGtpT","outputId":"61c789f4-c1cf-42ad-e179-f87f4b92466c"},"outputs":[],"source":["print(sys.path)\n","import numpy as np"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XArYSkLiJhH3"},"outputs":[],"source":["# The packs below may raise 'import error'\n","# uninstall then install may solve the problem but I haven't found the reason\n","\n","# import fvcore\n","# import iopath\n","# import portalocker\n","# import yacs"]},{"cell_type":"markdown","metadata":{"id":"rdGin15pC8lz"},"source":["### Run with cell"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oFGOYtzU4Xqu"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","\n","# import libs\n","import argparse\n","import glob\n","import multiprocessing as mp\n","import os\n","import time\n","import cv2\n","import tqdm\n","from skimage import io\n","\n","from detectron2.config import get_cfg\n","from detectron2.data.detection_utils import read_image\n","from detectron2.utils.logger import setup_logger\n","\n","from sparseinst import VisualizationDemo, add_sparse_inst_config"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"d737hslZ4105"},"outputs":[],"source":["WINDOW_NAME = \"COCO detections -- val2017\"\n","def proc_single(image_path, args, cfg, demo):\n"," # cfg = setup_cfg(args)\n","\n"," # demo = VisualizationDemo(cfg)\n","\n"," image_path = './val2017/000000003934.jpg'\n"," args.input = glob.glob(os.path.expanduser(image_path))\n","\n"," start_time = time.time()\n"," img = read_image(path, format=\"RGB\")\n"," predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n"," print(\n"," \"{}: {} in {:.2f}s\".format(\n"," path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," )\n"," )\n","\n"," if args.output:\n"," if os.path.isdir(args.output):\n"," assert os.path.isdir(args.output), args.output\n"," out_filename = os.path.join(\n"," args.output, os.path.basename(path))\n"," else:\n"," assert len(\n"," args.output) > 0, \"Please specify a directory with args.output\"\n"," out_filename = args.output\n"," visualized_output.save(out_filename)\n"," else:\n"," cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U0JutUnfMpMH"},"outputs":[],"source":["class parser():\n"," def __init__(self, image_path, output_dir='/content/SparseInst/results/'):\n"," # reset to your own model-config-file path\n"," self.config_file = '/content/SparseInst/configs/sparse_inst_r50vd_giam_aug.yaml'\n","\n"," self.opts = [\"MODEL.WEIGHTS\", \n"," # reset to your own model.pth path\n"," \"/content/SparseInst/sparse_inst_r50vd_giam_aug_8bc5b3.pth\",\n"," \"INPUT.MIN_SIZE_TEST\", \n"," 512]\n","\n"," self.input = image_path\n"," if not os.path.exists(output_dir):\n"," os.makedirs(output_dir)\n"," self.output = output_dir\n","\n"," # reset to your desired threshold\n"," self.confidence_threshold = 0.3\n","\n","\n","def _setup_cfg(args):\n"," cfg = get_cfg()\n"," add_sparse_inst_config(cfg)\n","\n"," cfg.merge_from_file(args.config_file)\n"," cfg.merge_from_list(args.opts)\n","\n"," cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold\n"," cfg.freeze()\n"," return cfg\n","\n","args = parser('/content/val2017/000000003934.jpg')\n","cfg = _setup_cfg(args)\n","\n","\n","demo = VisualizationDemo(cfg)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cY_PXqhYT98U"},"outputs":[],"source":["# ATTENTION HERE!\n","# Before run the code, please modify 2 lines of code in detetron2\n","# See issue#18, issue#10, issue#13 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# This bug will be fixed soon"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"yqduAWwM5GHf"},"outputs":[],"source":["# run the cell\n","# change the image path to your own\n","image_path = \"/content/drive/MyDrive/SparseInst_Colab/569021651119418_.pic_hd.jpg\"\n","args.input = image_path\n","\n","img = read_image(image_path, format=\"RGB\")\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(image_path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(image_path))\n","print(out_filename)\n","visualized_output.save(out_filename)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pM3UHL405ZsZ"},"outputs":[],"source":["# run the cell\n","# change the image url to your own\n","img_src = \"http://n1image.hjfile.cn/shetuan/2017-05-17-1495016837-986-732.jpg\"\n","url_img = io.imread(img_src)\n","\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," url_img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(img_src,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(img_src))\n","print(out_filename)\n","visualized_output.save(out_filename)"]}],"metadata":{"accelerator":"GPU","colab":{"authorship_tag":"ABX9TyNsJUuDC3aUUGN30rZTquYI","collapsed_sections":["-RKGUL3zCvt_"],"mount_file_id":"1C5IoDr5FUIljYflQD67G9fxuDqCwcfy1","name":"SparseInst.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0} From dcf3876da61735273e9d3bf844f261c20d8963be Mon Sep 17 00:00:00 2001 From: KSkun Date: Wed, 18 May 2022 17:52:53 +0800 Subject: [PATCH 2/2] chore: unify title --- SparseInst.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SparseInst.ipynb b/SparseInst.ipynb index a9c576b..edd79be 100644 --- a/SparseInst.ipynb +++ b/SparseInst.ipynb @@ -1 +1 @@ -{"cells":[{"cell_type":"markdown","metadata":{"id":"mhca95Zr_kCm"},"source":["# mount drive"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"F01WX-ON4K2l"},"outputs":[],"source":["import os\n","import sys\n","from google.colab import drive\n","drive.mount('/content/drive')\n","path = \"/content/drive/My Drive\"\n","\n","os.chdir(path)\n","os.listdir(path)\n","sys.path.append(\"/content/drive/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"owvzOlpN81QS"},"outputs":[],"source":["# prepare SpareInst code\n","!pwd\n","os.chdir('/content/')\n","!git clone https://github.com/hustvl/SparseInst.git\n","\n","sys.path.append(\"/content/SparseInst/sparseinst\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J7VQ-7NcMwBU"},"outputs":[],"source":["# prepare model\n","# upload from your local or load from your google drive\n","!cp '/content/drive/My Drive/SparseInst_Colab/sparse_inst_r50vd_giam_aug_8bc5b3.pth' '/content/SparseInst'"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":18037,"status":"ok","timestamp":1651148757167,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"2t94Cbj6CxbU","outputId":"8e841d41-4436-4200-fc64-5f55c18249ce"},"outputs":[],"source":["# unzip coco val2017 dataset, this will cost a little time\n","# or you can use your own images\n","!pwd\n","!unzip -uq '/content/drive/My Drive/data/coco/val2017.zip' -d '/content'"]},{"cell_type":"markdown","metadata":{"id":"fbNKjLN85Y3t"},"source":["# 安装环境"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":404,"status":"ok","timestamp":1651148787149,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"NPfrLm71DXZW","outputId":"ab8359a8-b099-442a-d73b-1d9f08de8f9e"},"outputs":[],"source":["import os\n","# change to workdirectory\n","os.chdir('/content/SparseInst')\n","# %cd /content/drive/My Drive/AlexNet\n","!pwd\n","!ls"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z7UtV1h0HMo-"},"outputs":[],"source":["!pip install timm\n","!pip install pyyaml==5.1"]},{"cell_type":"markdown","metadata":{"id":"zTZTr5e7Ab0Q"},"source":["**Install detectron2--v0.3**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"smaim4McQHe5"},"outputs":[],"source":["!pwd\n","!git clone https://github.com/facebookresearch/detectron2.git"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oWwMeY0GJYEx"},"outputs":[],"source":["os.chdir('/content/SparseInst/detectron2')\n","!git checkout tags/v0.3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z9HRqaSX5Re1"},"outputs":[],"source":["# import detectron2\n","# print(detectron2.version)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3Uf4BhxO2nkO"},"outputs":[],"source":["# ATTENTION HERE !!!\n","\n","# Before run the code, please modify a line of code in detetron2\n","# See issue#18 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# Then run setup.py, otherwise there will be error occured.\n","\n","# This bug will be fixed soon\n","\n","# build detectron2\n","os.chdir('/content/SparseInst/detectron2')\n","!python setup.py build develop\n","\n","sys.path.append(\"/content/SparseInst/detectron2/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ipWjzaECZwPU"},"outputs":[],"source":["# remove built detectron2\n","# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"5BqQzqI0SsaA"},"source":["# Fix package bug"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hN1AJSBlIbHW"},"outputs":[],"source":["# The following operartion may be much confusing, but\n","# there will be some \"import errors\" occur if not do this\n","\n","# 1. reinstall fvcore\n","!pip uninstall fvcore\n","!pip install fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Neo1NrnDSGZ6"},"outputs":[],"source":["# 2. reinstall iopath\n","!pip uninstall iopath\n","!pip install iopath"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jfj6BsceSRXK"},"outputs":[],"source":["# 3. reinstall portalocker\n","!pip uninstall portalocker\n","!pip install portalocker"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xQwX_PQhSdc-"},"outputs":[],"source":["# 4. reinstall yacs\n","!pip uninstall yacs\n","!pip install yacs"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZSmhRrLMDCrr"},"outputs":[],"source":["# !pip list fvcore\n","# os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4zESNpt-SAri"},"outputs":[],"source":["# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"jGT7Uyhi590J"},"source":["# SparseInst Demo"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cAyIRrECIEVn"},"outputs":[],"source":["!nvidia-smi"]},{"cell_type":"markdown","metadata":{"id":"-RKGUL3zCvt_"},"source":["### Run demo with command line format\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"burRHlTZEBeq"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","!python demo.py --config-file configs/sparse_inst_r50vd_giam_aug.yaml --input ../_val2017_part/* --output results --opt MODEL.WEIGHTS sparse_inst_r50vd_giam_aug_8bc5b3.pth INPUT.MIN_SIZE_TEST 512"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kjKlo4iO8qDk"},"outputs":[],"source":["os.environ[\"PATH\"] = '/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Wpuht-T4E3iQ"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":5,"status":"ok","timestamp":1651115674638,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"rQWV2ZUOGtpT","outputId":"61c789f4-c1cf-42ad-e179-f87f4b92466c"},"outputs":[],"source":["print(sys.path)\n","import numpy as np"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XArYSkLiJhH3"},"outputs":[],"source":["# The packs below may raise 'import error'\n","# uninstall then install may solve the problem but I haven't found the reason\n","\n","# import fvcore\n","# import iopath\n","# import portalocker\n","# import yacs"]},{"cell_type":"markdown","metadata":{"id":"rdGin15pC8lz"},"source":["### Run with cell"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oFGOYtzU4Xqu"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","\n","# import libs\n","import argparse\n","import glob\n","import multiprocessing as mp\n","import os\n","import time\n","import cv2\n","import tqdm\n","from skimage import io\n","\n","from detectron2.config import get_cfg\n","from detectron2.data.detection_utils import read_image\n","from detectron2.utils.logger import setup_logger\n","\n","from sparseinst import VisualizationDemo, add_sparse_inst_config"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"d737hslZ4105"},"outputs":[],"source":["WINDOW_NAME = \"COCO detections -- val2017\"\n","def proc_single(image_path, args, cfg, demo):\n"," # cfg = setup_cfg(args)\n","\n"," # demo = VisualizationDemo(cfg)\n","\n"," image_path = './val2017/000000003934.jpg'\n"," args.input = glob.glob(os.path.expanduser(image_path))\n","\n"," start_time = time.time()\n"," img = read_image(path, format=\"RGB\")\n"," predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n"," print(\n"," \"{}: {} in {:.2f}s\".format(\n"," path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," )\n"," )\n","\n"," if args.output:\n"," if os.path.isdir(args.output):\n"," assert os.path.isdir(args.output), args.output\n"," out_filename = os.path.join(\n"," args.output, os.path.basename(path))\n"," else:\n"," assert len(\n"," args.output) > 0, \"Please specify a directory with args.output\"\n"," out_filename = args.output\n"," visualized_output.save(out_filename)\n"," else:\n"," cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U0JutUnfMpMH"},"outputs":[],"source":["class parser():\n"," def __init__(self, image_path, output_dir='/content/SparseInst/results/'):\n"," # reset to your own model-config-file path\n"," self.config_file = '/content/SparseInst/configs/sparse_inst_r50vd_giam_aug.yaml'\n","\n"," self.opts = [\"MODEL.WEIGHTS\", \n"," # reset to your own model.pth path\n"," \"/content/SparseInst/sparse_inst_r50vd_giam_aug_8bc5b3.pth\",\n"," \"INPUT.MIN_SIZE_TEST\", \n"," 512]\n","\n"," self.input = image_path\n"," if not os.path.exists(output_dir):\n"," os.makedirs(output_dir)\n"," self.output = output_dir\n","\n"," # reset to your desired threshold\n"," self.confidence_threshold = 0.3\n","\n","\n","def _setup_cfg(args):\n"," cfg = get_cfg()\n"," add_sparse_inst_config(cfg)\n","\n"," cfg.merge_from_file(args.config_file)\n"," cfg.merge_from_list(args.opts)\n","\n"," cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold\n"," cfg.freeze()\n"," return cfg\n","\n","args = parser('/content/val2017/000000003934.jpg')\n","cfg = _setup_cfg(args)\n","\n","\n","demo = VisualizationDemo(cfg)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cY_PXqhYT98U"},"outputs":[],"source":["# ATTENTION HERE!\n","# Before run the code, please modify 2 lines of code in detetron2\n","# See issue#18, issue#10, issue#13 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# This bug will be fixed soon"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"yqduAWwM5GHf"},"outputs":[],"source":["# run the cell\n","# change the image path to your own\n","image_path = \"/content/drive/MyDrive/SparseInst_Colab/569021651119418_.pic_hd.jpg\"\n","args.input = image_path\n","\n","img = read_image(image_path, format=\"RGB\")\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(image_path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(image_path))\n","print(out_filename)\n","visualized_output.save(out_filename)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pM3UHL405ZsZ"},"outputs":[],"source":["# run the cell\n","# change the image url to your own\n","img_src = \"http://n1image.hjfile.cn/shetuan/2017-05-17-1495016837-986-732.jpg\"\n","url_img = io.imread(img_src)\n","\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," url_img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(img_src,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(img_src))\n","print(out_filename)\n","visualized_output.save(out_filename)"]}],"metadata":{"accelerator":"GPU","colab":{"authorship_tag":"ABX9TyNsJUuDC3aUUGN30rZTquYI","collapsed_sections":["-RKGUL3zCvt_"],"mount_file_id":"1C5IoDr5FUIljYflQD67G9fxuDqCwcfy1","name":"SparseInst.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0} +{"cells":[{"cell_type":"markdown","metadata":{"id":"mhca95Zr_kCm"},"source":["# Mount Drive"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"F01WX-ON4K2l"},"outputs":[],"source":["import os\n","import sys\n","from google.colab import drive\n","drive.mount('/content/drive')\n","path = \"/content/drive/My Drive\"\n","\n","os.chdir(path)\n","os.listdir(path)\n","sys.path.append(\"/content/drive/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"owvzOlpN81QS"},"outputs":[],"source":["# prepare SpareInst code\n","!pwd\n","os.chdir('/content/')\n","!git clone https://github.com/hustvl/SparseInst.git\n","\n","sys.path.append(\"/content/SparseInst/sparseinst\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J7VQ-7NcMwBU"},"outputs":[],"source":["# prepare model\n","# upload from your local or load from your google drive\n","!cp '/content/drive/My Drive/SparseInst_Colab/sparse_inst_r50vd_giam_aug_8bc5b3.pth' '/content/SparseInst'"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":18037,"status":"ok","timestamp":1651148757167,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"2t94Cbj6CxbU","outputId":"8e841d41-4436-4200-fc64-5f55c18249ce"},"outputs":[],"source":["# unzip coco val2017 dataset, this will cost a little time\n","# or you can use your own images\n","!pwd\n","!unzip -uq '/content/drive/My Drive/data/coco/val2017.zip' -d '/content'"]},{"cell_type":"markdown","metadata":{"id":"fbNKjLN85Y3t"},"source":["# Prepare Environment"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":404,"status":"ok","timestamp":1651148787149,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"NPfrLm71DXZW","outputId":"ab8359a8-b099-442a-d73b-1d9f08de8f9e"},"outputs":[],"source":["import os\n","# change to workdirectory\n","os.chdir('/content/SparseInst')\n","# %cd /content/drive/My Drive/AlexNet\n","!pwd\n","!ls"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z7UtV1h0HMo-"},"outputs":[],"source":["!pip install timm\n","!pip install pyyaml==5.1"]},{"cell_type":"markdown","metadata":{"id":"zTZTr5e7Ab0Q"},"source":["## Install detectron2-v0.3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"smaim4McQHe5"},"outputs":[],"source":["!pwd\n","!git clone https://github.com/facebookresearch/detectron2.git"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oWwMeY0GJYEx"},"outputs":[],"source":["os.chdir('/content/SparseInst/detectron2')\n","!git checkout tags/v0.3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z9HRqaSX5Re1"},"outputs":[],"source":["# import detectron2\n","# print(detectron2.version)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3Uf4BhxO2nkO"},"outputs":[],"source":["# ATTENTION HERE !!!\n","\n","# Before run the code, please modify a line of code in detetron2\n","# See issue#18 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# Then run setup.py, otherwise there will be error occured.\n","\n","# This bug will be fixed soon\n","\n","# build detectron2\n","os.chdir('/content/SparseInst/detectron2')\n","!python setup.py build develop\n","\n","sys.path.append(\"/content/SparseInst/detectron2/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ipWjzaECZwPU"},"outputs":[],"source":["# remove built detectron2\n","# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"5BqQzqI0SsaA"},"source":["# Fix Package Bugs"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hN1AJSBlIbHW"},"outputs":[],"source":["# The following operartion may be much confusing, but\n","# there will be some \"import errors\" occur if not do this\n","\n","# 1. reinstall fvcore\n","!pip uninstall fvcore\n","!pip install fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Neo1NrnDSGZ6"},"outputs":[],"source":["# 2. reinstall iopath\n","!pip uninstall iopath\n","!pip install iopath"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jfj6BsceSRXK"},"outputs":[],"source":["# 3. reinstall portalocker\n","!pip uninstall portalocker\n","!pip install portalocker"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xQwX_PQhSdc-"},"outputs":[],"source":["# 4. reinstall yacs\n","!pip uninstall yacs\n","!pip install yacs"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZSmhRrLMDCrr"},"outputs":[],"source":["# !pip list fvcore\n","# os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4zESNpt-SAri"},"outputs":[],"source":["# os.chdir('/content/SparseInst/detectron2')\n","# !rm -rf build/ **/*.so"]},{"cell_type":"markdown","metadata":{"id":"jGT7Uyhi590J"},"source":["# SparseInst Demo"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cAyIRrECIEVn"},"outputs":[],"source":["!nvidia-smi"]},{"cell_type":"markdown","metadata":{"id":"-RKGUL3zCvt_"},"source":["### Run demo with command line format\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"burRHlTZEBeq"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","!python demo.py --config-file configs/sparse_inst_r50vd_giam_aug.yaml --input ../_val2017_part/* --output results --opt MODEL.WEIGHTS sparse_inst_r50vd_giam_aug_8bc5b3.pth INPUT.MIN_SIZE_TEST 512"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kjKlo4iO8qDk"},"outputs":[],"source":["os.environ[\"PATH\"] = '/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Wpuht-T4E3iQ"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","import fvcore"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":5,"status":"ok","timestamp":1651115674638,"user":{"displayName":"范子蔚","userId":"15551234769924635327"},"user_tz":-480},"id":"rQWV2ZUOGtpT","outputId":"61c789f4-c1cf-42ad-e179-f87f4b92466c"},"outputs":[],"source":["print(sys.path)\n","import numpy as np"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XArYSkLiJhH3"},"outputs":[],"source":["# The packs below may raise 'import error'\n","# uninstall then install may solve the problem but I haven't found the reason\n","\n","# import fvcore\n","# import iopath\n","# import portalocker\n","# import yacs"]},{"cell_type":"markdown","metadata":{"id":"rdGin15pC8lz"},"source":["### Run with cell"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oFGOYtzU4Xqu"},"outputs":[],"source":["os.chdir('/content/SparseInst')\n","\n","# import libs\n","import argparse\n","import glob\n","import multiprocessing as mp\n","import os\n","import time\n","import cv2\n","import tqdm\n","from skimage import io\n","\n","from detectron2.config import get_cfg\n","from detectron2.data.detection_utils import read_image\n","from detectron2.utils.logger import setup_logger\n","\n","from sparseinst import VisualizationDemo, add_sparse_inst_config"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"d737hslZ4105"},"outputs":[],"source":["WINDOW_NAME = \"COCO detections -- val2017\"\n","def proc_single(image_path, args, cfg, demo):\n"," # cfg = setup_cfg(args)\n","\n"," # demo = VisualizationDemo(cfg)\n","\n"," image_path = './val2017/000000003934.jpg'\n"," args.input = glob.glob(os.path.expanduser(image_path))\n","\n"," start_time = time.time()\n"," img = read_image(path, format=\"RGB\")\n"," predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n"," print(\n"," \"{}: {} in {:.2f}s\".format(\n"," path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," )\n"," )\n","\n"," if args.output:\n"," if os.path.isdir(args.output):\n"," assert os.path.isdir(args.output), args.output\n"," out_filename = os.path.join(\n"," args.output, os.path.basename(path))\n"," else:\n"," assert len(\n"," args.output) > 0, \"Please specify a directory with args.output\"\n"," out_filename = args.output\n"," visualized_output.save(out_filename)\n"," else:\n"," cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U0JutUnfMpMH"},"outputs":[],"source":["class parser():\n"," def __init__(self, image_path, output_dir='/content/SparseInst/results/'):\n"," # reset to your own model-config-file path\n"," self.config_file = '/content/SparseInst/configs/sparse_inst_r50vd_giam_aug.yaml'\n","\n"," self.opts = [\"MODEL.WEIGHTS\", \n"," # reset to your own model.pth path\n"," \"/content/SparseInst/sparse_inst_r50vd_giam_aug_8bc5b3.pth\",\n"," \"INPUT.MIN_SIZE_TEST\", \n"," 512]\n","\n"," self.input = image_path\n"," if not os.path.exists(output_dir):\n"," os.makedirs(output_dir)\n"," self.output = output_dir\n","\n"," # reset to your desired threshold\n"," self.confidence_threshold = 0.3\n","\n","\n","def _setup_cfg(args):\n"," cfg = get_cfg()\n"," add_sparse_inst_config(cfg)\n","\n"," cfg.merge_from_file(args.config_file)\n"," cfg.merge_from_list(args.opts)\n","\n"," cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold\n"," cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold\n"," cfg.freeze()\n"," return cfg\n","\n","args = parser('/content/val2017/000000003934.jpg')\n","cfg = _setup_cfg(args)\n","\n","\n","demo = VisualizationDemo(cfg)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cY_PXqhYT98U"},"outputs":[],"source":["# ATTENTION HERE!\n","# Before run the code, please modify 2 lines of code in detetron2\n","# See issue#18, issue#10, issue#13 in hustvl/SparseInst\n","# link: https://github.com/hustvl/SparseInst/issues/18\n","\n","# This bug will be fixed soon"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"yqduAWwM5GHf"},"outputs":[],"source":["# run the cell\n","# change the image path to your own\n","image_path = \"/content/drive/MyDrive/SparseInst_Colab/569021651119418_.pic_hd.jpg\"\n","args.input = image_path\n","\n","img = read_image(image_path, format=\"RGB\")\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(image_path,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(image_path))\n","print(out_filename)\n","visualized_output.save(out_filename)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pM3UHL405ZsZ"},"outputs":[],"source":["# run the cell\n","# change the image url to your own\n","img_src = \"http://n1image.hjfile.cn/shetuan/2017-05-17-1495016837-986-732.jpg\"\n","url_img = io.imread(img_src)\n","\n","start_time = time.time()\n","predictions, visualized_output = demo.run_on_image(\n"," url_img, args.confidence_threshold)\n","\n","print(\"\\n{}: {} in {:.2f}s\".format(img_src,\n"," \"detected {} instances\".format(\n"," len(predictions[\"instances\"]))\n"," if \"instances\" in predictions\n"," else \"finished\",\n"," time.time() - start_time,\n"," ))\n","out_filename = os.path.join(\n"," args.output, os.path.basename(img_src))\n","print(out_filename)\n","visualized_output.save(out_filename)"]}],"metadata":{"accelerator":"GPU","colab":{"authorship_tag":"ABX9TyNsJUuDC3aUUGN30rZTquYI","collapsed_sections":["-RKGUL3zCvt_"],"mount_file_id":"1C5IoDr5FUIljYflQD67G9fxuDqCwcfy1","name":"SparseInst.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}