ubuntu 安裝深度學習目標檢測工具箱mmdetection

目錄

1.Anaconda 安裝

2.環境配置

3.安裝Pytorch

4.安裝mmdetection

4.1安裝mmcv

5.demo測試mmdetection

error:

1 . ImportError: cannot import name 'PILLOW_VERSION'

2.ImportError: No module named 'matplotlib'

3.ImportError: No module named 'albumentations'

4.pip安裝error


 

1.Anaconda 安裝

下載,下載前添加以下數據源,提高下載速度

### 設置清華源鏡像
 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
 conda config --set show_channel_urls yes
### 設置pytorch鏡像
 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/
bash Anaconda3-2019.07-Linux-x86_64.sh

下載Anaconda3文件之後,根據提示安裝即可。

2.環境配置

## 創建指定python3.5版本的環境,環境名爲mmdection
conda create -n mmdection python=3.5
## 查看安裝的環境名
conda info --envs
## 激活環境名爲mmdection的環境
conda activate mmdection
## 退出環境名爲mmdection的環境
conda deactivate mmdection

3.安裝Pytorch

conda install pytorch torchvision -c pytorch

測試Pytorch是否安裝成功:

python 
import torch

4.安裝mmdetection

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
python setup.py develop  # or "pip install -v -e .pip"

4.1安裝mmcv

pip install mmcv
##這個過程很慢,漫長的等待
安裝報錯
(mmdection) **@**-Precision-7920-Tower:~/mmdetection$ pip install mmcv
Collecting mmcv
  Using cached https://files.pythonhosted.org/packages/9b/7c/f6c20b54bb22d47cb87cc48ee626aab290584458e38bc1bbb76c868afc82/mmcv-0.2.15.tar.gz
Requirement already satisfied: numpy>=1.11.1 in /home/**/.conda/envs/mmdection/lib/python3.5/site-packages (from mmcv) (1.15.2)
Collecting pyyaml
  Using cached https://files.pythonhosted.org/packages/3d/d9/ea9816aea31beeadccd03f1f8b625ecf8f645bd66744484d162d84803ce5/PyYAML-5.3.tar.gz
Requirement already satisfied: six in /home/**/.conda/envs/mmdection/lib/python3.5/site-packages/six-1.13.0-py3.5.egg (from mmcv) (1.13.0)
Collecting addict
  Using cached https://files.pythonhosted.org/packages/14/6f/beb258220417c1a0fe11e842f2e012a1be7eeeaa72a1d10ba17a804da367/addict-2.2.1-py3-none-any.whl
Collecting requests
  Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Collecting opencv-python>=3
  Downloading https://files.pythonhosted.org/packages/59/dc/f4a3c35673512e3bfaa48c4e3b6c1bc3e262a8a2bfc9ffef986780d15ad3/opencv_python-4.1.2.30-cp35-cp35m-manylinux1_x86_64.whl (28.3MB)
     |▏                               | 112kB 7.5kB/s eta 1:02:54ERROR: Exception:
Traceback (most recent call last):
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py", line 425, in _error_catcher
    yield
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py", line 507, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in read
    data = self.__fp.read(amt)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/http/client.py", line 448, in read
    n = self.readinto(b)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/http/client.py", line 488, in readinto
    n = self.fp.readinto(b)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/socket.py", line 576, in readinto
    return self._sock.recv_into(b)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/ssl.py", line 937, in recv_into
    return self.read(nbytes, buffer)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/ssl.py", line 799, in read
    return self._sslobj.read(len, buffer)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/ssl.py", line 583, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/cli/base_command.py", line 153, in _main
    status = self.run(options, args)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/commands/install.py", line 382, in run
    resolver.resolve(requirement_set)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/legacy_resolve.py", line 201, in resolve
    self._resolve_one(requirement_set, req)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/legacy_resolve.py", line 365, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/legacy_resolve.py", line 313, in _get_abstract_dist_for
    req, self.session, self.finder, self.require_hashes
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/operations/prepare.py", line 194, in prepare_linked_requirement
    progress_bar=self.progress_bar
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 465, in unpack_url
    progress_bar=progress_bar
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 316, in unpack_http_url
    progress_bar)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 551, in _download_http_url
    _download_url(resp, link, content_file, hashes, progress_bar)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 253, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/utils/hashes.py", line 80, in check_against_chunks
    for chunk in chunks:
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 223, in written_chunks
    for chunk in chunks:
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/utils/ui.py", line 160, in iter
    for x in it:
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_internal/download.py", line 212, in resp_read
    decode_content=False):
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py", line 564, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py", line 529, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/**/.conda/envs/mmdection/lib/python3.5/site-packages/pip/_vendor/urllib3/response.py", line 430, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

分析原因,是opencv_python-4.1.2.30-cp35-cp35m-manylinux1_x86_64.whl該文件下載中斷導致的,

手動下載opencv_python-4.1.2.30-cp35-cp35m-manylinux1_x86_64.whl文件安裝。

5.demo測試mmdetection

下載checkpoints: https://pan.baidu.com/s/1jTgTkoVWZ_Jl9iVxkLGisw

 

