Ejemplo n.º 1
0
        fbn.NumPyModel(model, bounds=(0, 1), data_format="foo")

    fmodel = fbn.NumPyModel(model, bounds=(0, 1))
    with pytest.raises(ValueError, match="data_format"):
        x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=16)

    fmodel = fbn.NumPyModel(model, bounds=(0, 1), data_format="channels_first")
    with pytest.warns(UserWarning, match="returning NumPy arrays"):
        x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=16)

    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y


@pytest.fixture(scope="session", params=list(models.keys()))
def fmodel_and_data_ext(request: Any) -> ModelDescriptionAndData:
    global models
    model_description = models[request.param]
    model_and_data = model_description.model_fn(request)
    return ModelDescriptionAndData(model_and_data, *model_description[1:])


@pytest.fixture(scope="session", params=models_for_attacks)
def fmodel_and_data_ext_for_attacks(request: Any) -> ModelDescriptionAndData:
    global models
    model_description = models[request.param]
    model_and_data = model_description.model_fn(request)
    return ModelDescriptionAndData(model_and_data, *model_description[1:])

Ejemplo n.º 2
0
        transforms.ToTensor(),
        transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )
    ]
    trans = transforms.Compose(trans)
    test_dataset, test_char_idx = create_dataset(args.root, test_data, trans)

    # Load model
    device = "cpu" if args.gpu < 0 else "cuda:{}".format(args.gpu)

    saved_models = torch.load(args.model_fn, map_location="cpu")
    trunk, model = create_models(*saved_models["args"])
    models = {"trunk": trunk, "embedder": model}
    for key in models.keys():
        models[key].load_state_dict(saved_models[key])
        models[key].to(device)

    colormap = plt.get_cmap("hsv")
    plots = []
    for enroll, color in zip(args.n_enroll, np.linspace(0,0.8,len(args.n_enroll))):
        print("Enroll", enroll)
        enrolls, targets = split_enroll_target(test_char_idx, n_enroll=enroll, min_nimg=args.min_freq)
        print(f"Found {len(enrolls)} characters.")
        tops, prrcs = evaluate(models, test_dataset, enrolls, targets, mode=args.mode)

        glpr, glrc, lopr, lorc = prrcs
        p_glo, = plt.plot(glrc, glpr, color=colormap(color), ls="-")
        p_loc, = plt.plot(lorc, lopr, color=colormap(color), ls=":")
        plots.append((p_glo, p_loc))
Ejemplo n.º 3
0
        'densenet169': 224,
        'densenet201': 224,
        'mobilenet_v2': 224,
        'vgg11': 224,
        'vgg11_bn': 224,
        'vgg13': 224,
        'vgg13_bn': 224,
        'vgg16': 224,
        'vgg16_bn': 224,
        'vgg19': 224,
        'vgg19_bn': 224,
        'shufflenet_v2_x0_5': 224,
        'shufflenet_v2_x1_0': 224,
        'shufflenet_v2_x1_5': 224,
        'shufflenet_v2_x2_0': 224,
        'segmentation.deeplabv3_resnet101': 513,
        #'segmentation.deeplabv3_resnet50':513
    }

    parser.add_argument('model', choices=sorted(models.keys()))

    args = parser.parse_args()
    modelFunc = getattr(torchvision.models, args.model)
    model = modelFunc(pretrained=True)
    if hasattr(model, 'transform_input'):
        model.transform_input = False
    input_size = models[args.model]
    x = torch.randn(1, 3, input_size, input_size)
    torch.onnx.export(model, x, args.model + '.onnx')
    sys.exit(0)
models = {
    'resnet18_clnet': resnet18_clnet,
    'resnet34_clnet': resnet34_clnet,
    'resnet50_clnet': resnet50_clnet,
    'mobilenetv2_clnet': mobilenetv2_clnet,
    'sphereface4': sphereface4,
    'sphereface10': sphereface10,
    'sphereface20': sphereface20,
    'sphereface36': sphereface36,
    'sphereface64': sphereface64,
    'sphereface_mb2': mobilenet_sphereface,
    'resnet18_ccs_net': resnet18_ccs_net,
    'resnet34_ccs_net': resnet34_ccs_net,
    'mobilenetv2_ccs_net': mobilenetv2_ccs_net
}
model_names = list(models.keys())

parser = argparse.ArgumentParser(
    description='PyTorch Facial Recognition Net Training')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('--arch',
                    '-a',
                    metavar='ARCH',
                    default='resnet34_clnet',
                    choices=model_names,
                    help='model architecture: ' + ' | '.join(model_names) +
                    ' (default: resnet18)')
parser.add_argument('-d',
                    '--dim',
                    default=128,
                    type=int,
Ejemplo n.º 5
0
#https://pytorch.org/docs/stable/torchvision/models.html

import os
import torch
import torchvision.models as models

models = {}
models['resnet18'] = {'dummy_input':'torch.randn(1, 3, 224, 224)'}

for model_name in models.keys():
	dummy_input = models[model_name]['dummy_input']
	
	py_file = '''
# Copyright 2018 The DNNC Authors. All Rights Reserved.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.