一、基础环境
打开Anaconda Prompt
1.创建新的虚拟环境:
备注:如果创建不了,请参考我的上一篇文章(其实就是资源下载通道的问题):http://t.csdnimg.cn/AoUTn
conda create -n mmdet python==3.7
激活mmdet环境:
conda acticate mmdet
2.安装 cudatoolkit和cudnn(分别运行):
conda install cudatoolkit==11.1.1
conda install cudnn==8.2.0.53
如果出现报错,比如 :
PackagesNotFoundError: The following packages are not available from current channels:
- cudatoolkit==11.1.1
那么要么换源,要么去官网,我推荐直接去官网方便:anaconda搜索,界面如下:
然后进入你需要的版本,这里我使用的是第四个(按理来说前面几个都可以):
进去的界面:
任意复制其中的一句,一个不行就换另一句,去Anaconda Prompt
同样的安装cudnn(备注,我使用的是cudnn8.8.0.121,的第二条指令)。1.
3.然后安装torch:
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html
4.安装成功之后,下载mmdetection:
,github地址。
二、配置mmdetection环境
1.添加刚创建的环境:
下载后打开mmdetection-main,
随便新建一个文件,运行:
import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.cuda.is_available())
如上显示则表示基础环境创建成功。
2.安装依赖包:
pip install -U openmim -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmengine -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install "mmcv>=2.0.0" -i https://pypi.tuna.tsinghua.edu.cn/simple
以及
pip install terminaltables -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pycocotools -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install shapely -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install cython -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install imagecorruptions -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install cityscapesscripts -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple
3.下载训练权重:
放在项目下的weights文件夹内,名字可以随便取,地址:https://github.com/open-mmlab/mmdetection/tree/main/configs/centernet
4.新建测试文件:
my_predect.py:内容如下:
import os
import cv2
import mmcv
from mmdet.registry import VISUALIZERS
from mmdet.apis import init_detector, inference_detector
# 指定模型的配置文件和 checkpoint 文件路径
config_file = 'configs/centernet/centernet_r18-dcnv2_8xb16-crop512-140e_coco.py'
checkpoint_file = 'weigthts/centernet_resnet18_dcnv2_140e_coco_20210702_155131-c8cd631f.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.dataset_meta = model.dataset_meta
def detect_image(model, visualizer, img_path):
img = mmcv.imread(img_path)
result = inference_detector(model, img)
img = mmcv.imconvert(img, 'bgr', 'rgb')
visualizer.add_datasample(
'result',
img,
data_sample=result,
draw_gt=False,
show=False)
img_with_bbox = visualizer.get_image()
save_path = img_path.replace('.jpg', '_result.jpg')
cv2.imwrite(save_path, img_with_bbox[:, :, ::-1])
def detect_images_in_folder(model, visualizer, folder_path):
image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.jpg')]
for img_path in image_paths:
detect_image(model, visualizer, img_path)
def main():
# 图片预测
folder_path = 'my_images/'
detect_images_in_folder(model, visualizer, folder_path)
if __name__ == '__main__':
main()
config_filed文件路径:configs->centernet->centernet_r18-dcnv2_8xb16-crop512-140e_coco.py
checkpoint_file路径就是之前下载的训练权重的路径,
三、下载数据集
可以参考:http://t.csdnimg.cn/w1VlW
或者直接下载2017val数据集:http://images.cocodataset.org/zips/val2017.zip
创建文件夹,放入图片(三四张就行,在my_predict.py文件中的folder_pat更改为你的路径)
四、检测测试
直接运行my_predict.py,生成的结果会在你放图片的路径下,
五、分割测试
1.测试demo
import os
import cv2
import mmcv
from mmdet.registry import VISUALIZERS
from mmdet.apis import init_detector, inference_detector
# 指定模型的配置文件和 checkpoint 文件路径
#分割
config_file = 'configs/mask_rcnn/mask-rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'weigthts/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.dataset_meta = model.dataset_meta
def detect_image(model, visualizer, img_path):
img = mmcv.imread(img_path)
result = inference_detector(model, img)
img = mmcv.imconvert(img, 'bgr', 'rgb')
visualizer.add_datasample(
'result',
img,
data_sample=result,
draw_gt=False,
show=False)
img_with_bbox = visualizer.get_image()
save_path = img_path.replace('.jpg', '_result.jpg')
cv2.imwrite(save_path, img_with_bbox[:, :, ::-1])
def detect_images_in_folder(model, visualizer, folder_path):
image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.jpg')]
for img_path in image_paths:
detect_image(model, visualizer, img_path)
def main():
# 图片预测
folder_path = 'my_images/'
detect_images_in_folder(model, visualizer, folder_path)
#分割
folder_path = 'my_images/'
detect_images_in_folder(model, visualizer, folder_path)
if __name__ == '__main__':
main()
2.下载训练权重
地址:https://github.com/open-mmlab/mmdetection/tree/main/configs/mask_rcnn
备注:我使用的是mask-rcnn,对于预训练模型选择你需要的,配置文件可以下载上方图片对应的连接,也可以去mmdetection项目下的configs->mask_rcnn下面对应的配置文件,比如我使用的上图所示的模型那么在mmdetection当中对应的是:
那么测试图片按照上面检测的图片,测试结果同样放在了同一个目录下:
图片