Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #527 from pluskid/multiclass-ecoc
Multiclass ecoc
- Loading branch information
Showing
9 changed files
with
202 additions
and
23 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
examples/undocumented/python_modular/classifier_multiclass_ecoc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import classifier_multiclass_shared | ||
# run with toy data | ||
[traindat, label_traindat, testdat, label_testdat] = classifier_multiclass_shared.prepare_data() | ||
# run with opt-digits if available | ||
#[traindat, label_traindat, testdat, label_testdat] = classifier_multiclass_shared.prepare_data(False) | ||
|
||
|
||
import shogun.Classifier as Classifier | ||
from shogun.Classifier import ECOCStrategy | ||
from shogun.Features import RealFeatures, Labels | ||
from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC, LinearMulticlassMachine | ||
from shogun.Evaluation import MulticlassAccuracy | ||
|
||
import re | ||
encoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Encoder', x)] | ||
decoders = [x for x in dir(Classifier) | ||
if re.match(r'ECOC.+Decoder', x)] | ||
|
||
fea_train = RealFeatures(traindat) | ||
fea_test = RealFeatures(testdat) | ||
gnd_train = Labels(label_traindat) | ||
if label_testdat is None: | ||
gnd_test = None | ||
else: | ||
gnd_test = Labels(label_testdat) | ||
|
||
base_classifier = LibLinear(L2R_L2LOSS_SVC) | ||
base_classifier.set_bias_enabled(True) | ||
|
||
print('Testing with %d encoders and %d decoders' % (len(encoders), len(decoders))) | ||
print('-' * 70) | ||
format_str = '%%15s + %%-10s %%-10%s %%-10%s %%-10%s' | ||
print((format_str % ('s', 's', 's')) % ('encoder', 'decoder', 'codelen', 'time', 'accuracy')) | ||
|
||
def run_ecoc(ier, idr): | ||
encoder = getattr(Classifier, encoders[ier])() | ||
decoder = getattr(Classifier, decoders[idr])() | ||
|
||
# whether encoder is data dependent | ||
if hasattr(encoder, 'set_labels'): | ||
encoder.set_labels(gnd_train) | ||
encoder.set_features(fea_train) | ||
|
||
strategy = ECOCStrategy(encoder, decoder) | ||
classifier = LinearMulticlassMachine(strategy, fea_train, base_classifier, gnd_train) | ||
classifier.train() | ||
label_pred = classifier.apply(fea_test) | ||
if gnd_test is not None: | ||
evaluator = MulticlassAccuracy() | ||
acc = evaluator.evaluate(label_pred, gnd_test) | ||
else: | ||
acc = None | ||
|
||
return (classifier.get_num_machines(), acc) | ||
|
||
|
||
import time | ||
for ier in range(len(encoders)): | ||
for idr in range(len(decoders)): | ||
t_begin = time.clock() | ||
(codelen, acc) = run_ecoc(ier, idr) | ||
if acc is None: | ||
acc_fmt = 's' | ||
acc = 'N/A' | ||
else: | ||
acc_fmt = '.4f' | ||
|
||
t_elapse = time.clock() - t_begin | ||
print((format_str % ('d', '.3f', acc_fmt)) % | ||
(encoders[ier][4:-7], decoders[idr][4:-7], codelen, t_elapse, acc)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Chiyuan Zhang | ||
* Copyright (C) 2012 Chiyuan Zhang | ||
*/ | ||
|
||
#include <shogun/multiclass/ecoc/ECOCForestEncoder.h> | ||
|
||
using namespace shogun; | ||
|
||
CECOCForestEncoder::CECOCForestEncoder() | ||
{ | ||
m_num_trees = 3; | ||
SG_ADD(&m_num_trees, "num_trees", "number of trees", MS_NOT_AVAILABLE); | ||
} | ||
|
||
void CECOCForestEncoder::set_num_trees(int32_t num_trees) | ||
{ | ||
if (num_trees < 1) | ||
SG_ERROR("number of trees (%d) should be >= 1", num_trees); | ||
m_num_trees = num_trees; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Chiyuan Zhang | ||
* Copyright (C) 2012 Chiyuan Zhang | ||
*/ | ||
|
||
#ifndef ECOCFORESTENCODER_H__ | ||
#define ECOCFORESTENCODER_H__ | ||
|
||
#include <shogun/multiclass/ecoc/ECOCDiscriminantEncoder.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
/** Forest ECOC Encoder. | ||
* | ||
* A data-dependent ECOC coding scheme that learns a tree-style codebook. See the | ||
* following paper for details | ||
* | ||
* Sergio Escalera, Oriol Pujol, Petia Radeva. Boosted Landmarks of | ||
* Contextual Descriptors and Forest-ECOC: A novel framework to detect and | ||
* classify objects in cluttered scenes. Pattern Recognition Letters, 2007. | ||
* | ||
*/ | ||
class CECOCForestEncoder: public CECOCDiscriminantEncoder | ||
{ | ||
public: | ||
/** constructor */ | ||
CECOCForestEncoder(); | ||
|
||
/** destructor */ | ||
virtual ~CECOCForestEncoder() {} | ||
|
||
/** get name */ | ||
virtual const char* get_name() const { return "ECOCForestEncoder"; } | ||
|
||
/** get number of trees */ | ||
int32_t get_num_trees() const { return m_num_trees; } | ||
|
||
/** set number of trees */ | ||
void set_num_trees(int32_t num_trees); | ||
}; | ||
|
||
} /* shogun */ | ||
|
||
#endif /* end of include guard: ECOCFORESTENCODER_H__ */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters