Пример #1
0
def do_3d_pooling(feature_matrix,
                  stride_length_px=2,
                  pooling_type_string=MAX_POOLING_TYPE_STRING):
    """Pools 3-D feature maps.

    :param feature_matrix: Input feature maps (numpy array).  Dimensions must be
        M x N x H x C or 1 x M x N x H x C.
    :param stride_length_px: See doc for `do_2d_pooling`.
    :param pooling_type_string: Pooling type (must be accepted by
        `_check_pooling_type`).
    :return: feature_matrix: Output feature maps (numpy array).  Dimensions will
        be 1 x m x n x h x C.
    """

    error_checking.assert_is_numpy_array_without_nan(feature_matrix)
    error_checking.assert_is_integer(stride_length_px)
    error_checking.assert_is_geq(stride_length_px, 2)
    _check_pooling_type(pooling_type_string)

    if len(feature_matrix.shape) == 4:
        feature_matrix = numpy.expand_dims(feature_matrix, axis=0)
    error_checking.assert_is_numpy_array(feature_matrix, num_dimensions=5)

    feature_tensor = K.pool3d(x=K.variable(feature_matrix),
                              pool_mode=pooling_type_string,
                              pool_size=(stride_length_px, stride_length_px,
                                         stride_length_px),
                              strides=(stride_length_px, stride_length_px,
                                       stride_length_px),
                              padding='valid',
                              data_format='channels_last')
    return feature_tensor.eval(session=K.get_session())
Пример #2
0
    def loss_fn(y_true, y_pred):
        y_pred = K.clip(y_pred, epsilon, 1 - epsilon)
        cross_entropy = K.stack(
            [-(y_true * K.log(y_pred)), -((1 - y_true) * K.log(1 - y_pred))],
            axis=-1)
        loss = cross_entropy * w

        if boundary_weight is not None:
            y_true_avg = K.pool3d(y_true,
                                  pool_size=(pool, ) * 3,
                                  padding='same',
                                  pool_mode='avg')
            boundaries = K.cast(y_true_avg >= epsilon, 'float32') \
                         * K.cast(y_true_avg <= 1 - epsilon, 'float32')
            loss += cross_entropy * K.stack([boundaries, boundaries],
                                            axis=-1) * boundary_weight

        return K.mean(K.sum(loss, axis=-1))