Skip to content

Instantly share code, notes, and snippets.

@mthomure
Created January 13, 2014 01:41
Show Gist options
  • Save mthomure/8393325 to your computer and use it in GitHub Desktop.
Save mthomure/8393325 to your computer and use it in GitHub Desktop.
This script illustrates the use of an existing set of prototypes to compute image features. This data is then used to evaluate classifier performance.
from glimpse.experiment import *
def make_model_for_protos(protos):
"""Create a model for an existing set of prototypes."""
model = Model()
# Here, we assume that all prototypes have the same spatial extent.
model.s2_kernels = [protos]
return model
def train_classifier(model, pool, images, labels):
"""Train a classifier on features extracted from a set of images.
Features are extracted using an existing model.
"""
layer = model.LayerClass.C2
# Compute model activity for each image.
images = map(model.MakeState, images)
builder = Callback(BuildLayer, model, layer, save_all=False)
states = pool.map(builder, images)
# Create feature vectors from model activity.
features = ExtractFeatures(layer, states)
# Train the classifier.
clf = FitClassifier(features, labels)
return clf
def test_classifier(model, pool, clf, images, labels):
"""Test an existing classifier on features extracted from a set of images.
Features are extracted using an existing model.
"""
layer = model.LayerClass.C2
# Compute model activity for each image.
images = map(model.MakeState, images)
builder = Callback(BuildLayer, model, layer, save_all=False)
states = pool.map(builder, images)
# Create feature vectors from model activity.
features = ExtractFeatures(layer, states)
# Test the classifier.
acc,_ = ScoreClassifier(features, labels, clf=clf)
return acc
def example_protos():
"""Create an arbitrary set of example prototypes."""
from glimpse import corpora
corpus = os.path.dirname(corpora.__file__) + "/data/easy"
exp = ExperimentData()
SetModel(exp)
SetCorpus(exp, corpus)
MakePrototypes(exp, 10, 'imprint')
# Here, we assume that all prototypes have the same spatial extent.
protos = exp.extractor.model.s2_kernels[0]
return protos
def example_corpus():
"""Return information about an arbitrary image corpus."""
from glimpse import corpora
corpus = os.path.dirname(corpora.__file__) + "/data/easy"
exp = ExperimentData()
SetCorpus(exp, corpus)
return exp.corpus.paths,exp.corpus.labels
def main():
"""Illustrate the use of the above functions."""
# Generate a bunch of example data.
images,labels = example_corpus()
mask = ChooseTrainingSet(labels, train_size=0.5)
train_images = images[mask]
test_images = images[~mask]
train_labels = labels[mask]
test_labels = labels[~mask]
protos = example_protos()
# Run the functions on the example data.
model = make_model_for_protos(protos)
pool = MakePool()
clf = train_classifier(model, pool, train_images, train_labels)
acc = test_classifier(model, pool, clf, test_images, test_labels)
print "learned classifier with accuracy: %s" % acc
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment