def transfer2tf_minst(modelName): datatype = "mnist" modelPath = "../build-in-resource/pretrained-model/" + "/" + datatype + "/" + +modelName + ".pkl" tmp_to_save = os.path.join("./", "parameter_" + modelName + ".pkl") if modelName == "googlenet": seed_model = GoogLeNet() elif modelName == "lenet": seed_model = MnistNet4() seed_model.load_state_dict(torch.load(modelPath)) parameters, ordered_keys = extract_parametes_pytorch(seed_model) with open(tmp_to_save, "w") as f: pickle.dump({"parameters": parameters, "ordered_keys": ordered_keys}, f)
def build_model(self): import theano theano.config.on_unused_input = 'warn' if self.model_name == 'googlenet': from models.googlenet import GoogLeNet self.model = GoogLeNet(self.config) elif self.model_name == 'alexnet': from models.alex_net import AlexNet self.model = AlexNet(self.config) elif self.model_name == 'vggnet': if self.config['pretrain']: from models.vggnet_11_shallow import VGGNet_11 as VGGNet else: if self.config['source'] == 'lasagne': from models.lasagne_model_zoo.vgg import VGG as VGGNet elif self.config['source'] == 'Theano-MPI': from models.vggnet_16 import VGGNet_16 as VGGNet else: raise NotImplementedError self.model = VGGNet(self.config) elif self.model_name == 'customized': from models.customized import Customized self.model = Customized(self.config) else: raise NotImplementedError("wrong model name") self.model.img_mean = self.data[4]
def get_net(network: str, num_classes) -> torch.nn.Module: return VGG('VGG16', num_classes=num_classes) if network == 'VGG16' else \ ResNet34(num_classes=num_classes) if network == 'ResNet34' else \ PreActResNet18(num_classes=num_classes) if network == 'PreActResNet18' else \ GoogLeNet(num_classes=num_classes) if network == 'GoogLeNet' else \ densenet_cifar(num_classes=num_classes) if network == 'densenet_cifar' else \ ResNeXt29_2x64d(num_classes=num_classes) if network == 'ResNeXt29_2x64d' else \ MobileNet(num_classes=num_classes) if network == 'MobileNet' else \ MobileNetV2(num_classes=num_classes) if network == 'MobileNetV2' else \ DPN92(num_classes=num_classes) if network == 'DPN92' else \ ShuffleNetG2(num_classes=num_classes) if network == 'ShuffleNetG2' else \ SENet18(num_classes=num_classes) if network == 'SENet18' else \ ShuffleNetV2(1, num_classes=num_classes) if network == 'ShuffleNetV2' else \ EfficientNetB0( num_classes=num_classes) if network == 'EfficientNetB0' else None
def __init__(self, threshold, sigma, beta, alpha, seed_name, max_mutated_numbers, data_type, device='cpu', models_folder=None): self.threshold = threshold self.sigma = sigma self.beta = beta self.alpha = alpha self.device = device self.data_type = data_type self.models_folder = models_folder self.seed_name = seed_name self.max_mutated_numbers = max_mutated_numbers self.start_no = 1 self.seed_model_shell = GoogLeNet( ) if seed_name == "googlenet" else MnistNet4() if data_type == DATA_MNIST: self.max_models_in_memory = self.max_mutated_numbers self.mutated_models = fetch_models(models_folder, self.max_models_in_memory, self.device, self.seed_model_shell, start_no=self.start_no) else: self.max_models_in_memory = 20 self.mutated_models = fetch_models(models_folder, self.max_models_in_memory, self.device, self.seed_model_shell, start_no=self.start_no) self.start_no += self.max_models_in_memory
def test_adv_samples(): parser = argparse.ArgumentParser("Test Adversarial samples") parser.add_argument("--dataType", type=int, help="The data set that the given model is tailored to. Three types are available: mnist,0; " "cifar10, 1", default=0, required=True) parser.add_argument("--filePath", type=str, help="The path of samples to be tested", required=True) parser.add_argument("--device", type=int, help="The index of GPU used. If -1 is assigned,then only cpu is available", required=True) parser.add_argument("--advGround", type=int, help="1,if use adversarial label as ground truth;0,otherwise", required=True) args = parser.parse_args() use_adv_ground = True if args.advGround == 1 else False device = "cuda:" + str(args.device) if args.device >= 0 else "cpu" if args.dataType == DATA_CIFAR10: data_name = 'cifar10' dim_features = 3 * 32 * 32 num_out = 10 img_shape = {'C': 3, 'H': 32, 'W': 32} model = GoogLeNet() modelPath = "../build-in-resource/pretrained-model/googlenet.pkl" normalize = normalize_cifar10 img_mode = None elif args.dataType == DATA_MNIST: data_name = 'mnist' dim_features = 28 * 28 num_out = 10 img_shape = {'C': 1, 'H': 28, 'W': 28} model = MnistNet4() normalize = normalize_mnist modelPath = "../build-in-resource/pretrained-model/lenet.pkl" img_mode = "L" else: raise Exception("{} data set is not supported".format(args.dataType)) model.load_state_dict(torch.load(modelPath)) model = model.to(device) model.eval() dataset = MyDataset(root=args.filePath, transform=transforms.Compose([ transforms.ToTensor(), normalize ]), show_file_name=True, img_mode=img_mode, max_size=10000) dataLoader = DataLoader(dataset, batch_size=1, shuffle=False) correct = 0 for data_tuple in dataLoader: data, target, adv_label, file_name = data_tuple if use_adv_ground: target = adv_label data, target = data.to(device), target.to(device) output = model(data) pred = output.data.max(1, keepdim=True)[1] rst = pred.eq(target).sum().item() correct += rst print("{},Adversarial Label:{},Current Predict Label:{}").format(file_name, adv_label.item(), pred.item()) print("Total:{},Success:{}".format(len(dataLoader), correct))
def run(): parser = argparse.ArgumentParser("Prarameters for Detection") parser.add_argument( "--threshold", type=float, help= "The lcr_auc of normal samples. The value is equal to: avg+99%confidence.", required=True) parser.add_argument("--extendScale", type=float, help="The scale to extend the thrshold", required=True) parser.add_argument( "--relaxScale", type=float, help= "The proportion of threhold,which is useed to control the scale of indifference region", required=True) parser.add_argument("--mutatedModelsPath", type=str, help="The path where the pre-mutated models are", required=True) parser.add_argument("--alpha", type=float, help="probability od Type-I error", required=True) parser.add_argument("--beta", type=float, help="probability od Type-2 error", required=True) parser.add_argument("--testSamplesPath", type=str, help="The path where the samples to be tested are") parser.add_argument( "--dataType", type=int, help= "The data set that the given model is tailored to. Three types are available: mnist,0; " "cifar10, 1", default=0, required=True) parser.add_argument( "--testType", type=str, help= "Tree types are available: [adv], advesarial data; [normal], test on normal data; [wl],test on wrong labeled data", required=True) parser.add_argument("--seedModelPath", type=str, help="the path of the targeted model", required=True) parser.add_argument( "--device", type=int, help= "The index of GPU used. If -1 is assigned,then only cpu is available", required=True) args = parser.parse_args() threshold, sigma = get_threshold_relax(args.threshold, args.extendScale, args.relaxScale) device = "cuda:" + str(args.device) if args.device >= 0 else "cpu" seed_model_name = "googlenet" if args.dataType == DATA_CIFAR10 else "lenet" seed_model = GoogLeNet() if args.dataType == DATA_CIFAR10 else MnistNet4() if args.testType == "normal": data_loader = get_data_loader(args.testSamplesPath, is_adv_data=False, data_type=args.dataType) avg_accuracy, avg_mutated_used = main('normal', data_loader, args.mutatedModelsPath, seed_model_name, threshold, sigma, 'normal', device=device, data_type=args.dataType) elif args.testType == "wl": seed_model.load_state_dict(torch.load(args.seedModelPath)) data_loader = get_wrong_label_data_loader(args.testSamplesPath, seed_model, args.dataType, device=device) avg_accuracy, avg_mutated_used = main('adv', data_loader, args.mutatedModelsPath, seed_model_name, threshold, sigma, 'wl', device=device, data_type=args.dataType) elif args.testType == "adv": data_loader = get_data_loader(args.testSamplesPath, is_adv_data=True, data_type=args.dataType) avg_accuracy, avg_mutated_used = main('adv', data_loader, args.mutatedModelsPath, seed_model_name, threshold, sigma, "adv", device=device, data_type=args.dataType) else: raise Exception("Unsupported test type.") print("average accuracy:{}, average mutants used:{}".format( avg_accuracy, avg_mutated_used))
def get_network(name: str, num_classes: int) -> None: return \ AlexNet( num_classes=num_classes) if name == 'AlexNet' else\ DenseNet201( num_classes=num_classes) if name == 'DenseNet201' else\ DenseNet169( num_classes=num_classes) if name == 'DenseNet169' else\ DenseNet161( num_classes=num_classes) if name == 'DenseNet161' else\ DenseNet121( num_classes=num_classes) if name == 'DenseNet121' else\ DenseNet121CIFAR( num_classes=num_classes) if name == 'DenseNet121CIFAR' else\ GoogLeNet( num_classes=num_classes) if name == 'GoogLeNet' else\ InceptionV3( num_classes=num_classes) if name == 'InceptionV3' else\ MNASNet_0_5( num_classes=num_classes) if name == 'MNASNet_0_5' else\ MNASNet_0_75( num_classes=num_classes) if name == 'MNASNet_0_75' else\ MNASNet_1( num_classes=num_classes) if name == 'MNASNet_1' else\ MNASNet_1_3( num_classes=num_classes) if name == 'MNASNet_1_3' else\ MobileNetV2( num_classes=num_classes) if name == 'MobileNetV2' else\ ResNet18( num_classes=num_classes) if name == 'ResNet18' else\ ResNet34( num_classes=num_classes) if name == 'ResNet34' else\ ResNet34CIFAR( num_classes=num_classes) if name == 'ResNet34CIFAR' else\ ResNet50CIFAR( num_classes=num_classes) if name == 'ResNet50CIFAR' else\ ResNet101CIFAR( num_classes=num_classes) if name == 'ResNet101CIFAR' else\ ResNet18CIFAR( num_classes=num_classes) if name == 'ResNet18CIFAR' else\ ResNet50( num_classes=num_classes) if name == 'ResNet50' else\ ResNet101( num_classes=num_classes) if name == 'ResNet101' else\ ResNet152( num_classes=num_classes) if name == 'ResNet152' else\ ResNeXt50( num_classes=num_classes) if name == 'ResNext50' else\ ResNeXtCIFAR( num_classes=num_classes) if name == 'ResNeXtCIFAR' else\ ResNeXt101( num_classes=num_classes) if name == 'ResNext101' else\ WideResNet50( num_classes=num_classes) if name == 'WideResNet50' else\ WideResNet101( num_classes=num_classes) if name == 'WideResNet101' else\ ShuffleNetV2_0_5( num_classes=num_classes) if name == 'ShuffleNetV2_0_5' else\ ShuffleNetV2_1( num_classes=num_classes) if name == 'ShuffleNetV2_1' else\ ShuffleNetV2_1_5( num_classes=num_classes) if name == 'ShuffleNetV2_1_5' else\ ShuffleNetV2_2( num_classes=num_classes) if name == 'ShuffleNetV2_2' else\ SqueezeNet_1( num_classes=num_classes) if name == 'SqueezeNet_1' else\ SqueezeNet_1_1( num_classes=num_classes) if name == 'SqueezeNet_1_1' else\ VGG11( num_classes=num_classes) if name == 'VGG11' else\ VGG11_BN( num_classes=num_classes) if name == 'VGG11_BN' else\ VGG13( num_classes=num_classes) if name == 'VGG13' else\ VGG13_BN( num_classes=num_classes) if name == 'VGG13_BN' else\ VGG16( num_classes=num_classes) if name == 'VGG16' else\ VGG16_BN( num_classes=num_classes) if name == 'VGG16_BN' else\ VGG19( num_classes=num_classes) if name == 'VGG19' else\ VGG19_BN( num_classes=num_classes) if name == 'VGG19_BN' else \ VGGCIFAR('VGG16', num_classes=num_classes) if name == 'VGG16CIFAR' else \ EfficientNetB4( num_classes=num_classes) if name == 'EfficientNetB4' else \ EfficientNetB0CIFAR( num_classes=num_classes) if name == 'EfficientNetB0CIFAR' else\ None
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=4) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=50, shuffle=False, num_workers=4) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') # Model print('==> Building model..') if cnet == 'vgg': net = VGG('VGG19') elif cnet == 'resnet': net = ResNet18() elif cnet == 'googlenet': net = GoogLeNet() # net = PreActResNet18() # net = GoogLeNet() # net = DenseNet121() # net = ResNeXt29_2x64d() # net = MobileNet() # net = MobileNetV2() # net = DPN92() # net = ShuffleNetG2() # net = SENet18() net = net.to(device) if device == 'cuda': net = torch.nn.DataParallel(net) cudnn.benchmark = True if args.resume: