SSD Network Architecture Special Lyaers--keras version

"""Some special pupropse layers for SSD."""
import keras.backend as K
from keras.engine.topology import InputSpec
from keras.engine.topology import Layer
import numpy as np
import tensorflow as tf

class Normalize(Layer):
    """Normalization layer as described in ParseNet paper.

    # Arguments
        scale: Default feature scale.

    # Input shape
        4D tensor with shape:
        `(samples, channels, rows, cols)` if dim_ordering=‘th‘
        or 4D tensor with shape:
        `(samples, rows, cols, channels)` if dim_ordering=‘tf‘.

    # Output shape
        Same as input
    """
    def __init__(self, scale, **kwargs):
        if K.image_dim_ordering() == ‘tf‘:
            self.axis = 3
        else:
            self.axis = 1
        self.scale = scale
        super(Normalize, self).__init__(**kwargs)

    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (input_shape[self.axis],)
        init_gamma = self.scale * np.ones(shape)
        self.gamma = K.variable(init_gamma, name=‘{}_gamma‘.format(self.name))
        self.trainable_weights = [self.gamma]

    def call(self, x, mask=None):
        output = K.l2_normalize(x, self.axis)
        output *= self.gamma
        return output
#上面这个层就不多说了,在keras中自定义Layer中以及详细介绍了,下面还是说说那个产生default box的层吧
class PriorBox(Layer):
    """Generate the prior boxes of designated sizes and aspect ratios.

    # Arguments
        img_size: Size of the input image as tuple (w, h).
        min_size: Minimum box size in pixels.
        max_size: Maximum box size in pixels.
        aspect_ratios: List of aspect ratios of boxes.
        flip: Whether to consider reverse aspect ratios.
        variances: List of variances for x, y, w, h.
        clip: Whether to clip the prior‘s coordinates
            such that they are within [0, 1].

    # Input shape
        4D tensor with shape:
        `(samples, channels, rows, cols)` if dim_ordering=‘th‘
        or 4D tensor with shape:
        `(samples, rows, cols, channels)` if dim_ordering=‘tf‘.

    # Output shape
        3D tensor with shape:
        (samples, num_boxes, 8)
    """
    def __init__(self, img_size, min_size, max_size=None, aspect_ratios=None,
                 flip=True, variances=[0.1], clip=True, **kwargs):
        if K.image_dim_ordering() == ‘tf‘:
            self.waxis = 2
            self.haxis = 1
        else:
            self.waxis = 3
            self.haxis = 2
        self.img_size = img_size
#         print "self.img_size"
#         print self.img_size
#         print "self.img_siez"
        if min_size <= 0:
            raise Exception(‘min_size must be positive.‘)
        self.min_size = min_size
        self.max_size = max_size
        self.aspect_ratios = [1.0]
        if max_size:
            if max_size < min_size:
                raise Exception(‘max_size must be greater than min_size.‘)
            self.aspect_ratios.append(1.0)
        if aspect_ratios:
            for ar in aspect_ratios:
                if ar in self.aspect_ratios:
                    continue
                self.aspect_ratios.append(ar)
                if flip:
                    self.aspect_ratios.append(1.0 / ar)
        self.variances = np.array(variances)
        self.clip = True
        super(PriorBox, self).__init__(**kwargs)

    def get_output_shape_for(self, input_shape):
#         print "input_shape start"
#         print input_shape
#         print "input_shape end"
        num_priors_ = len(self.aspect_ratios)
#         print "-----------------------------------"
#         print num_priors
        layer_width = input_shape[self.waxis]
#         print layer_width
        layer_height = input_shape[self.haxis]
#         print layer_height
#         print "----------------------------------"
        num_boxes = num_priors_ * layer_width * layer_height
#         print (input_shape[0], num_boxes, 8)
        return (input_shape[0], num_boxes, 8)

    def call(self, x, mask=None):
#         print dir(x)
        if hasattr(x, ‘_keras_shape‘):
            input_shape = x._keras_shape
#             print "1"
        elif hasattr(K, ‘int_shape‘):
            input_shape = K.int_shape(x)
#             print "2"
#         print input_shape
        layer_width = input_shape[self.waxis]
        layer_height = input_shape[self.haxis]
        img_width = self.img_size[0]
        img_height = self.img_size[1]
#         print img_width,img_height,layer_width,layer_height
        # define prior boxes shapes
        box_widths = []
        box_heights = []
#         print self.min_size
#         print self.aspect_ratios
        for ar in self.aspect_ratios:
            if ar == 1 and len(box_widths) == 0:
                box_widths.append(self.min_size)
                box_heights.append(self.min_size)
            elif ar == 1 and len(box_widths) > 0:
                box_widths.append(np.sqrt(self.min_size * self.max_size))
                box_heights.append(np.sqrt(self.min_size * self.max_size))
            elif ar != 1:
                box_widths.append(self.min_size * np.sqrt(ar))
                box_heights.append(self.min_size / np.sqrt(ar))
        box_widths = 0.5 * np.array(box_widths)
        box_heights = 0.5 * np.array(box_heights)
#         print len(box_widths)
#         print len(box_heights)
        # define centers of prior boxes
        step_x = img_width / layer_width
        step_y = img_height / layer_height
#         print step_x,step_y
#         print img_width,img_height
        linx = np.linspace(0.5 * step_x, img_width - 0.5 * step_x,
                           layer_width)
        liny = np.linspace(0.5 * step_y, img_height - 0.5 * step_y,
                           layer_height)
#         print linx.shape,liny.shape
        centers_x, centers_y = np.meshgrid(linx, liny)
        centers_x = centers_x.reshape(-1, 1)
        centers_y = centers_y.reshape(-1, 1)
