Exemple #1
0
def convert():
    architecture = [0, 0, 0, 1, 0, 0, 1, 0, 3, 2, 0, 1, 2, 2, 1, 2, 0, 0, 2, 0]
    scale_ids = [8, 7, 6, 8, 5, 7, 3, 4, 2, 4, 2, 3, 4, 5, 6, 6, 3, 3, 4, 6]
    net = get_shufflenas_oneshot(architecture=architecture, scale_ids=scale_ids, use_se=True,
                                 last_conv_after_pooling=True,  shuffle_by_conv=True)

    # load params
    net._initialize(force_reinit=True, dtype='float32')
    net.cast('float16')
    net.load_parameters('../models/oneshot-s+model-0000.params', allow_missing=True)
    net.cast('float32')

    # save both gluon model and symbols
    test_data = nd.ones([5, 3, 224, 224], dtype='float32')
    _ = net(test_data)
    net.summary(test_data)
    net.hybridize()

    if not os.path.exists('./symbols'):
        os.makedirs('./symbols')
    if not os.path.exists('./params'):
        os.makedirs('./params')
    net.cast('float16')
    net.load_parameters('../models/oneshot-s+model-0000.params', allow_missing=True)
    net.cast('float32')
    net.hybridize()
    net(test_data)
    net.save_parameters('./params/ShuffleNas_fixArch_shuffleByConv-0000.params')
    net.export("./symbols/ShuffleNas_fixArch_shuffleByConv", epoch=0)
    flops, model_size = get_flops(symbol_path='./symbols/ShuffleNas_fixArch_shuffleByConv-symbol.json')
    print("Last conv after pooling: {}, use se: {}".format(True, True))
    print("FLOPS: {}M, # parameters: {}M".format(flops, model_size))
def get_flop_param_score(block_choices,
                         channel_choices,
                         comparison_model='SinglePathOneShot',
                         use_se=False,
                         last_conv_after_pooling=False,
                         channels_layout='OneShot'):
    """ Return the flops and num of params """
    # build fix_arch network and calculate flop
    fixarch_net = get_shufflenas_oneshot(
        block_choices,
        channel_choices,
        use_se=use_se,
        last_conv_after_pooling=last_conv_after_pooling,
        channels_layout=channels_layout)
    fixarch_net._initialize()
    if not os.path.exists('./symbols'):
        os.makedirs('./symbols')
    fixarch_net.hybridize()

    # calculate flops and num of params
    dummy_data = nd.ones([1, 3, 224, 224])
    fixarch_net(dummy_data)
    fixarch_net.export("./symbols/ShuffleNas_fixArch", epoch=1)

    flops, model_size = get_flops(
        symbol_path="./symbols/ShuffleNas_fixArch-symbol.json"
    )  # both in Millions

    # proves ShuffleNet series calculate == google paper's
    if comparison_model == 'MobileNetV3_large':
        flops_constraint = 217
        parameter_number_constraint = 5.4

    # proves MicroNet challenge doubles what google paper claimed
    elif comparison_model == 'MobileNetV2_1.4':
        flops_constraint = 585
        parameter_number_constraint = 6.9

    elif comparison_model == 'SinglePathOneShot':
        flops_constraint = 328
        parameter_number_constraint = 3.4

    # proves mine calculation == ShuffleNet series' == google paper's
    elif comparison_model == 'ShuffleNetV2+_medium':
        flops_constraint = 222
        parameter_number_constraint = 5.6

    else:
        raise ValueError(
            "Unrecognized comparison model: {}".format(comparison_model))

    flop_score = flops / flops_constraint
    model_size_score = model_size / parameter_number_constraint

    return flops, model_size, flop_score, model_size_score
