DL之MaskR-CNN:基于类MaskR-CNN算法(RetinaNet+mask head)利用数据集(resnet50_coco_v0.2.0.h5)实现图像分割(一)

简介: DL之MaskR-CNN:基于类MaskR-CNN算法(RetinaNet+mask head)利用数据集(resnet50_coco_v0.2.0.h5)实现图像分割

输出结果


image.png

image.png


image.png


image.png




设计思路


参考文章:DL之MaskR-CNN:Mask R-CNN算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

   在ResNet的基础上,增加了ROI_Align、mask_submodel、masks(ConcatenateBoxes,计算loss的拼接)。

image.png




核心代码



1、retinanet.py


default_mask_model函数内,定义了类别个数num_classes、金字塔特征的大小pyramid_feature_size=256等

   mask_feature_size=256,

   roi_size=(14, 14),

   mask_size=(28, 28),

"""

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

"""

import keras

import keras.backend

import keras.models

import keras_retinanet.layers

import keras_retinanet.models.retinanet

import keras_retinanet.backend.tensorflow_backend as backend

from ..layers.roi import RoiAlign

from ..layers.upsample import Upsample

from ..layers.misc import Shape, ConcatenateBoxes, Cast

def default_mask_model(

   num_classes,

   pyramid_feature_size=256,

   mask_feature_size=256,

   roi_size=(14, 14),

   mask_size=(28, 28),

   name='mask_submodel',

   mask_dtype=keras.backend.floatx(),

   retinanet_dtype=keras.backend.floatx()

):

   options = {

       'kernel_size'        : 3,

       'strides'            : 1,

       'padding'            : 'same',

       'kernel_initializer' : keras.initializers.normal(mean=0.0, stddev=0.01, seed=None),

       'bias_initializer'   : 'zeros',

       'activation'         : 'relu',

   }

   inputs  = keras.layers.Input(shape=(None, roi_size[0], roi_size[1], pyramid_feature_size))

   outputs = inputs

   # casting to the desidered data type, which may be different than

   # the one used for the underlying keras-retinanet model

   if mask_dtype != retinanet_dtype:

       outputs = keras.layers.TimeDistributed(

           Cast(dtype=mask_dtype),

           name='cast_masks')(outputs)

   for i in range(4):

       outputs = keras.layers.TimeDistributed(keras.layers.Conv2D(

           filters=mask_feature_size,

           **options

       ), name='roi_mask_{}'.format(i))(outputs)

   # perform upsampling + conv instead of deconv as in the paper

   # https://distill.pub/2016/deconv-checkerboard/

   outputs = keras.layers.TimeDistributed(

       Upsample(mask_size),

       name='roi_mask_upsample')(outputs)

   outputs = keras.layers.TimeDistributed(keras.layers.Conv2D(

       filters=mask_feature_size,

       **options

   ), name='roi_mask_features')(outputs)

   outputs = keras.layers.TimeDistributed(keras.layers.Conv2D(

       filters=num_classes,

       kernel_size=1,

       activation='sigmoid'

   ), name='roi_mask')(outputs)

   # casting back to the underlying keras-retinanet model data type

   if mask_dtype != retinanet_dtype:

       outputs = keras.layers.TimeDistributed(

           Cast(dtype=retinanet_dtype),

           name='recast_masks')(outputs)

   return keras.models.Model(inputs=inputs, outputs=outputs, name=name)

def default_roi_submodels(num_classes, mask_dtype=keras.backend.floatx(), retinanet_dtype=keras.backend.floatx()):

   return [

       ('masks', default_mask_model(num_classes, mask_dtype=mask_dtype, retinanet_dtype=retinanet_dtype)),

   ]

def retinanet_mask(

   inputs,

   num_classes,

   retinanet_model=None,

   anchor_params=None,

   nms=True,

   class_specific_filter=True,

   name='retinanet-mask',

   roi_submodels=None,

   mask_dtype=keras.backend.floatx(),

   modifier=None,

   **kwargs

):

   """ Construct a RetinaNet mask model on top of a retinanet bbox model.

   This model uses the retinanet bbox model and appends a few layers to compute masks.

   # Arguments

       inputs                : List of keras.layers.Input. The first input is the image, the second input the blob of masks.

       num_classes           : Number of classes to classify.

       retinanet_model       : keras_retinanet.models.retinanet model, returning regression and classification values.

       anchor_params         : Struct containing anchor parameters. If None, default values are used.

       nms                   : Use NMS.

       class_specific_filter : Use class specific filtering.

       roi_submodels         : Submodels for processing ROIs.

       mask_dtype            : Data type of the masks, can be different from the main one.

       modifier              : Modifier for the underlying retinanet model, such as freeze.

       name                  : Name of the model.

       **kwargs              : Additional kwargs to pass to the retinanet bbox model.

   # Returns

       Model with inputs as input and as output the output of each submodel for each pyramid level and the detections.

       The order is as defined in submodels.

       ```

       [

           regression, classification, other[0], other[1], ..., boxes_masks, boxes, scores, labels, masks, other[0], other[1], ...

       ]

       ```

   """

   if anchor_params is None:

       anchor_params = keras_retinanet.utils.anchors.AnchorParameters.default

   if roi_submodels is None:

       retinanet_dtype = keras.backend.floatx()

       keras.backend.set_floatx(mask_dtype)

       roi_submodels = default_roi_submodels(num_classes, mask_dtype, retinanet_dtype)

       keras.backend.set_floatx(retinanet_dtype)

   image = inputs

   image_shape = Shape()(image)

   if retinanet_model is None:

       retinanet_model = keras_retinanet.models.retinanet.retinanet(

           inputs=image,

           num_classes=num_classes,

           num_anchors=anchor_params.num_anchors(),

           **kwargs

       )

   if modifier:

       retinanet_model = modifier(retinanet_model)

   # parse outputs

   regression     = retinanet_model.outputs[0]

   classification = retinanet_model.outputs[1]

   other          = retinanet_model.outputs[2:]

   features       = [retinanet_model.get_layer(name).output for name in ['P3', 'P4', 'P5', 'P6', 'P7']]

   # build boxes

   anchors = keras_retinanet.models.retinanet.__build_anchors(anchor_params, features)

   boxes = keras_retinanet.layers.RegressBoxes(name='boxes')([anchors, regression])

   boxes = keras_retinanet.layers.ClipBoxes(name='clipped_boxes')([image, boxes])

   # filter detections (apply NMS / score threshold / select top-k)

   detections = keras_retinanet.layers.FilterDetections(

       nms                   = nms,

       class_specific_filter = class_specific_filter,

       max_detections        = 100,

       name                  = 'filtered_detections'

   )([boxes, classification] + other)

   # split up in known outputs and "other"

   boxes  = detections[0]

   scores = detections[1]

   # get the region of interest features

   rois = RoiAlign()([image_shape, boxes, scores] + features)

   # execute maskrcnn submodels

   maskrcnn_outputs = [submodel(rois) for _, submodel in roi_submodels]

   # concatenate boxes for loss computation

   trainable_outputs = [ConcatenateBoxes(name=name)([boxes, output]) for (name, _), output in zip(roi_submodels, maskrcnn_outputs)]

   # reconstruct the new output

   outputs = [regression, classification] + other + trainable_outputs + detections + maskrcnn_outputs

   return keras.models.Model(inputs=inputs, outputs=outputs, name=name)


