예제 #1
0
def weighted_bce_dice_loss(y_true, y_pred):
    y_true = K.cast(y_true, 'float32')
    y_pred = K.cast(y_pred, 'float32')
    # if we want to get same size of output, kernel size must be odd number
    if K.int_shape(y_pred)[1] == 128:
        kernel_size = 11
    elif K.int_shape(y_pred)[1] == 256:
        kernel_size = 21
    elif K.int_shape(y_pred)[1] == 512:
        kernel_size = 21
    elif K.int_shape(y_pred)[1] == 1024:
        kernel_size = 41
    else:
        raise ValueError('Unexpected image size')
    averaged_mask = K.pool2d(y_true,
                             pool_size=(kernel_size, kernel_size),
                             strides=(1, 1),
                             padding='same',
                             pool_mode='avg')
    border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(
        K.less(averaged_mask, 0.995), 'float32')
    weight = K.ones_like(averaged_mask)
    w0 = K.sum(weight)
    weight += border * 2
    w1 = K.sum(weight)
    weight *= (w0 / w1)
    loss = weighted_bce_loss(y_true, y_pred, weight) + (
        1 - weighted_dice_coeff(y_true, y_pred, weight))
    return loss
 def pooling_function(inputs, pool_size, strides, padding,
                      data_format):
     return -K.pool2d(-inputs,
                      pool_size,
                      strides,
                      padding,
                      data_format,
                      pool_mode='max')
예제 #3
0
 def context_gating(self, x, w, rx, rw, b=None, padding='valid'):
   input_shape = x.get_shape().as_list()
   if self.data_format == 'channels_first':
     x = K.pool2d(x, (input_shape[2], input_shape[3]), pool_mode='avg')
     rx = K.pool2d(rx, (input_shape[2], input_shape[3]), pool_mode='avg')
   elif self.data_format == 'channels_last':
     x = K.pool2d(x, (input_shape[1], input_shape[2]), pool_mode='avg')
     rx = K.pool2d(rx, (input_shape[1], input_shape[2]), pool_mode='avg')
   conv_out1 = K.conv2d(
       x,
       w,
       strides=self.strides,
       padding=padding,
       data_format=self.data_format)
   conv_out2 = K.conv2d(
       rx,
       rw,
       strides=self.strides,
       padding=padding,
       data_format=self.data_format)
   conv_out = conv_out1 + conv_out2
   if b is not None:
     conv_out = K.bias_add(conv_out, b, data_format=self.data_format)
   return conv_out
예제 #4
0
def do_2d_pooling(feature_matrix,
                  stride_length_px=2,
                  pooling_type_string=MAX_POOLING_TYPE_STRING):
    """Pools 2-D feature maps.

    m = number of rows after pooling
    n = number of columns after pooling

    :param feature_matrix: Input feature maps (numpy array).  Dimensions must be
        M x N x C or 1 x M x N x C.
    :param stride_length_px: Stride length (pixels).  The pooling window will
        move by this many rows or columns at a time as it slides over each input
        feature map.
    :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 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) == 3:
        feature_matrix = numpy.expand_dims(feature_matrix, axis=0)

    error_checking.assert_is_numpy_array(feature_matrix, num_dimensions=4)

    feature_tensor = K.pool2d(x=K.variable(feature_matrix),
                              pool_mode=pooling_type_string,
                              pool_size=(stride_length_px, stride_length_px),
                              strides=(stride_length_px, stride_length_px),
                              padding='valid',
                              data_format='channels_last')

    return feature_tensor.numpy()
예제 #5
0
def conv2d_offset_average_pool_eval(_, padding, x):
    """Perform an average pooling operation on x"""
    return K.pool2d(x, (1, 1),
                    strides=(3, 3),
                    padding=padding,
                    pool_mode='avg')
예제 #6
0
def conv2d_offset_max_pool_eval(_, padding, x):
    """Perform a max pooling operation on x"""
    return K.pool2d(x, (1, 1),
                    strides=(3, 3),
                    padding=padding,
                    pool_mode='max')
def conv2d_offset_average_pool_eval(_, padding, x):
    """Perform an average pooling operation on x"""
    return K.pool2d(x, (1, 1), strides=(3, 3), padding=padding, pool_mode='avg')
def conv2d_offset_max_pool_eval(_, padding, x):
    """Perform a max pooling operation on x"""
    return K.pool2d(x, (1, 1), strides=(3, 3), padding=padding, pool_mode='max')