(feat_path,model_path, target_class,base_class, poison_ids, title, device)
| 506 | plt.show() |
| 507 | |
| 508 | def generate_plot_lda(feat_path,model_path, target_class,base_class, poison_ids, title, device): |
| 509 | |
| 510 | [ops_all, labels_all, ids_all] = pickle.load( open( feat_path, "rb" ) ) |
| 511 | left_labels = np.array(labels_all)[np.in1d(labels_all,[target_class,base_class])] |
| 512 | left_ids = np.array(ids_all)[np.in1d(labels_all,[target_class,base_class])] |
| 513 | left_ops = ops_all[np.in1d(labels_all,[target_class,base_class])] |
| 514 | id2label = dict(zip(left_ids, left_labels)) |
| 515 | |
| 516 | tags = [] |
| 517 | for i in left_ids: |
| 518 | if i in poison_ids: |
| 519 | tags.append('poison') |
| 520 | elif i == 'target': |
| 521 | tags.append('target') |
| 522 | elif id2label[i]== target_class: |
| 523 | tags.append(str(target_class)) |
| 524 | else: |
| 525 | tags.append(str(base_class)) |
| 526 | |
| 527 | tags = np.array(tags) |
| 528 | |
| 529 | basefeats = left_ops[tags == str(base_class)] |
| 530 | targfeats = left_ops[tags == str(target_class)] |
| 531 | poisonfeats = left_ops[tags == 'poison'] |
| 532 | targetfeats = left_ops[tags == 'target'] |
| 533 | basecent = np.mean(basefeats, axis=0) |
| 534 | targcent = np.mean(targfeats, axis=0) |
| 535 | poisoncent = np.mean(poisonfeats, axis=0) |
| 536 | |
| 537 | ol_tags = np.concatenate([tags[tags == str(base_class)], tags[tags == str(target_class)], tags[tags == str('poison')]]) |
| 538 | ol_feats = np.concatenate([basefeats, targfeats, poisonfeats]) |
| 539 | from sklearn.discriminant_analysis import LinearDiscriminantAnalysis |
| 540 | lda = LinearDiscriminantAnalysis(n_components=2) |
| 541 | X_r2 = lda.fit(ol_feats, ol_tags).transform(ol_feats) |
| 542 | |
| 543 | colors = ['green', 'royalblue','red'] |
| 544 | target_names = [str(base_class), str(target_class), 'poison'] |
| 545 | alphas = [0.02,0.02,0.5] |
| 546 | sizes = [5,5,10] |
| 547 | plt.figure() |
| 548 | for color, i, target_name,al,si in zip(colors, target_names, target_names,alphas,sizes): |
| 549 | plt.scatter(X_r2[ol_tags == i, 0], X_r2[ol_tags == i, 1], alpha= al, color=color, |
| 550 | label=i,s=si) |
| 551 | target_proj = lda.fit(ol_feats, ol_tags).transform(targetfeats) |
| 552 | plt.scatter(target_proj[0][0], target_proj[0][1], alpha=1, color='dimgray', marker='^',label='target',s=50,edgecolors = 'black', linewidth=3) |
| 553 | plt.xlabel('LD1',fontsize=15,fontweight='medium',fontvariant='small-caps') |
| 554 | plt.ylabel('LD2',fontsize=15,fontweight='medium',fontvariant='small-caps') |
| 555 | figname = title.replace(" ", "_")+ "_3d_lda.pdf" |
| 556 | plt.savefig(os.path.join('./plots', figname), bbox_inches='tight') |
| 557 | # plt.legend(loc='best', shadow=False, scatterpoints=1) |
| 558 | # plt.title(title) |
| 559 | plt.show() |
| 560 | |
| 561 | def genplot_centroid_prob_3d(feat_path, model_path, target_class,base_class, poison_ids, title, device): |
| 562 |
nothing calls this directly
no outgoing calls
no test coverage detected