Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed prediction to run with multithreading #54

Open
wants to merge 6 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
six
scikit-learn>=0.14.1
joblib
16 changes: 11 additions & 5 deletions sklearn_porter/Porter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import os
import sys
import types
import subprocess

import numpy as np

from joblib import Parallel, delayed, cpu_count

from sklearn.metrics import accuracy_score
from sklearn.tree.tree import DecisionTreeClassifier
from sklearn.ensemble.weight_boosting import AdaBoostClassifier
Expand Down Expand Up @@ -375,11 +378,14 @@ def predict(self, X, class_name=None, method_name=None, tnp_dir='tmp',

# Multiple feature sets:
if exec_cmd is not None and len(X.shape) > 1:
pred_y = np.empty(X.shape[0], dtype=int)
for idx, features in enumerate(X):
full_exec_cmd = exec_cmd + [str(f).strip() for f in features]
pred = Shell.check_output(full_exec_cmd, cwd=tnp_dir)
pred_y[idx] = int(pred)
tnp_dir = './' + tnp_dir
exec_cmd = [os.path.join(os.path.abspath(tnp_dir), exec_cmd[0])]
cmds = [exec_cmd + [str(f).strip() for f in feat] for feat in X]
max_threads = cpu_count()
# using threading will increase speed n-fold, depending on CPUs
preds = Parallel(n_jobs=max_threads, backend='threading') \
(delayed(subprocess.check_output)(cmd, cwd=tnp_dir) for cmd in cmds)
pred_y = np.array([int(pred) for pred in preds], dtype=int)

# Cleanup:
if not keep_tmp_dir:
Expand Down