def train_classifier(): # first, load target and non-target training data train_target = png2fea(TRAIN_TARGET) # target training data train_ntarget = png2fea(TRAIN_NTARGET) # non-target training data # convert the grayscale images to 1d arrays x1 = []; x2 = [] for im in train_target.values(): x1.append(inflate_data(im)) for im in train_ntarget.values(): x2.append(inflate_data(im)) x1 = np.vstack(x1) x2 = np.vstack(x2) # convert data to numpy arrays x1 = np.array(x1) # target x2 = np.array(x2) # non-target dim = x1.shape[1] # standardise the data train_mean = np.mean(np.vstack((x1, x2)), axis=0) train_std = np.std(np.vstack((x1, x2)), axis=0) x1 -= train_mean x1 /= train_std x2 -= train_mean x2 /= train_std cov_tot = np.cov(np.vstack([x1, x2]).T, bias=True) # PCA - reduce the dimensionality d_pca, e_pca = scipy.linalg.eigh(cov_tot, eigvals=(dim-150, dim-1)) x1_pca = x1.dot(e_pca) x2_pca = x2.dot(e_pca) # perform the LDA on the reduced data space cov_tot_pca = np.cov(np.vstack([x1_pca, x2_pca]).T, bias=True) dim_pca = x1_pca.shape[1] n_x1 = len(x1_pca) n_x2 = len(x2_pca) cov_wc = (n_x1*np.cov(x1_pca.T, bias=True) + n_x2*np.cov(x2_pca.T, bias=True)) / (n_x1 + n_x2) cov_ac = cov_tot_pca - cov_wc d_lda, e_lda = scipy.linalg.eigh(cov_ac, cov_wc, eigvals=(dim_pca-1, dim_pca-1)) # now we've got our one dimensional data x1_lda = x1_pca.dot(e_lda) x2_lda = x2_pca.dot(e_lda) # compute the gaussian distributions for our classes and evalueate the test data mean_x1, cov_x1 = train_gauss(x1_lda) mean_x2, cov_x2 = train_gauss(x2_lda) # now save the model parameters model_parameters = [e_pca, e_lda, (mean_x1, cov_x1), (mean_x2, cov_x2), train_mean, train_std] with open('img_pca_lda.model', 'wb') as f: pickle.dump(model_parameters, f) f.close()
def main(): # # # Getting all training data from data/train print("STEP1: getting training and evaluation data") toTrain = [] filenames = os.listdir(TRAIN_DIR) for filename in filenames: person = png2fea(TRAIN_DIR + "/" + filename).values() CLASSES.extend( [filename] * len(person) ) # array of names of classes corresponding with each person toTrain.extend(person) # adding feature of one person in the arre # # # Getting all evaluation data from data/evaluate toEvaluate = [] toEvalNames = [] evaluateDict = png2fea(EVAL_DIR) toEvalNames.extend(evaluateDict.keys()) temp = evaluateDict.values() toEvaluate.extend(temp) print("STEP1 done.") toTrain_2d, toEvaluate_2d = imagesModify(toTrain, toEvaluate) pca = performPCA(toTrain_2d) # To see the graph of PCA and all eigenfaces, set this variable to 1 showPCAandEigenss = 0 if (showPCAandEigenss == 1): showPCAandEigens(pca) eigenfaces = pca.components_.reshape((COMPONENTS, WIDTH, HEIGHT)) trainPCA, evalPCA = projectEigens(toTrain_2d, toEvaluate_2d, pca) classifier = setUpClassifier() print(CLASSES) classifier = fitClassifier(classifier, trainPCA, CLASSES) predict(classifier, evalPCA, toEvalNames) print( "CLASSIFICATION is done successfuly. \nCheck file image_PCA_MLPerceptron.txt for the results." )
def classify(): try: f = open('img_pca_lda.model', 'rb') except IOError: print('Error: File with model parameters not found, please train the ', + 'classifier first.') sys.exit() # load the model parameters model_parameters = pickle.load(f) e_pca = model_parameters[0] e_lda = model_parameters[1] mean_x1 = model_parameters[2][0] cov_x1 = model_parameters[2][1] mean_x2 = model_parameters[3][0] cov_x2 = model_parameters[3][1] train_mean = model_parameters[4] train_std = model_parameters[5] # load the evaluation data eval_data = list(png2fea(EVAL_DATA).items()) # evaluation data # ...aaand classify the given data count = 0 results = [] for filename, data in eval_data: data = crop_image(data, 2, 2, 2, 2) # standardise the data data = data.flatten() data -= train_mean data /= train_std data = (data.dot(e_pca)).dot(e_lda) # transform the test data # compute the log-likelihood for the classified picture ll_target = logpdf_gauss(data, mean_x1, np.atleast_2d(cov_x1)) ll_ntarget = logpdf_gauss(data, mean_x2, np.atleast_2d(cov_x2)) # compute the score of the picture score = sum(ll_target) - sum(ll_ntarget) # make the hard decision if score >= THRESHOLD: decision = 1; count += 1 # target else: decision = 0 # non-target results.append(filename.split('/')[-1].split('.')[0] + ' ' + str(np.around(score, decimals=8)) + ' ' + str(decision)) results.sort() output = open('image_pca_lda_gaussian.txt', 'w') for result in results: print(result, file=output) if PRINT_STATS: print(f'Targets found: {count}') output.close()
import numpy as np from ikrlib import png2fea, wav16khz2mfcc import re data_folder_path = "../data/eval" img_model_path = "cnn_image.h5" voice_model_path = "nn_voice.h5" data = [] labels = [] voice_model = load_model(voice_model_path, compile=True) img_model = load_model(img_model_path, compile=True) data = {} img_data = png2fea(data_folder_path) img_model.summary() output_file = open('./predict_image_output.txt', 'w') print("[INFO] evaluating image classifier...") for k, v in img_data.items(): #prepare data for model i_test_x = np.vstack(tuple(v)) i_test_x = i_test_x.reshape(1, 80, 80, 3) i_test_x = np.r_[i_test_x] i_test_x = i_test_x.astype('float32') i_test_x /= 255 #prediction predictions = img_model.predict_proba(i_test_x) p = np.argmax(predictions)
from keras import layers from keras import models from keras import optimizers from keras import utils from keras.layers.advanced_activations import LeakyReLU from ikrlib import png2fea import numpy as np import matplotlib.pyplot as plt import datetime import time # Load data train_t = np.vstack(tuple(png2fea('../data/target_train').values())) print(train_t.shape) train_t = train_t.reshape(train_t.shape[0] // 80, 80, 80, 3) print(train_t.shape) train_n = np.vstack(tuple(png2fea('../data/non_target_train').values())) print(train_n.shape) train_n = train_n.reshape(train_n.shape[0] // 80, 80, 80, 3) print(train_t.shape) val_t = np.vstack(tuple(png2fea('../data/target_dev').values())) print(val_t.shape) val_t = val_t.reshape(val_t.shape[0] // 80, 80, 80, 3) print(val_t.shape) val_n = np.vstack(tuple(png2fea('../data/non_target_dev').values())) print(val_n.shape) val_n = val_n.reshape(val_n.shape[0] // 80, 80, 80, 3)
test = {} average = np.zeros((80, 80), dtype=np.float64).reshape(-1) train_set_names = [] train_set_list = [] test_set_names = [] test_set_list = [] num = 185 img_counter = 0 for i in range(1, 32): train[i] = png2fea("./train/" + str(i)) for key in train[i]: tmp = train[i][key].reshape(-1) train_set_list.append(tmp) train_set_names.append(i) average += tmp img_counter += 1 average = average / float(img_counter) #plt.imshow(average.reshape(80, 80), cmap="gray") #plt.show() test_counter = 0 for i in range(1, 2):