Args: freeze (bool): whether to freeze all the variables under the scope
(freeze)
| 64 | |
| 65 | @contextmanager |
| 66 | def 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 | |
| 96 | def image_preprocess(image, bgr=True): |
no test coverage detected