models/dense_net.py
def reconstruct_transition_to_classes_post_pruning(self, _input,
                                                       block, layer,
                                                       kernels_to_prune):
        """
        Reconstruct the transition layer to classes after pruning kernels in
        the last layer of the last block (in such a case, the transition layer
        must remain mostly unchanged and unused weights must be removed).

        Args:
            _input: tensor, the operation's input;
            block: `int`, identifier number for the last block;
            layer: `int`, identifier number for the last layer in that block;
            kernels_to_prune: `list` of `int`, gives the specific kernels that
                were pruned in the last layer.
        """
        new_features_total = int(_input.get_shape()[-1])
        var_scope = "Transition_to_FC_block_%d" % block
        if not self.preserve_transition_l:
            var_scope += "_layer_%d" % layer

        with tf.variable_scope(var_scope, reuse=tf.AUTO_REUSE):
            # Copy the batch norm beta and gamma param values from old kernels.
            beta_values = self.sess.run(tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/beta",
                [self.features_total]))
            gamma_values = self.sess.run(tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/gamma",
                [self.features_total]))
            # Create a new batch norm and get its param variables.
            self.batch_norm_counter += 1
            output = self.batch_norm(
                _input, scope='BatchNorm_'+str(self.batch_norm_counter))
            new_beta = tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/beta",
                [new_features_total])
            new_gamma = tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/gamma",
                [new_features_total])
            # self.sess.run(tf.variables_initializer([new_beta, new_gamma]))
            # Copy the param values corresponding to the remaining kernels.
            prepruning_kernel_count = len(self.kernels_ref_list[block][-1])
            prepruning_kernel_count += len(kernels_to_prune)
            difference = self.features_total - prepruning_kernel_count
            new_beta_values = beta_values[:difference]
            new_gamma_values = gamma_values[:difference]
            for k in range(prepruning_kernel_count):
                if k not in kernels_to_prune:
                    new_beta_values = np.append(
                        new_beta_values, beta_values[k+difference])
                    new_gamma_values = np.append(
                        new_gamma_values, gamma_values[k+difference])
            print(new_features_total)
            print(len(new_beta_values))
            print("%d (difference) = %d (new_features_total) - %d (current_kernel_count)" % (
                difference, new_features_total, prepruning_kernel_count-len(kernels_to_prune)))
            print("%d (old difference) = %d (features_total) - %d (prepruning_kernel_count)" % (
                difference, self.features_total, prepruning_kernel_count))
            # Assign those param values to reconstruct the batch norm.
            self.sess.run(new_beta.assign(new_beta_values))
            self.sess.run(new_gamma.assign(new_gamma_values))
            self.features_total = new_features_total
            # Rewrite: ReLU, average pooling, and reshaping into 1d.
            output = tf.nn.relu(output)
            last_pool_kernel = int(output.get_shape()[-2])
            output = self.avg_pool(output, k=last_pool_kernel)
            features_total = int(output.get_shape()[-1])
            output = tf.reshape(output, [-1, features_total])

        # For the FC layer: remove weights for unpruned kernels, keep all else.
        for i in reversed(kernels_to_prune):
            self.pruned_varnames.append(self.FC_W[difference+i].name)
            del self.FC_W[difference+i]
        stacked_FC_W = tf.stack(self.FC_W, axis=0)
        logits = tf.matmul(output, stacked_FC_W) + self.FC_bias
        return logits

        Reconstruct the transition layer to classes after pruning kernels in
        the last layer of the last block (in such a case, the transition layer
        must remain mostly unchanged and unused weights must be removed).

        Args:
            _input: tensor, the operation's input;
            block: `int`, identifier number for the last block;
            layer: `int`, identifier number for the last layer in that block;
            kernels_to_prune: `list` of `int`, gives the specific kernels that
                were pruned in the last layer.
        
Reconstruct the transition layer to classes after pruning kernels in
the last layer of the last block (in such a case, the transition layer
must remain mostly unchanged and unused weights must be removed).
Reconstruct the transition layer to classes after pruning kernels in the last layer of the last block ( in such a case the transition layer must remain mostly unchanged and unused weights must be removed ) .
def reconstruct_transition_to_classes_post_pruning(self, _input,
                                                       block, layer,
                                                       kernels_to_prune):
        new_features_total = int(_input.get_shape()[-1])
        var_scope = "Transition_to_FC_block_%d" % block
        if not self.preserve_transition_l:
            var_scope += "_layer_%d" % layer
        with tf.variable_scope(var_scope, reuse=tf.AUTO_REUSE):
            beta_values = self.sess.run(tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/beta",
                [self.features_total]))
            gamma_values = self.sess.run(tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/gamma",
                [self.features_total]))
            self.batch_norm_counter += 1
            output = self.batch_norm(
                _input, scope='BatchNorm_'+str(self.batch_norm_counter))
            new_beta = tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/beta",
                [new_features_total])
            new_gamma = tf.get_variable(
                "BatchNorm_"+str(self.batch_norm_counter)+"/gamma",
                [new_features_total])
            prepruning_kernel_count = len(self.kernels_ref_list[block][-1])
            prepruning_kernel_count += len(kernels_to_prune)
            difference = self.features_total - prepruning_kernel_count
            new_beta_values = beta_values[:difference]
            new_gamma_values = gamma_values[:difference]
            for k in range(prepruning_kernel_count):
                if k not in kernels_to_prune:
                    new_beta_values = np.append(
                        new_beta_values, beta_values[k+difference])
                    new_gamma_values = np.append(
                        new_gamma_values, gamma_values[k+difference])
            print(new_features_total)
            print(len(new_beta_values))
            print("%d (difference) = %d (new_features_total) - %d (current_kernel_count)" % (
                difference, new_features_total, prepruning_kernel_count-len(kernels_to_prune)))
            print("%d (old difference) = %d (features_total) - %d (prepruning_kernel_count)" % (
                difference, self.features_total, prepruning_kernel_count))
            self.sess.run(new_beta.assign(new_beta_values))
            self.sess.run(new_gamma.assign(new_gamma_values))
            self.features_total = new_features_total
            output = tf.nn.relu(output)
            last_pool_kernel = int(output.get_shape()[-2])
            output = self.avg_pool(output, k=last_pool_kernel)
            features_total = int(output.get_shape()[-1])
            output = tf.reshape(output, [-1, features_total])
        for i in reversed(kernels_to_prune):
            self.pruned_varnames.append(self.FC_W[difference+i].name)
            del self.FC_W[difference+i]
        stacked_FC_W = tf.stack(self.FC_W, axis=0)
        logits = tf.matmul(output, stacked_FC_W) + self.FC_bias
        return logits
