예제 #1
0
        def call(self, inputs):
            depthwise_conv_on_filters = []
            sliced_inputs = [sliced for sliced in tf.split(inputs, self.input_dim, self.channel_axis)]
            sliced_kernels = [sliced for sliced in tf.split(self.depthwise_kernel, self.input_dim, 3)]
            #See https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops/slicing_and_joining
            for i in range(self.input_dim):
               depthwise_conv_on_filters.append (  K.conv3d(sliced_inputs[i], 
                                                     sliced_kernels[i],
                                                     strides=self.strides,
                                                     padding=self.padding,
                                                     data_format=self.data_format,
                                                     dilation_rate=self.dilation_rate)   )

            depthwise_conv = K.concatenate(depthwise_conv_on_filters)
            pointwise_conv = K.conv3d(depthwise_conv, self.pointwise_kernel,
                                     strides = (1, 1, 1), padding = self.padding,
                                     data_format = self.data_format,
                                     dilation_rate=self.dilation_rate)
            
            outputs = pointwise_conv
            
            if self.use_bias:
                outputs = K.bias_add(
                        outputs,
                        self.bias,
                        data_format=self.data_format)

            if self.activation is not None:
                return self.activation(outputs)
            return outputs
예제 #2
0
    def call(self, inputs, mask=None):
        def flatten(x):
            input_shape = K.shape(x)
            output_shape = (input_shape[0],
                            input_shape[1] * input_shape[2] * input_shape[3],
                            input_shape[4])
            x_flat = K.reshape(x, shape=output_shape)
            return (x_flat)

        f = K.conv3d(inputs,
                     kernel=self.kernel_f,
                     strides=(1, 1, 1),
                     padding='same')
        f = K.bias_add(f, self.bias_f)
        g = K.conv3d(inputs,
                     kernel=self.kernel_g,
                     strides=(1, 1, 1),
                     padding='same')
        g = K.bias_add(g, self.bias_g)
        h = K.conv3d(inputs,
                     kernel=self.kernel_h,
                     strides=(1, 1, 1),
                     padding='same')
        h = K.bias_add(h, self.bias_h)

        f_flat = flatten(f)
        g_flat = flatten(g)
        h_flat = flatten(h)

        s = tf.matmul(g_flat, f_flat, transpose_b=True)
        beta = K.softmax(s, axis=-1)
        o = K.reshape(K.batch_dot(beta, h_flat), shape=K.shape(inputs))

        x = self.gamma * o + inputs
        return (x)
    def call(self, inputs):
        def _l2normalize(v, eps=1e-12):
            return v / (K.sum(v**2)**0.5 + eps)

        def power_iteration(W, u):
            _u = u
            _v = _l2normalize(K.dot(_u, K.transpose(W)))
            _u = _l2normalize(K.dot(_v, W))
            return _u, _v

        if self.spectral_normalization:
            W_shape = self.kernel.shape.as_list()
            #Flatten the Tensor
            W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
            _u, _v = power_iteration(W_reshaped, self.u)
            #Calculate Sigma
            sigma = K.dot(_v, W_reshaped)
            sigma = K.dot(sigma, K.transpose(_u))
            #normalize it
            W_bar = W_reshaped / sigma
            #reshape weight tensor
            if training in {0, False}:
                W_bar = K.reshape(W_bar, W_shape)
            else:
                with tf.control_dependencies([self.u.assign(_u)]):
                    W_bar = K.reshape(W_bar, W_shape)

            #update weitht
            self.kernel = W_bar

        if self.rank == 1:
            outputs = K.conv1d(inputs,
                               self.kernel,
                               strides=self.strides[0],
                               padding=self.padding,
                               data_format=self.data_format,
                               dilation_rate=self.dilation_rate[0])
        if self.rank == 2:
            outputs = K.conv2d(inputs,
                               self.kernel,
                               strides=self.strides,
                               padding=self.padding,
                               data_format=self.data_format,
                               dilation_rate=self.dilation_rate)
        if self.rank == 3:
            outputs = K.conv3d(inputs,
                               self.kernel,
                               strides=self.strides,
                               padding=self.padding,
                               data_format=self.data_format,
                               dilation_rate=self.dilation_rate)

        if self.use_bias:
            outputs = K.bias_add(outputs,
                                 self.bias,
                                 data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)
        return outputs
예제 #4
0
def weighted_dice_coef(y_true, y_pred):
    k_width = 24
    w_max = 1.
    edge = K.abs(
        K.conv3d(y_true,
                 np.float32([[[0, 1, 0], [1, -4, 1], [0, 1, 0]]]).reshape(
                     (3, 3, 1, 1, 1)),
                 padding='same',
                 data_format='channels_last'))
    gk = w_max * np.ones((k_width, k_width, 1, 1, 1), dtype='float32') / 4.
    x_edge = K.clip(
        K.conv3d(edge, gk, padding='same', data_format='channels_last'), 0.,
        w_max)
    w_f = K.flatten(x_edge + 1.)
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(w_f * y_true_f * y_pred_f)
    return (2. * intersection + ep) / (K.sum(w_f * y_true_f) +
                                       K.sum(w_f * y_pred_f) + ep)
    def call(self, inputs):
        if self.rank == 1:
            outputs = K.conv1d(
                inputs,
                self.kernel,
                strides=self.strides[0],
                padding=self.padding,
                data_format=self.data_format,
                dilation_rate=self.dilation_rate[0],
            )
        if self.rank == 2:
            kernel = self.kernel
            if self.standardization:
                kernel_mean = K.mean(kernel, axis=[0, 1, 2], keepdims=True)
                kernel = kernel - kernel_mean
                kernel_std = K.std(kernel, axis=[0, 1, 2], keepdims=True)
                kernel = kernel / (kernel_std + 1e-5)
            outputs = K.conv2d(
                inputs,
                kernel,
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
                dilation_rate=self.dilation_rate,
            )
        if self.rank == 3:
            outputs = K.conv3d(
                inputs,
                self.kernel,
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
                dilation_rate=self.dilation_rate,
            )

        if self.use_bias:
            outputs = K.bias_add(outputs, self.bias, data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)
        return outputs