Exemple #1
0
def sdn_ic_only_backdoored(device):
    params = arcs.create_vgg16bn(None, 'cifar10', None, True)

    path = 'backdoored_models'
    backdoored_cnn_name = 'VGG16_cifar10_backdoored'
    save_sdn_name = 'VGG16_cifar10_backdoored_SDN'

    # Use the class VGG
    backdoored_cnn = VGG(params)
    backdoored_cnn.load_state_dict(torch.load('{}/{}'.format(
        path, backdoored_cnn_name),
                                              map_location='cpu'),
                                   strict=False)

    # convert backdoored cnn into a sdn
    backdoored_sdn, sdn_params = af.cnn_to_sdn(
        None, backdoored_cnn, params,
        preloaded=backdoored_cnn)  # load the CNN and convert it to a sdn
    arcs.save_model(backdoored_sdn, sdn_params, path, save_sdn_name,
                    epoch=0)  # save the resulting sdn

    networks = [save_sdn_name]

    train(path, networks, sdn=True, ic_only_sdn=True, device=device)
Exemple #2
0
def load_model(models_path, model_name, epoch=0):
    model_params = load_params(models_path, model_name, epoch)

    architecture = 'empty' if 'architecture' not in model_params else model_params['architecture']
    network_type = model_params['network_type']

    if architecture == 'sdn' or 'sdn' in model_name:

        if 'wideresnet' in network_type:
            model = WideResNet_SDN(model_params)
        elif 'resnet' in network_type:
            model = ResNet_SDN(model_params)
        elif 'vgg' in network_type:
            model = VGG_SDN(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet_SDN(model_params)

    elif architecture == 'cnn' or 'cnn' in model_name:
        if 'wideresnet' in network_type:
            model = WideResNet(model_params)
        elif 'resnet' in network_type:
            model = ResNet(model_params)
        elif 'vgg' in network_type:
            model = VGG(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet(model_params)

    elif 'iterative' or 'dense' in model_name:
        model = ResNet_Baseline(model_params)
        num_to_grow = sum([1 if epoch > grow else 0 for grow in model_params['epoch_growth']]) if epoch != -1 else len(model_params['epoch_growth'])
        for _ in range(num_to_grow):
            model.grow()
        

    network_path = models_path + '/' + model_name

    if epoch == 0:  # untrained model
        load_path = network_path + '/untrained'
    elif epoch == -1:  # last model
        load_path = network_path + '/last'
    else:
        load_path = network_path + '/' + str(epoch)

    model.load_state_dict(torch.load(load_path), strict=False)

    return model, model_params
Exemple #3
0
def save_networks(model_name, model_params, models_path, save_type, return_model=False):
    cnn_name = model_name + '_cnn'
    sdn_name = model_name + '_sdn'
    model = None
    if 'c' in save_type:
        print('Saving CNN...')
        model_params['architecture'] = 'cnn'
        model_params['base_model'] = cnn_name
        network_type = model_params['network_type']

        if 'wideresnet' in network_type:
            model = WideResNet(model_params)
        elif 'resnet' in network_type:
            model = ResNet(model_params)
        elif 'vgg' in network_type:
            model = VGG(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet(model_params)

        save_model(model, model_params, models_path, cnn_name, epoch=0)

    if 'd' in save_type:
        print('Saving SDN...')
        model_params['architecture'] = 'sdn'
        model_params['base_model'] = sdn_name
        network_type = model_params['network_type']

        if 'wideresnet' in network_type:
            model = WideResNet_SDN(model_params)
        elif 'resnet' in network_type:
            model = ResNet_SDN(model_params)
        elif 'vgg' in network_type:
            model = VGG_SDN(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet_SDN(model_params)

        save_model(model, model_params, models_path, sdn_name, epoch=0)
    if return_model:
        return model
    return cnn_name, sdn_name
Exemple #4
0
def load_model(models_path, model_name, epoch=0):
    model_params = load_params(models_path, model_name, epoch)

    architecture = 'empty' if 'architecture' not in model_params else model_params[
        'architecture']
    network_type = model_params['network_type']

    if architecture == 'sdn' or 'sdn' in model_name:

        if 'wideresnet' in network_type:
            model = WideResNet_SDN(model_params)
        elif 'resnet' in network_type:
            model = ResNet_SDN(model_params)
        elif 'vgg' in network_type:
            model = VGG_SDN(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet_SDN(model_params)

    elif architecture == 'cnn' or 'cnn' in model_name:
        if 'wideresnet' in network_type:
            model = WideResNet(model_params)
        elif 'resnet' in network_type:
            model = ResNet(model_params)
        elif 'vgg' in network_type:
            model = VGG(model_params)
        elif 'mobilenet' in network_type:
            model = MobileNet(model_params)

    network_path = models_path + '/' + model_name

    if epoch == 0:  # untrained model
        load_path = network_path + '/untrained'
    elif epoch == -1:  # last model
        load_path = network_path + '/last'
    else:
        load_path = network_path + '/' + str(epoch)

    model.load_state_dict(torch.load(load_path), strict=False)

    return model, model_params