density_prior_box

paddle.fluid.layers.detection. density_prior_box ( input, image, densities=None, fixed_sizes=None, fixed_ratios=None, variance=[0.1, 0.1, 0.2, 0.2], clip=False, steps=[0.0, 0.0], offset=0.5, flatten_to_2d=False, name=None ) [source]

This op generates density prior boxes for SSD(Single Shot MultiBox Detector) algorithm. Each position of the input produce N prior boxes, N is determined by the count of densities, fixed_sizes and fixed_ratios. Boxes center at grid points around each input position is generated by this operator, and the grid points is determined by densities and the count of density prior box is determined by fixed_sizes and fixed_ratios. Obviously, the number of fixed_sizes is equal to the number of densities.

For densities_i in densities:

\[N\_density_prior\_box = SUM(N\_fixed\_ratios * densities\_i^2)\]

N_density_prior_box is the number of density_prior_box and N_fixed_ratios is the number of fixed_ratios.

Parameters
  • input (Variable) – 4-D tensor(NCHW), the data type should be float32 of float64.

  • image (Variable) – 4-D tensor(NCHW), the input image data of PriorBoxOp, the data type should be float32 or float64. the layout is NCHW.

  • densities (list|tuple|None) – The densities of generated density prior boxes, this attribute should be a list or tuple of integers. Default: None.

  • fixed_sizes (list|tuple|None) – The fixed sizes of generated density prior boxes, this attribute should a list or tuple of same length with densities. Default: None.

  • fixed_ratios (list|tuple|None) – The fixed ratios of generated density prior boxes, if this attribute is not set and densities and fix_sizes is set, aspect_ratios will be used to generate density prior boxes.

  • variance (list|tuple) – The variances to be encoded in density prior boxes. Default:[0.1, 0.1, 0.2, 0.2].

  • clip (bool) – Whether to clip out of boundary boxes. Default: False.

  • step (list|tuple) – Prior boxes step across width and height, If step[0] equals 0.0 or step[1] equals 0.0, the density prior boxes step across height or weight of the input will be automatically calculated. Default: [0., 0.]

  • offset (float) – Prior boxes center offset. Default: 0.5

  • flatten_to_2d (bool) – Whether to flatten output prior boxes and variance to 2D shape, the second dim is 4. Default: False.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name

Returns

A tuple with two Variable (boxes, variances)

boxes: the output density prior boxes of PriorBox. 4-D tensor, the layout is [H, W, num_priors, 4] when flatten_to_2d is False. 2-D tensor, the layout is [H * W * num_priors, 4] when flatten_to_2d is True. H is the height of input, W is the width of input, and num_priors is the total box count of each position of input.

variances: the expanded variances of PriorBox. 4-D tensor, the layout is [H, W, num_priors, 4] when flatten_to_2d is False. 2-D tensor, the layout is [H * W * num_priors, 4] when flatten_to_2d is True. H is the height of input, W is the width of input, and num_priors is the total box count of each position of input.

Return type

Tuple

Examples

#declarative mode

import paddle.fluid as fluid
import numpy as np
import paddle
paddle.enable_static()

input = fluid.data(name="input", shape=[None,3,6,9])
image = fluid.data(name="image", shape=[None,3,9,12])
box, var = fluid.layers.density_prior_box(
     input=input,
     image=image,
     densities=[4, 2, 1],
     fixed_sizes=[32.0, 64.0, 128.0],
     fixed_ratios=[1.],
     clip=True,
     flatten_to_2d=True)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

# prepare a batch of data
input_data = np.random.rand(1,3,6,9).astype("float32")
image_data = np.random.rand(1,3,9,12).astype("float32")

box_out, var_out = exe.run(
    fluid.default_main_program(),
    feed={"input":input_data,
          "image":image_data},
    fetch_list=[box,var],
    return_numpy=True)

# print(box_out.shape)
# (1134, 4)
# print(var_out.shape)
# (1134, 4)


#imperative mode
import paddle.fluid.dygraph as dg

with dg.guard(place) as g:
    input = dg.to_variable(input_data)
    image = dg.to_variable(image_data)
    box, var = fluid.layers.density_prior_box(
        input=input,
        image=image,
        densities=[4, 2, 1],
        fixed_sizes=[32.0, 64.0, 128.0],
        fixed_ratios=[1.],
        clip=True)

    # print(box.shape)
    # [6L, 9L, 21L, 4L]
    # print(var.shape)
    # [6L, 9L, 21L, 4L]