Esempio n. 1
0
 def quant_fc(self,
              input_tensor,
              shape,
              init_func=msra_init,
              enable_bias=True,
              quant_bits=8,
              quant_input=True,
              enable_prune=False):
     W = init_func(shape, "fc_weights")
     if enable_prune:
         W = pruning.apply_mask(W)
         self.prune_weights.append(W)
     W = fake_quant(W,
                    min=tf.reduce_min(W),
                    max=tf.reduce_max(W),
                    num_bits=quant_bits)
     if quant_input == True:
         input_tensor = fake_quant(input_tensor,
                                   min=tf.reduce_min(input_tensor),
                                   max=tf.reduce_max(input_tensor),
                                   num_bits=quant_bits)
     current_tensor = tf.matmul(input_tensor, W)
     if enable_bias:
         bias = zeros_init([shape[1]], "fc_bias")
         current_tensor = tf.add(current_tensor, bias)
     return current_tensor
Esempio n. 2
0
 def quant_conv2d_bn(self,
                     name,
                     input_tensor,
                     kernel,
                     stride,
                     init_func=msra_init,
                     quant_bits=8,
                     enable_scale=True,
                     enable_bias=True,
                     enable_prune=False):
     with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
         current_tensor = self.quant_conv2d(input_tensor,
                                            kernel,
                                            stride,
                                            init_func,
                                            quant_bits=quant_bits,
                                            enable_prune=enable_prune)
         scale = None
         bias = None
         if enable_scale:
             scale = ones_init([kernel[3]], "bn_scale")
         if enable_bias:
             bias = zeros_init([kernel[3]], "bn_bias")
         current_tensor = self.bn(current_tensor, scale, bias)
         return current_tensor
Esempio n. 3
0
 def bnn_fc(self,
            input_tensor,
            shape,
            init_func=msra_init,
            enable_bn=False,
            enable_bias=True):
     W = init_func(shape, "fc_weights")
     W = binary(W)
     input_tensor = binary(input_tensor)
     current_tensor = tf.matmul(input_tensor, W)
     if enable_bias:
         bias = zeros_init([shape[1]], "fc_bias")
         current_tensor = tf.add(current_tensor, bias)
     if enable_bn:
         scale = ones_init([shape[1]], "bn_scale")
         bias = zeros_init([shape[1]], "bn_bias")
         current_tensor = self.bn(current_tensor,
                                  scale,
                                  bias,
                                  is_conv=False)
     return current_tensor
Esempio n. 4
0
 def fc(self,
        input_tensor,
        shape,
        init_func=msra_init,
        enable_bn=False,
        enable_bias=True,
        enable_prune=False):
     W = init_func(shape, "fc_weights")
     if enable_prune:
         W = pruning.apply_mask(W)
         self.prune_weights.append(W)
     current_tensor = tf.matmul(input_tensor, W)
     if enable_bias:
         bias = zeros_init([shape[1]], "fc_bias")
         current_tensor = tf.add(current_tensor, bias)
     if enable_bn:
         scale = ones_init([shape[1]], "bn_scale")
         bias = zeros_init([shape[1]], "bn_bias")
         current_tensor = self.bn(current_tensor,
                                  scale,
                                  bias,
                                  is_conv=False)
     return current_tensor
Esempio n. 5
0
 def bnn_conv2d_bn(self,
                   name,
                   input_tensor,
                   kernel,
                   stride,
                   init_func=msra_init,
                   bn_input=True,
                   enable_scale=True,
                   enable_bias=True,
                   padding="SAME"):
     with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
         current_tensor = self.bnn_conv2d(input_tensor, kernel, stride,
                                          init_func, bn_input, padding)
         scale = None
         bias = None
         if enable_scale:
             scale = ones_init([kernel[3]], "bn_scale")
         if enable_bias:
             bias = zeros_init([kernel[3]], "bn_bias")
         current_tensor = self.bn(current_tensor, scale, bias)
         return current_tensor