The gcForest algorithm was suggested in Zhou and Feng 2017 ( https://arxiv.org/abs/1702.08835 , refer for this paper for technical details) and I provide here a python3 implementation of this algorithm.
I chose to adopt the scikit-learn syntax for ease of use and hereafter I present how it can be used.
In [1]:
from GCForest import gcForest
from sklearn.datasets import load_iris, load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
*Note* : I recommend the reader to look at this section with the original paper next to the computer to see what I am talking about.
The main technical problem in the present gcForest implementation so far is the memory usage when slicing the input data. A naive calculation can actually give you an idea of the number and sizes of objects the algorithm will be dealing with.
Starting with a dataset of $N$ samples of size $[l,L]$ and with $C$ classes, the initial "size" is:
$S_{D} = N.l.L$
**Slicing Step**
If my window is of size $[w_l,w_L]$ and the chosen stride are $[s_l,s_L]$ then the number of slices per sample is :
$n_{slices} = \left(\frac{l-w_l}{s_l}+1\right)\left(\frac{L-w_L}{s_L}+1\right)$
Obviously the size of slice is $w_l.w_L$ hence the total size of the sliced data set is :
$S_{sliced} = N.w_l.w_L.\left(\frac{l-w_l}{s_l}+1\right)\left(\frac{L-w_L}{s_L}+1\right)$
This is when the memory consumption is its peak maximum.
**Class Vector after Multi-Grain Scanning**
Now all slices are fed to the random forest to generate *class vectors*.
The number of class vector per random forest per window per sample is simply equal to the number of slices given to the random forest $n_{cv}(w) = n_{slices}(w)$.
Hence, if we have $N_{RF}$ random forest per window the size of a class vector is (recall we have $N$ samples and $C$ classes):
$S_{cv}(w) = N.n_{cv}(w).N_{RF}.C$
And finally the total size of the Multi-Grain Scanning output will be:
$S_{mgs} = N.\sum_{w} N_{RF}.C.n_{cv}(w)$
This short calculation is just meant to give you an idea of the data processing during the Multi-Grain Scanning phase. The actual memory consumption depends on the format given (aka float, int, double, etc.) and it might be worth looking at it carefully when dealing with large datasets.
The iris data set is actually not a very good example as the gcForest algorithm is better suited for time series and images where informations can be found at different scales in one sample.
Nonetheless it is still an easy way to test the method.
In [2]:
# loading the data
iris = load_iris()
X = iris.data
y = iris.target
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.33)
First calling and training the algorithm.
A specificity here is the presence of the 'shape_1X' keyword to specify the shape of a single sample.
I have added it as pictures fed to the machinery might not be square.
Obviously it is not very relevant for the iris data set but still, it has to be defined.
**New in version 0.1.3** : possibility to directly use an int as shape_1X for sequence data.
In [3]:
gcf = gcForest(shape_1X=4, window=2, tolerance=0.0)
gcf.fit(X_tr, y_tr)
Now checking the prediction for the test set:
In [4]:
pred_X = gcf.predict(X_te)
print(pred_X)
In [5]:
# evaluating accuracy
accuracy = accuracy_score(y_true=y_te, y_pred=pred_X)
print('gcForest accuracy : {}'.format(accuracy))
A much better example is the digits data set containing images of hand written digits. The scikit data set can be viewed as a mini-MNIST for training purpose.
In [6]:
# loading the data
digits = load_digits()
X = digits.data
y = digits.target
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.4)
... taining gcForest ... (can take some time...)
In [7]:
gcf = gcForest(shape_1X=[8,8], window=[4,6], tolerance=0.0, min_samples_mgs=10, min_samples_cascade=7)
gcf.fit(X_tr, y_tr)
... and predicting classes ...
In [8]:
pred_X = gcf.predict(X_te)
print(pred_X)
In [9]:
# evaluating accuracy
accuracy = accuracy_score(y_true=y_te, y_pred=pred_X)
print('gcForest accuracy : {}'.format(accuracy))
You probably don't want to re-train your classifier every day especially if you're using it on large data sets. Fortunately there is a very easy way to save and load models to disk using ```sklearn.externals.joblib```
__Saving model:__
In [10]:
from sklearn.externals import joblib
joblib.dump(gcf, 'gcf_model.sav')
Out[10]:
__Loading model__:
In [11]:
gcf = joblib.load('gcf_model.sav')
As the Multi-Grain scanning and the cascade forest modules are quite independent it is possible to use them seperately.
If a target `y` is given the code automaticcaly use it for training otherwise it recalls the last trained Random Forests to slice the data.
In [12]:
gcf = gcForest(shape_1X=[8,8], window=5, min_samples_mgs=10, min_samples_cascade=7)
X_tr_mgs = gcf.mg_scanning(X_tr, y_tr)
In [13]:
X_te_mgs = gcf.mg_scanning(X_te)
It is now possible to use the mg_scanning output as input for cascade forests using different parameters. Note that the cascade forest module does not directly return predictions but probability predictions from each Random Forest in the last layer of the cascade. Hence the need to first take the mean of the output and then find the max.
In [14]:
gcf = gcForest(tolerance=0.0, min_samples_mgs=10, min_samples_cascade=7)
_ = gcf.cascade_forest(X_tr_mgs, y_tr)
In [15]:
pred_proba = gcf.cascade_forest(X_te_mgs)
tmp = np.mean(pred_proba, axis=0)
preds = np.argmax(tmp, axis=1)
accuracy_score(y_true=y_te, y_pred=preds)
Out[15]:
In [16]:
gcf = gcForest(tolerance=0.0, min_samples_mgs=20, min_samples_cascade=10)
_ = gcf.cascade_forest(X_tr_mgs, y_tr)
In [17]:
pred_proba = gcf.cascade_forest(X_te_mgs)
tmp = np.mean(pred_proba, axis=0)
preds = np.argmax(tmp, axis=1)
accuracy_score(y_true=y_te, y_pred=preds)
Out[17]:
It is also possible to directly use the cascade forest and skip the multi grain scanning step.
In [18]:
gcf = gcForest(tolerance=0.0, min_samples_cascade=20)
_ = gcf.cascade_forest(X_tr, y_tr)
In [19]:
pred_proba = gcf.cascade_forest(X_te)
tmp = np.mean(pred_proba, axis=0)
preds = np.argmax(tmp, axis=1)
accuracy_score(y_true=y_te, y_pred=preds)
Out[19]:
In [ ]: