def ssd_multibox_layer(inputs, num_classes, sizes, ratios=[1], normalization=-1, bn_normalization=False): """Construct a multibox layer, return a class and localization predictions. """ net = inputs if normalization > 0: net = custom_layers.l2_normalization(net, scaling=True) # Number of anchors. num_anchors = len(sizes) + len(ratios) # Location. num_loc_pred = num_anchors * 4 loc_pred = slim.conv2d(net, num_loc_pred, [3, 3], activation_fn=None, scope='conv_loc') loc_pred = custom_layers.channel_to_last(loc_pred) loc_pred = tf.reshape(loc_pred, tensor_shape(loc_pred, 4)[:-1] + [num_anchors, 4]) # Class prediction. num_cls_pred = num_anchors * num_classes cls_pred = slim.conv2d(net, num_cls_pred, [3, 3], activation_fn=None, scope='conv_cls') cls_pred = custom_layers.channel_to_last(cls_pred) cls_pred = tf.reshape( cls_pred, tensor_shape(cls_pred, 4)[:-1] + [num_anchors, num_classes]) return cls_pred, loc_pred
def ssd_multibox_layer( inputs, # Feature maps num_classes, ratios=[1], # Default H:W = 1:1 normalization=-1): """ Construct a multibox layer, return a class and localization predictions. :param inputs: :param num_classes: :param ratios: :param normalization: :return: the class and localization prediction """ net = inputs if normalization > 0: net = custom_layers.l2_normalization(net) # Number of anchors num_anchors = len(ratios) + 2 # Location: each anchor has 4 location num_loc_pred = num_anchors * 4 # Have a 3x3 conv on feature map to get location prediction, and the number of location prediction is num_loc_pred loc_pred = slim.conv2d(net, num_loc_pred, [3, 3], activation_fn=None, scope='conv_loc') loc_shape = [tf.shape(loc_pred)[0] ] + loc_pred.get_shape().as_list()[1:3] + [num_anchors, 4] loc_pred = tf.reshape(loc_pred, loc_shape) # Class prediction : each anchor has 2 classifaction num_cls_pred = num_anchors * num_classes cls_pred = slim.conv2d(net, num_cls_pred, [3, 3], activation_fn=None, scope='conv_cls') cls_shape = [ tf.shape(cls_pred)[0] ] + cls_pred.get_shape().as_list()[1:3] + [num_anchors, num_classes] cls_pred = tf.reshape(cls_pred, cls_shape) return cls_pred, loc_pred