forked from light-dawn/GIAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aft.py
69 lines (58 loc) · 2.26 KB
/
aft.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# encoding=utf-8
import torch.nn.functional as F
import numpy as np
from utils.utils import *
from tqdm import tqdm
import torch
from dataset import image_transform
def compute_all_probs(model, patch_path, device, idx):
model['classifier'].eval()
model['module'].eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
with torch.no_grad():
all_probs = []
for candidate in tqdm(np.array(os.listdir(patch_path))[idx]):
candidate_probs = []
for patch in os.listdir(os.path.join(patch_path, candidate)):
image = Image.open(os.path.join(patch_path, candidate, patch))
image_tensor = image_transform(image)
image_tensor.unsqueeze_(0)
image_tensor = image_tensor.to(device)
# �����ر��ģ�ͣ��ڶ������������ͼ
output_tensor = model['classifier'](image_tensor)[0]
prob = F.softmax(output_tensor, dim=1)
candidate_probs.append(prob.cpu().numpy().squeeze())
all_probs.append(np.array(candidate_probs))
return np.array(all_probs)
def find_dominant_class(ps):
probs_array = np.array(ps)
prob_sum = np.sum(probs_array, axis=0)
index = np.argmax(prob_sum)
return index
def sort_probs(ps, di):
probs_array = np.array(ps)
dominant_probs = probs_array[:, di]
sorted_index = np.argsort(-dominant_probs)
sorted_probs_array = probs_array[list(sorted_index)]
return sorted_probs_array
def majority(input_p, alpha=0.25):
top = round(len(input_p) * alpha)
return input_p[0:top]
# lambda_1:entropy lambda_2:diversity
def compute_diversity(ps, lambda_1=0, lambda_2=1):
r = np.zeros((len(ps), len(ps)))
for i in range(0, len(ps)):
for j in range(0, i + 1):
temp = 0.0
if i == j:
for pi in ps[i]:
temp += pi * np.log(pi)
r[i, i] = - lambda_1 * temp
else:
for k in range(len(ps[0])):
temp += (ps[i][k] - ps[j][k]) * np.log(ps[i][k] / ps[j][k])
r[i, j] = lambda_2 * temp
r[j, i] = r[i, j]
values = sum(r)
score = sum(values)
return score