Detectron2 adalah library open-source yang dikembangkan oleh Facebook AI Research (FAIR) untuk melakukan deteksi objek. Library ini merupakan penerus dari Detectron dan dirancang untuk menjadi lebih modular, fleksibel, dan efisien dalam berbagai aplikasi visi komputer, seperti deteksi objek, segmentasi instance, dan segmentasi semantik. Berikut beberapa fitur dan penjelasan tentang Detectron2:
- Modular dan Fleksibel:
- Detectron2 memiliki arsitektur yang modular, sehingga komponen-komponen seperti backbone, kepala (head), dan loss function dapat dengan mudah dikombinasikan dan dikustomisasi sesuai kebutuhan.
- Library ini mendukung berbagai model backbone seperti ResNet, ResNeXt, dan FPN.
- Kinerja Tinggi:
- Optimisasi yang dilakukan pada Detectron2 membuatnya sangat efisien dalam hal penggunaan memori dan waktu komputasi.
- Dukungan penuh untuk pemrosesan paralel dengan GPU memungkinkan eksekusi yang cepat dan efisien.
- Implementasi SOTA (State-Of-The-Art):
- Detectron2 menyediakan implementasi dari berbagai algoritma deteksi dan segmentasi objek terkini yang sering digunakan dalam penelitian dan industri.
- Contoh model yang didukung termasuk Faster R-CNN, Mask R-CNN, RetinaNet, dan lain-lain.
- Dukungan untuk Pelatihan dan Inference:
- Detectron2 mendukung pelatihan model dari awal (scratch) atau fine-tuning model yang sudah dilatih sebelumnya.
- Library ini juga mendukung inference atau prediksi dengan model yang sudah dilatih.
- Integrasi yang Mudah:
- Detectron2 dibangun di atas PyTorch, membuatnya mudah diintegrasikan dengan ekosistem PyTorch dan framework deep learning lainnya.
- API yang disediakan user-friendly dan dirancang untuk memudahkan peneliti dan praktisi dalam mengembangkan dan menguji model.
- Dokumentasi dan Komunitas:
- Detectron2 memiliki dokumentasi yang komprehensif serta banyak contoh dan tutorial untuk membantu pengguna memulai.
- Komunitas yang aktif dan dukungan dari Facebook AI Research memastikan bahwa library ini terus diperbarui dan diperbaiki.
Cara Install Detectron2
Contents
Kalian bisa mengikuti langkah berikut
https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5
Untuk detectron2 saat ini masih menggunakan torch version 2 kebawah
https://detectron2.readthedocs.io/en/latest/tutorials/install.html
It would be useful to have the wheels for newer versions of torch. It seems like they haven’t been updated since November 2021. I am using the more limited installation method for now:
mari kita coba menggunakan perintah
!python -m pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.6'
tetap nggak bisa! karena emang terakhir diperbaharui yaitu 2011
https://github.com/facebookresearch/detectron2/releases
Sehingga kita harus menggunakan python 3.7 untuk bisa install torch 1.9.0
Membuat virtual environment pada conda dengan version python 3.8
Kalian bisa menggunakan cara berikut, misalkan kita akan membuat virtual environment dengan nama virtual1
Minimal untuk menggunakan versi 3.8 karena ada error jika menggunakan 3.7
ImportError: cannot import name ‘cached_property’ from ‘functools’ (/opt/anaconda3/envs/virtual1/lib/python3.7/functools.py)
conda create -n virtual1 python==3.8
kemudian pilih yes saja, seperti berikut tampilannya
(base) user@MacBook-Air detectron2 % conda create -n virtual1 python==3.8 Channels: - defaults Platform: osx-64 Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: /opt/anaconda3/envs/virtual1 added / updated specs: - python==3.7 The following packages will be downloaded: package | build ---------------------------|----------------- ca-certificates-2024.7.2 | hecd8cb5_0 128 KB certifi-2022.12.7 | py37hecd8cb5_0 151 KB libffi-3.2.1 | h0a44026_1007 43 KB openssl-1.0.2u | h1de35cc_0 2.0 MB pip-22.3.1 | py37hecd8cb5_0 2.7 MB python-3.7.0 | hc167b69_0 13.2 MB readline-7.0 | h1de35cc_5 316 KB setuptools-65.6.3 | py37hecd8cb5_0 1.1 MB sqlite-3.33.0 | hffcf06c_0 1.3 MB tk-8.6.14 | h4d00af3_0 3.4 MB wheel-0.38.4 | py37hecd8cb5_0 65 KB xz-5.4.6 | h6c40b1e_1 371 KB zlib-1.2.13 | h4b97444_1 102 KB ------------------------------------------------------------ Total: 24.8 MB The following NEW packages will be INSTALLED: ca-certificates pkgs/main/osx-64::ca-certificates-2024.7.2-hecd8cb5_0 certifi pkgs/main/osx-64::certifi-2022.12.7-py37hecd8cb5_0 libcxx pkgs/main/osx-64::libcxx-14.0.6-h9765a3e_0 libedit pkgs/main/osx-64::libedit-3.1.20230828-h6c40b1e_0 libffi pkgs/main/osx-64::libffi-3.2.1-h0a44026_1007 ncurses pkgs/main/osx-64::ncurses-6.4-hcec6c5f_0 openssl pkgs/main/osx-64::openssl-1.0.2u-h1de35cc_0 pip pkgs/main/osx-64::pip-22.3.1-py37hecd8cb5_0 python pkgs/main/osx-64::python-3.7.0-hc167b69_0 readline pkgs/main/osx-64::readline-7.0-h1de35cc_5 setuptools pkgs/main/osx-64::setuptools-65.6.3-py37hecd8cb5_0 sqlite pkgs/main/osx-64::sqlite-3.33.0-hffcf06c_0 tk pkgs/main/osx-64::tk-8.6.14-h4d00af3_0 wheel pkgs/main/osx-64::wheel-0.38.4-py37hecd8cb5_0 xz pkgs/main/osx-64::xz-5.4.6-h6c40b1e_1 zlib pkgs/main/osx-64::zlib-1.2.13-h4b97444_1 Proceed ([y]/n)? y Downloading and Extracting Packages: Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate virtual1 # # To deactivate an active environment, use # # $ conda deactivate
Activate Virtual Environment
seperti biasa, kita install dengan perintah conda activate virtual1
(base) user@MacBook-Air detectron2 % conda activate virtual1 (virtual1) user@MacBook-Air detectron2 % python --version Python 3.7.0
Install torch 1.9
Lanjut untuk install torch versi 1.9
(virtual1) user@MacBook-Air detectron2 % pip install torch==1.9 Collecting torch==1.9 Downloading torch-1.9.0-cp37-none-macosx_10_9_x86_64.whl (127.9 MB) ━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━ 64.7/127.9 MB 186.8 kB/s eta 0:05:39
Install Opencv
jangan lupa install opencv menggunakan version 3.4.10.37
pip install opencv-python==3.4.10.37
Install TorchVision
dan juga torchvision dengan version 0.10.0
pip install torchvision==0.10.0
Install detectron2
jangan lupa kalian pengguna mac, buat perintah berikut di terminal
yaitu dengan memberikan perintah
export MACOSX_DEPLOYMENT_TARGET=10.9
setelah itu, dilanjut dengan install detectron2
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
Mencoba Detectron2
Mari kita test import package nya, apakah berhasil?
from detectron2 import model_zoo from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog
kalau error pada saat install
pip install cython
juga install
pip install pyyaml
kalau masih ada problem bisa baca https://github.com/facebookresearch/detectron2/issues/5216
Berikut hasil running detectron2 di Linux
Saya menggunakan VPS linux dengan informasi sebagai berikut
(vm2) root@upbeat-turing:/home/bejo# screenfetch ./+o+- root@upbeat-turing.xxx-xx-xx-xx.plesk.page yyyyy- -yyyyyy+ OS: Ubuntu 22.04 jammy ://+//////-yyyyyyo Kernel: x86_64 Linux 5.15.0-46-generic .++ .:/++++++/-.+sss/` Uptime: 10d 17h 29m .:++o: /++++++++/:--:/- Packages: 1292 o:+o+:++.`..```.-/oo+++++/ Shell: bash 5.1.16 .:+o:+o/. `+sssoo+/ Disk: 48G / 158G (32%) .++/+:+oo+o:` /sssooo. CPU: Intel Xeon E5-2680 v3 @ 4x 2.5GHz /+++//+:`oo+o /::--:. GPU: Cirrus Logic GD 5446 \+/+o+++`o++o ++////. RAM: 7717MiB / 7949MiB .++.o+++oo+:` /dddhhh. .+.o+oo:. `oddhhhh+ \+.++o+o``-````.:ohdhhhhh+ `:o+++ `ohhhhhhhhyo++os: .o:`.syhhhhhhh/.oo++o` /osyyyyyyo++ooo+++/ ````` +oo+++o\: `oo++.
yang sebelumnya menggunakan vm dengan informasi sebagai berikut
Python 3.8.0 (default, Nov 6 2019, 21:49:08) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>>
versi torch, torchvision, dan opencv sebagai berikut
>>> import torch >>> import torchvision >>> import cv2 >>> torch.__version__ '1.9.0+cu102' >>> torchvision.__version__ '0.10.0+cu102' >>> cv2.__version__ '4.10.0' >>>
Bila kalian punya GPU wajib install
apt install nvidia-cuda-toolkit --fix-missing
jika tidak punya GPU wajib setting CPU, mari kita coba
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Jul 25 14:03:04 2024 @author: user """ import sys, os, distutils.core import torch, detectron2 TORCH_VERSION = ".".join(torch.__version__.split(".")[:2]) CUDA_VERSION = torch.__version__.split("+")[-1] print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION) print("detectron2:", detectron2.__version__) import detectron2 from detectron2.utils.logger import setup_logger setup_logger() # import some common libraries import numpy as np import os, json, cv2, random # from google.colab.patches import cv2_imshow # import some common detectron2 utilities from detectron2 import model_zoo from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog im = cv2.imread("image.jpeg") cfg = get_cfg() # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") cfg.MODEL.DEVICE = 'cpu' predictor = DefaultPredictor(cfg) outputs = predictor(im)
karena menggunakan CPU tentu hasilnya akan sangat lama sekali! berikut hasil uji coba diatas
(vm2) root@upbeat-turing:/home/bejo# python Python 3.8.0 (default, Nov 6 2019, 21:49:08) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys, os, distutils.core >>> import torch, detectron2 >>> >>> >>> TORCH_VERSION = ".".join(torch.__version__.split(".")[:2]) >>> CUDA_VERSION = torch.__version__.split("+")[-1] >>> print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION) torch: 1.9 ; cuda: cu102 >>> print("detectron2:", detectron2.__version__) detectron2: 0.6 >>> >>> >>> import detectron2 >>> from detectron2.utils.logger import setup_logger >>> setup_logger() <Logger detectron2 (DEBUG)> >>> >>> # import some common libraries ... import numpy as np >>> import os, json, cv2, random >>> # from google.colab.patches import cv2_imshow ... >>> # import some common detectron2 utilities ... from detectron2 import model_zoo >>> from detectron2.engine import DefaultPredictor >>> from detectron2.config import get_cfg >>> from detectron2.utils.visualizer import Visualizer >>> from detectron2.data import MetadataCatalog, DatasetCatalog >>> im = cv2.imread("image.jpeg") >>> cfg = get_cfg() >>> # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library ... cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) >>> cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model >>> # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well ... cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") >>> cfg.MODEL.DEVICE = 'cpu' >>> predictor = DefaultPredictor(cfg) [07/27 23:21:54 d2.checkpoint.detection_checkpoint]: [DetectionCheckpointer] Loading from https://dl.fbaipublicfiles.com/detectron2/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl ... model_final_f10217.pkl: 178MB [00:40, 4.36MB/s] >>> outputs = predictor(im) /root/anaconda3/envs/vm2/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.) return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode) >>> outputs {'instances': Instances(num_instances=15, image_height=480, image_width=640, fields=[pred_boxes: Boxes(tensor([[126.6035, 244.8977, 459.8291, 480.0000], [251.1083, 157.8127, 338.9731, 413.6379], [114.8496, 268.6864, 148.2351, 398.8111], [ 0.8217, 281.0327, 78.6072, 478.4209], [ 49.3953, 274.1229, 80.1545, 342.9808], [561.2248, 271.5816, 596.2755, 385.2552], [385.9072, 270.3125, 413.7130, 304.0397], [515.9295, 278.3743, 562.2792, 389.3803], [335.2410, 251.9167, 414.7491, 275.9375], [350.9300, 269.2060, 386.0984, 297.9081], [331.6292, 230.9996, 393.2759, 257.2009], [510.7349, 263.2656, 570.9865, 295.9194], [409.0841, 271.8646, 460.5582, 356.8722], [506.8767, 283.3257, 529.9403, 324.0392], [594.5663, 283.4820, 609.0577, 311.4124]])), scores: tensor([0.9997, 0.9957, 0.9915, 0.9882, 0.9861, 0.9840, 0.9769, 0.9716, 0.9062, 0.9037, 0.8870, 0.8575, 0.6592, 0.5899, 0.5767]), pred_classes: tensor([17, 0, 0, 0, 0, 0, 0, 0, 25, 0, 25, 25, 0, 0, 24]), pred_masks: tensor([[[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], [[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], [[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], ..., [[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], [[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], [[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]]])])}
lebih lanjut ref: https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5
Untu lebih jelasnya mengenai inference detectron2 bisa dibaca link diatas
untuk menampilkan hasilnya kedalam gambar yaitu
from matplotlib import pyplot as plt v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2) out = v.draw_instance_predictions(outputs["instances"].to("cpu")) plt.figure, plt.imshow(out.get_image()[:, :, ::-1]),plt.show()
Berikut hasil install detectron2 Mac OS
dengan spesifikasi sebagai berikut
(base) user@MacBook-Air / % screenfetch -/+:. user@MacBook-Air :++++. OS: 64bit macOS /+++/. Kernel: x86_64 Darwin 21.6.0 .:-::- .+/:-``.::- Uptime: 6h 15m .:/++++++/::::/++++++/:` Packages: 146 .:///////////////////////:` Shell: zsh 5.8.1 ////////////////////////` Resolution: 1440x900 -+++++++++++++++++++++++` DE: Aqua /++++++++++++++++++++++/ WM: Quartz Compositor /sssssssssssssssssssssss. WM Theme: Blue :ssssssssssssssssssssssss- Font: SFMonoRegular osssssssssssssssssssssssso/` Disk: 111G / 251G (48%) `syyyyyyyyyyyyyyyyyyyyyyyy+` CPU: Intel Core i5-5250U @ 1.60GHz `ossssssssssssssssssssss/ GPU: Intel HD Graphics 6000 :ooooooooooooooooooo+. RAM: 3353MiB / 8192MiB `:+oo+/:-..-:/+o+/-
import sys, os, distutils.core import torch, detectron2 TORCH_VERSION = ".".join(torch.__version__.split(".")[:2]) CUDA_VERSION = torch.__version__.split("+")[-1] print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION) print("detectron2:", detectron2.__version__) torch: 1.9 ; cuda: 1.9.0 detectron2: 0.6