Created on Jan 16, 2013 @author: jiayq ''' import cPickle as pickle import logging from matplotlib import pyplot from iceberk import mpi, visiondata, pipeline, datasets, mathutil, visualize import numpy as np mpi.root_log_level(logging.DEBUG) # compute the features bird = visiondata.CUBDataset('/u/vis/x1/common/CUB_200_2011', is_training=True, crop=1.2, prefetch=True, target_size=[256, 256]) logging.info("Generating the data...") regions = pipeline.PatchExtractor([5, 5], 1).sample(bird, 400000) normalizer = pipeline.MeanvarNormalizer({'reg': 10}) whitener = pipeline.LinearEncoder({}, trainer=pipeline.ZcaTrainer({'reg': 0.1})) encoder = pipeline.ReLUEncoder({'twoside': False}, trainer=pipeline.OMPTrainer({ 'k': 1600, 'max_iter': 100 })) regions_n = normalizer.process(regions) whitener.train(regions_n) regions_w = whitener.process(regions_n)
gflags.DEFINE_bool("flat", False, "If set, perform flat classification.") gflags.DEFINE_bool("hier", False, "If set, perform hierarchical classification.") gflags.DEFINE_bool("hierlog", False, "If set, perform hierarchical classification with log info gain.") gflags.DEFINE_bool("treereg", False, "If set, perform classification with tree regularization.") gflags.DEFINE_float("reg", 0.01, "The regularization term used in the classification.") gflags.FLAGS(sys.argv) FLAGS = gflags.FLAGS mpi.root_log_level(level=logging.DEBUG) if FLAGS.extract: train_data = visiondata.CUBDataset(ROOT, True, crop = CROP, target_size = TARGET_SIZE, prefetch = True) test_data = visiondata.CUBDataset(ROOT, False, crop = CROP, target_size = TARGET_SIZE, prefetch = True) if MIRRORED: train_data = datasets.MirrorSet(train_data) CONV.train(train_data, 400000, exhaustive = True) mpi.root_pickle(CONV, __file__ + ".conv.pickle") Xtrain = CONV.process_dataset(train_data, as_2d = True) Xtest = CONV.process_dataset(test_data, as_2d = True) Ytrain = train_data.labels() Ytest = test_data.labels() m, std = classifier.feature_meanstd(Xtrain) Xtrain -= m Xtrain /= std Xtest -= m Xtest /= std
def bird_demo(): logging.info('Loading data...') bird = visiondata.CUBDataset(FLAGS.root, is_training=True, crop=FLAGS.crop, version=FLAGS.version, prefetch=True, target_size=TARGET_SIZE) bird_test = visiondata.CUBDataset(FLAGS.root, is_training=False, crop=FLAGS.crop, version=FLAGS.version, prefetch=True, target_size=TARGET_SIZE) if FLAGS.mirrored: bird = datasets.MirrorSet(bird) conv = pipeline.ConvLayer( [ pipeline.PatchExtractor([FLAGS.patch, FLAGS.patch], 1), # extracts patches pipeline.MeanvarNormalizer({'reg': 10}), # normalizes the patches pipeline.LinearEncoder({}, trainer=pipeline.ZcaTrainer({'reg': 0.1})), pipeline.ThresholdEncoder({ 'alpha': 0.25, 'twoside': True }, trainer=pipeline.OMPTrainer( { 'k': FLAGS.k, 'max_iter': 100 })), pipeline.SpatialPooler({ 'grid': 4, 'method': 'max' }) ], fixed_size=True) logging.info('Training the pipeline...') conv.train(bird, 400000, exhaustive=True) logging.info('Extracting features...') Xtrain = conv.process_dataset(bird, as_2d=True) Ytrain = bird.labels().astype(np.int) Xtest = conv.process_dataset(bird_test, as_2d=True) Ytest = bird_test.labels().astype(np.int) # normalization m, std = classifier.feature_meanstd(Xtrain, reg=0.01) # to match Adam Coates' pipeline Xtrain -= m Xtrain /= std Xtest -= m Xtest /= std w, b = classifier.l2svm_onevsall(Xtrain, Ytrain, 0.005, fminargs={'maxfun': 1000}) accu_train = classifier.Evaluator.accuracy(Ytrain, np.dot(Xtrain, w) + b) accu_test = classifier.Evaluator.accuracy(Ytest, np.dot(Xtest, w) + b) logging.info('Training accuracy: %f' % accu_train) logging.info('Testing accuracy: %f' % accu_test) mpi.root_pickle((m, std, w, b, conv[-2].dictionary), 'debug_features.pickle')