-
Notifications
You must be signed in to change notification settings - Fork 6
/
baselineFeatures.py
211 lines (173 loc) · 7.93 KB
/
baselineFeatures.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
import os
import sys
import time
import numpy as np
from datetime import datetime, timedelta
from logger import Logger
from sklearn.metrics import accuracy_score
import shutil
import multiprocessing
import cv2
from tqdm import tqdm
from option import Options, tranform_options
# Omniglot dataset
from omniglotDataLoader import omniglotDataLoader
from dataset.omniglot import Omniglot
# Mini-imagenet dataset
from miniimagenetDataLoader import miniImagenetDataLoader
from dataset.mini_imagenet import MiniImagenet
# Banknote dataset
from banknoteDataLoader import banknoteDataLoader
from dataset.banknote_pytorch import FullBanknote
# Features
from features.HoGFeature import HoGFeature
from features.UCIFeature import UCIFeature
from features.SIFTFeature import SIFTFeature
from features.LBPFeature import LBPFeature
from features.HaralickFeature import HaralickFeature
from features.BhavaniFeature import BahavaniFeature
# statistics
from util.show_results import show_results
from classifiers.xgboost import XGBoostClassifier
def train(index = 14):
# change parameters
opt = Options().parse()
#opt = Options().parse() if opt is None else opt
opt = tranform_options(index, opt)
opt.cuda = False
# set the mode of the dataset to generator_processor
# which generates and processes the images without saving them.
opt.mode = 'generator_processor'
# Load Dataset
opt.setType='set1'
if opt.datasetName == 'miniImagenet':
dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=None)
elif opt.datasetName == 'omniglot':
dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=None,train_mean=None,
train_std=None)
elif opt.datasetName == 'banknote':
dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=None, train_mean=None,
train_std=None)
else:
pass
# Use the same seed to split the train - val - test
if os.path.exists(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy')):
rnd_seed = np.load(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy'))
else:
rnd_seed = np.random.randint(0, 100000)
np.save(os.path.join(opt.save, 'dataloader_rnd_seed_arc.npy'), rnd_seed)
# Get the DataLoaders from train - val - test
train_loader, val_loader, test_loader = dataLoader.get(rnd_seed=rnd_seed,dataPartition = ['train+val',None,'test'])
if opt.name is None:
# if no name is given, we generate a name from the parameters.
# only those parameters are taken, which if changed break torch.load compatibility.
#opt.name = "train_{}_{}_{}_{}_{}_wrn".format(str_model_fn, opt.numGlimpses, opt.glimpseSize, opt.numStates,
opt.name = "{}_{}_{}_{}_{}_{}_wrn".format(opt.naive_full_type,
"fcn" if opt.apply_wrn else "no_fcn",
opt.arc_numGlimpses,
opt.arc_glimpseSize, opt.arc_numStates,
"cuda" if opt.cuda else "cpu")
print("[{}]. Will start training {} with parameters:\n{}\n\n".format(multiprocessing.current_process().name,
opt.name, opt))
# make directory for storing models.
models_path = os.path.join(opt.save, opt.name)
if not os.path.isdir(models_path):
os.makedirs(models_path)
else:
shutil.rmtree(models_path)
# create logger
logger = Logger(models_path)
# create object features
#nameFeatures = 'HoGFeature'
#nameFeatures = 'UCIFeature'
nameFeatures = opt.wrn_name_type
objFeatures = eval(nameFeatures + '()')
objClassifier = XGBoostClassifier()
train_features = []
train_labels = []
# if the training features exists go to testing
if not(os.path.exists(os.path.join(opt.save,'train_features.npy')) and os.path.exists(os.path.join(opt.save,'train_labels.npy'))):
## EXTRACT FEATURES TRAIN
for i in range(opt.train_num_batches):
for batch_idx, (data, labels) in enumerate(tqdm(train_loader)):
# transform batch of data and label tensors to numpy
data = data.numpy().transpose(0,2,3,1)
labels = labels.numpy().tolist()
for i in range(len(data)):
features = objFeatures.extract(data[i])
train_features.append(features)
train_labels.append(labels)
# save the features
train_features = np.stack(train_features)
train_labels = [item for sublist in train_labels for item in sublist]
np.save(os.path.join(opt.save,'train_features.npy'),train_features)
np.save(os.path.join(opt.save,'train_labels.npy'),train_labels)
else:
train_features = np.load(os.path.join(opt.save,'train_features.npy'))
train_labels = np.load(os.path.join(opt.save,'train_labels.npy'))
## TRAIN
objClassifier.train(train_features,train_labels)
objClassifier.save(opt.save)
## EXTRACT FEATURES TEST
for j in range(opt.test_num_batches):
test_features = []
test_labels = []
for batch_idx, (data, labels) in enumerate(tqdm(test_loader)):
# transform batch of data and label tensors to numpy
data = data.numpy().transpose(0,2,3,1)
labels = labels.numpy().tolist()
for i in range(len(data)):
features = objFeatures.extract(data[i])
test_features.append(features)
test_labels.append(labels)
## PREDICT
test_features = np.stack(test_features)
test_labels = [item for sublist in test_labels for item in sublist]
preds = objClassifier.predict(test_features)
test_features_set1 = test_features
test_labels_set1 = test_labels
preds_set1 = preds
show_results(test_labels,preds,'TEST SET 1. Iter: ' + str(j),show=False)
## Get the set2 and try
opt.setType='set2'
if opt.datasetName == 'miniImagenet':
dataLoader = miniImagenetDataLoader(type=MiniImagenet, opt=opt, fcn=None)
elif opt.datasetName == 'omniglot':
dataLoader = omniglotDataLoader(type=Omniglot, opt=opt, fcn=None,train_mean=None,
train_std=None)
elif opt.datasetName == 'banknote':
dataLoader = banknoteDataLoader(type=FullBanknote, opt=opt, fcn=None, train_mean=None,
train_std=None)
else:
pass
train_loader, val_loader, test_loader = dataLoader.get(rnd_seed=rnd_seed, dataPartition = [None,None,'train+val+test'])
## EXTRACT FEATURES TEST
for j in range(opt.test_num_batches):
test_features = []
test_labels = []
for batch_idx, (data, labels) in enumerate(tqdm(test_loader)):
# transform batch of data and label tensors to numpy
data = data.numpy().transpose(0,2,3,1)
labels = labels.numpy().tolist()
for i in range(len(data)):
features = objFeatures.extract(data[i])
test_features.append(features)
test_labels.append(labels)
## PREDICT
test_features = np.stack(test_features)
test_labels = [item for sublist in test_labels for item in sublist]
preds = objClassifier.predict(test_features)
test_features_set2 = test_features
test_labels_set2 = test_labels
preds_set2 = preds
show_results(test_labels,preds,'TEST SET 2. Iter: ' + str(j),show=False)
#''' UNCOMMENT!!!! TESTING NAIVE - FULLCONTEXT
# LOAD AGAIN THE FCN AND ARC models. Freezing the weights.
print ('[%s] ... Testing' % multiprocessing.current_process().name)
#TODO: TEST!!!
print ('[%s] ... FINISHED! ...' % multiprocessing.current_process().name)
#'''
def main():
train()
if __name__ == "__main__":
main()