コード例 #1
0
ファイル: backend.py プロジェクト: xuyingjie521/bertfamily
def pool1d(x,
           pool_size,
           strides=1,
           padding='valid',
           data_format=None,
           pool_mode='max'):
    """
    向量序列的pool函数
    """
    x = K.expand_dims(x, 1)
    x = K.pool2d(x,
                 pool_size=(1, pool_size),
                 strides=(1, strides),
                 padding=padding,
                 data_format=data_format,
                 pool_mode=pool_mode)
    return x[:, 0]
コード例 #2
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
    averaged_mask = K.pool2d(y_true,
                             pool_size=(11, 11),
                             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) + \
    weighted_dice_loss(y_true, y_pred, weight)
    return loss
コード例 #3
0
 def call(self, inputs):
     # Input = [X^H, X^L]
     high_input, low_input = inputs if type(inputs) is list else (inputs,
                                                                  None)
     # High -> High conv
     high_to_high = K.conv2d(high_input,
                             self.high_to_high_kernel,
                             strides=self.strides,
                             padding=self.padding,
                             data_format="channels_last")
     # High -> Low conv
     high_to_low = K.pool2d(high_input, (2, 2),
                            strides=(2, 2),
                            pool_mode="avg")
     high_to_low = K.conv2d(high_to_low,
                            self.high_to_low_kernel,
                            strides=self.strides,
                            padding=self.padding,
                            data_format="channels_last")
     if low_input is not None:
         # Low -> High conv
         low_to_high = K.conv2d(low_input,
                                self.low_to_high_kernel,
                                strides=self.strides,
                                padding=self.padding,
                                data_format="channels_last")
         low_to_high = K.repeat_elements(
             low_to_high, 2, axis=1)  # Nearest Neighbor Upsampling
         low_to_high = K.repeat_elements(low_to_high, 2, axis=2)
         # Low -> Low conv
         low_to_low = K.conv2d(low_input,
                               self.low_to_low_kernel,
                               strides=self.strides,
                               padding=self.padding,
                               data_format="channels_last")
         # Cross Add
         high_add = high_to_high + low_to_high
         low_add = high_to_low + low_to_low
     else:
         high_add = high_to_high
         low_add = high_to_low
     return [high_add, low_add]
コード例 #4
0
    def call(self, inputs):
        # Input=[x^H, x^L]
        assert len(inputs) == 2
        high_input, low_input = inputs
        # High -> High conv
        high_to_high = K.conv2d(high_input,
                                self.high_to_high_kernel,
                                strides=self.strides,
                                padding=self.padding,
                                data_format="channels_last")
        # High -> low conv
        high_to_low = K.pool2d(high_input, (2, 2),
                               strides=(2, 2),
                               pool_mode="avg")
        high_to_low = K.conv2d(high_to_low,
                               self.high_to_low_kernel,
                               strides=self.strides,
                               padding=self.padding,
                               data_format="channels_last")

        # Low -> high conv
        low_to_high = K.conv2d(low_input,
                               self.low_to_high_kernel,
                               strides=self.strides,
                               padding=self.padding,
                               data_format="channels_last")
        low_to_high = K.repeat_elements(low_to_high, 2, axis=1)
        low_to_high = K.repeat_elements(low_to_high, 2, axis=2)

        # Low -> low conv
        low_to_low = K.conv2d(low_input,
                              self.low_to_low_kernel,
                              strides=self.strides,
                              padding=self.padding,
                              data_format="channels_last")

        # cross add
        high_add = high_to_high + low_to_high
        low_add = low_to_low + high_to_low

        return [high_add, low_add]
コード例 #5
0
    def _get_laplacian_pyramid(self, inputs: tf.Tensor) -> List[tf.Tensor]:
        """ Obtain the Laplacian Pyramid.

        Parameters
        ----------
        inputs: :class:`tf.Tensor`
            The input batch of images to run through the Laplacian Pyramid

        Returns
        -------
        list
            The tensors produced from the Laplacian Pyramid
        """
        pyramid = []
        current = inputs
        for _ in range(self._max_levels):
            gauss = self._conv_gaussian(current)
            diff = current - gauss
            pyramid.append(diff)
            current = K.pool2d(gauss, (2, 2), strides=(2, 2), padding="valid", pool_mode="avg")
        pyramid.append(current)
        return pyramid
コード例 #6
0
ファイル: losses.py プロジェクト: BrWillian/api-cerberus
def weighted_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
    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 = 1 - weighted_dice_coeff(y_true, y_pred, weight)
    return loss
コード例 #7
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')
コード例 #8
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')
コード例 #9
0
ファイル: Decoder.py プロジェクト: EverySports/Deep-Learning
def _nms(heat, kernel=3):
    hmax = K.pool2d(heat, (kernel, kernel), padding='same', pool_mode='max')
    keep = K.cast(K.equal(hmax, heat), K.floatx())
    return heat * keep