当前位置: 首页 > article >正文

MaskRCNN训练自己的数据集

一. 数据标注

1. 安装labelme软件
conda install labelme
2. 运行labelme
# 命令行中直接输入
labelme
3. 标注

在这里插入图片描述
在这里插入图片描述

二、训练数据处理

1. 在根目录下创建datasets/demo1文件夹,创建如下几个文件夹

在这里插入图片描述

2. 将标注好的.json文件放在json文件夹中

在这里插入图片描述

3. 原图放在pic文件夹中

在这里插入图片描述

4. 批量处理JSON文件

现在的.json包含了我们标注的信息,但是还不是可以让代码直接读取的数据格式,对于不同的深度学习代码,数据存放的格式要根据不同代码的写法来定,下面以上述的mask-rcnn为例,将数据修改为我们需要的格式。

在json文件夹中创建test.bat脚本

@echo off
for %%i in (*.json) do labelme_json_to_dataset "%%i"
pause

在json文件夹中运行(要激活maskrcnn虚拟环境)

在这里插入图片描述
批处理生成的文件夹
在这里插入图片描述
在这里插入图片描述

5. 将批处理生成的文件夹复制到labelme_json文件夹中

在这里插入图片描述

6. 将labelme_json文件夹中的label.png复制到cv2_mask中

在这里插入图片描述
单个复制太麻烦,我们写脚本批处理, 创建copy.py

import os 
import shutil

for dir_name in os.listdir('./labelme_json'):
    pic_name = dir_name[:-5] + '.png'
    from_dir = './labelme_json/'+dir_name+'./label.png'
    to_dir = './cv2_mask/'+pic_name
    shutil.copyfile(from_dir, to_dir)
        
    print (from_dir)
    print (to_dir)

在这里插入图片描述
在这里插入图片描述

三、训练数据

根目录下创建train_test.py

# -*- coding: utf-8 -*-
'''
Author: qingqingdan 1306047688@qq.com
Date: 2024-11-22 17:48:33
LastEditTime: 2024-11-29 12:16:01
Description: labelme标注图片 训练自己的数据集
'''

import os
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf
from mrcnn.config import Config
#import utils
from mrcnn import model as modellib,utils
from mrcnn import visualize
import yaml
from mrcnn.model import log
from PIL import Image
"""
加入自己类别名称
更改类别个数
"""

#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# Root directory of the project
ROOT_DIR = os.getcwd()

#ROOT_DIR = os.path.abspath("../")
# 训练后的模型
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

iter_num=0

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)


#基础设置
dataset_root_path="datasets/demo2/"
img_floder = dataset_root_path + "pic"
mask_floder = dataset_root_path + "cv2_mask"
imglist = os.listdir(img_floder)
count = len(imglist)



class ShapesConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "shapes"

    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 2  # background + 2 shapes

    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 320
    IMAGE_MAX_DIM = 384

    # Use smaller anchors because our image and objects are small
    RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6)  # anchor side in pixels

    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE = 100

    # Use a small epoch since the data is simple
    STEPS_PER_EPOCH = 100

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 50


config = ShapesConfig()
config.display()

