根据冻结的模型创建可训练的Tensorflow图

时间:2019-07-24 05:22:08

标签: python tensorflow

我试图从冻结模型创建可训练的张量流模型。

我拥有冻结模型的所有权重和张量流层。

使用我创建的新图并使用这些权重进行初始化。

看起来还可以。

但是当我打印所有图层名称时,每个图层都会打印三个名称,例如

Mconv4_stage6_L2/biases
Mconv4_stage6_L2/biases/Assign
Mconv4_stage6_L2/biases/read
Mconv4_stage6_L2/weights
Mconv4_stage6_L2/weights/Assign
Mconv4_stage6_L2/weights/read

那些分配和读取层是什么?

权重和偏差的初始化如下。

    def make_var(self, name, initializer=[], trainable=True):
       '''Creates a new TensorFlow variable.'''
       return tf.get_variable(name, trainable=self.trainable & trainable, initializer=initializer)

    @layer
    def conv(self,
             input,
             k_h,
             k_w,
             c_o,
             s_h,
             s_w,
             name,
             relu=True,
             padding=DEFAULT_PADDING,
             group=1,
             trainable=False,
             biased=True):
        # Verify that the padding is acceptable
        self.validate_padding(padding)
        # Get the number of channels in the input
        c_i = int(input.get_shape()[-1])
        # Verify that the grouping parameter is valid
        assert c_i % group == 0
        assert c_o % group == 0
        # Convolution for a given input and kernel
        convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding)
        with tf.variable_scope(name) as scope:
            for init_zer in self.inits_:
                if(init_zer['name'] == name+'/'+'weights' and 'conv' in init_zer['name']):
                    #print('weights '+init_zer['name'])
                    kernel = self.make_var('weights', initializer=tf.constant(init_zer['tensor']), trainable=self.trainable & trainable)#shape=[k_h, k_w, c_i / group, c_o]
            if group == 1:
                # This is the common-case. Convolve the input without any further complications.
                output = convolve(input, kernel)
            else:
                # Split the input into groups and then convolve each of them independently
                input_groups = tf.split(3, group, input)
                kernel_groups = tf.split(3, group, kernel)
                output_groups = [convolve(i, k) for i, k in zip(input_groups, kernel_groups)]
                # Concatenate the groups
                output = tf.concat(3, output_groups)
            # Add the biases
            if biased:
                for init_zer in self.inits_:
                    if(init_zer['name'] == name+'/'+'biases' and 'conv' in init_zer['name']):
                        #print('bias '+init_zer['name'])
                        biases = self.make_var('biases', initializer=tf.constant(init_zer['tensor']), trainable=self.trainable & trainable)
                output = tf.nn.bias_add(output, biases)

            if relu:
                # ReLU non-linearity
                output = tf.nn.relu(output, name=scope.name)
            return output

在这种情况下,我是否需要再次运行global_variables_initializer? 我的整个代码是 https://www.dropbox.com/s/rcoc1hsd3j2i0od/CMUNet.py?dl=0

https://www.dropbox.com/s/kcmrm06k9dz40em/network_base.py?dl=0

https://www.dropbox.com/s/2r164gdk2fzshhr/network_cmu.py?dl=0

1 个答案:

答案 0 :(得分:0)

我的代码是正确的。我所做的是上传模型并初始化权重和偏差。然后保存模型。从保存的模型和检查点冻结新模型。我在Tensorboard上比较了旧的冻结模型和新的冻结模型。它们是相同的,我看不到具有Assign和read的任何节点。但是我想知道当我打印节点名称时它们是什么。