Skip to content

Instantly share code, notes, and snippets.

@mthomure
Last active August 29, 2015 14:16
Show Gist options
  • Save mthomure/15fbd7d91a9f0f98693b to your computer and use it in GitHub Desktop.
Save mthomure/15fbd7d91a9f0f98693b to your computer and use it in GitHub Desktop.
Glimpse as Feature Extraction (for Kendall) -- load activity from a glimpse experiment as features for a classifier
from cPickle import load as load_pickle
from glimpse.experiment import Layer, ExtractFeatures, GetTrainingSet
# load an experiment object from disk, as saved by the 'glab' command.
def load_experiment(path):
with open(path) as f:
# load saved experiment. return type is described here:
# https://github.com/mthomure/glimpse-project/blob/master/glimpse/experiment/experiment.py#L77
return load_pickle(f)
# summarize data in experiment object for use by a scikit-learn classifier.
def get_classifier_input(exp):
# get all features as a numpy array. this assumes you want activity from the
# C2 layer.
xs = ExtractFeatures(Layer.C2, exp.extractor.activation)
# get class labels as a numpy array. you may also find the function
# glimpse.experiment.GetLabelNames(exp)
# useful, which returns the class name as a string for each image.
ys = exp.corpus.labels
# find out which images were used for training, and which for testing. this
# is an array of booleans, which is useful for the kind of array indexing
# described here:
# http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing
trn = GetTrainingSet(exp)
return dict(
# get features for training set as 2d numpy array
xs_trn = xs[trn],
# get features for testing set
xs_test = xs[~trn],
# get class labels for training set
ys_trn = ys[trn],
# get class labels for testing set
ys_tst = ys[~trn])
if __name__ == "__main__":
import sys, pprint
if len(sys.argv) < 2:
sys.exit("usage: %s EXPERIMENT.dat" % sys.argv[0])
path = sys.argv[1]
exp = load_experiment(path)
data = get_classifier_input(exp)
pprint.pprint(data)
# here's a simple example exercising the script load-activity.py
# first, compute activity for a set of images.
mthomure:~$ glab --corpus-name easy -p imprint -n 10 --verbose -o out.dat
INFO:root:Reading class sub-directories from: /Users/mthomure/anaconda/lib/python2.7/site-packages/glimpse/corpora/data/easy
INFO:root:Reading images from class directories: ['/Users/mthomure/anaconda/lib/python2.7/site-packages/glimpse/corpora/data/easy/circle',
'/Users/mthomure/anaconda/lib/python2.7/site-packages/glimpse/corpora/data/easy/cross']
INFO:root:Using pool: MulticorePool
INFO:root:Learning 10 prototypes at 1 sizes from 4 images by imprinting
INFO:root:Learning prototypes took 0.335s
INFO:root:Computing C2 activation maps for 10 images
INFO:root:Computing activation maps took 0.726s
INFO:root:Writing experiment data to file -- out.pkl
# then, extract feature vectors and labels for use in a scikit-learn model.
mthomure:~$ python load_activity.py out.pkl
{'xs_test': array([[ 0.8270908 , 0.99733424, 0.78693205, 0.84405392, 0.36080733,
0.88399971, 0.33603266, 0.84947342, 0.94479066, 0.69329476],
[ 0.81615609, 0.99654359, 0.87037575, 0.81144947, 0.32163975,
0.90692639, 0.29797998, 0.92359161, 0.95646822, 0.65744346],
[ 0.43396509, 0.99613065, 0.48352337, 0.62949139, 0.43605357,
0.81298745, 0.36959648, 0.87253934, 0.94972253, 0.7010594 ],
[ 0.27677462, 0.9949404 , 0.25747168, 0.14426161, 0.57402003,
0.22920944, 0.51329136, 0.34722003, 0.46306002, 0.86341625],
[ 0.21736699, 0.9950096 , 0.20971335, 0.14578031, 0.58230257,
0.2581763 , 0.46910548, 0.33803439, 0.35121593, 0.95228946],
[ 0.21323967, 0.99529862, 0.19002655, 0.10273797, 0.4512893 ,
0.1622162 , 0.44975236, 0.18094081, 0.25892368, 0.66407734]], dtype=float32),
'xs_trn': array([[ 1. , 0.99805045, 1. , 0.6989426 , 0.40098032,
1. , 0.35889021, 0.8306604 , 0.88369536, 0.73072773],
[ 0.63719684, 0.99605894, 0.66267645, 1. , 0.32934377,
0.92108756, 0.32438299, 1. , 1. , 0.69304198],
[ 0.20787469, 1. , 0.18297496, 0.10189372, 1. ,
0.1832864 , 1. , 0.18063352, 0.33744204, 0.71738458],
[ 0.21831419, 0.99456131, 0.20686066, 0.1402391 , 0.51418829,
0.28021285, 0.42769903, 0.37925315, 0.37054563, 1. ]], dtype=float32),
'ys_trn': array([0, 0, 1, 1]),
'ys_tst': array([0, 0, 0, 1, 1, 1])}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment