Example #1
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")
     # print('low_to_high',low_to_high)
     # low_to_high = K.repeat_elements(low_to_high, 2, axis=1) # Nearest Neighbor Upsampling
     # print('low_to_high', low_to_high)
     # low_to_high = K.repeat_elements(low_to_high, 2, axis=2)
     # print('low_to_high', low_to_high)
     low_to_high = tf.keras.layers.UpSampling2D(size=(2, 2), data_format='channels_last')(low_to_high)
     # 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
     return [high_add, low_add]
Example #2
0
    def call(self, x, **kwargs):
        def hw_flatten(x):
            return K.reshape(x,
                             shape=[
                                 K.shape(x)[0],
                                 K.shape(x)[1] * K.shape(x)[2],
                                 K.shape(x)[-1]
                             ])

        f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        f = K.bias_add(f, self.bias_f)
        g = K.conv2d(x, kernel=self.kernel_g, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        g = K.bias_add(g, self.bias_g)
        h = K.conv2d(x, kernel=self.kernel_h, strides=(1, 1),
                     padding='same')  # [bs, h, w, c]
        h = K.bias_add(h, self.bias_h)

        s = tf.matmul(hw_flatten(g), hw_flatten(f),
                      transpose_b=True)  # # [bs, N, N]

        beta = K.softmax(s, axis=-1)  # attention map

        self.beta = beta

        o = K.batch_dot(beta, hw_flatten(h))  # [bs, N, C]

        o = K.reshape(o, shape=K.shape(x))  # [bs, h, w, C]
        x = self.gamma * o + x

        return x
Example #3
0
def var_yhat(y_true, y_pred):
    n = 5
    sk = K.constant(value=np.ones((n,n,1,1), dtype=np.float32), dtype='float32')
    
    Var = K.mean((K.conv2d(K.square(y_pred), sk) - K.square(K.conv2d(y_pred, sk)/n**2))/n**2, axis=-1)

    return Var
Example #4
0
    def call(self, x):
        f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        g = K.conv2d(x, kernel=self.kernel_g, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        h = K.conv2d(x, kernel=self.kernel_h, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']

        f_ = K.permute_dimensions(self._hw_flatten(f),
                                  (0, 2, 1))  # [bs, 3c', N]
        s = K.batch_dot(self._hw_flatten(g), f_)  # [bs, N, N]
        beta = K.softmax(s, axis=-1)  # attention map

        double_attn = K.batch_dot(f_, self._hw_flatten(x))  # [bs, 3c', 3c]
        double_attn = K.softmax(double_attn, axis=1)

        h_tmp, shape_tmp = self._hw_flatten(h,
                                            return_shape=True)  # [bs, N, 3c']
        o_tmp = K.batch_dot(beta, h_tmp)  # [bs, N, 3c']
        o = K.batch_dot(o_tmp, double_attn)  # [bs, N, 3c]
        o = self._hw_recover(o, shape_tmp)  # [bs, h, w, C]

        x = self.gamma * o + x

        return x
Example #5
0
 def call(self, inputs):
     # Input = [X^H, X^L]
     assert len(inputs) == 2
     high_input, low_input = inputs
     # Convolution: High Channels -> High Channels
     high_to_high = K.conv2d(high_input, self.high_to_high_kernel,
                             strides=self.strides, padding=self.padding,
                             data_format="channels_last")
     # Convolution: High Channels -> Low Channels
     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")
     # Convolution: Low Channels -> High Channels
     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)
     # Convolution: Low Channels -> Low Channels
     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
     return [high_add, low_add]
Example #6
0
    def call(self, x):
        def hw_flatten(x):
            return K.reshape(x,
                             shape=[
                                 K.shape(x)[0],
                                 K.shape(x)[1] * K.shape(x)[2],
                                 K.shape(x)[3]
                             ])

        f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        g = K.conv2d(x, kernel=self.kernel_g, strides=(1, 1),
                     padding='same')  # [bs, h, w, c']
        h = K.conv2d(x, kernel=self.kernel_h, strides=(1, 1),
                     padding='same')  # [bs, h, w, c]

        s = K.batch_dot(hw_flatten(g),
                        K.permute_dimensions(hw_flatten(f),
                                             (0, 2, 1)))  # # [bs, N, N]

        beta = K.softmax(s, axis=-1)  # attention map

        o = K.batch_dot(beta, hw_flatten(h))  # [bs, N, C]

        o = K.reshape(o, shape=K.shape(x))  # [bs, h, w, C]
        x = self.gamma * o + x

        return x
Example #7
0
    def call(self, inputs, mask=None):
        '''
        We will be using the Keras conv2d method, and essentially we have
        to do here is multiply the mask with the input X, before we apply the
        convolutions. For the mask itself, we apply convolutions with all weights
        set to 1.
        Subsequently, we clip mask values to between 0 and 1
        '''

        # Both image and mask must be supplied
        if type(inputs) is not list or len(inputs) != 2:
            raise Exception(
                'PartialConvolution2D must be called on a list of two tensors [img, mask]. Instead got: ' + str(inputs))

        # Padding done explicitly so that padding becomes part of the masked partial convolution
        images = K.spatial_2d_padding(inputs[0], self.pconv_padding, self.data_format)
        masks = K.spatial_2d_padding(inputs[1], self.pconv_padding, self.data_format)

        # Apply convolutions to mask
        mask_output = K.conv2d(
            masks, self.kernel_mask,
            strides=self.strides,
            padding='valid',
            data_format=self.data_format,
            dilation_rate=self.dilation_rate
        )

        # Apply convolutions to image
        img_output = K.conv2d(
            (images * masks), self.kernel,
            strides=self.strides,
            padding='valid',
            data_format=self.data_format,
            dilation_rate=self.dilation_rate
        )

        # Calculate the mask ratio on each pixel in the output mask
        mask_ratio = self.window_size / (mask_output + 1e-8)

        # Clip output to be between 0 and 1
        mask_output = K.clip(mask_output, 0, 1)

        # Remove ratio values where there are holes
        mask_ratio = mask_ratio * mask_output

        # Normalize iamge output
        img_output = img_output * mask_ratio

        # Apply bias only to the image (if chosen to do so)
        if self.use_bias:
            img_output = K.bias_add(
                img_output,
                self.bias,
                data_format=self.data_format)

        # Apply activations on the image
        if self.activation is not None:
            img_output = self.activation(img_output)

        return [img_output, mask_output]
    def call(self, inputs):
        print(self.kernel)
        outputs_plus = K.conv2d(
                inputs,
                self.kernel,
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
                dilation_rate=self.dilation_rate)
        
        outputs_minus = K.conv2d(
                inputs,
                K.reverse(self.kernel,0),
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
                dilation_rate=self.dilation_rate)

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

        if self.activation is not None:
            return [self.activation(outputs_plus),self.activation(outputs_minus)]
        
        return [outputs_plus,outputs_minus]
Example #9
0
 def _spectrogram_mono(self, x):
     '''x.shape : (None, 1, len_src),
     returns 2D batch of a mono power-spectrogram'''
     x = K.permute_dimensions(x, [0, 2, 1])
     x = K.expand_dims(x, 3)  # add a dummy dimension (channel axis)
     subsample = (self.n_hop, 1)
     output_real = K.conv2d(
         x,
         self.dft_real_kernels,
         strides=subsample,
         padding=self.padding,
         data_format='channels_last',
     )
     output_imag = K.conv2d(
         x,
         self.dft_imag_kernels,
         strides=subsample,
         padding=self.padding,
         data_format='channels_last',
     )
     output = output_real**2 + output_imag**2
     # now shape is (batch_sample, n_frame, 1, freq)
     if self.image_data_format == 'channels_last':
         output = K.permute_dimensions(output, [0, 3, 1, 2])
     else:
         output = K.permute_dimensions(output, [0, 2, 3, 1])
     return output
Example #10
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])
            x_flat = K.reshape(x, shape=output_shape)
            return (x_flat)

        f = K.conv2d(inputs,
                     kernel=self.kernel_f,
                     strides=(1, 1),
                     padding='same')
        f = K.bias_add(f, self.bias_f)
        g = K.conv2d(inputs,
                     kernel=self.kernel_g,
                     strides=(1, 1),
                     padding='same')
        g = K.bias_add(g, self.bias_g)
        h = K.conv2d(inputs,
                     kernel=self.kernel_h,
                     strides=(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)
Example #11
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]
Example #12
0
def conv_loss(y_true, y_pred):
    n = 5
    mk = K.constant(value=np.ones((n,n,1,1), dtype=np.float32) / n**2, dtype='float32')
    sk = K.constant(value=np.ones((n,n,1,1), dtype=np.float32), dtype='float32')
    
    Bias = K.mean(K.abs(K.conv2d(y_true, mk)-K.conv2d(y_pred, mk)), axis=-1)
    Var = K.mean(K.abs((K.conv2d(K.square(y_true), sk) - K.square(K.conv2d(y_true, sk)/n**2))/n**2 - (K.conv2d(K.square(y_pred), sk) - K.square(K.conv2d(y_pred, sk)/n**2))/n**2), axis=-1)
    
    return Var + 4*Bias
    def call(self, x):
        y = K.conv2d(x, self.conv2d_w1, padding="same")
        y = K.bias_add(y, self.conv2d_b1)
        y = K.relu(y)

        y = K.conv2d(y, self.conv2d_w2, padding="same")
        y = K.bias_add(y, self.conv2d_b2)
        y = K.relu(y)
        y = y + x

        return y
    def ode_func(self, x, t):
        y = self.concat_t(x, t)
        y = K.conv2d(y, self.conv2d_w1, padding="same")
        y = K.bias_add(y, self.conv2d_b1)
        y = K.relu(y)

        y = self.concat_t(y, t)
        y = K.conv2d(y, self.conv2d_w2, padding="same")
        y = K.bias_add(y, self.conv2d_b2)
        y = K.relu(y)

        return y
Example #15
0
    def call(self, x):
        f = K.conv2d(x, kernel=self.kernel_f, strides=(1, 1), padding='same')
        f = K.bias_add(f, self.bias_f)
        g = K.conv2d(x, kernel=self.kernel_g, strides=(1, 1), padding='same')
        g = K.bias_add(g, self.bias_g)
        h = K.conv2d(x, kernel=self.kernel_h, strides=(1, 1), padding='same')
        h = K.bias_add(h, self.bias_h)

        s = tf.matmul(hw_flatten(g), hw_flatten(f), transpose_b=True)
        beta = K.softmax(s, axis=-1)  # attention map
        o = K.batch_dot(beta, hw_flatten(h))  # [bs, N, C]
        o = K.reshape(o, shape=K.int_shape(x))  # [bs, h, w, C]
        return self.gamma * o + x
Example #16
0
def Active_Contour_Loss(y_true, y_pred):
    coef = 1
    
    sober_y_len = K.sum(K.relu(K.conv2d(y_pred, sobel_operator_y, padding='same')))
    sober_x_len = K.sum(K.relu(K.conv2d(y_pred, sobel_operator_x, padding='same')))
    sober_len = sober_y_len + sober_x_len
    area = K.sum(y_pred) + K.epsilon()
    shape_metric = sober_len/ area
    
