forked from hysts/pytorch_image_classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensemble.py
230 lines (189 loc) · 7.46 KB
/
ensemble.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
import argparse
import pathlib
import time
import numpy as np
import torch
import torch.nn.functional as F
import tqdm
from stack_ensemble import ShallowNetwork
from scipy.stats import mode
from fvcore.common.checkpoint import Checkpointer
from torch.utils.data import DataLoader
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from pytorch_image_classification import (
apply_data_parallel_wrapper,
create_dataloader,create_dataset,
create_loss,
create_model,
get_default_config,
update_config,
)
from pytorch_image_classification.utils import (
AverageMeter,
create_logger,
get_rank,
)
def load_config():
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, required=True)
parser.add_argument('options', default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
config = get_default_config()
config.merge_from_file(args.config)
config.merge_from_list(args.options)
update_config(config)
config.freeze()
return config
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def main():
config = load_config()
npz_files = ['/data/nextcloud/dbc2017/files/jupyter/me/pytorch_image_classification/outputs/imagenet/efficientnet-b5/xp02_RC_Aug/predictions.npz',
'/data/nextcloud/dbc2017/files/jupyter/me/pytorch_image_classification/outputs/imagenet/efficientnet-b5/exp02_CMAug/predictions.npz',
'/data/nextcloud/dbc2017/files/jupyter/me/pytorch_image_classification/outputs/imagenet/efficientnet-b5/exp02_SCAug/predictions.npz',
'/data/nextcloud/dbc2017/files/jupyter/me/pytorch_image_classification/outputs/imagenet/efficientnet-b5/exp02_MU_Aug/predictions.npz'
]
# '/content/pytorch_image_classification/outputs/imagenet/efficientnet-b5/exp_Aug6/predictions.npz'
whole_set = list(powerset(npz_files))
test_loader = create_dataloader(config, is_train=False)
_, test_loss = create_loss(config)
probs=[[0]*len(np.load(npz_files[0])['preds'][0])]*len(np.load(npz_files[0])['preds'])
device = torch.device(config.device)
for files in whole_set:
if len(files)<2:
continue
for f in files:
print(f)
probs+= np.load(f)['preds']
loss_meter = AverageMeter()
correct_meter = AverageMeter()
gt=[]
for data, targets in tqdm.tqdm(test_loader):
targets = targets.to(device)
gt.extend(targets)
probs=torch.tensor(probs)
gt=torch.tensor(gt)
loss = test_loss(probs, gt)
_, preds = torch.max(probs, dim=1)
# pred_prob_all=F.softmax(outputs, dim=1)
correct_ = preds.eq(gt).sum().item()
correct_meter.update(correct_, 1)
accuracy = correct_meter.sum / len(test_loader.dataset)
print("new acc: ",accuracy,"loss: ",loss,"preds: ",preds)
def randomForest():
X=[]
npz_files = [
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp010_sc/predictions_test.npz',
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp09_MU/predictions_test.npz'
]
for f in npz_files:
print(f)
X.append(np.load(f)['preds'])
X=np.concatenate(X,axis=1)
config = load_config()
test_loader = create_dataloader(config, is_train=False)
gt=[]
device = torch.device(config.device)
for _, targets in tqdm.tqdm(test_loader):
# targets = targets.to(device)
gt.extend(targets.numpy())
clf = RandomForestClassifier(n_estimators=10)
# print(clf.score(X, gt))
scores = cross_val_score(clf, X, gt, cv=5)
print(scores.mean())
# clf = clf.fit(X, gt)
def logitregression():
X=[]
npz_files = [
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp010_sc/predictions_train.npz',
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp09_MU/predictions_train.npz'
]
npz_test_files = [
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp010_sc/predictions_test.npz',
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp09_MU/predictions_test.npz'
]
for f in npz_files:
# print(f)
X.append(np.load(f)['preds'])
X=np.concatenate(X,axis=1)
print(X.shape)
X_test=[]
for f in npz_test_files:
# print(np.load(f)['preds'].shape)
X_test.append(np.load(f)['preds'])
X_test=np.concatenate(X_test,axis=1)
print(X_test.shape)
config = load_config()
data_root = config.dataset.dataset_dir
batch_size=config.train.batch_size
num_workers = 2
# labeled_dataset = MyDataset(train_clean, data_root, transforms=create_transform(config, is_train=False),data_type=config.dataset.subname)
train_dataset, val_dataset = create_dataset(config, True)
labeled_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)
# test_dataset = MyDataset(test_clean, data_root, transforms=create_transform(config, is_train=False),data_type=config.dataset.subname)
test_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)
gt=[]
device = torch.device(config.device)
gt_test=[]
for _, targets in tqdm.tqdm(test_dataloader):
# targets = targets.to(device)
gt_test.extend(targets.numpy())
gt_test = np.array(gt_test)
print(gt_test.shape)
for _, targets in tqdm.tqdm(labeled_dataloader):
if targets=='unknown':
targets=0
# targets = targets.to(device)
gt.extend(targets.numpy())
# print(len(gt),len(gt[0]))
gt = np.array(gt)
print(gt.shape)
model = ShallowNetwork()#LogisticRegression()
model.fit(X, gt)
yhat = model.predict(X_test)
acc = accuracy_score(gt_test, yhat)
print("acc: ",acc)
# clf = clf.fit(X, gt)
def voting():
npz_files = [
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp010_sc/predictions_test.npz',
'/content/pytorch_image_classification/outputs/wiki22/efficientnet-b5/exp09_MU/predictions_test.npz'
]
labels = []
for f in npz_files:
predicts = np.argmax(np.load(f)['preds'], axis=1)
labels.append([predicts])
# Ensemble with voting
labels = np.array(labels)
labels = np.transpose(labels, (1, 0))
labels = mode(labels,axis=1)[0]
labels = np.squeeze(labels)
print(labels)
config = load_config()
_, test_loss = create_loss(config)
test_loader = create_dataloader(config, is_train=False)
gt=[]
device = torch.device(config.device)
correct_meter = AverageMeter()
for _, targets in tqdm.tqdm(test_loader):
targets = targets.to(device)
gt.append([targets])
# loss = test_loss(labels, gt)
labels=torch.tensor(np.array(labels))
print(labels,gt)
correct_ = labels.eq(gt).sum().item()
correct_meter.update(correct_, 1)
accuracy = correct_meter.sum / len(test_loader.dataset)
print("new acc: ",accuracy,"preds: ",labels)
if __name__ == '__main__':
# logitregression()
randomForest()
# voting()
# main