Example #1
0
def train_and_validate():
    # load data
    raw_data = load_digits()
    X = raw_data.data
    y = raw_data.target

    # organize data
    X_train = X[:60]
    y_train = y[:60]

    X_valid = X[60:80]
    y_valid = y[60:80]

    # train and validate
    md_clf = svm_train(X_train, y_train)
    y_pred = md_clf.predict(X_valid)

    # check the result
    idx = [i for i in range(len(y_pred)) if y_valid[i] != y_pred[i]]
    for i in idx:
        print("index, actual digit, svm_prediction: {:d} {:d} {:d}".format(
            i + 60, y_valid[i], y_pred[i]))

    precision = (20 - len(idx)) / 20
    print("Total number of mid-identifications: {:d}".format(len(idx)))
    print("Success rate: {:.2f}".format(precision))

    return md_clf
Example #2
0
def pred_on_pca():
    # load data
    raw_data = load_digits()
    X = raw_data.data
    y = raw_data.target

    # organize data
    X_train = X[:60]
    y_train = y[:60]

    X_valid = X[60:80]
    y_valid = y[60:80]

    # train
    pca, X_proj = pca_X(X_train, n_comp=30)
    md_clf_new = svm_train(X_proj, y_train)

    # validate
    file = "unseen_dig.png"
    prediction = pca_svm_pred(file, pca, md_clf_new)
    print("The prediction: ", prediction[0])
Example #3
0
X = prf.load_images(paths, names)
name_dic = {1:" Luke", 2:" Janet", 3:" Gilbert"} #change target dictionary for your own photos and names.

# creating target
y = np.concatenate((np.ones(40), np.ones(37) *2, np.ones(40) * 3)) ## change depending how many targets you will have.

Testing
Errors = 0
for i in range(X.shape[0]):
    Xtest = X[i]
    ytest = y[i]
    Xtrain = np.delete(X, i, axis = 0)
    ytrain = np.delete(y, i, axis = 0)
    md_pca, Xproj = prf.pca_X(Xtrain, n_comp = 50)
    XtestProj = md_pca.transform(Xtest.reshape(1,-1))
    md_clf = prf.svm_train(Xproj, ytrain)
    predic = md_clf.predict(XtestProj)
    if(predic[0] != ytest):
         Errors +=1


print("success rate: ", (1 - (Errors/X.shape[0])) * 100, "%")

# START OF PART C
#NOTE MUST CHANGE PICTURE DIRECTORY TO MATCH YOUR INDIVIDUAL DIRECTORY
GuessPicture = "../Pictures/whoswho.JPG"
faces = prf.get_faces(GuessPicture)
predictions = prf.pca_svm_pred(faces, md_pca, md_clf)
print("PCA+SVM predition for person 1: {}".format(name_dic[predictions[0]]))
print("PCA+SVM predition for person 2: {}".format(name_dic[predictions[1]]))
print("PCA+SVM predition for person 3: {}".format(name_dic[predictions[2]]))
Example #4
0
    im = mpimg.imread('bohr' + str(i) + '.jpeg')
    flat_bohr.append(interpol_im(im, dim1=45, dim2=60))
    y.append(0)
for i in range(ein):
    im = mpimg.imread('ein' + str(i) + '.jpeg')
    flat_ein.append(interpol_im(im, dim1=45, dim2=60))
    y.append(1)

# concatenate into a 21x2700 array
X = np.vstack((np.array(flat_bohr), np.array(flat_ein)))

###################################### Training ########################################

### instantiating PCA and CLF
md_pca, X_proj = pca_X(X)
md_clf = svm_train(X_proj, y)

### all the training data's predictions are correct
# for i in range(21):
#     p = md_clf.predict(X_proj[i].reshape(1, -1))
#     print(p, y[i])

###################################### Validation ########################################

print('Below is the Validation part')

perc = 0
tot = 21

for select_idx in range(21):
Example #5
0
perc = 0
tot = 21

for select_idx in range(tot):
    #     set_trace()
    Xtrain = np.delete(X, select_idx, axis=0)
    ytrain = np.delete(y, select_idx)

    md_pca.fit(Xtrain)
    Xtrain_proj = md_pca.transform(Xtrain)

    Xtest = X[select_idx].reshape(1, -1)
    ytest = y[select_idx]
    Xtest_proj = md_pca.transform(Xtest)

    md_clf = svm_train(Xtrain_proj, ytrain)
    pre = md_clf.predict(Xtest_proj)
    if pre == ytest:
        perc += 1.0

perc /= tot / 100.0
print("Success rate: {:f}".format(perc))