#    shape_metric = tensorflow.keras.backend.print_tensor(shape_metric)
    binary_cross_loss = tf.keras.losses.binary_crossentropy(y_pred, y_true)

    return binary_cross_loss + coef*shape_metric
Example #17
0
 def loss(y_true, y_pred):
     mse_pointwise = K.mean(mse(y_true, y_pred), axis=(1, 2))
     true_vol = K.conv2d(y_pred,
                         kernel,
                         strides=(scale, scale),
                         padding='same')
     pred_vol = K.conv2d(y_pred,
                         kernel,
                         strides=(scale, scale),
                         padding='same')
     mse_spatial = K.mean(mse(true_vol - input_lr, pred_vol - input_lr),
                          axis=(1, 2))
     return beta * mse_pointwise + (1. - beta) * mse_spatial
    def call(self, input_tensor, training=None):
        input_transposed = tf.transpose(input_tensor, [3, 0, 1, 2, 4])
        input_shape = K.shape(input_transposed)
        input_tensor_reshaped = K.reshape(input_transposed, [
            input_shape[1] * input_shape[0], self.input_height, self.input_width, self.input_num_atoms])
        input_tensor_reshaped.set_shape((None, self.input_height, self.input_width, self.input_num_atoms))

        if self.upsamp_type == 'resize':
            upsamp = K.resize_images(input_tensor_reshaped, self.scaling, self.scaling, 'channels_last')
            outputs = K.conv2d(upsamp, kernel=self.W, strides=(1, 1), padding=self.padding, data_format='channels_last')
        elif self.upsamp_type == 'subpix':
            conv = K.conv2d(input_tensor_reshaped, kernel=self.W, strides=(1, 1), padding='same',
                            data_format='channels_last')
            outputs = tf.depth_to_space(conv, self.scaling)
        else:
            batch_size = input_shape[1] * input_shape[0]

            # Infer the dynamic output shape:
            out_height = deconv_length(self.input_height, self.scaling, self.kernel_size, self.padding,
                                       output_padding=None)
            out_width = deconv_length(self.input_width, self.scaling, self.kernel_size, self.padding,
                                      output_padding=None)
            output_shape = (batch_size, out_height, out_width, self.num_capsule * self.num_atoms)

            outputs = K.conv2d_transpose(input_tensor_reshaped, self.W, output_shape, (self.scaling, self.scaling),
                                         padding=self.padding, data_format='channels_last')

        votes_shape = K.shape(outputs)
        _, conv_height, conv_width, _ = outputs.get_shape()

        votes = K.reshape(outputs, [input_shape[1], input_shape[0], votes_shape[1], votes_shape[2],
                                    self.num_capsule, self.num_atoms])
        votes.set_shape((None, self.input_num_capsule, conv_height, conv_width,
                         self.num_capsule, self.num_atoms))

        logit_shape = K.stack([
            input_shape[1], input_shape[0], votes_shape[1], votes_shape[2], self.num_capsule])
        biases_replicated = K.tile(self.b, [votes_shape[1], votes_shape[2], 1, 1])

        activations = update_routing(
            votes=votes,
            biases=biases_replicated,
            logit_shape=logit_shape,
            num_dims=6,
            input_dim=self.input_num_capsule,
            output_dim=self.num_capsule,
            num_routing=self.routings)

        return activations
Example #19
0
    def call(self, x, h2_prev, timestep, rand_seed=None):
        
        # NOTE: expected input shape: (batch, height, width, channel)
        
        # init h2 and w
        if timestep == 0:
            # dirty workaround as glorot_normal won't take None as batch dim
            if x.shape[0] == None: 
                h2_prev = K.random_normal(K.shape(x))
            else:
                h2_prev = keras.initializers.glorot_normal(seed=rand_seed)(x.shape)
        
        if self.batchnorm: # ReLU with recurrent batchnorm

            # calculate gain G(1)[t]
            g1 = K.sigmoid(self.bn[timestep*4](K.conv2d(h2_prev, self.u1) + self.b1))

            # horizontal inhibition C(1)[t]
            if self.channel_sym:
                conv_inh = channel_sym_conv2d((g1 * h2_prev), self.w_inh)
            else:
                conv_inh = K.conv2d((g1 * h2_prev), self.w_inh, padding='same')
            c1 = self.bn[timestep*4+1](conv_inh)

            # apply gain gate and inhibition to get H(1)[t]
            h1 = K.relu(x - K.relu(c1 * (self.alpha * h2_prev + self.mu)))

            # mix gate G(2)[t]
            g2 = K.sigmoid(self.bn[timestep*4+2](K.conv2d(h1, self.u2) + self.b2))

            # horizontal excitation C(2)[t]
            if self.channel_sym:
                conv_exc = channel_sym_conv2d(h1, self.w_exc)
            else:
                conv_exc = K.conv2d(h1, self.w_exc , padding='same')
            c2 = self.bn[timestep*4+3](conv_exc)

            # output candidate H_tilda(2)[t] via excitation
            h2_tilda = K.relu(self.kappa * h1 + self.beta * c2 + self.omega * h1 * c2)

            # apply mix gate to get H(2)[t]
            h2_t = g2 * h2_tilda + (1 - g2) * h2_prev

        else: # tanh with timestep weights, no batchnorm except at g2
            g1 = K.sigmoid(K.conv2d(h2_prev, self.u1) + self.b1)
            if self.channel_sym:
                c1 = channel_sym_conv2d((g1 * h2_prev), self.w_inh)
            else:
                c1 = K.conv2d((g1 * h2_prev), self.w_inh, padding='same')
            h1 = K.tanh(x - c1 * (self.alpha * h2_prev + self.mu))
            g2 = K.sigmoid(self.bn[timestep*4+2](K.conv2d(h1, self.u2) + self.b2))
            if self.channel_sym:
                c2 = channel_sym_conv2d(h1, self.w_exc)
            else:
                c2 = K.conv2d(h1, self.w_exc , padding='same')
            h2_tilda = K.tanh(self.kappa * h1 + self.beta * c2 + self.omega * h1 * c2)
            h2_t = self.eta[timestep] * (g2 * h2_tilda + (1 - g2) * h2_prev)
      
        return h2_t
