forked from rs-dl/TSAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idecRS_utils.py
91 lines (75 loc) · 2.45 KB
/
idecRS_utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
#
# Copyright © dawnranger.
#
# 2018-05-08 10:15 <[email protected]>
#
# Distributed under terms of the MIT license.
from __future__ import division, print_function
import numpy as np
import tqdm
import torch
from torch.utils.data import Dataset
class Image_Dataset(Dataset):
def __init__(self,path):
self.x, self.y = load_all(path)
def __len__(self):
return self.x.shape[0]
def __getitem__(self, idx):
return torch.from_numpy(np.array(self.x[idx])), torch.from_numpy(
np.array(self.y[idx])), torch.from_numpy(np.array(idx))
def load_all(path):
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
y_train = y_train.reshape(-1)
f.close()
x = x_train
y = y_train.astype(np.int32)
x = x.reshape((x.shape[0], -1)).astype(np.float32)
x = np.divide(x, 255.)
print('samples', x.shape)
return x, y
def count_difference(idec_args,y_pred):
list = [0,0,0,0]
for num in y_pred:
list[num]+=1
#return abs(list[0]-list[1])
return np.std(list)
def write_list(y_pred,idec_args):
target_name_dict = {
'A': 'NPRU',
'N': 'APRU',
'P': 'ANRU',
'R': 'ANPU',
'U': 'ANPR',
}
fin = open("dataset/MTRS/"+target_name_dict[idec_args.source_name]+"_domain_List.txt", "r")
fout = open(idec_args.update_list_file+"/cluster_label.txt","w")
count = 0
for line in fin:
data = line.strip().split(" ")
fout.write(data[0]+" "+str(y_pred[count])+"\n")
count +=1
fin.close()
fout.close()
#######################################################
# Evaluate Critiron
#######################################################
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size