目录
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
下载,下载前添加以下数据源,提高下载速度
### 设置清华源镜像
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文件之后,根据提示安装即可。
## 创建指定python3.5版本的环境,环境名为mmdection
conda create -n mmdection python=3.5
## 查看安装的环境名
conda info --envs
## 激活环境名为mmdection的环境
conda activate mmdection
## 退出环境名为mmdection的环境
conda deactivate mmdection
conda install pytorch torchvision -c pytorch
测试Pytorch是否安装成功:
python
import torch
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
python setup.py develop # or "pip install -v -e .pip"
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文件安装。
下载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)
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__
##anconda环境下安装matplotlib
conda install matplotlib
sudo pip install albumentations
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/
数据集类别数据也要改,自己检测的数据,有多少种类型,就改多少
修改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 '
]
修改自己的图像分辨率:
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