class DrugDataset(utils.Dataset):
    # 得到该图中有多少个实例(物体)
    def get_obj_index(self, image):
        n = np.max(image)
        return n

    # 解析labelme中得到的yaml文件,从而得到mask每一层对应的实例标签
    def from_yaml_get_class(self, image_id):
        info = self.image_info[image_id]
        with open(info['yaml_path']) as f:
            temp = yaml.load(f.read(), Loader=yaml.FullLoader)
            labels = temp['label_names']
            del labels[0]
        return labels

    # 重新写draw_mask
    def draw_mask(self, num_obj, mask, image,image_id):
        info = self.image_info[image_id]
        for index in range(num_obj):
            for i in range(info['width']):
                for j in range(info['height']):
                    at_pixel = image.getpixel((i, j))
                    if at_pixel == index + 1:
                        mask[j, i, index] = 1
        return mask

    # 重新写load_shapes,里面包含自己的自己的类别
    # 并在self.image_info信息中添加了path、mask_path 、yaml_path
    # yaml_pathdataset_root_path = "/tongue_dateset/"
    # img_floder = dataset_root_path + "rgb"
    # mask_floder = dataset_root_path + "mask"
    # dataset_root_path = "/tongue_dateset/"
    def load_shapes(self, count, img_floder, mask_floder, imglist, dataset_root_path):
        """Generate the requested number of synthetic images.
        count: number of images to generate.
        height, width: the size of the generated images.
        """
        # Add classes
        #self.add_class("shapes", 1, "tank") # 黑色素瘤
        #
        self.add_class("shapes", 1, "danhe") 
        self.add_class("shapes", 2, "linba")  

        for i in range(count):
            # 获取图片宽和高

            filestr = imglist[i].split(".")[0]
            
            mask_path = mask_floder + "/" + filestr + ".png"
            yaml_path = dataset_root_path + "labelme_json/" + filestr + "_json/info.yaml"
            print(dataset_root_path + "labelme_json/" + filestr + "_json/img.png")
            cv_img = cv2.imread(dataset_root_path + "labelme_json/" + filestr + "_json/img.png")

            self.add_image("shapes", image_id=i, path=img_floder + "/" + imglist[i],
                           width=cv_img.shape[1], height=cv_img.shape[0], mask_path=mask_path, yaml_path=yaml_path)

    # 重写load_mask
    def load_mask(self, image_id):
        """Generate instance masks for shapes of the given image ID.
        """
        global iter_num
        print("image_id",image_id)
        info = self.image_info[image_id]
        count = 1  # number of object
        img = Image.open(info['mask_path'])
        num_obj = self.get_obj_index(img)
        mask = np.zeros([info['height'], info['width'], num_obj], dtype=np.uint8)
        mask = self.draw_mask(num_obj, mask, img,image_id)
        occlusion = np.logical_not(mask[:, :, -1]).astype(np.uint8)
        for i in range(count - 2, -1, -1):
            mask[:, :, i] = mask[:, :, i] * occlusion

            occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
        labels = []
        labels = self.from_yaml_get_class(image_id)
        labels_form = []
        for i in range(len(labels)):
            if labels[i].find("danhe") != -1:
                labels_form.append("danhe")
            if labels[i].find("linba") != -1:
                labels_form.append("linba")

        class_ids = np.array([self.class_names.index(s) for s in labels_form])
        return mask, class_ids.astype(np.int32)

def get_ax(rows=1, cols=1, size=8):
    """Return a Matplotlib Axes array to be used in
    all visualizations in the notebook. Provide a
    central point to control graph sizes.

    Change the default size attribute to control the size
    of rendered images
    """
    _, ax = plt.subplots(rows, cols, figsize=(size * cols, size * rows))
    return ax


#train与val数据集准备
dataset_train = DrugDataset()
dataset_train.load_shapes(count, img_floder, mask_floder, imglist,dataset_root_path)
dataset_train.prepare()

print("dataset_train-->",dataset_train._image_ids)

dataset_val = DrugDataset()
dataset_val.load_shapes(7, img_floder, mask_floder, imglist,dataset_root_path)
dataset_val.prepare()

print("dataset_val-->",dataset_val._image_ids)

# Load and display random samples
#image_ids = np.random.choice(dataset_train.image_ids, 4)
#for image_id in image_ids:
#    image = dataset_train.load_image(image_id)
#    mask, class_ids = dataset_train.load_mask(image_id)
#    visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names)

# Create model in training mode
model = modellib.MaskRCNN(mode="training", config=config,
                          model_dir=MODEL_DIR)

# Which weights to start with?
init_with = "coco"  # imagenet, coco, or last

if init_with == "imagenet":
    model.load_weights(model.get_imagenet_weights(), by_name=True)
elif init_with == "coco":
    # Load weights trained on MS COCO, but skip layers that
    # are different due to the different number of classes
    # See README for instructions to download the COCO weights
    model.load_weights(COCO_MODEL_PATH, by_name=True,
                       exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
                                "mrcnn_bbox", "mrcnn_mask"])
elif init_with == "last":
    # Load the last model you trained and continue training
    model.load_weights(model.find_last()[1], by_name=True)

