Created
May 10, 2016 09:17
-
-
Save autosquid/4c5b72b195a4d65008347c7920ef8273 to your computer and use it in GitHub Desktop.
Bow + SVM for front/back face determination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""BoW on SIFT""" | |
import changetoworkingdir #changes to working dir. | |
import os | |
import hammer | |
import pongeecore as pc | |
import cv2 | |
import numpy as np | |
import random | |
import matplotlib | |
import matplotlib.pyplot as plt | |
from matplotlib.backends.backend_pdf import PdfPages | |
from confpro import * | |
from evalstubs import * | |
from capgevalconf import * | |
from matplotlib import offsetbox | |
from sklearn import (manifold, datasets, decomposition, ensemble, discriminant_analysis, random_projection, svm) | |
from time import time | |
# all our data | |
d = list(pc.loaddatasetconfig( os.path.join(testdataroot, 'fan', 'real_cake.json'))) | |
def createEnv(identifier, basedir = bfworkingdir): | |
steps = ["Crop", "Features", "BoW", "SVM"] | |
dirs = [os.path.join(basedir, identifier, s) for s in steps] | |
map(hammer.makedir_p , dirs) | |
hammer.makedir_p(os.path.join(basedir,'BOW', 'SVM')) | |
return dirs | |
def extractVisualFeature(filelist, dstdir): | |
sift = cv2.xfeatures2d.SIFT_create() | |
descriptors = [] | |
for fname in filelist: | |
img = cv2.imread(fname) | |
gray = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2Lab))[0] | |
kp, des = sift.detectAndCompute (gray,None) | |
descriptors.append(des) | |
if dstdir: | |
dstfile = os.path.join(dstdir, os.path.basename(fname)) | |
img=cv2.drawKeypoints(gray,kp, img) | |
cv2.imwrite(dstfile, img) | |
return descriptors | |
def bow(filelist, dstdir, nbins): | |
detect = cv2.xfeatures2d.SIFT_create() | |
extract = cv2.xfeatures2d.SIFT_create() | |
bow_kmeans_tranner = cv2.BOWKMeansTrainer(nbins) | |
for f in filelist: | |
im = cv2.imread(f) | |
gray = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2Lab))[0] | |
bow_kmeans_tranner.add(extract.compute(gray, detect.detect(im))[1]) | |
voc = bow_kmeans_tranner.cluster() | |
return voc | |
def train(d): | |
#------------------- | |
# Training Set | |
frontset = ['SJ31', 'SJ32', 'SJ33', 'SJ34', 'SJ41', 'SJ51', 'SJ52', 'SJ53', 'SJ54'] | |
backset = ['SJ21', 'SJ22', 'SJ23'] | |
def is_front(f): | |
for s in f.split('/'): | |
if s in frontset: | |
return True | |
return False | |
def build_vocabulary(allinput, nbins=30): | |
return bow(allinput, None, nbins) | |
#-------------------------------------------------------------------- | |
allinput = [] | |
allfeatures = [] | |
for s in d: | |
adataconf = pc.convert2DatasetConfig(s) | |
envdirs = createEnv(adataconf.datasetname , bfworkingdir) | |
cropped = crop(adataconf, envdirs[0])[-1] | |
allinput.extend(cropped) | |
features = extractVisualFeature(cropped, None) | |
allfeatures.extend(features) | |
voc = build_vocabulary(allinput) | |
pos_data = [] | |
neg_data = [] | |
train_data, train_label = [], [] | |
for f, des in zip(allinput, allfeatures): | |
feature = pc.extractBow_hackfix(voc, des) | |
if is_front(f): | |
pos_data.extend(feature) | |
else: | |
neg_data.extend(feature) | |
random.shuffle(pos_data) | |
nsample = len(neg_data) | |
train_data = neg_data[0:nsample] + pos_data[0:nsample] | |
train_label = [-1,] * nsample + [1,]*nsample | |
X = np.array(train_data) | |
y = (np.array(train_label) + 1)/2 | |
#------------------------------------------------------- | |
# Train | |
clf = svm.SVC() | |
clf.fit(X, y) | |
return voc, clf | |
def predict(voc, clf, adataconf): | |
#--------------------------- | |
# Predict Picture Wise | |
# print "predict image-wisely" | |
envdirs = createEnv(adataconf.datasetname , bfworkingdir) | |
cropped = crop(adataconf, envdirs[0])[-1] | |
features = extractVisualFeature(cropped, None) | |
featuresim = np.array([pc.extractBow_hackfix(voc, des) for des in features]) | |
predicted = np.array([clf.predict(f) for f in featuresim])*2 - 1 | |
print np.sum(predicted) > 0 | |
#--------------------------- | |
# Predict Groupwise | |
# print "predict group-wisely" | |
featureim = np.average(np.array([pc.extractBow_hackfix(voc, des) for des in features]), axis=0) | |
predicted = clf.predict(featureim)*2 - 1 | |
return predicted[0] > 0 | |
def Main(): | |
voc, clf = train(d) | |
frontset = ['SJ31', 'SJ32', 'SJ33', 'SJ34', 'SJ41', 'SJ51', 'SJ52', 'SJ53', 'SJ54'] | |
for s in d: # here we test all in `training` set | |
dconf = pc.convert2DatasetConfig(s) | |
print dconf.datasetname, "Real:", dconf.datasetname in frontset | |
print ' === ' | |
print predict(voc, clf, dconf) | |
if __name__ == '__main__': | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment