Ejemplo n.º 1
0
def __build_anchors(anchor_parameters, features):
    """Builds anchors for the shape of the features from FPN.

    Args:
        anchor_parameters (AnchorParameters): Parameters that determine how
            anchors are generated.
        features (list): The FPN features.

    Returns:
        tensor: The anchors for the FPN features.
            The shape is:
            ```
            (batch_size, num_anchors, 4)
            ```
    """

    if len(features) == 1:
        anchors = Anchors(size=anchor_parameters.sizes[0],
                          stride=anchor_parameters.strides[0],
                          ratios=anchor_parameters.ratios,
                          scales=anchor_parameters.scales,
                          name='anchors')(features[0])
        return anchors
    else:
        anchors = [
            Anchors(size=anchor_parameters.sizes[i],
                    stride=anchor_parameters.strides[i],
                    ratios=anchor_parameters.ratios,
                    scales=anchor_parameters.scales,
                    name='anchors_{}'.format(i))(f)
            for i, f in enumerate(features)
        ]
        return Concatenate(axis=1, name='anchors')(anchors)
Ejemplo n.º 2
0
def __build_anchors(anchor_parameters, features, frames_per_batch=1):
    """Builds anchors for the shape of the features from FPN.

    Args:
        anchor_parameters (AnchorParameters): Parameters that determine how
            anchors are generated.
        features (list): The FPN features.
        frames_per_batch (int): Size of z axis in generated batches.
            If equal to 1, assumes 2D data.

    Returns:
        tensor: The anchors for the FPN features.
            The shape is:
            ```
            (batch_size, num_anchors, 4)
            ```
    """

    if len(features) == 1:
        if frames_per_batch > 1:
            anchors = TimeDistributed(
                Anchors(size=anchor_parameters.sizes[0],
                        stride=anchor_parameters.strides[0],
                        ratios=anchor_parameters.ratios,
                        scales=anchor_parameters.scales,
                        name='anchors'))(features[0])
        else:
            anchors = Anchors(size=anchor_parameters.sizes[0],
                              stride=anchor_parameters.strides[0],
                              ratios=anchor_parameters.ratios,
                              scales=anchor_parameters.scales,
                              name='anchors')(features[0])
        return anchors
    else:
        if frames_per_batch > 1:
            anchors = [
                TimeDistributed(
                    Anchors(size=anchor_parameters.sizes[i],
                            stride=anchor_parameters.strides[i],
                            ratios=anchor_parameters.ratios,
                            scales=anchor_parameters.scales,
                            name='anchors_{}'.format(i)))(f)
                for i, f in enumerate(features)
            ]
        else:
            anchors = [
                Anchors(size=anchor_parameters.sizes[i],
                        stride=anchor_parameters.strides[i],
                        ratios=anchor_parameters.ratios,
                        scales=anchor_parameters.scales,
                        name='anchors_{}'.format(i))(f)
                for i, f in enumerate(features)
            ]
        return Concatenate(axis=-2, name='anchors')(anchors)