-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification.py
89 lines (80 loc) · 3.76 KB
/
classification.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
from nbnn import *
from pascal import *
import logging,logging.config
from memuse import *
import os,sys, ConfigParser, shutil
def parse_cfg(cfg_file):
with open(cfg_file) as cfg:
test_parameters = dict()
data_args = dict()
descriptors = []
flann_args = dict()
config = ConfigParser.RawConfigParser()
config.readfp(cfg)
sections = config.sections()
for section in sections:
if section == 'Test':
for name, value in config.items(section):
if name == 'batch_size':
if value == 'None':
test_parameters[name] = None
else:
test_parameters[name] = int(value)
else:
test_parameters[name] = value
elif section == 'Dataset':
for name, value in config.items(section):
if name == 'classes':
data_args[name] = value.split(',')
else:
data_args[name] = value
elif section == 'Flann':
for name, value in config.items(section):
if name in ['k','checks','trees']:
flann_args[name] = int(value)
else:
flann_args[name] = value
else:
d = [section, dict()]
for name, value in config.items(section):
if name=='alpha':
d[1][name] = float(value)
elif name=='verbose':
d[1][name] = value=='True'
else:
d[1][name] = value
descriptors.append(d)
return test_parameters, data_args, descriptors, flann_args
if __name__ == "__main__":
if len(sys.argv) < 2:
raise Exception("Please give a config file as command line argument")
configfile = sys.argv[1]
test_params, data_args, descriptor_args, flann_args = parse_cfg(configfile)
logging.config.fileConfig(test_params['log_config'],disable_existing_loggers=False)
log = logging.getLogger('')
f = MemuseFilter()
log.handlers[0].addFilter(f)
log.info('===================VOC CLASSIFICATION ===================')
log.info('=========================================================')
# classes =['aeroplane','bicycle','bird','boat','bottle','bus','car','cat',\
# 'chair','cow','diningtable','dog','horse','motorbike','person',\
# 'pottedplant','sheep','sofa','train','tvmonitor']
log.info('===================INIT VOCCLAS DATASET===================')
dataset = VOCClassification(**data_args)
log.debug(dataset.train_set)
log.info("===================INIT RESULTSHANDLER===================")
vrh = VOCClassificationResultsHandler(dataset,test_params['result_path'],th=1)
log.info('=====================INIT DESCRIPTOR=====================')
descriptors = [eval(d)(**kwargs) for d,kwargs in descriptor_args]
log.info('=====================INIT ESTIMATOR =====================')
estimators = [NBNNEstimator.from_dataset(test_params['temp_path'], dataset, \
descriptor, **flann_args) for descriptor in descriptors]
log.info("======================STARTING TEST======================")
dataset.toggle_training()
run_test(dataset, descriptors, estimators, vrh.set_results, \
batch_size=test_params['batch_size'], output_function=ranked_classify)
#vrh.print_results()
log.info("=====================SAVING RESULTS =====================")
vrh.save_to_files()
log.info("=======================CLEANING UP=======================")
shutil.rmtree(test_params['temp_path'])