#         print centers_x.shape,centers_y.shape
        # define xmin, ymin, xmax, ymax of prior boxes
        num_priors_ = len(self.aspect_ratios)
#         print num_priors
        prior_boxes = np.concatenate((centers_x, centers_y), axis=1)
#         print prior_boxes.shape
        prior_boxes = np.tile(prior_boxes, (1, 2 * num_priors_))
#         print prior_boxes.shape
        prior_boxes[:, ::4] -= box_widths
        prior_boxes[:, 1::4] -= box_heights
        prior_boxes[:, 2::4] += box_widths
        prior_boxes[:, 3::4] += box_heights
        prior_boxes[:, ::2] /= img_width
        prior_boxes[:, 1::2] /= img_height
        prior_boxes = prior_boxes.reshape(-1, 4)
        if self.clip:
            prior_boxes = np.minimum(np.maximum(prior_boxes, 0.0), 1.0)
        # define variances
        num_boxes = len(prior_boxes)
        if len(self.variances) == 1:
            variances = np.ones((num_boxes, 4)) * self.variances[0]
        elif len(self.variances) == 4:
            variances = np.tile(self.variances, (num_boxes, 1))
        else:
            raise Exception(‘Must provide one or four variances.‘)
        prior_boxes = np.concatenate((prior_boxes, variances), axis=1)
        prior_boxes_tensor = K.expand_dims(K.variable(prior_boxes), 0)
        if K.backend() == ‘tensorflow‘:
            pattern = [tf.shape(x)[0], 1, 1]
            prior_boxes_tensor = tf.tile(prior_boxes_tensor, pattern)
        elif K.backend() == ‘theano‘:
            #TODO
            pass
#         print prior_boxes_tensor.shape
        return prior_boxes_tensor
# print dir(PriorBox((300,300),100))
时间: 2024-11-10 00:57:37

SSD Network Architecture Special Lyaers--keras version的相关文章

【Network Architecture】Densely Connected Convolutional Networks 论文解析

0. Paper link 1. Overview ??文章开篇提到了如果在靠近输入与输出的层之间存在短连接(shorter connections),可以训练更深.更准确.更有效的卷积网络,DenseNet利用了这个性质,每层都与之前所有的层进行连接,即之前所有层的feature map都作为这一层的输入.DenseNet有减少梯度消失,增强特征传递,鼓励特征重利用同时极大的减少了参数的数量.在很多任务上达到了state-of-the-art. ??另外DenseNet并不是像ResNet那样

SSD Network Architecture--keras version

这里的网络架构和论文中插图中的网络架构是相一致的.对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面.可别直接粘贴复制,里面有些中文的解释,不一定可行的.#defint input shapeinput_shape = (300,300,3)#defint the number of classes num_classes = 21 #Here the network is wrapped in to a dictory bec

【Network Architecture】Feature Pyramid Networks for Object Detection(FPN)论文解析(转)

目录 0. 前言 1. 博客一 2.. 博客二 0. 前言 ??这篇论文提出了一种新的特征融合方式来解决多尺度问题, 感觉挺有创新性的, 如果需要与其他网络进行拼接,还是需要再回到原文看一下细节.这里转了两篇比较好的博客作为备忘. 1. 博客一 这篇论文是CVPR2017年的文章,采用特征金字塔做目标检测,有许多亮点,特来分享. 论文:feature pyramid networks for object detection 论文链接:https://arxiv.org/abs/1612.031

Message 1070 not found; No message file for product=network, facility=TNSTNSLSNR for Linux: Version

接到一个朋友打来的求助电话,在安装oracle11g单机版的时候新建监听报错如下: ............ 原因:在oracle用户配置环境变量文件.bash_profile时没有把ORACLE_BASE/ORACLE_HOME/ORACLE_SID 用 export 输出,环境变量问题!!! 输出后解决,再安装时请仔细阅读安装文档!

【Network architecture】Rethinking the Inception Architecture for Computer Vision(inception-v3)论文解析

0. paper link inception-v3 1. Overview ??这篇文章很多"经验"性的东西,因此会写的比较细,把文章里的一些话摘取出来,多学习一下,希望对以后自己设计网络有帮助. 2. Four General Design Principles ??这里文章介绍了四种设计网络设计原则,这是作者利用各种卷积网络结构,通过大量的实验推测的. 避免特征表示瓶颈,尤其是在网络的前面.要避免严重压缩导致(pooling,卷积等操作)的瓶颈.特征表示尺寸应该温和的减少,从输入

【Network Architecture】SegNet论文解析(转)

文章来源: https://blog.csdn.net/fate_fjh/article/details/53467948 Introduction 自己制作国内高速公路label,使用SegNet训练高速公路模型,测试效果 参考:http://mi.eng.cam.ac.uk/projects/segnet/tutorial.html SegNet是Cambridge提出旨在解决自动驾驶或者智能机器人的图像语义分割深度网络,开放源码,基于caffe框架.SegNet基于FCN,修改VGG-16

Keras vs. PyTorch in Transfer Learning

We perform image classification, one of the computer vision tasks deep learning shines at. As training from scratch is unfeasible in most cases (as it is very data hungry), we perform transfer learning using ResNet-50 pre-trained on ImageNet. We get

(转) Learning Deep Learning with Keras

Learning Deep Learning with Keras Piotr Migda? - blog Projects Articles Publications Resume About Photos Learning Deep Learning with Keras 30 Apr 2017 ? Piotr Migda? ? [machine-learning] [deep-learning] [overview] I teach deep learning both for a liv

Is VXLAN the Answer to the Network Virtualization Question?

Network virtualization is a growing topic of interest and for some good reasons as networks scale to meet the challenges of cloud computing they are running up against VLAN scaling limitations. There have been several network overlay technologies rel