def __init__(self):
     self.convnet = AllConvModel(num_classes=128,
                                 num_filters=64,
                                 input_shape=[32, 32, 3])
     tf.train.Checkpoint(model=self.convnet).restore(
         get_checkpoint_abs_path(MODEL_PATH))
     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
Ejemplo n.º 2
0
 def __init__(self):
     self.convnet = AllConvModel(num_classes=10,
                                 num_filters=64,
                                 input_shape=[32, 32, 3])
     tf.train.Checkpoint(model=self.convnet).restore(
         get_checkpoint_abs_path(MODEL_PATH))
     self.signature = np.load(get_checkpoint_abs_path(SIGNATURE_PATH))
     self.backdoor = np.load(get_checkpoint_abs_path(BACKDOOR_PATH))
     self.hidden = get_hidden_layer(self.convnet, 3, (1, 2))
     self.to_tensor = lambda x: x
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)
 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
Ejemplo n.º 5
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
Ejemplo n.º 6
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"))
 def __init__(self):
     self.convnet = RandomDropModel(num_classes=10,
                                    num_filters=64,
                                    input_shape=[32, 32, 3])
     tf.train.Checkpoint(model=self.convnet).restore(
         get_checkpoint_abs_path(MODEL_PATH))
     self.to_tensor = lambda x: x
 def __init__(self):
     self.convnets = [
         AllConvModel(num_classes=10,
                      num_filters=64,
                      input_shape=[32, 32, 3]) for _ in range(3)
     ]
     tf.train.Checkpoint(model=self.convnets).restore(
         get_checkpoint_abs_path(MODEL_PATH))
Ejemplo n.º 9
0
 def __init__(self):
     self.class_nets = []
     for class_idx in range(10):
         self.class_nets.append(
             AllConvModel(num_classes=2,
                          num_filters=16,
                          input_shape=[32, 32, 3]))
         chkpt_rel_path = MODEL_PATH.format(class_idx)
         tf.train.Checkpoint(model=self.class_nets[-1]).restore(
             get_checkpoint_abs_path(chkpt_rel_path))
Ejemplo n.º 10
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)))
Ejemplo n.º 11
0
    def __init__(self):
        def jump(x):
            x = tf.nn.leaky_relu(x)
            x += tf.cast(x > 0, dtype=tf.float32) * 5
            return x

        self.convnet = AllConvModel(num_classes=10,
                                    num_filters=64,
                                    input_shape=[32, 32, 3],
                                    activation=jump)
        tf.train.Checkpoint(model=self.convnet).restore(
            get_checkpoint_abs_path(MODEL_PATH))
    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)
    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)
Ejemplo n.º 14
0
 def __init__(self):
     self.convnet = AllConvModel(num_classes=10,
                                 num_filters=64,
                                 input_shape=[32, 32, 3 * 20])
     tf.train.Checkpoint(model=self.convnet).restore(
         get_checkpoint_abs_path(MODEL_PATH))