Scikit Learn OpenCV SVM IndexError: list index out of range

Viewed 286

I'm training an SVM based on features extracted from a pictures dataset. The code goes as follows:

import os
import sys
import argparse

import pickle as cPickle
import numpy as np
from sklearn.multiclass import OneVsOneClassifier
from sklearn.svm import LinearSVC
from sklearn import preprocessing
from sklearn.svm import SVC
from sklearn.utils import check_random_state


def build_arg_parser():
    parser = argparse.ArgumentParser(description='Trains the classifier models')
    parser.add_argument("--feature-map-file", dest="feature_map_file", required=True,
            help="Input pickle file containing the feature map")
    parser.add_argument("--svm-file", dest="svm_file", required=False,
            help="Output file where the pickled SVM model will be stored")
    return parser

class ClassifierTrainer(object):
    def __init__(self, X, label_words):
        self.le = preprocessing.LabelEncoder()
        self.clf = OneVsOneClassifier(LinearSVC(random_state=0))

        y = self._encodeLabels(label_words)
        X = np.asarray(X)
        self.clf.fit(X, y)

    def _fit(self, X):
        X = np.asarray(X)
        return self.clf.predict(X)

    def _encodeLabels(self, labels_words):
        self.le.fit(labels_words)
        return np.array(self.le.transform(labels_words), dtype=np.float32)

    def classify(self, X):
        labels_nums = self._fit(X)
        labels_words = self.le.inverse_transform([int(x) for x in labels_nums])
        return labels_words

if __name__=='__main__':
    args = build_arg_parser().parse_args()
    feature_map_file = args.feature_map_file
    svm_file = args.svm_file

    # Load the feature map
    with open(feature_map_file, 'rb') as f:
        feature_map = cPickle.load(f)

    # Extract feature vectors and the labels
    labels_words = [x['label'] for x in feature_map]
    dim_size = feature_map[0]['feature_vector'].shape[1]
    X = [np.reshape(x['feature_vector'], (dim_size,)) for x in feature_map]

    # Train the SVM
    svm = ClassifierTrainer(X, labels_words)
    if args.svm_file:
        with open(args.svm_file, 'wb') as f:
            cPickle.dump(svm, f)

This is the error that the system throws:

Traceback (most recent call last):
  File "training.py", line 59, in <module>
    svm = ClassifierTrainer(X, labels_words)
  File "training.py", line 29, in __init__
    self.clf.fit(X, y)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/sklearn/multiclass.py", line 496, in fit
    self.estimators_ = estimators_indices[0]
IndexError: list index out of range

Any ideas what am I doing wrong? It seems there is a problem with the multiclass.py in Python site packages.

0 Answers
Related