Example #20
0
    def call(self, x):
        # soft-assignment.
        s = K.conv2d(x, self.kernel, padding='same') + self.bias
        print('s.shape=', s.shape)
        a = K.softmax(s)
        self.amap = K.argmax(a, -1)
        # print 'amap.shape', self.amap.shape

        # Dims used hereafter: batch, H, W, desc_coeff, cluster
        a = K.expand_dims(a, -2)
        # print 'a.shape=',a.shape

        # Core
        v = K.expand_dims(x, -1) + self.C
        # print 'v.shape', v.shape
        v = a * v
        # print 'v.shape', v.shape
        v = K.sum(v, axis=[1, 2])
        # print 'v.shape', v.shape
        v = K.permute_dimensions(v, pattern=[0, 2, 1])
        # print 'v.shape', v.shape
        #v.shape = None x K x D

        # Normalize v (Intra Normalization)
        v = K.l2_normalize(v, axis=-1)
        v = K.batch_flatten(v)
        v = K.l2_normalize(v, axis=-1)

        # return [v, self.amap]
        return v
    def call(self, input_tensor, training=None):
        input_transposed = tf.transpose(input_tensor, [3, 0, 1, 2, 4])
        input_shape = K.shape(input_transposed)
        input_tensor_reshaped = K.reshape(input_transposed, [
            input_shape[0] * input_shape[1], self.input_height, self.input_width, self.input_num_atoms])
        input_tensor_reshaped.set_shape((None, self.input_height, self.input_width, self.input_num_atoms))

        conv = K.conv2d(input_tensor_reshaped, self.W, (self.strides, self.strides),
                        padding=self.padding, data_format='channels_last')

        votes_shape = K.shape(conv)
        _, conv_height, conv_width, _ = conv.get_shape()

        votes = K.reshape(conv, [input_shape[1], input_shape[0], votes_shape[1], votes_shape[2],
                                 self.num_capsule, self.num_atoms])
        votes.set_shape((None, self.input_num_capsule, conv_height, conv_width,
                         self.num_capsule, self.num_atoms))

        logit_shape = K.stack([
            input_shape[1], input_shape[0], votes_shape[1], votes_shape[2], self.num_capsule])
        biases_replicated = K.tile(self.b, [conv_height, conv_width, 1, 1])

        activations = update_routing(
            votes=votes,
            biases=biases_replicated,
            logit_shape=logit_shape,
            num_dims=6,
            input_dim=self.input_num_capsule,
            output_dim=self.num_capsule,
            num_routing=self.routings)

        return activations
