import numpy import numpy.random import pylab from dispims import dispims import logreg rng = numpy.random.RandomState(1) SMALL = 0.001 patchsize = 10 numfeatures = 100 def crop_patches_gray(image, keypoints, patchsize): patches = numpy.zeros((len(keypoints), patchsize**2)) for i, k in enumerate(keypoints): patches[i, :] = image[k[0]-patchsize/2:k[0]+patchsize/2, k[1]-patchsize/2:k[1]+patchsize/2].flatten() return patches def pca(data, var_fraction): """ principal components analysis of data (columnwise in array data), retaining as many components as required to retain var_fraction of the variance """ from numpy.linalg import eigh u, v = eigh(numpy.cov(data, rowvar=0, bias=1)) v = v[:, numpy.argsort(u)[::-1]] u.sort() u = u[::-1] u = u[u.cumsum()<=(u.sum()*var_fraction)] numprincomps = u.shape[0] u[u0) #LOAD DATA trainimages = numpy.loadtxt("cifarmini_gray_images_train.txt") trainlabels = numpy.loadtxt("cifarmini_labels_train.txt") numtrain = trainimages.shape[0] testimages = numpy.loadtxt("cifarmini_gray_images_test.txt") testlabels = numpy.loadtxt("cifarmini_labels_test.txt") numtest = testimages.shape[0] R = rng.permutation(trainimages.shape[0]) trainimages = trainimages[R] trainlabels = trainlabels[R] allimages = numpy.concatenate((trainimages, testimages), 0) #CROP PATCHES print "generating patches" somepatches = numpy.concatenate([crop_patches_gray(im.reshape(32,32), numpy.array([rng.randint(patchsize, 32-patchsize, 1), rng.randint(patchsize, 32-patchsize, 1)]).T, patchsize) for im in trainimages]).astype("float32") R = rng.permutation(somepatches.shape[0]) somepatches = somepatches[R, :] print "numpatches: ", somepatches.shape[0] print "done" #LEARN WHITENING MATRICES print "whitening" meanstd = somepatches.std() somepatches -= somepatches.mean(1)[:,None] somepatches /= somepatches.std(1)[:,None] + 0.1 * meanstd somepatches -= somepatches.mean(0)[None,:] somepatches /= somepatches.std(0)[None,:] pca_backward, pca_forward, zca_backward, zca_forward = pca(somepatches, 1.0) print "done" #DISPLAY WHITENING MATRICES pylab.figure(1) dispims(somepatches[:100].T, patchsize, patchsize, 2) pylab.figure(2) dispims(pca_backward.T, patchsize, patchsize, 2) pylab.figure(3) dispims(zca_backward.T, patchsize, patchsize, 2) #EXTRACT FEATURES WITH POOLING allfeatures = [] prototypes = somepatches[:numfeatures] prototypes_whitened = numpy.dot(prototypes, pca_backward.T) #prototypes_whitened = prototypes print "extracting features", print "xxxxx", for i, image in enumerate(allimages): print "\b\b\b\b\b\b{0:5d}".format(i), image = image.reshape(32, 32) keypoints = numpy.array([c.flatten() for c in numpy.meshgrid(numpy.arange(patchsize/2, 32-patchsize/2), numpy.arange(patchsize/2, 32-patchsize/2))]).T patches = crop_patches_gray(image, keypoints, patchsize) patches -= patches.mean(1)[:,None] patches /= patches.std(1)[:,None] + 0.1 * meanstd patches = numpy.dot(patches, pca_backward.T).astype("float32") allfeatures.append(extract_features(patches, prototypes_whitened).mean(0).astype("float32")) print trainfeatures = numpy.vstack(allfeatures[:numtrain]) testfeatures = numpy.vstack(allfeatures[numtrain:]) pylab.figure(4) pylab.plot(trainfeatures.mean(0)) #CLASSIFICATION lr = logreg.Logreg(10, trainfeatures.shape[1]) lr.train(trainfeatures.T, trainlabels.T, numsteps=100, verbose=False, weightcost=0.0) lr.train_cg(trainfeatures.T, trainlabels.T, weightcost=0.0, maxnumlinesearch=1000) print "logreg train performance: ", 1.0 - lr.zeroone(trainfeatures.T, trainlabels.T) print "logreg test performance: ", 1.0 - lr.zeroone(testfeatures.T, testlabels.T)