This module provides functions to train image classification models, like Logistic Regressors and Conditional Random Fields. It provides functions for feature extraction that are largely based on the scikit-image toolbox and it provides functions for model training and optimization that are largely based on the pystruct and scikit-learn toolbox.
Conditional Random Field
Equal to pystruct.learners.OneSlackSSVM (inherited), but also takes string class labels as input.
| Parameters: | clist (list) – List with possible class names | 
|---|
Notes
Only arguments additional to pystruct.learners.OneSlackSSVM are listed.
Conditional Random Field
Equal to pystruct.learners.StructuredPerceptron (inherited), but also takes string class labels as input.
| Parameters: | clist (list) – List with possible class names | 
|---|
Notes
Only arguments additional to pystruct.learners.StrcturedPerceptron are listed.
Logistic Regressor
Equal to sklearn.linear_model.LogisticRegression (inherited), but also takes non-linearized structured data as input.
Logistic Regressor with support for Relative Location Priors
Equal to flamingo.classification.models.LogisticRegression (inherited), but supports the use of Relative Location Priors as proposed by [Gould2008].
| Parameters: | 
|---|
Notes
Only arguments additional to flamingo.classification.models.LogisticRegression are listed.
| [Gould2008] | Stephen Gould, Jim Rodgers, David Cohen, Gal Elidan, Daphne Koller (2008). Multi-Class Segmentation with Relative Location Prior. International Journal of Computer Vision. doi:10.1007/s11263-008-0140-x | 
Support Vecor Machine
Equal to sklearn.svm.LinearSVC (inherited), but also takes non-linearized structured data as input.
Returns a bare model object
| Parameters: | model_type (string, optional) – String indicating the type of model to be constructed. LR = Logistic Regressor (default), LR_RLP = Logistic Regressor with Relative Location Prior, SVM = Support Vector Machine, CRF = Conditional Random Field  | 
|---|---|
| Returns: | Bare model object  | 
| Return type: | object  | 
| Other Parameters: | |
  | 
|
Run class prediction of image with a single model
| Parameters: | 
  | 
|---|---|
| Returns: | Class prediction for image  | 
| Return type: | np.ndarray  | 
Run class predictions for a set of trained models and corresponding data
| Parameters: | |
|---|---|
| Returns: | List of lists containing np.ndarrays with class predictions for each image and each model.  | 
| Return type: | list  | 
Notes
Models should be trained. Model and set lists should be of equal length. In case of N models and M training sets the models should be organized in a N-length list of M-length lists.
Scores a single model using a train and test set
| Parameters: | 
  | 
|---|---|
| Returns: | 
  | 
Compute train/test scores for a set of trained models
| Parameters: | 
  | 
|---|---|
| Returns: | MultiIndex DataFrame containing training and test scores. Indices “model” and “set” indicate the model and training set number used. Columns “train” and “test” contain the train and test scores respectively.  | 
| Return type: | pandas.DataFrame  | 
Notes
Models should be trained. Model and set lists should be of equal length. In case of N models and M training sets the models should be organized in a N-length list of M-length lists. The train and test sets should both be M-length lists.
Examples
>>> models = [models.get_model(model_type='LR'),
              models.get_model(model_type='CRF', n_states=5, n_features=10)]
>>> models_trained = models.train_models(models, [(X_train, Y_train)])
>>> scores = test.score_models(models, [(X_train, Y_train)], [(X_test, Y_test)])
Trains a single model against a single training set
| Parameters: | 
  | 
|---|
Notes
Models are passed by reference and trained without copying.
Trains a set of model against a series of training sets
| Parameters: | 
  | 
|---|---|
| Returns: | List of lists with each item a trained instance of one of the models.  | 
| Return type: | list  | 
convert all items in each matrix feature into individual features
Compute relative location prior according to Gould et al. (2008)
| Parameters: | ds (string) – String indicating the dataset to be used. | 
|---|---|
| Returns: | maps – 4D panel containing the relative location prior maps: maps[<other class>][<given class>] gives a n*n dataframe representing the dimensionless image map | 
| Return type: | pandas.Panel4D | 
| Other Parameters: | |
| n (integer) – Half the size of the dimensionless image map | |
Convolve relative location prior maps with a gaussian filter for smoothing purposes
| Parameters: | 
  | 
|---|---|
| Returns: | maps – 4D panel containing the smoothed relative location prior maps.  | 
| Return type: | pandas.Panel4D  | 
| Other Parameters: | |
sigma (integer) – Size of the gaussian kernel that is to be convolved with the relative location prior maps  | 
|
Class voting based on 1st order prediction and relative location prior maps
| Parameters: | 
  | 
|---|---|
| Returns: | 
  | 
Add artificial channels to an image
| Parameters: | 
  | 
|---|
Notes
Currently implemented channels are: * gabor, with options frequencies and thetas * gaussian, with option sigmas * sobel, without any options
| Returns: | NxMx(3+P) array with image data with extra channels where P is the number of channels added | 
|---|---|
| Return type: | np.ndarray | 
Get theoretical bounds of channel values
| Parameters: | 
  | 
|---|---|
| Returns: | List of dicts with keys min and max indicating the theoretical boundaries of the channel values  | 
| Return type: | list  | 
Get number of artificial channels
| Parameters: | 
  | 
|---|---|
| Returns: | Number of channels added when using the specified settings  | 
| Return type: | int  | 
Scale channel to uint8 based on maximum possible filter response
| Parameters: | 
  | 
|---|---|
| Returns: | Array with normalized channel values  | 
| Return type: | np.ndarray  | 
Aggregate model scores over training and test sets
| Parameters: | scores (pandas.DataFrame) – DataFrame with test scores for different models and training sets. Should have at least one level index named “model”. | 
|---|---|
| Returns: | DataFrame averaged over all indices except “model”. | 
| Return type: | pandas.DataFrame | 
Computes confusion matrix for combinations of models and training/test sets
| Parameters: | |
|---|---|
| Returns: | 
  | 
Computes learning curves for combinations of models and training/test sets
| Parameters: | 
  | 
|---|---|
| Returns: | 
  | 
Plots learning curves
| Parameters: | 
  | 
|---|---|
| Returns: | 
  | 
Save figure to file
| Parameters: | 
  | 
|---|
Aggregate class labels into a subsection of class labels
Replaces all class labels in Y with substitutes from the dictionary aggregation.
| Parameters: | 
  | 
|---|---|
| Returns: | Aggregated class labels  | 
| Return type: | np.ndarray  | 
Checks if train sets, test sets and models have matching dimensions
| Parameters: | |
|---|---|
| Raises: | ValueError –  | 
De-linearizes structured label data
Transforms linearized labell data suitible for the use with LR and SVM into structured data for the use with CRF and SSVM.
| Parameters: | |
|---|---|
| Returns: | Delinearized Y data  | 
| Return type: | list  | 
Get list of unique classes in Y
Returns a list of unique classes in Y with all None values removed and regardless of the shape and type of Y.
| Parameters: | Y (list or np.ndarray) – List with np.ndarrays or np.ndarray with class labels | 
|---|---|
| Returns: | Array with unique class labels in Y not being None | 
| Return type: | np.ndarray | 
Transforms class numbers in string class labels
| Parameters: | 
  | 
|---|---|
| Returns: | Array with class labels rather than numbers  | 
| Return type: | np.ndarray  | 
Transforms class labels and segmentation into class image
| Parameters: | 
  | 
|---|---|
| Returns: | Unnormalized single-channel image of class assignments  | 
| Return type: | np.ndarray  | 
Transforms string class labels in numbers
| Parameters: | 
  | 
|---|---|
| Returns: | Array with class numbers rather than labels  | 
| Return type: | np.ndarray  | 
Linearizes structured data
Transforms structured data suitible for the use with CRF and SSVM into non-structrued data with a single dimension suitible for the use with LR or SVM.
| Parameters: | 
  | 
|---|---|
| Returns: | Either linearized X, linearized Y or both are returned depending on the input  | 
| Return type: | np.ndarray or 2-tuple  |