xcyao00 / ResAD

[NeurIPS 2024 Spotlight] Pytorch Implementation for NeurIPS 2024 paper: ResAD: A Simple Framework for Class Generalizable Anomaly Detection
MIT License
28 stars 0 forks source link

a visualization code #1

Open shihy2988 opened 3 weeks ago

shihy2988 commented 3 weeks ago

I am impressed by your wonderful work. Can you provide a visualization code using the pre-trained model

xcyao00 commented 3 weeks ago

For qualitative results, you can use the code in visualizer.py. For feature t-SNE plot, you can save the initial features and residual features to numpy file features.npz, and use t-SNE to plot. A possible code may be like this:

data = np.load('features.npz') ini_feats = data['initial_feats'] res_feats = data['residual_feats'] mask = data['mask'] tsne = manifold.TSNE(n_components=2, init='pca', random_state=0) ini_feats = tsne.fit_transform(ini_feats).T tsne = manifold.TSNE(n_components=2, init='pca', random_state=0) res_feats = tsne.fit_transform(res_feats).T

fig, axs = plt.subplots(1, 2, figsize=(6, 3))
axs[0].scatter(ini_feats[0, mask == 0], ini_feats[1, mask == 0], s=10, c='mediumseagreen', cmap=plt.cm.rainbow)
axs[0].scatter(ini_feats[0, mask == 1], ini_feats[1, mask == 1], s=10, marker='^', c='orange', cmap=plt.cm.rainbow)
axs[1].scatter(res_feats[0, mask == 0], res_feats[1, mask == 0], s=10, c='mediumseagreen', cmap=plt.cm.rainbow)
axs[1].scatter(res_feats[0, mask == 1], res_feats[1, mask == 1], s=10, marker='^', c='orange', cmap=plt.cm.rainbow)

axs[0].set_xticks([])
axs[0].set_yticks([])
axs[1].set_xticks([])
axs[1].set_yticks([])

plt.savefig('tsne.jpg', dpi=1000)
plt.show()