unseen_phys1 = "unseen_phys1.jpg"
unseen_phys2 = "unseen_phys2.jpg"

pre1 = pca_svm_pred(unseen_phys1, md_pca, md_clf)
pre2 = pca_svm_pred(unseen_phys2, md_pca, md_clf)

pre_ein_statement = "PCA+SVM prediction for physicist 1: {:s}".format(phys_dict[pre1[0]])
pre_bohr_statement = "PCA+SVM prediction for physicist 2: {:s}".format(phys_dict[pre2[0]])
Example #6
0
dig_img = dig_data.images
unseen = mpimg.imread('unseen_dig.png')
flatten_unseen = interpol_im(unseen, plot_new_im = False)
rescaled_unseen = rescale_pixel(X, flatten_unseen, index, plot_ref = True, plot_unseen = True)

print('Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively'.format(index, np.mean(rescaled_unseen), np.mean(X[15])))
print('The rescaled unseen has all the pixel values as integers, flatten array shown as following:')
print(rescaled_unseen)

###################### Trianing ##########################

dig_data = load_digits()
X = dig_data.data
y = dig_data.target

md_clf = svm_train(X[0:60], y[0:60], gamma = 0.001, C = 100)

####################### Validation #########################

perc = 0
start = 60
end = 80
mis = 0
for i in range(start, end):
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    ans = y[i]
#     print(pre)
    if ans == pre:
        perc += 1
    else:
        plt.imshow(X[i].reshape((8,8)), cmap = 'binary')
Example #7
0
print(
    'Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively'
    .format(index, np.mean(rescaled_unseen), np.mean(X[15])))
print(
    'The rescaled unseen has all the pixel values as integers, flatten array shown as following:'
)
print(rescaled_unseen)

###################### Trianing ##########################

dig_data = load_digits()
X = dig_data.data
y = dig_data.target

md_clf = svm_train(X[0:60], y[0:60], gamma=0.001, C=100)

####################### Validation #########################

perc = 0
start = 60
end = 80
mis = 0
for i in range(start, end):
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    ans = y[i]
    #     print(pre)
    if ans == pre:
        perc += 1
    else:
        plt.imshow(X[i].reshape((8, 8)), cmap='binary')
Example #8
0
from pattern_recog_func import pca_svm_pred
from pdb import set_trace
from scipy.interpolate import interp2d
from skimage import transform, data, io
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn import preprocessing
from sklearn import svm


dig_data = load_digits()
X = dig_data.data
y = dig_data.target

md_clf = svm_train(X[0:60], y[0:60])
perc = 0
mis = 0
tot_start = 60
tot_end = 80
for i in range(tot_start, tot_end):
    ans = y[i]
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    if ans != pre:
        mis += 1
        plt.imshow(X[i].reshape((8, 8)), cmap='binary')
        plt.show()
        print("--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}".format(i, ans, pre))
    if ans == pre:
        perc += 1.
        
the digits provided
"""
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from pattern_recog_func import interpol_im
from pattern_recog_func import rescale_pixel
from pattern_recog_func import svm_train
from sklearn.datasets import load_digits

# loading the digits
dig_data = load_digits()
# getting the digits data
X = dig_data.data
y = dig_data.target
# using the first 60 images in X to train
md_clf = svm_train(X[0:60], y[0:60])
perc = 0
mis = 0
tot_start = 60
tot_end = 80
# applying md_clf for the next 20 images
for i in range(tot_start, tot_end):
    ans = y[i]
    pre = md_clf.predict(X[i].reshape(1, -1))[0]
    if ans != pre:
        mis += 1
        plt.imshow(X[i].reshape((8, 8)), cmap='binary')
        plt.show()
        print(
            "--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}".
            format(i, ans, pre))
Example #10
0
    im = mpimg.imread('bohr'+str(i)+'.jpeg')
    flat_bohr.append(interpol_im(im, dim1 = 45, dim2 = 60))
    y.append(0)
for i in range(ein):
    im = mpimg.imread('ein'+str(i)+'.jpeg')
    flat_ein.append(interpol_im(im, dim1 = 45, dim2 = 60))
    y.append(1)

# concatenate into a 21x2700 array
X = np.vstack((np.array(flat_bohr), np.array(flat_ein)))

###################################### Training ########################################
 
### instantiating PCA and CLF
md_pca, X_proj = pca_X(X)
md_clf = svm_train(X_proj, y)

### all the training data's predictions are correct
# for i in range(21):
#     p = md_clf.predict(X_proj[i].reshape(1, -1))
#     print(p, y[i])

###################################### Validation ########################################

print('Below is the Validation part')

perc = 0
tot = 21

for select_idx in range(21):