You are on page 1of 13

mint 17 4

2 cpu . host core-i7 8


SSD.
Anaconda python
2.7 numpy scikit scipy
. :

cmake >= 2.8


gcc >= 4.8
eigen >= 3.2 -> http://eigen.tuxfamily.org
openmp -> http://openmp.org/wp/
vlfeat -> http://www.vlfeat.org/
python 2.7
scipy
numpy
joblib
scikit-learn -> scikit-learn.org/
scikit-image -> http://scikit-image.org/


.
apt-get install python2.7 cmake build-essential libgomp1
libeigen3-dev
python-scipy python-numpy python-skimage python-sklearn pythonjoblib python-nose
virtualenv
:
apt-get install python-virtualenv

:

wget http://www.famaf.unc.edu.ar/~jsanchez/efv/vrl-0.1.tar.gz
:
tar -xf vrl-0.1.tar.gz
virtualenv ... numpy scipy
:
cd vrl-0.1
virtualenv venv --system-site-packages
source venv/bin/activate
: vlfeat
mkdir build; cd build
cmake ../
make
make install
cd ../
: KTH
wget http://www.nada.kth.se/cvap/databases/kth-tips/kth-tips2a_col_200x200.tar
:
tar -xf kth-tips2-a_col_200x200.tar
:
/home/dmuser/anaconda/bin/python
bin/classification_demo_kth_tips_2a.py

5 51
. .
precision recall f1 :
support

f1-score

recall

precision

108

0.99

0.99

0.99

aluminium_foil

108

0.27

0.19

0.50

brown_bread

108

0.97

0.96

0.97

corduroy

108

0.88

0.99

0.80

cork

108

0.67

0.54

0.88

cotton

108

0.47

0.43

0.53

cracker

108

0.96

0.95

0.97

lettuce_leaf

108

0.83

0.95

0.73

linen

108

0.65

0.92

0.51

white_bread

108

0.98

1.00

0.96

wood

108

0.89

0.82

0.98

wool

1188

0.78

0.79

0.80

avg / total

--accuracy: 79.46
:
# -*- coding: utf-8 -*-

"""Image classification with the exponential family Fisher vector


(eFV)
"""
from __future__ import print_function
from __future__ import division

import os

import numpy as np

from os.path import abspath, join, exists

import vrl

import inspect

from sklearn.multiclass import OneVsRestClassifier


from sklearn.svm import LinearSVC

# dataset
DATASET_PATH = abspath('KTH-TIPS2-a')

# output cache

OUTPUT_PATH = abspath('.')

def whoami():
return inspect.stack()[1][3]

def kth_tips_2a_lbp_bmm():
vrl.utils.set_loglevel(vrl.utils.logging.INFO)
logger = vrl.utils.get_logger(whoami())

# --- SETUP PIPELINE --# NOTE: read the documentation of vrl.base.VRLBaseEstimator


to understand how our
# file pipeline works

# sample 'a' is used for testing


dataset = vrl.datasets.KTH_TIPS2(path=DATASET_PATH,
sample='a')
split_id = str(abs(hash(tuple(dataset.get_images('train'))) +
hash(tuple(dataset.get_images('test')))))

# standard LBPs on a resolution pyramid with 5 layers

feat = vrl.cv.LBP()
feat.input_path = dataset.impath
feat.output_base_path = OUTPUT_PATH

# mixture model
bmm = vrl.ml.BernoulliMixture(ncomp=128, nvar=8)
bmm.input_path = feat.output_path
bmm.output_base_path = feat.output_path

# exponential family Fisher vector with a diagnonal


normalizer
efv = vrl.ml.FisherVector(model=bmm, normalizer='diag')
efv.input_path = feat.output_path
efv.output_base_path = bmm.output_path

# setup classifier
ovr = OneVsRestClassifier(LinearSVC(loss='l1', penalty='l2'))
clf = vrl.base.SKLEstimator(estimator=ovr,
to_show=('estimator',

'estimator__C',

'estimator__loss',

'estimator__penalty'))
clf.input_path = efv.output_path
clf.output_base_path = join(efv.output_path, split_id)

# --- RUN EXPERIMENT ---

# compute 1M random samples from the train set