def main():
    if FIX_ARCH:
        # architecture = [0, 0, 3, 1, 1, 1, 0, 0, 2, 0, 2, 1, 1, 0, 2, 0, 2, 1, 3, 2]
        # scale_ids = [6, 5, 3, 5, 2, 6, 3, 4, 2, 5, 7, 5, 4, 6, 7, 4, 4, 5, 4, 3]
        architecture = [0, 0, 0, 1, 0, 0, 1, 0, 3, 2, 0, 1, 2, 2, 1, 2, 0, 0, 2, 0]
        scale_ids = [8, 7, 6, 8, 5, 7, 3, 4, 2, 4, 2, 3, 4, 5, 6, 6, 3, 3, 4, 6]
        net = get_shufflenas_oneshot(architecture=architecture, scale_ids=scale_ids, channels_layout=CHANNELS_LAYOUT,
                                     use_se=USE_SE, last_conv_after_pooling=LAST_CONV_AFTER_POOLING)
    else:
        net = get_shufflenas_oneshot(use_se=USE_SE, last_conv_after_pooling=LAST_CONV_AFTER_POOLING,
                                     channels_layout=CHANNELS_LAYOUT)

    """ Test customized initialization """
    net._initialize(force_reinit=True)
    print(net)

    """ Test ShuffleNasOneShot """
    test_data = nd.ones([5, 3, 224, 224])
    for step in range(1):
        if FIX_ARCH:
            test_outputs = net(test_data)
            net.summary(test_data)
            net.hybridize()
        else:
            block_choices = net.random_block_choices(select_predefined_block=False, dtype='float32')
            full_channel_mask, _ = net.random_channel_mask(select_all_channels=False, dtype='float32')
            test_outputs = net(test_data, block_choices, full_channel_mask)
            net.summary(test_data, block_choices, full_channel_mask)
    if FIX_ARCH:
        if not os.path.exists('./symbols'):
            os.makedirs('./symbols')
        net(test_data)
        net.export("./symbols/ShuffleNas_fixArch", epoch=0)
        flops, model_size = get_flops()
        print("Last conv after pooling: {}, use se: {}".format(LAST_CONV_AFTER_POOLING, USE_SE))
        print("FLOPS: {}M, # parameters: {}M".format(flops, model_size))
    else:
        if not os.path.exists('./params'):
            os.makedirs('./params')
        net.save_parameters('./params/ShuffleNasOneshot-imagenet-supernet.params')

        """ Test generating random channels """
        epoch_start_cs = 30
        use_all_channels = True if epoch_start_cs != -1 else False
        dtype = 'float16'

        for epoch in range(120):
            if epoch == epoch_start_cs:
                use_all_channels = False
            for batch in range(1):
                full_channel_mask, channel_choices = net.random_channel_mask(select_all_channels=use_all_channels,
                                                                             epoch_after_cs=epoch - epoch_start_cs,
                                                                             dtype=dtype,
                                                                             ignore_first_two_cs=True)
                print("Epoch {}: {}".format(epoch, channel_choices))
Exemple #4
0
def search_supernet(net, search_iters=2000, bn_iters=50000, num_gpus=0):
    # TODO: use a heapq here to store top-5 models
    train_data, val_data, batch_fn = get_data(num_gpus=num_gpus)
    best_acc = 0
    best_block_choices = None
    best_channel_choices = None
    context = [mx.gpu(i)
               for i in range(num_gpus)] if num_gpus > 0 else [mx.cpu()]
    acc_top1 = mx.metric.Accuracy()
    acc_top5 = mx.metric.TopKAccuracy(5)
    for _ in range(search_iters):
        block_choices = net.random_block_choices(select_predefined_block=False,
                                                 dtype='float32')
        full_channel_mask, channel_choices = net.random_channel_mask(
            select_all_channels=False, dtype='float32')
        # Update BN
        # for _ in range(bn_iters):
        #     data, _ = generate_random_data_label()
        #     net(data, block_choices, full_channel_mask)
        # Get validation accuracy
        val_acc = get_accuracy(net,
                               val_data,
                               batch_fn,
                               block_choices,
                               full_channel_mask,
                               acc_top1=acc_top1,
                               acc_top5=acc_top5,
                               ctx=context)
        if val_acc > best_acc:
            best_acc = val_acc
            best_block_choices = copy.deepcopy(block_choices)
            best_channel_choices = copy.deepcopy(channel_choices)
        # build fix_arch network and calculate flop
        fixarch_net = get_shufflenas_oneshot(block_choices.asnumpy(),
                                             channel_choices)
        fixarch_net.initialize()
        if not os.path.exists('./symbols'):
            os.makedirs('./symbols')
        fixarch_net.hybridize()
        dummy_data = nd.ones([1, 3, 224, 224])
        fixarch_net(dummy_data)
        fixarch_net.export("./symbols/ShuffleNas_fixArch", epoch=1)
        flops, model_size = get_flops()
        print('-' * 40)
        print("Val accuracy: {}".format(val_acc))
        print('flops: ', str(flops), ' MFLOPS')
        print('model size: ', str(model_size), ' MB')

    print('-' * 40)
    print("Best val accuracy: {}".format(best_acc))
    print("Best block choices: {}".format(best_block_choices.asnumpy()))
    print("Best channel choices: {}".format(best_channel_choices))