def conv2d(x,
           kernel,
           strides=(1, 1),
           padding='valid',
           data_format='channels_first',
           image_shape=None,
           filter_shape=None):
    """2D convolution.

    # Arguments
        x: Input tensor
        kernel: kernel tensor.
        strides: strides tuple.
        padding: string, "same" or "valid".
        data_format: 'channels_first' or 'channels_last'.
            Whether to use Theano or TensorFlow dimension
            ordering in inputs/kernels/ouputs.
        image_shape: Optional, the input tensor shape
        filter_shape: Optional, the kernel shape.

    # Returns
        x convolved with the kernel.

    # Raises
        Exception: In case of invalid border mode or data format.
    """
    return K.conv2d(x, kernel, strides, padding, data_format)
Example #23
0
 def call(self, inputs, training=None):
     """
     Calls the layer on some input. If training,
     updates the `u` parameter with revised estimates
     of the right singular vector.
     """
     W_bar = spectrally_normalize_weight(
         weight=self.kernel,
         right_singular_vector=self.u,
         training=training,
         epsilon=self.epsilon,
     )
     outputs = K.conv2d(
         inputs,
         W_bar,
         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
Example #24
0
def gconv2d(x, kernel, gconv_indices, gconv_shape_info, strides=(1, 1), padding='valid',
            data_format=None, dilation_rate=(1, 1), transpose=False, output_shape=None):
    """2D group equivariant convolution.

    # Arguments
        x: Tensor or variable.
        kernel: kernel tensor.
        strides: strides tuple.
        padding: string, `"same"` or `"valid"`.
        data_format: string, `"channels_last"` or `"channels_first"`.
            Whether to use Theano or TensorFlow data format
            for inputs/kernels/ouputs.
        dilation_rate: tuple of 2 integers.

    # Returns
        A tensor, result of 2D convolution.

    # Raises
        ValueError: if `data_format` is neither `channels_last` or `channels_first`.
    """
    # Transform the filters
    transformed_filter = transform_filter_2d_nhwc(w=kernel, flat_indices=gconv_indices, shape_info=gconv_shape_info)
    if transpose:
        output_shape = (K.shape(x)[0], output_shape[1], output_shape[2], output_shape[3])
        transformed_filter = transform_filter_2d_nhwc(w=kernel, flat_indices=gconv_indices, shape_info=gconv_shape_info)
        transformed_filter = K.permute_dimensions(transformed_filter, [0, 1, 3, 2])
        return K.conv2d_transpose(x=x, kernel=transformed_filter, output_shape=output_shape, strides=strides,
                                padding=padding, data_format=data_format)
    return K.conv2d(x=x, kernel=transformed_filter, strides=strides, padding=padding, data_format=data_format,
                    dilation_rate=dilation_rate)
Example #25
0
    def _conv_gaussian(self, inputs: tf.Tensor) -> tf.Tensor:
        """ Perform Gaussian convolution on a batch of images.

        Parameters
        ----------
        inputs: :class:`tf.Tensor`
            The input batch of images to perform Gaussian convolution on.

        Returns
        -------
        :class:`tf.Tensor`
            The convolved images
        """
        channels = K.int_shape(inputs)[-1]
        gauss = K.tile(self._gaussian_kernel, (1, 1, 1, channels))

        # TF doesn't implement replication padding like pytorch. This is an inefficient way to
        # implement it for a square guassian kernel
        size = self._gaussian_kernel.shape[1] // 2
        padded_inputs = inputs
        for _ in range(size):
            padded_inputs = tf.pad(
                padded_inputs,  # noqa,pylint:disable=no-value-for-parameter,unexpected-keyword-arg
                ([0, 0], [1, 1], [1, 1], [0, 0]),
                mode="SYMMETRIC")

        retval = K.conv2d(padded_inputs, gauss, strides=1, padding="valid")
        return retval
Example #26
0
        def normalize_inference():
            dconvs = K.depthwise_conv2d(inputs,
                                        self.depthwise_kernel,
                                        strides=self.strides,
                                        padding=self.padding,
                                        data_format='channels_last')
            #            dconvs = K.clip(dconvs, min_value=0, max_value=self.max_activity)
            dconvs = K.clip(dconvs,
                            min_value=-self.max_activity_signed,
                            max_value=self.max_activity_signed)

            convs = K.conv2d(dconvs,
                             self.kernel * self.w_scale,
                             strides=(1, 1),
                             padding=self.padding,
                             data_format='channels_last',
                             dilation_rate=self.dilation_rate)
            convs = K.bias_add(convs,
                               self.bias * self.w_scale,
                               data_format='channels_last')

            #            outputs = convs
            #            outputs = K.clip(convs, min_value=-self.max_activity_signed, max_value=self.max_activity_signed)
            outputs = K.clip(convs, min_value=0, max_value=self.max_activity)
            return K.round(outputs * 2**
                           self.L_A[1]) * 2**-self.L_A[1]  # naechster Nachbar
    def call(self, x):

        # Calculate the pairwise distances between the codewords and the feature vectors
        x_square = K.sum(x ** 2, axis=3, keepdims=True)
        y_square = K.sum(self.V ** 2, axis=2, keepdims=True)
        dists = x_square + y_square - 2 * K.conv2d(x, self.V, strides=(1, 1), padding='valid')
        dists = K.maximum(dists, 0)

        # Quantize the feature vectors
        quantized_features = K.softmax(- dists / (self.sigmas ** 2))

        # Compile the histogram
        if self.spatial_level == 0:
            histogram = K.mean(quantized_features, [1, 2])
        elif self.spatial_level == 1:
            shape = K.shape(quantized_features)
            mid_1 = K.cast(shape[1] / 2, 'int32')
            mid_2 = K.cast(shape[2] / 2, 'int32')
            histogram1 = K.mean(quantized_features[:, :mid_1, :mid_2, :], [1, 2])
            histogram2 = K.mean(quantized_features[:, mid_1:, :mid_2, :], [1, 2])
            histogram3 = K.mean(quantized_features[:, :mid_1, mid_2:, :], [1, 2])
            histogram4 = K.mean(quantized_features[:, mid_1:, mid_2:, :], [1, 2])
            histogram = K.stack([histogram1, histogram2, histogram3, histogram4], 1)
            histogram = K.reshape(histogram, (-1, 4 * self.N_k))
        else:
            # No other spatial level is currently supported (it is trivial to extend the code)
            assert False

        # Simple trick to avoid rescaling issues
        return histogram * self.N_k
Example #28
0
    def call(self, x, training=None):
        W_shape = self.kernel.shape
        if training:
            W_bar, _u, sigma = spectral_normalization(
                self.kernel, self.u, niter=self.niter_spectral)
            self.sig.assign(sigma)
            self.u.assign(_u)
        else:
            W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
            W_bar = W_reshaped / self.sig
        W_bar = bjorck_normalization(W_bar, niter=self.niter_bjorck)
        W_bar = W_bar * self._get_coef()

        W_bar = K.reshape(W_bar, W_shape)
        outputs = K.conv2d(
            x,
            W_bar,
            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
Example #29
0
 def call(self,input):
     y = K.conv2d(input, self.kernel, strides=(1, 1), padding='same', data_format="channels_last",
                                 dilation_rate=(1, 1))
     y = K.relu(y)
     y = tf.depth_to_space(y, self.scale)
     y = K.pool2d(y, pool_size=(self.scale,self.scale), strides=(1, 1),  padding='same',  data_format="channels_last", pool_mode='avg')
     return y
Example #30
0
def mean_yhat(y_true, y_pred):
    n = 5
    mk = K.constant(value=np.ones((n,n,1,1), dtype=np.float32) / n**2, dtype='float32')
    
    Mean = K.mean(K.conv2d(y_pred, mk), axis=-1)

    return Mean