windows 安装detectron2 (过程记录)
文章目录
前言
detectron2按照网上的教程安装了许久后,终于成功安装!
一、参考文章
Windows11下安装detectron2超详细教程(免修改版本)(不用进行修改代码)
本机环境:
win10
cuda11.3
二、安装步骤
1.安装microsoft编译工具
1.1 安装Visual Studio 2019——MSVC编译环境,就不会有后续的报错。必须得安装VS2019编译环境才行!!!
2.安装pytorch
使用Anaconda3,创建一个python3.8环境,注意必须是3.8及以上,不然会有一个包需要的python版本不满足。
2.1打开Prompt,然后输入
conda create -n detectron2 python=3.8 -y
2.2激活环境
activate detectron2
2.3下载pytorchd的whl包
根据detectron2官网,cuda11.3版本安装torch1.10版本。
打开网站pytorch下载
ctrl+f:搜索cu113,找到cuda11.3对应的安装包。
选择cu113/torch-1.10.0%2Bcu113-cp38-cp38-win_amd64.whl、
cu113/torchaudio-0.10.0%2Bcu113-cp38-cp38-win_amd64.whl、
cu113/torchvision-0.11.0%2Bcu113-cp38-cp38-win_amd64.whl
在detectron2对应的环境,cd切换到whl文件的下载地址。
使用以下命令安装
pip install "torch-1.10.0+cu113-cp38-cp38-win_amd64.whl" "torchaudio-0.10.0+cu113-cp38-cp38-win_amd64.whl" "torchvision-0.
11.0+cu113-cp38-cp38-win_amd64.whl"
3.下载detectron2
3.1 安装pycocotools-windows,执行:
pip install pycocotools-windows
3.2 下载detectron2链接,免修改版detectron2
或者使用git下载
git clone https://gitcode.com/mirrors/ivanpp/detectron2.git
下载之后打开,在目录内有setup.py,运行。
pip install -e .
运行结果如下:
报错1:
‘ubprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned
nonzero exit state 1’
将setup.py代码最后一行,
cmdclass={“build_ext”: torch.utils.cpp_extension.BuildExtension},
改为:
cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension.with_options(use_ninja=False)},
修改后,重新运行,出现报错2:
error: command ‘C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe’ failed with exit code 2
安装了vs2019,没有出现这个错误,成功安装!!!
4.测试detectron
使用以下测试代码demo.py:
from detectron2.engine.defaults import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.data import MetadataCatalog # 元数据目录
from detectron2.utils.visualizer import Visualizer, ColorMode
from detectron2 import model_zoo # 图像库
"""
总之,DataCatalog和MetadataCatalog两个类实现了torch.utils.data.Dataset的创建与具体数据集读取的分,
提供了使用字符串创建Dataset和DataLoader的方法。
"""
import cv2
import numpy as np
class Detector:
"""
定义检测器
"""
def __init__(self, model_type='OD'):
self.cfg = get_cfg() # 配置文件
if model_type == 'OD': # 目标检测模式
self.cfg.merge_from_file(model_zoo.get_config_file(
"COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")) # coco图像库已经训练好的图像模型(Faster-RCNN;ResNet101)
self.cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url( # 模型权重
"COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")
elif model_type == "IS": # 实例分割模式
self.cfg.merge_from_file(model_zoo.get_config_file(
"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
self.cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # 分数阈值测试
self.cfg.MODEL.DEVICE = 'cuda' # 运算设备
self.predictor = DefaultPredictor(self.cfg) # 最基本的预测器
def onImage(self, imagePath):
image = cv2.imread(imagePath) # 读取图像
predictions = self.predictor(image) # 预测(置信度,位置,类别)
viz = Visualizer(image[:, :, ::-1],
metadata=MetadataCatalog.get(self.cfg.DATASETS.TRAIN[0]),
instance_mode=ColorMode.IMAGE_BW) # 可视化工具
output = viz.draw_instance_predictions(predictions["instances"].to('cpu')) # 把 GPU 跑的数据结果放到 CPU
cv2.imshow("Result", output.get_image()[:, :, ::-1])
cv2.waitKey()
if __name__ == '__main__':
detector = Detector(model_type='IS') # 这里选择检测模式('OD': 目标检测;'IS': 实例分割)
detector.onImage('./test1.jpg') # 这里放你的测试图片路径
运行过程中报错解决如:使用detectron2==0.5版本,遇到的问题汇总
运行后结果显示:
5.参考文档
1.windows 安装detectron2 ==0.5(过程记录) ————成功安装
2.Windows11下安装detectron2超详细教程(免修改版本)(不用进行修改代码)
3.detectron2官网
总结
最终解决方案:还是需要安装vs2019,自带的编译工具,能够顺利安装detectron2。