-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_classifier.py
180 lines (150 loc) · 8.79 KB
/
run_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Created on September 21st, 2020
@author: urikotlicki
"""
# import system modules
import os
import os.path as osp
import sys
import time
import argparse
import numpy as np
# add paths
parent_dir = osp.dirname(osp.dirname(osp.abspath(__file__)))
if parent_dir not in sys.path:
sys.path.append(parent_dir)
# import modules
from src.autoencoder import Configuration as Conf
from src.adversary_utils import load_data, prepare_data_for_attack, get_quantity_at_index
from src.in_out import create_dir
from src.general_utils import plot_3d_point_cloud
from src.tf_utils import reset_tf_graph
from classifier.pointnet_classifier import PointNetClassifier
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--classifier_folder', type=str, default='log/pointnet', help='Folder of the classifier to be used [default: log/pointnet]')
parser.add_argument('--classifier_restore_epoch', type=int, default=150, help='Restore epoch for the pre-trained classifier [default: 150]')
parser.add_argument('--data_type', type=str, default='adversarial', help='Data type to be classified [default: adversarial]')
parser.add_argument('--ae_folder', type=str, default='log/autoencoder_victim', help='Folder for loading a trained autoencoder model [default: log/autoencoder_victim]')
parser.add_argument('--num_points', type=int, default=2048, help='Number of points in the reconstructed point cloud [default: 2048]')
parser.add_argument("--attack_pc_idx", type=str, default='log/autoencoder_victim/eval/sel_idx_rand_100_test_set_13l.npy', help="List of indices of point clouds for the attack")
parser.add_argument('--attack_folder', type=str, default='attack_res', help='Folder for loading attack data')
parser.add_argument('--defense_folder', type=str, default='defense_critical_res', help='Folder for loading defense data')
parser.add_argument("--output_folder_name", type=str, default='classifier_res', help="Output folder name")
parser.add_argument('--num_classes', type=int, default=13, help='Number of classes [default: 13]')
flags = parser.parse_args()
print('Run classifier flags:', flags)
assert flags.data_type in ['target', 'adversarial', 'source', 'before_defense', 'after_defense'], 'wrong data_type: %s.' % flags.data_type
# define basic parameters
top_out_dir = osp.dirname(osp.dirname(osp.abspath(__file__))) # Use to save Neural-Net check-points etc.
data_path = osp.join(top_out_dir, flags.ae_folder, 'eval')
files = [f for f in os.listdir(data_path) if osp.isfile(osp.join(data_path, f))]
classifier_path = osp.join(top_out_dir, flags.classifier_folder)
if flags.data_type == 'target':
classifier_data_path = osp.join(data_path, flags.attack_folder)
output_path = create_dir(osp.join(classifier_data_path, flags.output_folder_name + '_orig'))
elif flags.data_type == 'adversarial':
classifier_data_path = osp.join(data_path, flags.attack_folder)
output_path = create_dir(osp.join(classifier_data_path, flags.output_folder_name))
elif flags.data_type == 'source':
classifier_data_path = osp.join(data_path, flags.attack_folder, flags.defense_folder)
output_path = create_dir(osp.join(classifier_data_path, flags.output_folder_name + '_orig'))
elif flags.data_type == 'before_defense':
classifier_data_path = osp.join(data_path, flags.attack_folder)
output_path = create_dir(osp.join(classifier_data_path, flags.defense_folder, flags.output_folder_name))
elif flags.data_type == 'after_defense':
classifier_data_path = osp.join(data_path, flags.attack_folder, flags.defense_folder)
output_path = create_dir(osp.join(classifier_data_path, flags.output_folder_name))
else:
assert False, 'wrong data_type: %s' % flags.data_type
# load configuration
if flags.data_type == 'target':
conf = Conf.load(osp.join(classifier_data_path, 'attack_configuration'))
elif flags.data_type == 'adversarial':
conf = Conf.load(osp.join(classifier_data_path, 'attack_configuration'))
elif flags.data_type == 'source':
conf = Conf.load(osp.join(classifier_data_path, 'defense_configuration'))
elif flags.data_type == 'before_defense':
conf = Conf.load(osp.join(classifier_data_path, flags.defense_folder, 'defense_configuration'))
elif flags.data_type == 'after_defense':
conf = Conf.load(osp.join(classifier_data_path, 'defense_configuration'))
else:
assert False, 'wrong data_type: %s' % flags.data_type
# update classifier configuration
conf.classifier_path = classifier_path
conf.classifier_restore_epoch = flags.classifier_restore_epoch
conf.classifier_data_path = classifier_data_path
conf.save(osp.join(output_path, 'classifier_configuration'))
# load data
point_clouds, pc_classes, slice_idx, reconstructions = \
load_data(data_path, files, ['point_clouds_test_set', 'pc_classes', 'slice_idx_test_set', 'reconstructions_test_set'])
nn_idx_dict = {'latent_nn': 'latent_nn_idx_test_set', 'chamfer_nn_complete': 'chamfer_nn_idx_complete_test_set'}
nn_idx = load_data(data_path, files, [nn_idx_dict[conf.target_pc_idx_type]])
correct_pred = None
if conf.correct_pred_only:
pc_labels, pc_pred_labels = load_data(data_path, files, ['pc_label_test_set', 'pc_pred_labels_test_set'])
correct_pred = (pc_labels == pc_pred_labels)
# load indices for attack
attack_pc_idx = np.load(osp.join(top_out_dir, flags.attack_pc_idx))
attack_pc_idx = attack_pc_idx[:, :conf.num_pc_for_attack]
# build classifier model and reload a saved model
reset_tf_graph()
classifier = PointNetClassifier(classifier_path, flags.classifier_restore_epoch, num_points=flags.num_points, batch_size=10, num_classes=flags.num_classes)
classes_for_attack = conf.class_names
classes_for_target = conf.class_names
for i in range(len(pc_classes)):
pc_class_name = pc_classes[i]
if pc_class_name not in classes_for_attack:
continue
save_dir = create_dir(osp.join(output_path, pc_class_name))
print('Classify shape class %s (%d out of %d classes) ' % (pc_class_name, i + 1, len(pc_classes)))
start_time = time.time()
# prepare target point clouds
source_recon_ref, target_recon_ref = prepare_data_for_attack(pc_classes, [pc_class_name], classes_for_target, reconstructions, slice_idx, attack_pc_idx, conf.num_pc_for_target, nn_idx, correct_pred)
# load data
load_dir = osp.join(classifier_data_path, pc_class_name)
if flags.data_type == 'target':
# add axis to keep the interface of dist_weight as the first dim
pc_recon = np.expand_dims(target_recon_ref, axis=0)
elif flags.data_type in ['adversarial', 'before_defense']:
adversarial_pc_recon = np.load(osp.join(load_dir, 'adversarial_pc_recon.npy'))
# take adversarial point clouds of selected dist weight per attack
source_target_norm_min_idx = np.load(osp.join(load_dir, 'analysis_results', 'source_target_norm_min_idx.npy'))
adversarial_pc_recon = get_quantity_at_index([adversarial_pc_recon], source_target_norm_min_idx)
# add axis to keep the interface of dist_weight as the first dim
pc_recon = np.expand_dims(adversarial_pc_recon, axis=0)
elif flags.data_type == 'source':
# add axis to keep the interface of dist_weight as the first dim
pc_recon = np.expand_dims(source_recon_ref, axis=0)
elif flags.data_type == 'after_defense':
defense_on_adv = osp.exists(osp.join(load_dir, 'defended_pc_recon.npy'))
if defense_on_adv:
pc_recon = np.load(osp.join(load_dir, 'defended_pc_recon.npy')) # defense on adversarial input
else:
defended_pc_recon = np.load(osp.join(load_dir, 'defended_source_recon.npy')) # defense on clean input
# add axis to keep the interface of dist_weight as the first dim
pc_recon = np.expand_dims(defended_pc_recon, axis=0)
else:
assert False, 'wrong data_type: %s' % flags.data_type
num_dist_weight, num_pc, _, _ = pc_recon.shape
pc_recon_pred = np.zeros([num_dist_weight, num_pc], dtype=np.int8)
# reconstructed pc prediction label
for j in range(num_dist_weight):
pc_recon_pred[j] = classifier.classify(pc_recon[j])
# save results
if flags.data_type == 'target':
np.save(osp.join(save_dir, 'target_pc_recon_pred'), pc_recon_pred)
elif flags.data_type in ['adversarial', 'before_defense']:
np.save(osp.join(save_dir, 'adversarial_pc_recon_pred'), pc_recon_pred)
elif flags.data_type == 'source':
np.save(osp.join(save_dir, 'source_pc_recon_pred'), pc_recon_pred)
elif flags.data_type == 'after_defense':
if defense_on_adv:
np.save(osp.join(save_dir, 'defended_pc_recon_pred'), pc_recon_pred)
else:
pc_recon_pred = np.squeeze(pc_recon_pred, axis=0)
np.save(osp.join(save_dir, 'defended_source_recon_pred'), pc_recon_pred)
else:
assert False, 'wrong data_type: %s' % flags.data_type
duration = time.time() - start_time
print("Duration (minutes): %.2f" % (duration / 60.0))