Open sixitingting opened 3 years ago
Hey, I will reopen this issue just in case other people have the same problem . To create the anomalous score figure, you can run the code below:
import matplotlib.pyplot as plt
import sklearn.metrics as skmetr
def show_anomalous_score(gt_file, score_path):
label_orig = np.load(gt_file, allow_pickle=True)
if type(score_path) is str:
score_orig = np.load(score_path)
else:
score_orig = score_path
score_after, label_after = [], []
init = 0
for i in range(len(label_orig)):
_label_use = label_orig[i]
_label_use = _label_use[8:-7]
_score_use = score_orig[init:init+len(_label_use)]
init += len(_label_use)
_score_use = _score_use - np.min(_score_use)
_score_use = 1 - _score_use / np.max(_score_use)
score_after.append(_score_use)
label_after.append(1 - _label_use + 1)
score_conc = np.concatenate(score_after, axis=0)
label_conc = np.concatenate(label_after, axis=0)
fpr, tpr, thresholds = skmetr.roc_curve(label_conc, score_conc, pos_label=2)
auc = skmetr.auc(fpr, tpr)
optimal_idx = np.argmax(tpr - fpr)
optimal_threshold = thresholds[optimal_idx]
return score_after, label_after, optimal_threshold
def show_tp(score_path, gt_file, vid_seq, dataset):
"""
Args:
score_path: str, the path that saves the anomalous score
gt_file: str, the path that saves the gt label
vid_seq: the selected video sequence, int
dataset: str, "UCSD2", "Avenue"
"""
pred_tot, gt_tot, threshold = show_anomalous_score(gt_file, score_path)
pred_prob = pred_tot[vid_seq]
gt_label = gt_tot[vid_seq]
pred_label = (pred_prob <= threshold).astype('int32')
tp = [1 for v, j in zip(pred_label, gt_label) if v == 1 and j == 1]
tpr = np.sum(tp) / len(np.where(gt_label == 1)[0])
fig = plt.figure(figsize=(4, 2.5))
ax = fig.add_subplot(111)
ax.plot([], [], 'b')
ax.plot([], [], 'r.')
ax.plot([], [], 'g')
ax.legend(["anomalous score", "anomalous event", "threshold"], fontsize=7, loc='best')
num_frame = len(pred_prob)
ax.plot(np.arange(num_frame), pred_prob, 'b')
ax.plot(np.arange(num_frame)[gt_label == 1], pred_prob[gt_label == 1], 'r.')
ax.plot(np.arange(num_frame), [threshold for _ in range(len(gt_label))], 'g', ls=':')
ax.grid(ls=':', alpha=0.5)
ax.set_xlabel("Frame", fontsize=8)
ax.set_ylabel("Anomalous score", fontsize=8)
ax.set_title("TPR for sequence %d from dataset %s is: %.2f" % (vid_seq, dataset, tpr * 100),
fontsize=8)
#---------------------------------------------------------------#
# Create Figure #
#---------------------------------------------------------------#
gt_file = "ckpt/UCSDped2_gt.npy"
recons_score = "ckpt/UCSDped2/recons_error_original_1.0_40.npy"
index = 1
show_tp(recons_score, gt_file, index, "UCSD2").
Then you will be able to see a figure like this:
in author's paper in my results
and note: I used the pretrained model in ckpt(model-40.pt)