예제 #1
0
    def call(self, inputs):
        # self.depthwise_kernel = tf.Print(self.depthwise_kernel, [self.depthwise_kernel], "----- Conv2d-dep kernel-----", first_n = 3, summarize = 20)
        # self.pointwise_kernel = tf.Print(self.pointwise_kernel, [self.pointwise_kernel], "----- Conv2d-point kernel-----", first_n = 3, summarize = 20)
        
        depthwise_quantized_kernel = quantize(self.depthwise_kernel, nb=self.nb)
        pointwise_quantized_kernel = quantize(self.pointwise_kernel, nb=self.nb) 

        inverse_kernel_lr_multiplier = 1./self.kernel_lr_multiplier
        inputs_qnn_gradient = (inputs - (1. - 1./inverse_kernel_lr_multiplier) * K.stop_gradient(inputs))\
                  * inverse_kernel_lr_multiplier

        outputs_qnn_gradient = K.separable_conv2d(
            inputs_qnn_gradient,
            depthwise_quantized_kernel,
            pointwise_quantized_kernel,
            strides=self.strides,
            padding=self.padding,
            data_format=self.data_format,
            dilation_rate=self.dilation_rate)

        outputs = (outputs_qnn_gradient - (1. - 1./self.kernel_lr_multiplier) * K.stop_gradient(outputs_qnn_gradient))\
                  * self.kernel_lr_multiplier

        # depthwise_quantized_kernel = tf.Print(depthwise_quantized_kernel, [depthwise_quantized_kernel], "----- Conv2d-dep quantize kernel-----", first_n = 3, summarize = 20)
        # pointwise_quantized_kernel = tf.Print(pointwise_quantized_kernel, [pointwise_quantized_kernel], "----- Conv2d-point quantize kernel-----", first_n = 3, summarize = 20)

        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
    def call(self, inputs):
        # var = tf.Variable([-1.25, -1 , -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1.0, 1.25])
        # self.depthwise_kernel = tf.Print(self.depthwise_kernel, [var], "Debugging ternarize", first_n = 2, summarize =20)

        # self.depthwise_kernel = tf.Print(self.depthwise_kernel, [self.depthwise_kernel], "----- Conv2d-dep kernel-----", first_n = 3, summarize = 200)
        # self.pointwise_kernel = tf.Print(self.pointwise_kernel, [self.pointwise_kernel], "----- Conv2d-point kernel-----", first_n = 3, summarize = 200)

        depthwise_ternarized_kernel = ternarize(self.depthwise_kernel,
                                                H=self.H)
        pointwise_ternarized_kernel = ternarize(self.pointwise_kernel,
                                                H=self.H)
        # var = ternarize(var, H=self.H)
        # depthwise_ternarized_kernel = tf.Print(self.depthwise_kernel, [var], "Debugging ternarized", first_n = 2, summarize =20)

        # depthwise_ternarized_kernel = tf.Print(depthwise_ternarized_kernel, [depthwise_ternarized_kernel], "----- Conv2d-dep ternarize kernel-----", first_n = 3, summarize = 200)
        # pointwise_ternarized_kernel = tf.Print(pointwise_ternarized_kernel, [pointwise_ternarized_kernel], "----- Conv2d-point ternarize kernel-----", first_n = 3, summarize = 200)

        outputs = K.separable_conv2d(inputs,
                                     depthwise_ternarized_kernel,
                                     pointwise_ternarized_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
예제 #3
0
 def samplewise_call(self, acc, inputs):
     # parse inputs
     if len(inputs) == 4:
         x, depth_kernel, point_kernel, bias = inputs
     elif (len(inputs) == 3):
         x, depth_kernel, point_kernel = inputs
     elif (len(inputs) == 2):
         x, params = inputs
         d0, d1 = 0, self.param_indices[0]
         depth_kernel = params[d0:d1]
         p0, p1 = self.param_indices[:2]
         point_kernel = params[p0:p1]
         if self.use_bias:
             bias = params[p1:]
     else:
         raise NotImplementedError
     # reshape to conv kernels
     depth_kernel = BK.reshape(depth_kernel, self.depthwise_kernel_shape)
     point_kernel = BK.reshape(point_kernel, self.pointwise_kernel_shape)
     if (self.use_bias):
         bias = BK.reshape(bias, self.bias_shape)
     outputs = BK.separable_conv2d(BK.expand_dims(x, axis=0),
                                   depth_kernel,
                                   point_kernel,
                                   data_format=self.data_format,
                                   strides=self.strides,
                                   padding=self.padding,
                                   dilation_rate=self.dilation_rate)
     if self.use_bias:
         outputs = BK.bias_add(outputs, bias, data_format=self.data_format)
     if self.activation is not None:
         return self.activation(outputs)
     else:
         return outputs
예제 #4
0
def offset_sep_conv2d_eval(depth, padding, x):
    """Perform a separable conv2d on x with a given padding"""
    depthwise_kernel = K.variable(value=np.array([[[[1]] * depth]]),
                                  dtype='float32')
    pointwise_kernel = K.variable(value=np.array([[[[1]] + [[0]] * (depth - 1)]]),
        dtype='float32')
    return K.separable_conv2d(x, depthwise_kernel,
                              pointwise_kernel, strides=(3, 3), padding=padding)
예제 #5
0
def offset_sep_conv2d_eval(depth, padding, x):
    """Perform a separable conv2d on x with a given padding"""
    depthwise_kernel = K.variable(value=np.array([[[[1]] * depth]]),
                                  dtype='float32')
    pointwise_kernel = K.variable(value=np.array([[[[1]] + [[0]] * (depth - 1)]]),
                                  dtype='float32')
    return K.separable_conv2d(x, depthwise_kernel,
                              pointwise_kernel, strides=(3, 3), padding=padding)
예제 #6
0
 def seprable_conv(self, x, d_k, p_k, b=None, padding='same'):
     conv_out = K.separable_conv2d(x,
                                   d_k,
                                   p_k,
                                   strides=self.strides,
                                   padding=padding,
                                   data_format=self.data_format,
                                   dilation_rate=self.dilation_rate)
     if b is not None:
         conv_out = K.bias_add(conv_out, b, data_format=self.data_format)
     return conv_out
예제 #7
0
        def call(self, u):

            # edge detection (learned filter)
            edges = K.separable_conv2d(self.image,
                                       self.sep_kernel_depthwise,
                                       self.sep_kernel_pointwise,
                                       padding='same')
            edges = K.relu(edges)
            edges = K.sum(edges, axis=-1, keepdims=True)
            edges = K.softsign(edges)

            # grad( edge_detection ) approx (learned filter)
            grad_edges_x = K.conv2d(edges, self.conv_kernel_x, padding='same')
            grad_edges_x = K.relu(grad_edges_x)
            grad_edges_x = K.sum(grad_edges_x, axis=-1, keepdims=True)
            grad_edges_y = K.conv2d(edges, self.conv_kernel_y, padding='same')
            grad_edges_y = K.relu(grad_edges_y)
            grad_edges_y = K.sum(grad_edges_y, axis=-1, keepdims=True)

            # upwind approx to grad( edge_detection)^T grad( u )
            xp = K.conv2d(u, kXP, padding='same')
            xn = K.conv2d(u, kXN, padding='same')
            yp = K.conv2d(u, kYP, padding='same')
            yn = K.conv2d(u, kYN, padding='same')
            fxp = K.relu(grad_edges_x)
            fxn = -1.0 * K.relu(-1.0 * grad_edges_x)
            fyp = K.relu(grad_edges_y)
            fyn = -1.0 * K.relu(-1.0 * grad_edges_y)
            xpp = fxp * xp
            xnn = fxn * xn
            ypp = fyp * yp
            ynn = fyn * yn

            # curvature kappa( u ) approx (learned filter)
            kappa = K.conv2d(u, self.conv_kernel_curve_0, padding='same')
            kappa = K.conv2d(kappa, self.conv_kernel_curve_1, padding='same')
            kappa = K.conv2d(kappa, self.conv_kernel_curve_2, padding='same')
            kappa = K.sum(kappa, axis=-1, keepdims=True)
            kappa = kappa * self.kappa_kernel
            kappa = K.conv2d(kappa, self.conv_kernel_smoother, padding='same')
            #          uxx = K.conv2d(u, kXX, padding='same')
            #          uyy = K.conv2d(u, kYY, padding='same')
            #          uxy = K.conv2d(u, kXY, padding='same')
            #          uxc = K.conv2d(u, kXC, padding='same')
            #          uyc = K.conv2d(u, kYC, padding='same')
            #          kappa = uxx*(uyc*uyc) - 2.0*(uxc*uyc)*uxy + uyy*(uxc*uxc)

            return u + xpp + xnn + ypp + ynn + edges + kappa
    def call(self, inputs):
        self.depthwise_kernel = tf.Print(self.depthwise_kernel,
                                         [self.depthwise_kernel],
                                         "----- Conv2d-dep kernel-----",
                                         first_n=3,
                                         summarize=20)
        self.pointwise_kernel = tf.Print(self.pointwise_kernel,
                                         [self.pointwise_kernel],
                                         "----- Conv2d-point kernel-----",
                                         first_n=3,
                                         summarize=20)

        depthwise_binary_kernel = binarize(self.depthwise_kernel, H=self.H)
        pointwise_binary_kernel = binarize(self.pointwise_kernel, H=self.H)

        outputs = K.separable_conv2d(inputs,
                                     depthwise_binary_kernel,
                                     pointwise_binary_kernel,
                                     strides=self.strides,
                                     padding=self.padding,
                                     data_format=self.data_format,
                                     dilation_rate=self.dilation_rate)

        depthwise_binary_kernel = tf.Print(
            depthwise_binary_kernel, [depthwise_binary_kernel],
            "----- Conv2d-dep binarize kernel-----",
            first_n=3,
            summarize=20)
        pointwise_binary_kernel = tf.Print(
            pointwise_binary_kernel, [pointwise_binary_kernel],
            "----- Conv2d-point binarize kernel-----",
            first_n=3,
            summarize=20)

        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
    def call(self, inputs):
        depthwise_binary_kernel = binarize(self.depthwise_kernel, H=self.H)
        pointwise_binary_kernel = binarize(self.pointwise_kernel, H=self.H)

        outputs = K.separable_conv2d(inputs,
                                     depthwise_binary_kernel,
                                     pointwise_binary_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