我已经训练了 https://github.com/matterport/Mask_RCNN 的实例分割模型,以便在我的数据集上运行。
我的假设是您已完成所有基本设置,并且模型已经使用默认数据集(在 repo 中提供)运行,现在您希望它为自定义数据集运行。
以下是步骤
- 您需要拥有所有注释。
- 所有这些都需要转换为 VGG 多边形模式(是的,我的意思是多边形,即使您需要绑定框)。我在这个答案的末尾添加了一个示例 VGG 多边形格式。
- 您需要将自定义数据集划分为训练、测试和验证
- 默认情况下,注解在单个数据集文件夹中使用文件名
via_region_data.json 进行查看。例如,对于训练图像,它会查看train\via_region_data.json。您也可以根据需要进行更改。
- 在 Samples 文件夹中,您可以找到 Balloon、Nucleus、Shapes 等文件夹。复制其中一个文件夹。最好是气球。我们现在将尝试为我们的自定义数据集修改这个新文件夹。
- 在复制的文件夹中,您将有一个
.py 文件(对于气球,它将是气球.py),更改以下变量
-
ROOT_DIR : 克隆项目的绝对路径
-
DEFAULT_LOGS_DIR :这个文件夹会变大,所以相应地改变这个路径(如果你在低磁盘存储虚拟机中运行你的代码)。它还将存储.h5 文件。它将在日志文件夹中创建子文件夹,并附加时间戳。
-
.h5 文件大约为每个 epoch 200 - 300 MB。但是猜猜这个日志目录与 Tensorboard 兼容。您可以在运行 tensorboard 时将带时间戳的子文件夹作为 --logdir 参数传递。
- 这个
.py 文件也有两个类——一个类后缀为Config,另一个类后缀为Dataset。
- 在 Config 类中覆盖所需的内容,例如
-
NAME :您的项目的名称。
-
NUM_CLASSES : 它应该比你的标签类多一个,因为背景也被认为是一个标签
-
DETECTION_MIN_CONFIDENCE :默认为 0.9(如果您的训练图像质量不是很高或您没有太多训练数据,则将其减小)
-
STEPS_PER_EPOCH等
- 在 Dataset 类中重写以下方法。所有这些功能都已经很好地注释了,因此您可以按照 cmets 根据您的需要进行覆盖。
- load_(name_of_the_sample_project) 例如 load_balloon
- load_mask(请参阅答案的最后一个示例)
- 图像参考
- train 函数(在 Dataset 类之外):如果您必须更改 epoch 数或学习率等
您现在可以直接从终端运行它
python samples\your_folder_name\your_python_file_name.py train --dataset="location_of_custom_dataset" --weights=coco
有关上述行的命令行参数的完整信息,您可以在此 .py 文件的顶部将其作为注释查看。
这些是我能记得的事情,我想添加更多我记得的步骤。如果您卡在任何特定步骤,也许您可以告诉我,我会详细说明该特定步骤。
VGG 多边形架构
宽度和高度是可选的
[{
"filename": "000dfce9-f14c-4a25-89b6-226316f557f3.jpeg",
"regions": {
"0": {
"region_attributes": {
"object_name": "Cat"
},
"shape_attributes": {
"all_points_x": [75.30864197530865, 80.0925925925926, 80.0925925925926, 75.30864197530865],
"all_points_y": [11.672189112257607, 11.672189112257607, 17.72093488703078, 17.72093488703078],
"name": "polygon"
}
},
"1": {
"region_attributes": {
"object_name": "Cat"
},
"shape_attributes": {
"all_points_x": [80.40123456790124, 84.64506172839506, 84.64506172839506, 80.40123456790124],
"all_points_y": [8.114103362391036, 8.114103362391036, 12.205901974737595, 12.205901974737595],
"name": "polygon"
}
}
},
"width": 504,
"height": 495
}]
示例 load_mask 函数
def load_mask(self, image_id):
"""Generate instance masks for an image.
Returns:
masks: A bool array of shape [height, width, instance count] with
one mask per instance.
class_ids: a 1D array of class IDs of the instance masks.
"""
# If not your dataset image, delegate to parent class.
image_info = self.image_info[image_id]
if image_info["source"] != "name_of_your_project": //change your project name
return super(self.__class__, self).load_mask(image_id)
# Convert polygons to a bitmap mask of shape
# [height, width, instance_count]
info = self.image_info[image_id]
mask = np.zeros([info["height"], info["width"], len(info["polygons"])], dtype=np.uint8)
class_id = np.zeros([mask.shape[-1]], dtype=np.int32)
for i, p in enumerate(info["polygons"]):
# Get indexes of pixels inside the polygon and set them to 1
rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
# print(rr.shape, cc.shape, i, np.ones([mask.shape[-1]], dtype=np.int32).shape, info['classes'][i])
class_id[i] = self.class_dict[info['classes'][i]]
mask[rr, cc, i] = 1
# Return mask, and array of class IDs of each instance. Since we have
# one class ID only, we return an array of 1s
return mask.astype(np.bool), class_id