Write custom directory title here
Detectron2 is a powerful image recognition tool led by Facebook that supports image classification, object detection, semantic segmentation, instance segmentation, and panoramic segmentation.
Official Installation Instructions
1. Install python
sudo apt install python3
sudo ln -s /usr/bin/python3 /usr/bin/python
sudo apt install python3-pip
2. Install opencv
sudo apt install python3-opencv
3. Install the nvdia graphics card driver
Generally speaking, if you installed UBUNTU and chose to install a third-party graphics card driver during installation, then UBUNTU will support the driver when booting, and you can check it in Settings-About. If you see the graphics shown in the figure below and display the words NVIDIA GeForce RTX3060, it means that the driver has been installed.
If you have not installed the driver or there is a problem with the driver installation and you need to reinstall it, you can use the following steps. The third command will display the currently recommended driver, as shown in the figure below with the word recommended, select it and use the fourth command to install it.
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo ubuntu-drivers devices
sudo apt install nvidia-driver-525-open
4. Install cuda11.7
See the official installation link , cuda11.7 is the version officially recommended by detectron2, so install some versions here:
You can also directly refer to the following commands and install them in sequence.
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda-repo-ubuntu2204-11-7-local_11.7.1-515.65.01-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-11-7-local_11.7.1-515.65.01-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
After restarting the system, use nvidia-smi to see the following results, indicating that the driver and cuda have been installed
xxx@xxx-pc:~$ nvidia-smi
Sat Apr 15 14:15:04 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 515.65.01 Driver Version: 515.65.01 CUDA Version: 11.7 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... On | 00000000:01:00.0 On | N/A |
| 36% 34C P8 9W / 170W | 182MiB / 12288MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1831 G /usr/lib/xorg/Xorg 94MiB |
| 0 N/A N/A 2131 G /usr/bin/gnome-shell 85MiB |
+-----------------------------------------------------------------------------+
5. Install pytorch
You need to update the PIP installation source first. The default source of pip is abroad, and the speed is relatively slow, only tens of kilobytes, while pytorch is very large, with several gigabytes. Therefore, before starting the installation, you must first set the installation source to a domestic installation source. For details, see Permanently add domestic mirror installation source for pip
Open the website https://pytorch.org/ and select the appropriate installation parameters. After selecting the default CUDA 11.7 version recommended by detectron, the installation command as shown in the figure below appears.
Enter the command at the command line to start the installation automatically
pip install torch torchvision torchaudio
6. Install nijia
sudo apt install ninja-build
7. clone and install detectron
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
8. Trial run detectron
In the ~/detectron2/demo directory, put a photo input1.jpg of an office scene, and then run the following command to use the pre-trained model to demonstrate object detection.
cd demo
python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input input1.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
At the end of the demonstration, a photo will be opened automatically, as shown in the figure below, 5 people, 1 chair and 6 objects are detected. It means that detectron2 has been installed successfully.