相关文章
|
7月前
|
机器学习/深度学习 安全 Serverless
【创新未发表】【故障诊断】基于连续小波变换-CNN, ResNet, CNN-SVM, CNN-BiGRU, CNN-LSTM的故障诊断研究【凯斯西储大学数据】(Matlab代码实现)
【创新未发表】【故障诊断】基于连续小波变换-CNN, ResNet, CNN-SVM, CNN-BiGRU, CNN-LSTM的故障诊断研究【凯斯西储大学数据】(Matlab代码实现)
527 0
|
9月前
|
机器学习/深度学习 算法 安全
深度长文I 深度合成服务类-算法备案该怎么做?
本文详解“深度合成服务类”算法及其备案要求,涵盖定义、类型、备案流程等内容,助你全面理解合规要点。
|
机器学习/深度学习 人工智能 算法
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
鸟类识别系统。本系统采用Python作为主要开发语言,通过使用加利福利亚大学开源的200种鸟类图像作为数据集。使用TensorFlow搭建ResNet50卷积神经网络算法模型,然后进行模型的迭代训练,得到一个识别精度较高的模型,然后在保存为本地的H5格式文件。在使用Django开发Web网页端操作界面,实现用户上传一张鸟类图像,识别其名称。
780 12
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
|
算法 数据挖掘 数据安全/隐私保护
基于FCM模糊聚类算法的图像分割matlab仿真
本项目展示了基于模糊C均值(FCM)算法的图像分割技术。算法运行效果良好,无水印。使用MATLAB 2022a开发,提供完整代码及中文注释,附带操作步骤视频。FCM算法通过隶属度矩阵和聚类中心矩阵实现图像分割,适用于灰度和彩色图像,广泛应用于医学影像、遥感图像等领域。
|
存储 算法 Java
Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性
Java Set因其“无重复”特性在集合框架中独树一帜。本文解析了Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性,并提供了最佳实践建议,包括选择合适的Set实现类和正确实现自定义对象的hashCode()与equals()方法。
408 4
|
搜索推荐 算法 Java
现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法
该博客文章通过UML类图和Java源码示例,展示了如何使用适配器模式将QuickSort类和BinarySearch类的排序和查找功能适配到DataOperation接口中,实现算法的解耦和复用。
257 1
现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法
|
机器学习/深度学习 人工智能 算法
【昆虫识别系统】图像识别Python+卷积神经网络算法+人工智能+深度学习+机器学习+TensorFlow+ResNet50
昆虫识别系统,使用Python作为主要开发语言。通过TensorFlow搭建ResNet50卷积神经网络算法(CNN)模型。通过对10种常见的昆虫图片数据集('蜜蜂', '甲虫', '蝴蝶', '蝉', '蜻蜓', '蚱蜢', '蛾', '蝎子', '蜗牛', '蜘蛛')进行训练,得到一个识别精度较高的H5格式模型文件,然后使用Django搭建Web网页端可视化操作界面,实现用户上传一张昆虫图片识别其名称。
1116 7
【昆虫识别系统】图像识别Python+卷积神经网络算法+人工智能+深度学习+机器学习+TensorFlow+ResNet50
|
机器学习/深度学习 数据采集 数据可视化
深度学习实践:构建并训练卷积神经网络(CNN)对CIFAR-10数据集进行分类
本文详细介绍如何使用PyTorch构建并训练卷积神经网络(CNN)对CIFAR-10数据集进行图像分类。从数据预处理、模型定义到训练过程及结果可视化,文章全面展示了深度学习项目的全流程。通过实际操作,读者可以深入了解CNN在图像分类任务中的应用,并掌握PyTorch的基本使用方法。希望本文为您的深度学习项目提供有价值的参考与启示。
创建KNN类
【7月更文挑战第22天】创建KNN类。
160 8
|
算法 数据库

热门文章

最新文章

下一篇
开通oss服务