{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fisher vector feature encoding\n\nA Fisher vector is an image feature encoding and quantization technique that\ncan be seen as a soft or probabilistic version of the popular\nbag-of-visual-words or VLAD algorithms. Images are modelled using a visual\nvocabulary which is estimated using a K-mode Gaussian mixture model trained on\nlow-level image features such as SIFT or ORB descriptors. The Fisher vector\nitself is a concatenation of the gradients of the Gaussian mixture model (GMM)\nwith respect to its parameters - mixture weights, means, and covariance\nmatrices.\n\nIn this example, we compute Fisher vectors for the digits dataset in\nscikit-learn, and train a classifier on these representations.\n\nPlease note that scikit-learn is required to run this example.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\nimport numpy as np\nfrom sklearn.datasets import load_digits\nfrom sklearn.metrics import classification_report, ConfusionMatrixDisplay\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import LinearSVC\n\nfrom skimage.transform import resize\nfrom skimage.feature import fisher_vector, ORB, learn_gmm\n\n\ndata = load_digits()\nimages = data.images\ntargets = data.target\n\n# Resize images so that ORB detects interest points for all images\nimages = np.array([resize(image, (80, 80)) for image in images])\n\n# Compute ORB descriptors for each image\ndescriptors = []\nfor image in images:\n    detector_extractor = ORB(n_keypoints=5, harris_k=0.01)\n    detector_extractor.detect_and_extract(image)\n    descriptors.append(detector_extractor.descriptors.astype('float32'))\n\n# Split the data into training and testing subsets\ntrain_descriptors, test_descriptors, train_targets, test_targets = train_test_split(\n    descriptors, targets\n)\n\n# Train a K-mode GMM\nk = 16\ngmm = learn_gmm(train_descriptors, n_modes=k)\n\n# Compute the Fisher vectors\ntraining_fvs = np.array(\n    [fisher_vector(descriptor_mat, gmm) for descriptor_mat in train_descriptors]\n)\n\ntesting_fvs = np.array(\n    [fisher_vector(descriptor_mat, gmm) for descriptor_mat in test_descriptors]\n)\n\nsvm = LinearSVC().fit(training_fvs, train_targets)\n\npredictions = svm.predict(testing_fvs)\n\nprint(classification_report(test_targets, predictions))\n\nConfusionMatrixDisplay.from_estimator(\n    svm,\n    testing_fvs,\n    test_targets,\n    cmap=plt.cm.Blues,\n)\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}