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)实现图像分割

2、resnet.py

作为骨架,resnet_maskrcnn模型,代码中,也可选用resnet50、resnet101、resnet152骨架模型。

"""

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 warnings

import keras

import keras_resnet

import keras_resnet.models

import keras_retinanet.models.resnet

from ..models import retinanet, Backbone

class ResNetBackbone(Backbone, keras_retinanet.models.resnet.ResNetBackbone):

   def maskrcnn(self, *args, **kwargs):

       """ Returns a maskrcnn model using the correct backbone.

       """

       return resnet_maskrcnn(*args, backbone=self.backbone, **kwargs)

def resnet_maskrcnn(num_classes, backbone='resnet50', inputs=None, modifier=None, mask_dtype=keras.backend.floatx(), **kwargs):

   # choose default input

   if inputs is None:

       inputs = keras.layers.Input(shape=(None, None, 3), name='image')

   # create the resnet backbone

   if backbone == 'resnet50':

       resnet = keras_resnet.models.ResNet50(inputs, include_top=False, freeze_bn=True)

   elif backbone == 'resnet101':

       resnet = keras_resnet.models.ResNet101(inputs, include_top=False, freeze_bn=True)

   elif backbone == 'resnet152':

       resnet = keras_resnet.models.ResNet152(inputs, include_top=False, freeze_bn=True)

   # invoke modifier if given

   if modifier:

       resnet = modifier(resnet)

   # create the full model

   model = retinanet.retinanet_mask(inputs=inputs, num_classes=num_classes, backbone_layers=resnet.outputs[1:], mask_dtype=mask_dtype, **kwargs)

   return model

def resnet50_maskrcnn(num_classes, inputs=None, **kwargs):

   return resnet_maskrcnn(num_classes=num_classes, backbone='resnet50', inputs=inputs, **kwargs)

def resnet101_maskrcnn(num_classes, inputs=None, **kwargs):

   return resnet_maskrcnn(num_classes=num_classes, backbone='resnet101', inputs=inputs, **kwargs)

def resnet152_maskrcnn(num_classes, inputs=None, **kwargs):

   return resnet_maskrcnn(num_classes=num_classes, backbone='resnet152', inputs=inputs, **kwargs)


3、roi.py


包含RoiAlign类的实现,包含map_to_level等计算函数


import keras.backend

import keras.layers

import keras_retinanet.backend

from .. import backend

class RoiAlign(keras.layers.Layer):

   def __init__(self, crop_size=(14, 14), **kwargs):

       self.crop_size = crop_size

       super(RoiAlign, self).__init__(**kwargs)

   def map_to_level(self, boxes, canonical_size=224, canonical_level=1, min_level=0, max_level=4):

       x1 = boxes[:, 0]

       y1 = boxes[:, 1]

       x2 = boxes[:, 2]

       y2 = boxes[:, 3]

       w = x2 - x1

       h = y2 - y1

       size = keras.backend.sqrt(w * h)

       levels = backend.floor(canonical_level + backend.log2(size / canonical_size + keras.backend.epsilon()))

       levels = keras.backend.clip(levels, min_level, max_level)

       return levels

   def call(self, inputs, **kwargs):

       # TODO: Support batch_size > 1

       image_shape = keras.backend.cast(inputs[0], keras.backend.floatx())

       boxes       = keras.backend.stop_gradient(inputs[1][0])

       scores      = keras.backend.stop_gradient(inputs[2][0])

       fpn         = [keras.backend.stop_gradient(i[0]) for i in inputs[3:]]

       # compute from which level to get features from

       target_levels = self.map_to_level(boxes)

       # process each pyramid independently

       rois           = []

       ordered_indices = []

       for i in range(len(fpn)):

           # select the boxes and classification from this pyramid level

           indices = keras_retinanet.backend.where(keras.backend.equal(target_levels, i))

           ordered_indices.append(indices)

           level_boxes = keras_retinanet.backend.gather_nd(boxes, indices)

           fpn_shape   = keras.backend.cast(keras.backend.shape(fpn[i]), dtype=keras.backend.floatx())

           # convert to expected format for crop_and_resize

           x1 = level_boxes[:, 0]

           y1 = level_boxes[:, 1]

           x2 = level_boxes[:, 2]

           y2 = level_boxes[:, 3]

           level_boxes = keras.backend.stack([

               (y1 / image_shape[1] * fpn_shape[0]) / (fpn_shape[0] - 1),

               (x1 / image_shape[2] * fpn_shape[1]) / (fpn_shape[1] - 1),

               (y2 / image_shape[1] * fpn_shape[0] - 1) / (fpn_shape[0] - 1),

               (x2 / image_shape[2] * fpn_shape[1] - 1) / (fpn_shape[1] - 1),

           ], axis=1)

           # append the rois to the list of rois

           rois.append(backend.crop_and_resize(

               keras.backend.expand_dims(fpn[i], axis=0),

               level_boxes,

               keras.backend.zeros((keras.backend.shape(level_boxes)[0],), dtype='int32'),

               self.crop_size

           ))

       # concatenate rois to one blob

       rois = keras.backend.concatenate(rois, axis=0)

       # reorder rois back to original order

       indices = keras.backend.concatenate(ordered_indices, axis=0)

       rois    = keras_retinanet.backend.scatter_nd(indices, rois, keras.backend.cast(keras.backend.shape(rois), 'int64'))

       return keras.backend.expand_dims(rois, axis=0)

   def compute_output_shape(self, input_shape):

       return (input_shape[1][0], None, self.crop_size[0], self.crop_size[1], input_shape[3][-1])

   def get_config(self):

       config = super(RoiAlign, self).get_config()

       config.update({

           'crop_size' : self.crop_size,

       })

       return config

4、losses.py


MaskR-CNN计算损失

       # compute mask loss

       mask_loss  = keras.backend.binary_crossentropy(masks_target, masks)

       normalizer = keras.backend.shape(masks)[0] * keras.backend.shape(masks)[1] * keras.backend.shape(masks)[2]

       normalizer = keras.backend.maximum(keras.backend.cast(normalizer, keras.backend.floatx()), 1)

       mask_loss  = keras.backend.sum(mask_loss) / normalizer


import keras.backend

import keras_retinanet.backend

from . import backend

def mask(iou_threshold=0.5, mask_size=(28, 28)):

   def _mask_conditional(y_true, y_pred):

       # if there are no masks annotations, return 0; else, compute the masks loss

       loss = backend.cond(

           keras.backend.any(keras.backend.equal(keras.backend.shape(y_true), 0)),

           lambda: keras.backend.cast_to_floatx(0.0),

           lambda: _mask(y_true, y_pred, iou_threshold=iou_threshold, mask_size=mask_size)

       )

       return loss

   def _mask(y_true, y_pred, iou_threshold=0.5, mask_size=(28, 28)):

       # split up the different predicted blobs

       boxes = y_pred[:, :, :4]

       masks = y_pred[:, :, 4:]

       # split up the different blobs

       annotations  = y_true[:, :, :5]

       width        = keras.backend.cast(y_true[0, 0, 5], dtype='int32')

       height       = keras.backend.cast(y_true[0, 0, 6], dtype='int32')

       masks_target = y_true[:, :, 7:]

       # reshape the masks back to their original size

       masks_target = keras.backend.reshape(masks_target, (keras.backend.shape(masks_target)[0], keras.backend.shape(masks_target)[1], height, width))

       masks        = keras.backend.reshape(masks, (keras.backend.shape(masks)[0], keras.backend.shape(masks)[1], mask_size[0], mask_size[1], -1))

       # TODO: Fix batch_size > 1

       boxes        = boxes[0]

       masks        = masks[0]

       annotations  = annotations[0]

       masks_target = masks_target[0]

       # compute overlap of boxes with annotations

       iou                  = backend.overlap(boxes, annotations)

       argmax_overlaps_inds = keras.backend.argmax(iou, axis=1)

       max_iou              = keras.backend.max(iou, axis=1)

       # filter those with IoU > 0.5

       indices              = keras_retinanet.backend.where(keras.backend.greater_equal(max_iou, iou_threshold))

       boxes                = keras_retinanet.backend.gather_nd(boxes, indices)

       masks                = keras_retinanet.backend.gather_nd(masks, indices)

       argmax_overlaps_inds = keras.backend.cast(keras_retinanet.backend.gather_nd(argmax_overlaps_inds, indices), 'int32')

       labels               = keras.backend.cast(keras.backend.gather(annotations[:, 4], argmax_overlaps_inds), 'int32')

       # make normalized boxes

       x1 = boxes[:, 0]

       y1 = boxes[:, 1]

       x2 = boxes[:, 2]

       y2 = boxes[:, 3]

       boxes = keras.backend.stack([

           y1 / (keras.backend.cast(height, dtype=keras.backend.floatx()) - 1),

           x1 / (keras.backend.cast(width, dtype=keras.backend.floatx()) - 1),

           (y2 - 1) / (keras.backend.cast(height, dtype=keras.backend.floatx()) - 1),

           (x2 - 1) / (keras.backend.cast(width, dtype=keras.backend.floatx()) - 1),

       ], axis=1)

       # crop and resize masks_target

       masks_target = keras.backend.expand_dims(masks_target, axis=3)  # append a fake channel dimension

       masks_target = backend.crop_and_resize(

           masks_target,

           boxes,

           argmax_overlaps_inds,

           mask_size

       )

       masks_target = masks_target[:, :, :, 0]  # remove fake channel dimension

       # gather the predicted masks using the annotation label

       masks = backend.transpose(masks, (0, 3, 1, 2))

       label_indices = keras.backend.stack([

           keras.backend.arange(keras.backend.shape(labels)[0]),

           labels

       ], axis=1)

       masks = keras_retinanet.backend.gather_nd(masks, label_indices)

       # compute mask loss

       mask_loss  = keras.backend.binary_crossentropy(masks_target, masks)

       normalizer = keras.backend.shape(masks)[0] * keras.backend.shape(masks)[1] * keras.backend.shape(masks)[2]

       normalizer = keras.backend.maximum(keras.backend.cast(normalizer, keras.backend.floatx()), 1)

       mask_loss  = keras.backend.sum(mask_loss) / normalizer

       return mask_loss

   return _mask_conditional



 


相关文章
|
机器学习/深度学习 算法 数据挖掘
K-means聚类算法是机器学习中常用的一种聚类方法,通过将数据集划分为K个簇来简化数据结构
K-means聚类算法是机器学习中常用的一种聚类方法,通过将数据集划分为K个簇来简化数据结构。本文介绍了K-means算法的基本原理,包括初始化、数据点分配与簇中心更新等步骤,以及如何在Python中实现该算法,最后讨论了其优缺点及应用场景。
1977 6
|
机器学习/深度学习 安全 Serverless
【创新未发表】【故障诊断】基于连续小波变换-CNN, ResNet, CNN-SVM, CNN-BiGRU, CNN-LSTM的故障诊断研究【凯斯西储大学数据】(Matlab代码实现)
【创新未发表】【故障诊断】基于连续小波变换-CNN, ResNet, CNN-SVM, CNN-BiGRU, CNN-LSTM的故障诊断研究【凯斯西储大学数据】(Matlab代码实现)
811 0
|
机器学习/深度学习 算法 数据可视化
利用SVM(支持向量机)分类算法对鸢尾花数据集进行分类
本文介绍了如何使用支持向量机(SVM)算法对鸢尾花数据集进行分类。作者通过Python的sklearn库加载数据,并利用pandas、matplotlib等工具进行数据分析和可视化。
1586 70
|
机器学习/深度学习 算法 PyTorch
【从零开始学习深度学习】38. Pytorch实战案例:梯度下降、随机梯度下降、小批量随机梯度下降3种优化算法对比【含数据集与源码】
【从零开始学习深度学习】38. Pytorch实战案例:梯度下降、随机梯度下降、小批量随机梯度下降3种优化算法对比【含数据集与源码】
|
机器学习/深度学习 人工智能 算法
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
鸟类识别系统。本系统采用Python作为主要开发语言,通过使用加利福利亚大学开源的200种鸟类图像作为数据集。使用TensorFlow搭建ResNet50卷积神经网络算法模型,然后进行模型的迭代训练,得到一个识别精度较高的模型,然后在保存为本地的H5格式文件。在使用Django开发Web网页端操作界面,实现用户上传一张鸟类图像,识别其名称。
1107 12
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
|
机器学习/深度学习 人工智能 算法
【昆虫识别系统】图像识别Python+卷积神经网络算法+人工智能+深度学习+机器学习+TensorFlow+ResNet50
昆虫识别系统,使用Python作为主要开发语言。通过TensorFlow搭建ResNet50卷积神经网络算法(CNN)模型。通过对10种常见的昆虫图片数据集('蜜蜂', '甲虫', '蝴蝶', '蝉', '蜻蜓', '蚱蜢', '蛾', '蝎子', '蜗牛', '蜘蛛')进行训练,得到一个识别精度较高的H5格式模型文件,然后使用Django搭建Web网页端可视化操作界面,实现用户上传一张昆虫图片识别其名称。
1392 7
【昆虫识别系统】图像识别Python+卷积神经网络算法+人工智能+深度学习+机器学习+TensorFlow+ResNet50
|
监控 算法 自动驾驶
RetinaNet算法1
8月更文挑战第6天
|
机器学习/深度学习 监控 算法
RetinaNet算法2
8月更文挑战第7天
|
机器学习/深度学习 数据采集 监控
算法金 | DL 骚操作扫盲,神经网络设计与选择、参数初始化与优化、学习率调整与正则化、Loss Function、Bad Gradient
**神经网络与AI学习概览** - 探讨神经网络设计,包括MLP、RNN、CNN,激活函数如ReLU,以及隐藏层设计,强调网络结构与任务匹配。 - 参数初始化与优化涉及Xavier/He初始化,权重和偏置初始化,优化算法如SGD、Adam,针对不同场景选择。 - 学习率调整与正则化,如动态学习率、L1/L2正则化、早停法和Dropout,以改善训练和泛化。
568 0
算法金 | DL 骚操作扫盲,神经网络设计与选择、参数初始化与优化、学习率调整与正则化、Loss Function、Bad Gradient
|
存储 算法 Java
Java数据结构与算法:用于高效地存储和检索字符串数据集
Java数据结构与算法:用于高效地存储和检索字符串数据集

热门文章

最新文章