(feat_path,model_path, target_class,base_class, poison_ids, title, device)
| 86 | plt.show() |
| 87 | |
| 88 | def generate_plot_pca(feat_path,model_path, target_class,base_class, poison_ids, title, device): |
| 89 | |
| 90 | [ops_all, labels_all, ids_all] = pickle.load( open( feat_path, "rb" ) ) |
| 91 | |
| 92 | |
| 93 | left_labels = np.array(labels_all)[np.in1d(labels_all,[target_class,base_class])] |
| 94 | left_ids = np.array(ids_all)[np.in1d(labels_all,[target_class,base_class])] |
| 95 | left_ops = ops_all[np.in1d(labels_all,[target_class,base_class])] |
| 96 | id2label = dict(zip(left_ids, left_labels)) |
| 97 | |
| 98 | tags = [] |
| 99 | for i in left_ids: |
| 100 | if i in poison_ids: |
| 101 | tags.append('poison') |
| 102 | elif i == 'target': |
| 103 | tags.append('target') |
| 104 | elif id2label[i]== target_class: |
| 105 | tags.append(str(target_class)) |
| 106 | else: |
| 107 | tags.append(str(base_class)) |
| 108 | |
| 109 | tags = np.array(tags) |
| 110 | print(np.sum(tags == str(target_class)), |
| 111 | np.sum(tags == str(base_class)), np.sum(tags == str('poison')), np.sum(tags == str('target'))) |
| 112 | |
| 113 | basefeats = left_ops[tags == str(base_class)] |
| 114 | targfeats = left_ops[tags == str(target_class)] |
| 115 | |
| 116 | a = np.concatenate([basefeats, targfeats]) |
| 117 | from sklearn.decomposition import PCA |
| 118 | pca = PCA(n_components=2) |
| 119 | pca.fit(np.concatenate([basefeats, targfeats])) |
| 120 | distcent = pca.components_[0] |
| 121 | orthcent = pca.components_[1] |
| 122 | # print(pca.explained_variance_ratio_) |
| 123 | |
| 124 | baseproj = np.stack([basefeats.dot(distcent), basefeats.dot(orthcent)], axis=1) |
| 125 | targproj = np.stack([targfeats.dot(distcent), targfeats.dot(orthcent)], axis=1) |
| 126 | |
| 127 | |
| 128 | plt.plot(*baseproj.T, '.g', alpha=.03, markeredgewidth=0) |
| 129 | plt.plot(*targproj.T, '.b', alpha=.03, markeredgewidth=0) |
| 130 | |
| 131 | poisonfeats = left_ops[tags == str('poison')] |
| 132 | poisoncent = np.mean(poisonfeats, axis=0) |
| 133 | # print("Printing below distance between centroids") |
| 134 | # print(np.linalg.norm(basecent-targcent),np.linalg.norm(basecent-poisoncent), np.linalg.norm(poisoncent-targcent)) |
| 135 | poisonproj = np.stack([poisonfeats.dot(distcent), poisonfeats.dot(orthcent)], axis=1) |
| 136 | plt.plot(*poisonproj.T, 'or', alpha=1, markeredgewidth=0, markersize=7, label='poisons') |
| 137 | |
| 138 | targetfeats = left_ops[tags == str('target')] |
| 139 | targetproj = np.stack([targetfeats.dot(distcent), targetfeats.dot(orthcent)], axis=1) |
| 140 | plt.plot(*targetproj.T, '^b', markersize=12, markeredgewidth=0, label='target') |
| 141 | |
| 142 | # plt.xlim(-6, 6) |
| 143 | # plt.ylim(-4, 52) |
| 144 | plt.xlabel('PC1') |
| 145 | plt.ylabel('PC2') |
nothing calls this directly
no outgoing calls
no test coverage detected