部分代码:
def _get_variables_to_train(trainable_scopes = None):
"""Returns a list of variables to train.
Returns:
A list of variables to train by the optimizer.
"""
if trainable_scopes is None:
return tf.trainable_variables()
else:
scopes = [scope.strip() for scope in trainable_scopes.split(',')]
variables_to_train = []
for scope in scopes:
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
variables_to_train.extend(variables)
return variables_to_train
output_vars = _get_variables_to_train(Config.trainable_scopes)
#Config.trainable_scopes参数如下:
trainable_scopes = 'HCnet/Bottle_neck5,HCnet/Bottle_neck5_1,HCnet/Bottle_neck6_2,HCnet/Conv7'
当需要提取指定op进行操作时,可通过_get_variables_to_train函数可以获取指定的op层
结果如下:
output_vars:
[<tf.Variable 'HCnet/Bottle_neck5/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/weights:0' shape=(1, 1, 64, 80) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/BatchNorm/gamma:0' shape=(80,) dtype=float32_ref>,
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/BatchNorm/beta:0' shape=(80,) dtype=float32_ref>,
<tf.Variable 'HCnet/Conv7/weights:0' shape=(1, 1, 80, 24) dtype=float32_ref>,
<tf.Variable 'HCnet/Conv7/BatchNorm/gamma:0' shape=(24,) dtype=float32_ref>,
<tf.Variable 'HCnet/Conv7/BatchNorm/beta:0' shape=(24,) dtype=float32_ref>]