# Train the head branches
# Passing layers="heads" freezes all layers except the head
# layers. You can also pass a regular expression to select
# which layers to train by name pattern.
model.train(dataset_train, dataset_val,
            learning_rate=config.LEARNING_RATE,
            epochs=10,
            layers='heads')



# Fine tune all layers
# Passing layers="all" trains all layers. You can also
# pass a regular expression to select which layers to
# train by name pattern.
model.train(dataset_train, dataset_val,
            learning_rate=config.LEARNING_RATE / 10,
            epochs=10,
            layers="all")

代码有3处需要修改的地方
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、预测数据

1. 训练完成后,会在根目录下生成logs文件夹,选择最后生成的训练模型

在这里插入图片描述

2. 根目录下创建预测脚本 model_test.py
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import cv2
import time
from mrcnn.config import Config
from datetime import datetime 

import colorsys
from skimage.measure import find_contours

from PIL import Image

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
from samples.coco import coco


# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
MODEL_WEIGHT = './logs/shapes20241122T2251/mask_rcnn_shapes_0010.h5'

# 预测结果保存地址
OUTPUT_DIR = "./output_results"
if not os.path.exists(OUTPUT_DIR):
    os.makedirs(OUTPUT_DIR)

# 获取待预测文件名称,用于保存同名文件
def get_last_part_of_string(path):
    return os.path.basename(path)
    
# 需要预测的数据地址
IMAGE_DIR = os.path.join(ROOT_DIR, "datasets/demo2/pic")

class ShapesConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "shapes"
    # os.path.exists()
    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 2  # background + 3 shapes

    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 320
    IMAGE_MAX_DIM = 384

    # Use smaller anchors because our image and objects are small
    RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6)  # anchor side in pixels

    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE =100

    # Use a small epoch since the data is simple
    STEPS_PER_EPOCH = 100

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 50

#import train_tongue
#class InferenceConfig(coco.CocoConfig):
class InferenceConfig(ShapesConfig):
    # 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 = 1



# 以下def函数从 mrcnn/visualize.py 中复制过来

def apply_mask(image, mask, color, alpha=0.5):
    """
    打上mask图标
    """
    for c in range(3):
        image[:, :, c] = np.where(mask == 1,
                                  image[:, :, c] *
                                  (1 - alpha) + alpha * color[c] * 255,
                                  image[:, :, c])
    return image



def random_colors(N, bright=True):
    """
    生成随机颜色
    """
    brightness = 1.0 if bright else 0.7
    hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    return colors