def reconstruct_transition_to_classes_post_pruning ( self , _input , block , layer , kernels_to_prune ) : new_features_total = int ( _input . get_shape ( ) [ - 1 ] ) var_scope = "Transition_to_FC_block_%d" % block if not self . preserve_transition_l : var_scope += "_layer_%d" % layer with tf . variable_scope ( var_scope , reuse = tf . AUTO_REUSE ) : beta_values = self . sess . run ( tf . get_variable ( "BatchNorm_" + str ( self . batch_norm_counter ) + "/beta" , [ self . features_total ] ) ) gamma_values = self . sess . run ( tf . get_variable ( "BatchNorm_" + str ( self . batch_norm_counter ) + "/gamma" , [ self . features_total ] ) ) self . batch_norm_counter += 1 output = self . batch_norm ( _input , scope = 'BatchNorm_' + str ( self . batch_norm_counter ) ) new_beta = tf . get_variable ( "BatchNorm_" + str ( self . batch_norm_counter ) + "/beta" , [ new_features_total ] ) new_gamma = tf . get_variable ( "BatchNorm_" + str ( self . batch_norm_counter ) + "/gamma" , [ new_features_total ] ) prepruning_kernel_count = len ( self . kernels_ref_list [ block ] [ - 1 ] ) prepruning_kernel_count += len ( kernels_to_prune ) difference = self . features_total - prepruning_kernel_count new_beta_values = beta_values [ : difference ] new_gamma_values = gamma_values [ : difference ] for k in range ( prepruning_kernel_count ) : if k not in kernels_to_prune : new_beta_values = np . append ( new_beta_values , beta_values [ k + difference ] ) new_gamma_values = np . append ( new_gamma_values , gamma_values [ k + difference ] ) print ( new_features_total ) print ( len ( new_beta_values ) ) print ( "%d (difference) = %d (new_features_total) - %d (current_kernel_count)" % ( difference , new_features_total , prepruning_kernel_count - len ( kernels_to_prune ) ) ) print ( "%d (old difference) = %d (features_total) - %d (prepruning_kernel_count)" % ( difference , self . features_total , prepruning_kernel_count ) ) self . sess . run ( new_beta . assign ( new_beta_values ) ) self . sess . run ( new_gamma . assign ( new_gamma_values ) ) self . features_total = new_features_total output = tf . nn . relu ( output ) last_pool_kernel = int ( output . get_shape ( ) [ - 2 ] ) output = self . avg_pool ( output , k = last_pool_kernel ) features_total = int ( output . get_shape ( ) [ - 1 ] ) output = tf . reshape ( output , [ - 1 , features_total ] ) for i in reversed ( kernels_to_prune ) : self . pruned_varnames . append ( self . FC_W [ difference + i ] . name ) del self . FC_W [ difference + i ] stacked_FC_W = tf . stack ( self . FC_W , axis = 0 ) logits = tf . matmul ( output , stacked_FC_W ) + self . FC_bias return logits
Reconstruct the transition layer to classes after pruning kernels in
the last layer of the last block (in such a case, the transition layer
must remain mostly unchanged and unused weights must be removed).
Reconstruct the transition layer to classes after pruning kernels in the last layer of the last block ( in such a case the transition layer must remain mostly unchanged and unused weights must be removed ) .
"""
        Reconstruct the transition layer to classes after pruning kernels in
        the last layer of the last block (in such a case, the transition layer
        must remain mostly unchanged and unused weights must be removed).

        Args:
            _input: tensor, the operation's input;
            block: `int`, identifier number for the last block;
            layer: `int`, identifier number for the last layer in that block;
            kernels_to_prune: `list` of `int`, gives the specific kernels that
                were pruned in the last layer.
        """ # Copy the batch norm beta and gamma param values from old kernels. # Create a new batch norm and get its param variables. # self.sess.run(tf.variables_initializer([new_beta, new_gamma])) # Copy the param values corresponding to the remaining kernels. # Assign those param values to reconstruct the batch norm. # Rewrite: ReLU, average pooling, and reshaping into 1d. # For the FC layer: remove weights for unpruned kernels, keep all else.
self
_input
tensor, the operation's input.
tensor the operation ' s input .
block
`int`, identifier number for the last block.
` int ` identifier number for the last block .
layer
`int`, identifier number for the last layer in that block.
` int ` identifier number for the last layer in that block .
kernels_to_prune
`list` of `int`, gives the specific kernels that
were pruned in the last layer.
` list ` of ` int ` gives the specific kernels that were pruned in the last layer .