我已经恢复了预训练的模型,并且我想重写最后一层。但是我不怎么提取某些层并重写它们。 这是已加载的代码:
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) as sess:
saver = tf.train.import_meta_graph(meta_path)
saver.restore(sess, model_path)
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('Placeholder:0')
y = graph.get_tensor_by_name('Placeholder_1:0')
这是预先训练的网络模型:
def fw_net(x, training=True):
with tf.variable_scope('fw_net', reuse=tf.AUTO_REUSE):
with tf.variable_scope('fc1', reuse=tf.AUTO_REUSE):
x = ms.fc(x, 5*5*256, name='fc')
x = ms.bn(x, training=training, name='bn')
x = ms.activation(x, relu=True, name='relu')
x = tf.reshape(x, [-1, 5, 5, 256])
with tf.variable_scope('id_blk1', reuse=tf.AUTO_REUSE):
x = id_blk(x, 256, [3, 3], training)
with tf.variable_scope('id_blk2', reuse=tf.AUTO_REUSE):
x = id_blk(x, 256, [3, 3], training) # [-1, 5, 5, 256]
with tf.variable_scope('t_conv1', reuse=tf.AUTO_REUSE):
x = ms.t_conv2d(x, 128, [2, 2], 2, name='t_c')
x = ms.bn(x, training=training, name='bn')
x = ms.activation(x, relu=True, name='relu')
with tf.variable_scope('id_blk3', reuse=tf.AUTO_REUSE):
x = id_blk(x, 128, [3, 3], training)
with tf.variable_scope('id_blk4', reuse=tf.AUTO_REUSE):
x = id_blk(x, 128, [3, 3], training) # [-1, 10, 10, 128]
with tf.variable_scope('t_conv2', reuse=tf.AUTO_REUSE):
x = ms.t_conv2d(x, 64, [2, 2], 2, 't_c')
x = ms.bn(x, training=training, name='bn')
x = ms.activation(x, relu=True, name='relu')
with tf.variable_scope('id_blk5', reuse=tf.AUTO_REUSE):
x = id_blk(x, 64, [3, 3], training)
with tf.variable_scope('id_blk6', reuse=tf.AUTO_REUSE):
x = id_blk(x, 64, [3, 3], training) # [-1, 20, 20, 64]
with tf.variable_scope('t_conv3', reuse=tf.AUTO_REUSE):
x = ms.t_conv2d(x, 32, [2, 2], 2, name='t_c')
x = ms.bn(x, training=training, name='bn')
x = ms.activation(x, relu=True, name='relu')
with tf.variable_scope('id_blk7', reuse=tf.AUTO_REUSE):
x = id_blk(x, 32, [3, 3], training)
with tf.variable_scope('id_blk8', reuse=tf.AUTO_REUSE):
x = id_blk(x, 32, [3, 3], training) # [-1, 40, 40, 32]
x = tf.reshape(x, [-1, 40*40*32])
with tf.variable_scope('output', reuse=tf.AUTO_REUSE):
x = ms.fc(x, units=603, name='fc')
print(x.name)
return x
如何冻结预训练的模型并重新训练重写的图层?