-
Notifications
You must be signed in to change notification settings - Fork 10
/
MAV_Compute.py
51 lines (33 loc) · 1.48 KB
/
MAV_Compute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os, sys
import glob
import time
import scipy as sp
from scipy.io import loadmat, savemat
import pickle
import os.path as path
import torch
import numpy as np
def compute_mean_vector(category_index,save_path,featurefilepath,):
featurefile_list = os.listdir(os.path.join(featurefilepath,category_index))
correct_features = []
for featurefile in featurefile_list:
feature = torch.from_numpy(np.load(os.path.join(featurefilepath,folder_name,featurefile)))
predicted_category = torch.max(feature,dim=1)[1].item()
if(predicted_category == category_index):
correct_features.append(feature)
correct_features = torch.cat(correct_features,0)
mav = torch.mean(correct_features,dim=0)
np.save(os.path.join(save_path,folder_name+".npy"),mav.data.numpy(),allow_pickle=False)
def get_args():
parser = argparse.ArgumentParser(description='Get activation vectors')
parser.add_argument('--save_path',default="./saved_MAVs/cifar10/",type=str,help="Path to save the ensemble weights")
parser.add_argument('--feature_dir',default="./saved_features/cifar10",type=str,help="Path to save the ensemble weights")
parser.set_defaults(argument=True)
return parser.parse_args()
def main():
args = get_args()
for class_no in os.listdir(args.feature_dir):
print("Class index ",class_no)
compute_mean_vector(class_no,args.save_path,args.feature_dir)
if __name__ == "__main__":
main()