把標註數據文件“annotations.json”拷貝到“data”文件夾下:

 

修改參數文件faster_rcnn_r50_fpn_1x.py

 

執行以下測試代碼:

python tools/test.py configs/faster_rcnn_r50_fpn_1x.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth  --show

如果遇到下載依賴包,速度特別慢的問題,建議去添加清華鏡像源下載。

比如安裝scikit-image,docker鏡像文件下載特別慢,可以使用清華鏡像源下載

 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple  scikit-image
 ## -i https://pypi.tuna.tsinghua.edu.cn/simple  指定使用清華鏡像源

測試結果:說明mmdetection安裝成功。

 

測試demo.jpg,

新建demo.py文件,粘貼以下內容,

##執行命令
python demo.py
##demo.py
import argparse
import cv2
import torch
from mmdet.apis import inference_detector, init_detector, show_result

#configure the  file path
config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'

#initialize the model
model = init_detector(config_file, checkpoint_file)

#test data
img = 'demo/demo.jpg'
result = inference_detector(model, img)
show_result(img, result, model.CLASSES)

error:

1 . ImportError: cannot import name 'PILLOW_VERSION'

from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION'

版本問題,版本降低到7.0.0 版本或者“PILLOW_VERSION”改成“__version__”

pip3 install 'pillow<7.0.0'

在 7.0.0 後的版本就沒有 PILLOW_VERSION 這個值了

# VERSION was removed in Pillow 6.0.0.
# PILLOW_VERSION was removed in Pillow 7.0.0.
# Use __version__ instead.
__version__ = _version.__version__

2.ImportError: No module named 'matplotlib'

##anconda環境下安裝matplotlib
conda install matplotlib

3.ImportError: No module named 'albumentations'

sudo pip install albumentations

4.pip安裝error

The directory '/home/**/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
sudo chown -R USERNAME  /home/$USERNAME/.cache/pip/
sudo chown -R USERNAME  /home/$USERNAME/.cache/pip/http/

5.訓練自己的數據

數據集類別數據也要改,自己檢測的數據,有多少種類型,就改多少

 

修改coco.py和class_numes.py裏面的類別信息:

1.定義數據種類,需要修改的地方在mmdetection/mmdet/datasets/coco.py。把CLASSES的那個tuple改爲自己數據集對應的種類tuple即可。

例如:

CLASSES = ('Background', 'bottle cap deformation', 'bad cap', 'the cap spins', 'bottle cap break point', 'label askew',
'label wrinkled', 'labeled bubbles', 'spray code normal', 'spurt exception')

2.接着在mmdetection/mmdet/core/evaluation/class_names.py修改coco_classes數據集類別,這個關係到後面test的時候結果圖中顯示的類別名稱。

例如:

def coco_classes():
     reture ['bottle cap deformation ',
              'bad cap ',
              'the cap spins ',
              'bottle cap break point ',
              'label askew ',
              'label wrinkled ',
              'labeled bubbles ',
              'spray code normal ',
              'spurt exception '
             ]

不要用中文,會報utf-8的error

修改自己的圖像分辨率:

tools/dist_train.sh  configs/faster_rcnn_r50_fpn_1x.py  2 --validate
#2個GPU,訓練模型

 測試訓練的模型:

python tools/test.py  configs/faster_rcnn_r50_fpn_1x.py   work_dirs/faster_rcnn_r50_fpn_1x/epoch_12.pth  --show

此處的格式化輸出稱爲檢測評價矩陣(detection evaluation metrics)。

(mmdection) **@jdh-Precision-7920-Tower:~/mmdetection$ python tools/test.py  configs/cascade_rcnn_r50_fpn_1x.py   work_dirs/cascade_rcnn_r50_fpn_1x/epoch_10.pth  --out result/cascade.pkl  --eval bbox
loading annotations into memory...
Done (t=0.02s)
creating index...
index created!
[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 3371/3371, 22.8 task/s, elapsed: 148s, ETA:     0s
writing results to result/cascade.pkl
Starting evaluate bbox
Loading and preparing results...
DONE (t=0.10s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=2.37s).
Accumulating evaluation results...
DONE (t=0.53s).
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.047
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.086
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.045
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.058
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.075
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.056
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.074
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.093
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.094
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.221
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.128
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.084

此處摘錄COCO數據集文檔中對該評價矩陣的簡要說明:

Average Precision (AP):
	AP		% AP at IoU=.50:.05:.95 (primary challenge metric) 
	APIoU=.50	% AP at IoU=.50 (PASCAL VOC metric) 
	APIoU=.75	% AP at IoU=.75 (strict metric)
AP Across Scales:
	APsmall		% AP for small objects: area < 322 
	APmedium	% AP for medium objects: 322 < area < 962 
	APlarge		% AP for large objects: area > 962
Average Recall (AR):
	ARmax=1		% AR given 1 detection per image 
	ARmax=10	% AR given 10 detections per image 
	ARmax=100	% AR given 100 detections per image
AR Across Scales:
	ARsmall		% AR for small objects: area < 322 
	ARmedium	% AR for medium objects: 322 < area < 962 
	ARlarge		% AR for large objects: area > 962

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章