Example #1
0
from sklearn import cross_validation
from sklearn.linear_model.logistic import LogisticRegression
import numpy as np
from glob import glob
from edginess import edginess_sobel

basedir = 'simple-dataset'

def features_for(im):
    im = mh.imread(im,as_grey=True).astype(np.uint8)
    return mh.features.haralick(im).mean(0)

features = []
sobels = []
labels = []
images = glob('{}/*.jpg'.format(basedir))
for im in images:
    features.append(features_for(im))
    sobels.append(edginess_sobel(mh.imread(im, as_grey=True)))
    labels.append(im[:-len('00.jpg')])

features = np.array(features)
labels = np.array(labels)

scores = cross_validation.cross_val_score(LogisticRegression(), features, labels, cv=5)
print('Accuracy (5 fold x-val) with Logistic Regrssion [std features]: {}%'.format(0.1* round(1000*scores.mean())))

scores = cross_validation.cross_val_score(LogisticRegression(), np.hstack([np.atleast_2d(sobels).T,features]), labels, cv=5).mean()
print('Accuracy (5 fold x-val) with Logistic Regrssion [std features + sobel]: {}%'.format(0.1* round(1000*scores.mean())))

    fs : ndarray
        1-D array of features
    '''
    im = mh.imread(im, as_grey=True).astype(np.uint8)
    return mh.features.haralick(im).mean(0)


haralicks = []
sobels = []
labels = []

# Use glob to get all the images
images = glob('{}/*.jpg'.format(basedir))
for fname in images:
    haralicks.append(features_for(fname))
    sobels.append(edginess_sobel(mh.imread(fname, as_grey=True)))
    labels.append(fname[:-len('00.jpg')])

haralicks = np.array(haralicks)
sobels = np.array(sobels)
labels = np.array(labels)

# We use logistic regression because it is very fast.
# Feel free to experiment with other classifiers
scores = cross_validation.cross_val_score(LogisticRegression(),
                                          haralicks,
                                          labels,
                                          cv=5)
print('Accuracy (5 fold x-val) with Logistic Regrssion [std features]: {}%'.
      format(0.1 * round(1000 * scores.mean())))
Example #3
0
    print('picking ' + str(im) + ' ...')
    shutil.copy(im, dst)

####################################################
# Xtrain1, Xtrain2, Ytrain

basedir = 'train-dogs-vs-cats'

features = []
sobels = []
labels = []
labelVect = []  # cat 0 , dog 1
images = glob('{}/*.jpg'.format(basedir))
for im in images:
    features.append(features_for(im))
    sobels.append(edginess_sobel(mh.imread(im, as_grey=True)))
    #labels.append(im[:-len('00.jpg')])
    if ("train-dogs-vs-cats/cat" in im[:-len('.jpg')]):
        labels.append('cat')
        labelVect.append(np.array([0]))
    elif ("train-dogs-vs-cats/dog" in im[:-len('.jpg')]):
        labels.append('dog')
        labelVect.append(np.array([1]))
    else:
        raise Exception("unrecognized label:" + str(im[:-len('.jpg')]))

features = np.array(features)
labels = np.array(labels)
n = features.shape
nl = labels.shape
print('features=' + str(n))
def edginess_sobel_from_fname(fname):
    from edginess import edginess_sobel
    return edginess_sobel(mh.imread(fname, as_grey=True))
def edginess_sobel_from_fname(fname):
    from edginess import edginess_sobel
    return edginess_sobel(mh.imread(fname, as_grey=True))