How to efficiently parallelize a scikit SVM classification on a 2D image in Python
<p>I'm using the scikit-learn package to do a SVM classification on an 2D image. Each pixel has 9 features on which the classification is based. Let's assume I have a successfully trained classifier <code>clf</code>, and I want to use it to perform the classification on that 2D image, i.e. a 3D array <code>input</code> (Xdim,Ydim,#features). In the most simple, but extremely inefficient way, this would be something like:</p>

<pre><code>def svm_classification(svm_classifier,input_data):
    return svm_classifier.predict(input_data)

# Initialize output classification image
classified_image = np.empty([Xdim, Ydim])

# Do a very slow classification using nested loops
for x in range(Xdim):
    for y in range(Ydim):
        classified_image[x,y] = svm_classification(clf,input[x,y,:])
</code></pre>

<p>What would be an efficient way to speed up this process? In the example above, only one point is given to the classifier, while it can perfectly handle vectors (#samples X #features), so the <code>svm_classification</code> function should be used in a much smarter way. I was thinking about using 'joblib' and its 'parallel' module (<a href="https://pythonhosted.org/joblib/parallel.html" rel="nofollow">https://pythonhosted.org/joblib/parallel.html</a>), but I'm not sure how I should do this on a 3D array. Is there a way to specify a chunksize for automatic slicing of the input data? Should I manually reshape the input array? I guess what makes that I cannot find the right answer myself is the shape of the input data (3D) which should result in a 2D classified image. Any suggestion in the right direction would be much appreciated! </p>