MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / backbone_scope

Function backbone_scope

examples/FasterRCNN/modeling/backbone.py:66–93  ·  view source on GitHub ↗

Args: freeze (bool): whether to freeze all the variables under the scope

(freeze)

Source from the content-addressed store, hash-verified

64
65@contextmanager
66def backbone_scope(freeze):
67 """
68 Args:
69 freeze (bool): whether to freeze all the variables under the scope
70 """
71 def nonlin(x):
72 x = get_norm()(x)
73 return tf.nn.relu(x)
74
75 with argscope([Conv2D, MaxPooling, BatchNorm], data_format='channels_first'), \
76 argscope(Conv2D, use_bias=False, activation=nonlin,
77 kernel_initializer=tf.variance_scaling_initializer(
78 scale=2.0, mode='fan_out')), \
79 ExitStack() as stack:
80 if cfg.BACKBONE.NORM in ['FreezeBN', 'SyncBN']:
81 if freeze or cfg.BACKBONE.NORM == 'FreezeBN':
82 stack.enter_context(argscope(BatchNorm, training=False))
83 else:
84 stack.enter_context(argscope(
85 BatchNorm, sync_statistics='nccl' if cfg.TRAINER == 'replicated' else 'horovod'))
86
87 if freeze:
88 stack.enter_context(freeze_variables(stop_gradient=False, skip_collection=True))
89 else:
90 # the layers are not completely freezed, but we may want to only freeze the affine
91 if cfg.BACKBONE.FREEZE_AFFINE:
92 stack.enter_context(custom_getter_scope(freeze_affine_getter))
93 yield
94
95
96def image_preprocess(image, bgr=True):

Callers 3

resnet_c4_backboneFunction · 0.85
resnet_conv5Function · 0.85
resnet_fpn_backboneFunction · 0.85

Calls 3

argscopeFunction · 0.90
freeze_variablesFunction · 0.90
custom_getter_scopeFunction · 0.90

Tested by

no test coverage detected