models/dense_net.py
def reconstruct_transition_to_classes(self, _input, block, layer):
        """
        Reconstruct the transition layer to classes after adding a new kernel
        or layer in the last block (in such a case, the transition layer must
        remain mostly unchanged except for the new weights).

        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.
        """
        new_features_total = int(_input.get_shape()[-1])
        var_scope = "Transition_to_FC_block_%d" % block
        FC_name = "FC_block_%d" % block
        if not self.preserve_transition_l:
            var_scope += "_layer_%d" % layer
            FC_name += "_layer_%d" % layer

        with tf.variable_scope(var_scope, reuse=tf.AUTO_REUSE):
            # The batch norm contains beta and gamma params for each kernel,
            # we first copy the 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]))
            # Then we create a new batch norm and initialize its params.
            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]))
            # For these params, we copy the old param values, and leave
            # the remaining new values for the new kernels.
            new_beta_values = self.sess.run(new_beta)
            new_gamma_values = self.sess.run(new_gamma)
            difference = new_features_total-self.features_total
            new_beta_values[:-difference] = beta_values
            new_gamma_values[:-difference] = gamma_values
            # Then we assign the modified 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

            # ReLU, average pooling, and reshaping into 1d
            # these do not contain any trainable params, so they are rewritten.
            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: add new weights, keep biases and old weights.
        for i in range(len(self.FC_W), features_total):
            self.FC_W_counter += 1
            self.FC_W.append(self.weight_variable_xavier(
                [self.n_classes],
                name=FC_name+("_W%d" % self.FC_W_counter)))
        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 adding a new kernel
        or layer in the last block (in such a case, the transition layer must
        remain mostly unchanged except for the new weights).

        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.
        
Reconstruct the transition layer to classes after adding a new kernel
or layer in the last block (in such a case, the transition layer must
remain mostly unchanged except for the new weights).
Reconstruct the transition layer to classes after adding a new kernel or layer in the last block ( in such a case the transition layer must remain mostly unchanged except for the new weights ) .
def reconstruct_transition_to_classes(self, _input, block, layer):
        new_features_total = int(_input.get_shape()[-1])
        var_scope = "Transition_to_FC_block_%d" % block
        FC_name = "FC_block_%d" % block
        if not self.preserve_transition_l:
            var_scope += "_layer_%d" % layer
            FC_name += "_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])
            self.sess.run(tf.variables_initializer([new_beta, new_gamma]))
            new_beta_values = self.sess.run(new_beta)
            new_gamma_values = self.sess.run(new_gamma)
            difference = new_features_total-self.features_total
            new_beta_values[:-difference] = beta_values
            new_gamma_values[:-difference] = gamma_values
            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 range(len(self.FC_W), features_total):
            self.FC_W_counter += 1
            self.FC_W.append(self.weight_variable_xavier(
                [self.n_classes],
                name=FC_name+("_W%d" % self.FC_W_counter)))
        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 ( self , _input , block , layer ) : new_features_total = int ( _input . get_shape ( ) [ - 1 ] ) var_scope = "Transition_to_FC_block_%d" % block FC_name = "FC_block_%d" % block if not self . preserve_transition_l : var_scope += "_layer_%d" % layer FC_name += "_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 ] ) self . sess . run ( tf . variables_initializer ( [ new_beta , new_gamma ] ) ) new_beta_values = self . sess . run ( new_beta ) new_gamma_values = self . sess . run ( new_gamma ) difference = new_features_total - self . features_total new_beta_values [ : - difference ] = beta_values new_gamma_values [ : - difference ] = gamma_values 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 range ( len ( self . FC_W ) , features_total ) : self . FC_W_counter += 1 self . FC_W . append ( self . weight_variable_xavier ( [ self . n_classes ] , name = FC_name + ( "_W%d" % self . FC_W_counter ) ) ) 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 adding a new kernel
or layer in the last block (in such a case, the transition layer must
remain mostly unchanged except for the new weights).
Reconstruct the transition layer to classes after adding a new kernel or layer in the last block ( in such a case the transition layer must remain mostly unchanged except for the new weights ) .
"""
        Reconstruct the transition layer to classes after adding a new kernel
        or layer in the last block (in such a case, the transition layer must
        remain mostly unchanged except for the new weights).

        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.
        """ # The batch norm contains beta and gamma params for each kernel, # we first copy the param values from old kernels. # Then we create a new batch norm and initialize its params. # For these params, we copy the old param values, and leave # the remaining new values for the new kernels. # Then we assign the modified values to reconstruct the batch norm. # ReLU, average pooling, and reshaping into 1d # these do not contain any trainable params, so they are rewritten. # For the FC layer: add new weights, keep biases and old weights.
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 .