Exemple #5
0
def get_block_flop(block, input_data):
    fixarch_net = block
    fixarch_net.initialize()
    if not os.path.exists('./symbols'):
        os.makedirs('./symbols')
    fixarch_net.hybridize()

    # calculate flops and num of params
    output_data = fixarch_net(input_data)
    fixarch_net.export("./symbols/ShuffleNas_block", epoch=1)

    data_shapes = [('data', input_data.shape)]
    flops, model_size = get_flops(symbol_path="./symbols/ShuffleNas_block-symbol.json", data_shapes=data_shapes)
    return flops, model_size, output_data
def main():
    if FIX_ARCH:
        architecture = [
            0, 0, 3, 1, 1, 1, 0, 0, 2, 0, 2, 1, 1, 0, 2, 0, 2, 1, 3, 2
        ]
        scale_ids = [
            6, 5, 3, 5, 2, 6, 3, 4, 2, 5, 7, 5, 4, 6, 7, 4, 4, 5, 4, 3
        ]
        net = get_shufflenas_oneshot(
            architecture=architecture,
            scale_ids=scale_ids,
            use_se=USE_SE,
            last_conv_after_pooling=LAST_CONV_AFTER_POOLING)
    else:
        net = get_shufflenas_oneshot(
            use_se=USE_SE, last_conv_after_pooling=LAST_CONV_AFTER_POOLING)
    """ Test customized initialization """
    net._initialize(force_reinit=True)
    print(net)
    """ Test ShuffleNasOneShot """
    test_data = nd.ones([5, 3, 224, 224])
    for step in range(1):
        if FIX_ARCH:
            test_outputs = net(test_data)
            net.summary(test_data)
            net.hybridize()
        else:
            block_choices = net.random_block_choices(
                select_predefined_block=False, dtype='float32')
            full_channel_mask, _ = net.random_channel_mask(
                select_all_channels=False, dtype='float32')
            test_outputs = net(test_data, block_choices, full_channel_mask)
            net.summary(test_data, block_choices, full_channel_mask)
    if FIX_ARCH:
        if not os.path.exists('./symbols'):
            os.makedirs('./symbols')
        net(test_data)
        net.export("./symbols/ShuffleNas_fixArch", epoch=0)
        flops, model_size = get_flops()
        print("Last conv after pooling: {}, use se: {}".format(
            LAST_CONV_AFTER_POOLING, USE_SE))
        print("FLOPS: {}M, # parameters: {}M".format(flops, model_size))
    else:
        if not os.path.exists('./params'):
            os.makedirs('./params')
        net.save_parameters(
            './params/ShuffleNasOneshot-imagenet-supernet.params')
    print(test_outputs.shape)
def main():
    # Save a toy SE SuperNet for playing with the search codes
    supernet = get_shufflenas_oneshot(use_se=True,
                                      last_conv_after_pooling=True,
                                      channels_layout='OneShot')
    supernet._initialize(force_reinit=True)
    if not os.path.exists('./params'):
        os.makedirs('./params')
    test_data = nd.ones([5, 3, 224, 224])
    for step in range(1):
        block_choices = supernet.random_block_choices(
            select_predefined_block=False, dtype='float32')
        full_channel_mask, _ = supernet.random_channel_mask(
            select_all_channels=False, dtype='float32')
        _ = supernet(test_data, block_choices, full_channel_mask)

    if not os.path.exists('./params'):
        os.makedirs('./params')
    supernet.save_parameters(
        './params/ShuffleNasOneshot-imagenet-supernet.params')

    # Profiling the OneShot-s+
    architecture = [0, 0, 0, 1, 0, 0, 1, 0, 3, 2, 0, 1, 2, 2, 1, 2, 0, 0, 2, 0]
    scale_ids = [8, 7, 6, 8, 5, 7, 3, 4, 2, 4, 2, 3, 4, 5, 6, 6, 3, 3, 4, 6]
    net = get_shufflenas_oneshot(architecture=architecture,
                                 scale_ids=scale_ids,
                                 channels_layout='OneShot',
                                 use_se=True,
                                 last_conv_after_pooling=True)
    net._initialize(force_reinit=True)
    print(net)
    test_data = nd.ones([5, 3, 224, 224])
    for step in range(1):
        _ = net(test_data)
    net.summary(test_data)

    net.hybridize()
    if not os.path.exists('./symbols'):
        os.makedirs('./symbols')
    net(test_data)
    net.export("./symbols/ShuffleNas_fixArch", epoch=0)
    flops, model_size = get_flops()
    print("Last conv after pooling: {}, use se: {}".format(True, True))
    print("FLOPS: {}M, # parameters: {}M".format(flops, model_size))