def scale_model()

in pycls/models/scaler.py [0:0]


def scale_model():
    """
    Scale model blocks by the specified type and amount (note: alters global cfg).

    Scale a model using scaling strategies from "Fast and Accurate Model Scaling".
    For reference on scaling strategies, see: https://arxiv.org/abs/2103.06877.
    For example usage, see GETTING_STARTED, MODEL SCALING section.

    The actual scaling is specified by MODEL.SCALING_TYPE and MODEL.SCALING_FACTOR.
    For example, SCALING_TYPE of "d1_w8_g8_r1" is fast compound scaling and is the
    likely best default option, and SCALING_FACTOR indicates the scaling amount.
    For further details on controlling the scaling, see comments for scaling_factors().

    Note that the scaler must be employed on a standalone config outside of the main
    training loop. This is because it alters the global config, which is typically
    frozen during training. So one should use this function to generate a new config and
    save it to a file, and then evoke training separately on the new config.
    """
    assert cfg.MODEL.TYPE in ["anynet", "effnet", "regnet"]
    # Get scaling factors
    scale_type, scale = cfg.MODEL.SCALING_TYPE, cfg.MODEL.SCALING_FACTOR
    d_scale, w_scale, g_scale, r_scale = scaling_factors(scale_type, scale)
    if cfg.MODEL.TYPE == "regnet":
        # Convert a RegNet to an AnyNet prior to scaling
        regnet.regnet_cfg_to_anynet_cfg()
    if cfg.MODEL.TYPE == "anynet":
        # Scale AnyNet
        an = cfg.ANYNET
        ds, ws, bs, gs = an.DEPTHS, an.WIDTHS, an.BOT_MULS, an.GROUP_WS
        bs = bs if bs else [1] * len(ds)
        gs = gs if gs else [1] * len(ds)
        ds = [max(1, round(d * d_scale)) for d in ds]
        ws = [max(1, round(w * w_scale / 8)) * 8 for w in ws]
        gs = [max(1, round(g * g_scale)) for g in gs]
        gs = [g if g <= 2 else 4 if g <= 5 else round(g / 8) * 8 for g in gs]
        ws, bs, gs = adjust_block_compatibility(ws, bs, gs)
        an.DEPTHS, an.WIDTHS, an.BOT_MULS, an.GROUP_WS = ds, ws, bs, gs
    elif cfg.MODEL.TYPE == "effnet":
        # Scale EfficientNet
        en = cfg.EN
        ds, ws, bs, sw, hw = en.DEPTHS, en.WIDTHS, en.EXP_RATIOS, en.STEM_W, en.HEAD_W
        ds = [max(1, round(d * d_scale)) for d in ds]
        ws = [max(1, round(w * w_scale / 8)) * 8 for w in ws]
        sw = max(1, round(sw * w_scale / 8)) * 8
        hw = max(1, round(hw * w_scale / 8)) * 8
        ws, bs, _ = adjust_block_compatibility(ws, bs, [1] * len(ds))
        en.DEPTHS, en.WIDTHS, en.EXP_RATIOS, en.STEM_W, en.HEAD_W = ds, ws, bs, sw, hw
    # Scale image resolution
    cfg.TRAIN.IM_SIZE = round(cfg.TRAIN.IM_SIZE * r_scale / 4) * 4
    cfg.TEST.IM_SIZE = round(cfg.TEST.IM_SIZE * r_scale / 4) * 4