コード例 #1
0
 def __init__(self):
     import torch
     self.convnet = AllConvModelTorch(num_classes=10,
                                      num_filters=64,
                                      input_shape=[3, 32, 32])
     self.convnet.load_state_dict(
         torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))
コード例 #2
0
 def __init__(self):
     import torch
     self.convnet = AllConvModelTorch(num_classes=128,
                                      num_filters=64,
                                      input_shape=[3, 32, 32])
     self.convnet.load_state_dict(
         torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))
     self.convnet.layers = self.convnet.layers[:-1]
     self.features = np.load(get_checkpoint_abs_path(FEATURE_PATH)) # 50000 x 128
     self.labels = np.load(get_checkpoint_abs_path(LABEL_PATH)) # 50000 x 1
     self.NEAREST = 8
コード例 #3
0
class DefenseTorch(Defense):
    def __init__(self):
        import torch
        self.convnet = AllConvModelTorch(num_classes=10,
                                         num_filters=64,
                                         input_shape=[3, 32, 32])
        self.convnet.load_state_dict(
            torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))

    def augment(self, x):
        return data.augment_strong_np(x.transpose((0, 2, 3, 1))).transpose(
            (0, 3, 1, 2))
コード例 #4
0
    def __init__(self):
        import torch
        self.convnet = AllConvModelTorch(num_classes=10,
                                         num_filters=64,
                                         input_shape=[3, 32, 32])
        self.convnet.load_state_dict(
            torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))

        self.signature = np.load(get_checkpoint_abs_path(SIGNATURE_PATH))
        self.backdoor = np.load(get_checkpoint_abs_path(BACKDOOR_PATH))
        self.backdoor = self.backdoor.transpose((0, 3, 1, 2))
        self.hidden = get_hidden_layer(self.convnet, 3, (2, 3))
        self.to_tensor = torch.tensor
コード例 #5
0
class DefenseTorch(Defense):
    def __init__(self):
        import torch
        self.convnet = AllConvModelTorch(num_classes=10,
                                         num_filters=64,
                                         input_shape=[3, 32, 32])
        self.convnet.load_state_dict(
            torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))

    def blur(self, x):
        x_pad = np.pad(x, [(0, 0), (0, 0), (1, 1), (1, 1)])
        x_pad = (x_pad[:, :, :1] + x_pad[:, :, :-1]) / 2
        x_pad = (x_pad[:, :, :, :1] + x_pad[:, :, :, :-1]) / 2
        return x_pad
コード例 #6
0
    def __init__(self):
        import torch

        class Jump(torch.nn.Module):
            def forward(self, x):
                activation = torch.nn.LeakyReLU(0.2)
                x = activation(x)
                x += (x > 0).float() * 5
                return x

        self.convnet = AllConvModelTorch(num_classes=10,
                                         num_filters=64,
                                         input_shape=[3, 32, 32],
                                         activation=Jump())
        self.convnet.load_state_dict(torch.load(MODEL_PATH + ".torchmodel"))
コード例 #7
0
class DefenseTorch(Defense):
    def __init__(self):
        import torch
        self.convnet = AllConvModelTorch(num_classes=10,
                                         num_filters=64,
                                         input_shape=[3 * 20, 32, 32])
        self.convnet.load_state_dict(
            torch.load(get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel"))

    def encode(self, xs):
        thresholds = np.arange(0, 1, .05) + .05
        shape = xs.shape
        less_than_threshold = xs[:, :, None, :, :] < thresholds[None, None, :,
                                                                None, None]
        xs = np.array(less_than_threshold, dtype=np.float32)
        xs = np.reshape(xs,
                        [-1, shape[1] * len(thresholds), shape[2], shape[3]])
        return xs
コード例 #8
0
def fix(path):
    path_tf = path[:-6]
    path_torch = path_tf + ".torchmodel"
    if os.path.exists(path_torch):
        return

    print()
    print("Converting", path)
    
    # Get input sizes
    all_vars = tf.train.list_variables(
        get_checkpoint_abs_path(path_tf))

    # Is it a list of models? Or just one?
    if 'model/0' in "".join([x[0] for x in all_vars]):
        prefix = 'model/0'
    else:
        prefix = 'model'
        
    input_size, filter_size = [shape for name,shape in all_vars if prefix+'/layers/0/kernel' in name][0][2:]
    output_size = [shape for name,shape in all_vars if prefix+'/layers/9/kernel' in name][0][-1]

    num_models = sum('/0/kernel' in x for x,_ in all_vars)

    # Create the TF convnet
    convnet = [AllConvModel(num_classes=output_size,
                            num_filters=filter_size,
                            input_shape=(32, 32, input_size))
               for _ in range(num_models)]
    
    convnet_load = convnet[0] if num_models == 1 else convnet
    tf.train.Checkpoint(model=convnet_load).restore(
        get_checkpoint_abs_path(path_tf))

    weights = []
    for model in convnet:
        ws = []
        for layer in model.layers:
            if len(layer.weights) > 0:
                ws.append(layer.weights)
        weights.extend(ws[::-1])
    
    models = [AllConvModelTorch(10, 64, (input_size, 32, 32)) for _ in range(num_models)]
    for model in models:
        for layer in model.layers:
            if isinstance(layer, torch.nn.Conv2d):
                w, b = weights.pop()
                layer.weight = torch.nn.Parameter(torch.tensor(w.numpy().transpose((3,2,0,1))))
                layer.bias = torch.nn.Parameter(torch.tensor(b.numpy()))

    if len(models) == 1:
        torch.save(models[0].state_dict(), path_torch)
    else:
        torch.save([model.state_dict() for model in models], path_torch)
コード例 #9
0
 def __init__(self):
     import torch
     self.class_nets = []
     for class_idx in range(10):
         self.class_nets.append(
             AllConvModelTorch(num_classes=2,
                               num_filters=16,
                               input_shape=[3, 32, 32]))
         chkpt_rel_path = MODEL_PATH.format(class_idx) + ".torchmodel"
         self.class_nets[-1].load_state_dict(
             torch.load(get_checkpoint_abs_path(chkpt_rel_path)))
コード例 #10
0
    def __init__(self):
        import torch
        self.convnets = [
            AllConvModelTorch(num_classes=10,
                              num_filters=64 * 2 // 3,
                              input_shape=[3, 32, 32]) for _ in range(3)
        ]

        for i in range(3):
            model = torch.load(
                get_checkpoint_abs_path(MODEL_PATH % i) + ".torchmodel")
            self.convnets[i].load_state_dict(model)
コード例 #11
0
    def __init__(self):
        import torch
        self.convnets = [
            AllConvModelTorch(num_classes=10,
                              num_filters=64,
                              input_shape=[3, 32, 32]) for _ in range(3)
        ]

        models = torch.load(
            get_checkpoint_abs_path(MODEL_PATH) + ".torchmodel")
        for i, model in enumerate(models):
            self.convnets[i].load_state_dict(model)