rnd_samples_file = vrl.cv.utils.random_samples(feat,
dataset.get_images('train'),
int(1e6),
'random_samples.dat')

# fit BMM
bmm.fit(rnd_samples_file)

# set Poisson parameter from sample statistics


efv.theta = vrl.io.load(rnd_samples_file,
variable_names=('samples_per_file',))['samples_per_file']

# fit eFV normalizer


efv.fit(rnd_samples_file)

# compute low- and mid-level features

efv.transform(feat.transform(dataset.imlist))

x_train = dataset.get_images('train')
y_train = dataset.get_labels('train')

x_test = dataset.get_images('test')
y_test = dataset.get_labels('test')

# train classifier and evaluate performance


clf.estimator.set_params(**{'estimator__C': 10.0})
clf.fit(efv.pass_through(x_train), y_train)

# eval
scr = np.empty((len(x_test), dataset.nclasses))
for i, fname in enumerate(x_test):
x_test_ = vrl.io.load(efv.pass_through(fname))
scr[i, :] = clf.estimator.decision_function(x_test_)
dataset.eval(scr, y_test)

def kth_tips_2a_dsift_pca_gmm():
vrl.utils.set_loglevel(vrl.utils.logging.INFO)
logger = vrl.utils.get_logger(whoami())

# --- SETUP PIPELINE ---

# NOTE: see the docstring at vrl.base.VRLBaseEstimator to


understand how
# our file pipeline works

# sample 'a' is used for testing


dataset = vrl.datasets.KTH_TIPS2(path=DATASET_PATH,
sample='a')
split_id = str(abs(hash(tuple(dataset.get_images('train'))) +
hash(tuple(dataset.get_images('test')))))

# SIFT on a resolution pyramid with 5 layers


feat = vrl.cv.DSIFT()
feat.input_path = dataset.impath
feat.output_base_path = OUTPUT_PATH

# PCA projections
pca = vrl.ml.PCA(dim=64)
pca.input_path = feat.output_path
pca.output_base_path = feat.output_path

# mixture model
gmm = vrl.ml.GaussianMixture(ncomp=16, nvar=pca.dim)
gmm.input_path = pca.output_path
gmm.output_base_path = pca.output_path

# exponential family Fisher vector with a diagnonal


normalizer
efv = vrl.ml.FisherVector(model=gmm, normalizer='diag')
efv.input_path = pca.output_path
efv.output_base_path = gmm.output_path

# setup classifier
ovr = OneVsRestClassifier(LinearSVC(loss='l1', penalty='l2'))
clf = vrl.base.SKLEstimator(estimator=ovr,
to_show=('estimator',

'estimator__C',

'estimator__loss',

'estimator__penalty'))
clf.input_path = efv.output_path
clf.output_base_path = join(efv.output_path, split_id)

# --- RUN EXPERIMENT ---

# compute 1M random samples from the train set


rnd_samples_file = vrl.cv.utils.random_samples(feat,
dataset.get_images('train'),
int(1e6),
'random_samples.dat')

# fit PCA and project random samples


pca.fit(rnd_samples_file)
rnd_samples_file = pca.transform(rnd_samples_file)

# fit GMM
gmm.fit(rnd_samples_file)

# set Poisson parameter from sample statistics


efv.theta = vrl.io.load(pca.pass_through(rnd_samples_file),
variable_names=('samples_per_file',))
['samples_per_file']

# fit eFV normalizer


efv.fit(rnd_samples_file)

# compute low- and mid-level features

efv.transform(pca.transform(feat.transform(dataset.imlist)))

x_train = dataset.get_images('train')
y_train = dataset.get_labels('train')

x_test = dataset.get_images('test')
y_test = dataset.get_labels('test')

# train classifier and evaluate performance


clf.estimator.set_params(**{'estimator__C': 1.0})
clf.fit(efv.pass_through(x_train), y_train)

# eval
scr = np.empty((len(x_test), dataset.nclasses))
for i, fname in enumerate(x_test):
x_test_ = vrl.io.load(efv.pass_through(fname))
scr[i, :] = clf.estimator.decision_function(x_test_)
dataset.eval(scr, y_test)

if __name__ == "__main__":
#kth_tips_2a_lbp_bmm()
kth_tips_2a_dsift_pca_gmm()

You might also like