コード例 #1
0
    def __init__(self,
                 model_name="vgg16",
                 MODEL_DIR="",
                 EPOCHS=[],
                 P=[1, 2, 3],
                 SUBBANDS=["d", "h", "v"],
                 IMAGE_SIZE=32,
                 N_CHANELS=3,
                 use_cuda=True,
                 is_prefilt=False):
        super(MultiChannel, self).__init__()

        if model_name == "vgg16":
            net = modelvgg.VGG('VGG16')
        elif model_name == "resnet18":
            net = modelresnet.ResNet18()

        self.image_size = IMAGE_SIZE
        self.n_channel = N_CHANELS
        self.use_cuda = use_cuda
        self.is_prefilt = is_prefilt
        self.epochs = EPOCHS
        self.p = P
        self.subbands = SUBBANDS
        self.NET = []
        self.permutation = []

        i = -1
        for p in P:
            for sb in SUBBANDS:
                i += 1

                # --- load model ----
                FROM_DIR = MODEL_DIR % (sb, p)
                net.load_state_dict(
                    torch.load(FROM_DIR + "/%s_%d.pth" %
                               (model_name, EPOCHS[i]),
                               map_location=lambda storage, loc: storage))

                if use_cuda:
                    net.cuda()
                else:
                    net.cpu()
                net.eval()
                self.NET.append(net)
                # --- end load model ----

                # --- load permutation ----
                self.permutation.append(
                    np.load(
                        FROM_DIR +
                        "/permutation_cifar_%s_dct_sign_permutation_subband_%s_%d.npy"
                        % (model_name, sb, p)))
コード例 #2
0
    R = 10
    if args.permut == args.channel_n or not args.is_random_channels:
        R = 1

    data_dir_ = "data/attacked/cifar/multi_channel/%s/permutations_%d/p%d" % (
        args.model, args.permut, args.pixels)
    model_dir_ = "checkpoints/multi_channel/%s/permutations_%d" % (args.model,
                                                                   args.permut)

    SUBBANDS = ["d", "h", "v"]
    EPOCHS = [100, 100, 100, 100, 100, 100, 100, 100, 100]
    P = np.asarray(range(1, round(args.permut / 3) + 1))

    if args.model == "vgg16":
        Net = modelvgg.VGG('VGG16')
    elif args.model == "resnet18":
        Net = modelresnet.ResNet18()

    E = []
    for r in range(R):

        if args.is_random_channels:
            error = testClasifierRandom(Net,
                                        EPOCHS,
                                        model_dir_,
                                        SUBBANDS=SUBBANDS,
                                        P=P,
                                        channel_n=args.channel_n,
                                        is_vanila=args.is_vanila,
                                        is_prefilt=args.is_prefilt,