首页 > 编程知识 正文

maskrcnn训练自己数据,win10跳过磁盘修复

时间:2023-05-03 13:47:22 阅读:26646 作者:3465

硬件环境:笔记本电脑win10系统、1050Ti显卡

软件环境:Anaconda、Pycharm

3358 www.Sina.com/https://github.com/matter port/mask _ rcnn

1 .环境构建1. Anaconda创建虚拟环境

conda create-nmaskrcnnpython=3.62 .安装所需的依赖软件包(从github下载的项目中有requirements,为了保险起见,也是一个安装,http://)

首先,配置最重要的keras和tensorflow,并注意版本号

显卡使用gpu版,显卡不可用则不可用-gpu#此版本集CUDA9.0和Cu dnn7.6.4condainstalltensorflow-GPU==1. 11.0 #

condainstallpillowcondainstallcythoncondainstallmatplotlib #阉割版opencvpipinstallopencv-pythonconconnnencv-pythonconsthonstalal 请求没有说要安装shapely。 不安装这个就不能安装imgaug

如果condainstallshapelypipinstallimgaugimgaug有问题,请参阅https://blog.csdn.net/hesongzefairy/article/details/104693782

最后,安装pycocotools工具。 windows和linux的安装方法不同。 在windows上安装需要visual c 2015。 否则,请访问https://github.com/Phil ferriere/coco API安装vc 2015。

pipinstallgit https://github.com/Phil ferriere/coco API.git # subdirectory=python API的路径为d :anacondaenvs

至此,环境构建完成!

补:发现pip安装可能有问题。 在这里,您可以直接访问https://github.com/Phil ferriere/coco API下载项目,然后在anaconda的活动环境中进入cocoapiPythonAPI目录,执行以下命令

# installpycocotoolslocallypythonsetup.py build _ ext-- in place # installpycocotoolstothepythonsite-packagespythonsetup

样本里有气球和coco数据集。 看看名字就很容易分辨出来。 下载对应的东西就可以了

下载的mask_rcnn_coco.h5文件位于项目下的logs文件夹中

请注意,demo samples目录中的demo.ipynb文件在jupyter notebook上运行,在Anaconda附带的jupyter notebook上运行时,请选择相应的虚拟环境。 请参阅https://blog.csdn.net/Hesonnet

/104700167

这里我用Pycharm来复现,所以把demo文件直接转为py文件,小改动,大家可直接复制下面代码使用

import osimport sysimport randomimport mathimport numpy as npimport skimage.ioimport matplotlibimport matplotlib.pyplot as pltimport cv2import time# Root directory of the projectROOT_DIR = os.path.abspath("../")# Import Mask RCNNsys.path.append(ROOT_DIR) # To find local version of the libraryfrom mrcnn import utilsimport mrcnn.model as modellibfrom mrcnn import visualize# Import COCO configsys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local versionimport coco# Directory to save logs and trained modelMODEL_DIR = os.path.join(ROOT_DIR, "logs")# Local path to trained weights fileCOCO_MODEL_PATH = os.path.join(MODEL_DIR ,"mask_rcnn_coco.h5")# Download COCO trained weights from Releases if neededif not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH)# Directory of images to run detection onIMAGE_DIR = os.path.join(ROOT_DIR, "images")class InferenceConfig(coco.CocoConfig): # Set batch size to 1 since we'll be running inference on # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1config = InferenceConfig()config.display()# Create model object in inference mode.model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)# Load weights trained on MS-COCOmodel.load_weights(COCO_MODEL_PATH, by_name=True)# COCO Class names# Index of the class in the list is its ID. For example, to get ID of# the teddy bear class, use: class_names.index('teddy bear')class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']# Load a random image from the images folderfile_names = next(os.walk(IMAGE_DIR))[2]image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))# Run detectionresults = model.detect([image], verbose=1)# Visualize resultsr = results[0]visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])

注意,代码运行之前,还需要修改一个地方不然会报错:

UserWarning: Matplotlib is currently using agg,which is a non-GUI backend, so cannot show the figure

解决这个问题就需要用到之前说的pycocotool工具的路径,找到pycocotool文件夹修改其中的coco.py文件

matplotlib.use('Agg')改为matplotlib.use('TkAgg')即可

测试的图片是项目文件images中的图片,也可以换成自己的图片来测试,结果如下:

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。