-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils_imagenet.py
80 lines (64 loc) · 2.37 KB
/
utils_imagenet.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
import numpy as np
import tensorflow as tf
###################### Load the data #######################
def load_data(fpath):
lines = open(fpath)
data = []
label = []
for line in lines:
arr = line.strip().split()
data.append(arr[0])
label.append(arr[1])
# labels = np.asarray(d[label_key], np.int8)
return data, label
def load_data(fpath, order):
lines = open(fpath)
data = []
label = []
for line in lines:
arr = line.strip().split()
data.append(arr[0])
label.append(arr[1])
# labels = np.asarray(d[label_key], np.int8)
## map to new labels
mapping = {}
for i, j in enumerate(order):
mapping[j] = i
labels = [mapping[int(label_)] for label_ in label]
return data, labels
# return data, label
def prepare_validation(x_train, y_train, x_test, y_test, nb_groups, nb_cl, nb_val):
x_train_new = []
y_train_new = []
x_val_new = []
y_val_new = []
x_test_new = []
y_test_new = []
for _ in range(nb_groups):
x_train_new.append([])
y_train_new.append([])
x_test_new.append([])
y_test_new.append([])
for _ in range(nb_groups*nb_cl):
x_val_new.append([])
y_val_new.append([])
# get max val, the results for the first item
y_train = np.asarray(y_train, np.int16)
y_test = np.asarray(y_test, np.int16)
for i in range(nb_groups):
for j in range(nb_cl):
tmp_ind=np.where(y_train == nb_cl * i + j)[0]
# print (len(tmp_ind))
np.random.shuffle(tmp_ind)
# print (tmp_ind[0:len(tmp_ind)-nb_val])
# print ([x_train[k] for k in tmp_ind[0:len(tmp_ind)-nb_val]] )
x_train_new[i].extend( [x_train[k] for k in tmp_ind[0:len(tmp_ind)-nb_val] ])
y_train_new[i].extend(y_train[tmp_ind[0:len(tmp_ind)-nb_val]].tolist())
# x_val_new[i*nb_cl+j].extend(x_train[tmp_ind[len(tmp_ind)-nb_val:]])
x_val_new[i*nb_cl+j].extend([x_train[k] for k in tmp_ind[len(tmp_ind)-nb_val:]])
y_val_new[i*nb_cl+j].extend(y_train[tmp_ind[len(tmp_ind)-nb_val:]].tolist())
tmp_ind = np.where(y_test == nb_cl * i + j)[0]
# x_test_new[i].extend(x_test[tmp_ind])
x_test_new[i].extend([x_test[k] for k in tmp_ind])
y_test_new[i].extend(y_test[tmp_ind].tolist())
return x_train_new, y_train_new, x_val_new, y_val_new, x_test_new, y_test_new