# 绘制结果
def display_instances(image, boxes, masks, class_ids, class_names, scores=None, 
                      filename="", title="",
                      figsize=(16, 16),
                      show_mask=True, show_bbox=True,
                      colors=None, captions=None):
    # instance的数量
    N = boxes.shape[0]
    if not N:
        print("\n*** No instances to display *** \n")
    else:
        assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]
    colors = colors or random_colors(N)
 
    # 当masked_image为原图时是在原图上绘制
    # 如果不想在原图上绘制,可以把masked_image设置成等大小的全0矩阵
    masked_image = np.array(image, np.uint8)
    for i in range(N):
        color = colors[i]
 
        # 该部分用于显示bbox
        if not np.any(boxes[i]):
            continue
        y1, x1, y2, x2 = boxes[i]

        # 绘制方形边界框
        if show_bbox:
            # 打印边界框坐标
            # print(f"Box {i + 1}: (x1, y1, x2, y2) = ({x1}, {y1}, {x2}, {y2})")
            cropped_image_rgb = image[y1:y2, x1:x2]
            image_bgr = cv2.cvtColor(cropped_image_rgb, cv2.COLOR_RGB2BGR)
            
            # cv2.rectangle(masked_image, (x1, y1), (x2, y2), (color[0] * 255, color[1] * 255, color[2] * 255), 2)
 
        # 该部分用于显示文字与置信度
        if not captions:
            class_id = class_ids[i]
            score = scores[i] if scores is not None else None
            label = class_names[class_id]
            caption = "{} {:.3f}".format(label, score) if score else label
        else:
            caption = captions[i]
        
        # 绘制文字(类别、分数)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(masked_image, caption, (x1, y1 + 8), font, 0.5, (255, 255, 255), 1)
 
        # 绘制语义分割(遮挡物体面积部分)
        mask = masks[:, :, i]
        if show_mask:
            masked_image = apply_mask(masked_image, mask, color)
 
        # 画出语义分割的范围
        padded_mask = np.zeros(
            (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
        padded_mask[1:-1, 1:-1] = mask
        contours = find_contours(padded_mask, 0.5)

        # 绘制边缘轮廓
        for verts in contours:
            verts = np.fliplr(verts) - 1
            cv2.polylines(masked_image, [np.array([verts], np.int)], 1,
                          (color[0] * 255, color[1] * 255, color[2] * 255), 1)


    # 将绘制好的图片保存在制定文件夹中
    save_path = os.path.join(OUTPUT_DIR, filename)
    cv2.imwrite(save_path, masked_image)

    img = Image.fromarray(np.uint8(masked_image))
    return img


# 加载配置
config = InferenceConfig()

# 加载模型
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

#  加载权重
model.load_weights(MODEL_WEIGHT, 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', 'danhe', 'linba']
# Load a random image from the images folder


############################## 单张预测图片 ################################
# file_names = next(os.walk(IMAGE_DIR))[2]
# # 随机读取一张图片,进行预测
# image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
# # 当前图片名称
# filename = random.choice(file_names)

# a=datetime.now() 
# # Run detection
# results = model.detect([image], verbose=1)
# b=datetime.now() 
# # Visualize results
# print("time:",(b-a).seconds)
# r = results[0]
# display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], filename)


# # 获取物体数量
# num_objects = 0
# for roi, class_id, score, mask in zip(r['rois'],  r['class_ids'], r['scores'], r['masks']):
#     if score > 0.5:  # 设置置信度阈值
#         num_objects += 1
 
# print('Number of objects detected:', num_objects)





############################## 批量预测图片 ################################
count = os.listdir(IMAGE_DIR)
 
for i in range(0,len(count)):
    path = os.path.join(IMAGE_DIR, count[i])
    if os.path.isfile(path):
        file_names = next(os.walk(IMAGE_DIR))[2]
        image = skimage.io.imread(os.path.join(IMAGE_DIR, count[i]))
        # Run detection
        results = model.detect([image], verbose=1)
        r = results[0]
        print('FileNmame: ',count[i])
        display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], count[i])

http://www.kler.cn/a/415551.html

相关文章:

  • flutter gradle版本更新到8.9
  • 【C/C++】内存管理详解:从new/delete到智能指针的全面解析
  • Harbor安装、HTTPS配置、修改端口后不可访问?
  • 电脑无互联网连接怎么解决?分享5种解决方案
  • 重学 Android 自定义 View 系列(八):星星评分控件(RatingBar)
  • Linux中使用ping提示“未知的名称或服务”
  • OpenCV 图像轮廓查找与绘制全攻略:从函数使用到实战应用详解
  • 前端开发:构建高质量用户体验的全方位指南(含实际案例与示例)
  • 社群赋能电商:小程序 AI 智能名片与 S2B2C 商城系统的整合与突破
  • 实例分析基于RFCOMM协议大数据传输以及流控
  • 快速排序及其优化【图文详解】
  • falsk-模型基础
  • Android 12.0 DocumentsUI文件管理器首次进入默认显示内部存储文件功能实现
  • 篡改代码事件升级,字节索赔800万
  • Android 图形系统之四:Choreographer
  • 【verilog教程】verilog函数
  • wpf 的MVVM
  • 《数据挖掘:概念、模型、方法与算法(第三版)》
  • 阈值分割创新点探究(附带opencv c++代码)
  • leetcode:637二叉树的层平均值
  • 【力扣双周赛 144】贪心堆 网格图 DP
  • 重塑用户体验!快手电商智能巡检平台的实践与探索
  • 跨平台应用开发框架(4)----Qt(系统篇)
  • MarsCode青训营序章Day1|稀土掘金-1.找单独的数、47.完美偶数计数、3.数组字符格式化
  • 【Java基础入门篇】一、变量、数据类型和运算符
  • 数据结构---链表