boschresearch / LatentOE-AD

Coder of the paper 'Latent Outlier Exposure for Anomaly Detectin with Contaminated Data' published in ICML 2022
GNU Affero General Public License v3.0
37 stars 9 forks source link

Don't know how to reproduce the pixel segmentation experiment #1

Closed endrol closed 1 week ago

endrol commented 1 year ago

As written in the paper that LatentOE-AD also made experiment on MVTEC anomaly segmentation. I am not sure how it's done because there is little mention in the paper and code. Is it possible to release those code, thanks.

buehlpa commented 7 months ago

Hi, I would also be interested in the implementation of the framework for the MVtec AD dataset, Is there any chance of getting more information about that, specifically on the contamination process for the training data? thanks in advance

chenmorgen commented 4 months ago

Hi, I would also be interested in the implementation of the framework for the MVtec AD dataset, Is there any chance of getting more information about that, specifically on the contamination process for the training data? thanks in advance

Hi, thanks for being interested in the work. For creating the contaminated training data, zero-mean Gaussian noise is added to the abnormal images in the test set since MVTEC has no abnormal images in the training data. The example code is something like this with train_data and test_data from MVTEC:

 train_clean = train_data
 train_contamination = test_data[test_labels==1]
 num_clean = train_clean.shape[0]
 num_contamination = int(contamination_rate / (1 - contamination_rate) * num_clean)
 idx_contamination = np.random.choice(np.arange(train_contamination.shape[0]), num_contamination, replace=True)
 train_contamination = train_contamination[idx_contamination]
 train_contamination += np.random.randn(*train_contamination.shape)*0.1*np.std(train_contamination,0,keepdims=True)
 train_data = np.concatenate((train_clean, train_contamination), 0)
chenmorgen commented 4 months ago

As written in the paper that LatentOE-AD also made experiment on MVTEC anomaly segmentation. I am not sure how it's done because there is little mention in the paper and code. Is it possible to release those code, thanks.

Hi, thanks for being interested in the work. For anomaly segmentation, we obtain anomaly scores for each image patch as in SPADE. Since the dataset is contaminated, we need to infer what might be the abnormal image patches.

We first compute the normal loss loss_n and the abnormal loss loss_a for all image patches using their image patch embeddings. Then we can do the following to compute the loss in LOE:

score_patch = loss_n.reshape(num_batch, -1)-loss_a.reshape(num_batch, -1)
score_img, idx_max = torch.max(score_patch, dim=1)
_, idx_n = torch.topk(score_img, int(score_img.shape[0] * (1-contamination)), largest=False, sorted=False)
loss_normal = loss_n.reshape(num_batch,-1)[idx_n].reshape(-1)
max_snormal = torch.max(score_patch[idx_n].reshape(-1))
idx_abnormal = score_patch.reshape(-1)>max_snormal
loss_abnormal = (1-self.abnormal_label)*loss_n[idx_abnormal]+self.abnormal_label*loss_a[idx_abnormal]
loss = torch.cat([loss_normal,loss_abnormal], 0)