def plot_curve(): io.log('Loading cross-validation values...') cv_values = io.load_object('intersection_svm_CNNfeatures', ignore=True) io.log('Loading best parameters...') best_params = io.load_object('best_params_intersection_svm_CNNfeatures1', ignore=True) io.log('Plotting...') colors = itertools.cycle([ 'blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'darkolivegreen', 'darkviolet', 'black' ]) # Subplot parameters plt.figure(facecolor='white') num_subplots = len(codebook_size) num_columns = 1 num_rows = np.ceil(num_subplots / num_columns) # All subplots for ind, k in enumerate(codebook_size): # Search dictionary val = cv_values[(k)] results = val['cv_results'] feature_time = val['feature_time'] pca_time = val['pca_time'] gmm_time = val['gmm_time'] fisher_time = val['fisher_time'] scaler_time = val['scaler_time'] crossvalidation_time = val['crossvalidation_time'] total_time = val['total_time'] # Plot x = results['param_C'] y = results['mean_test_score'] e = results['std_test_score'] sorted_indices = x.argsort() x_sorted = np.asarray(x[sorted_indices], dtype=np.float64) y_sorted = np.asarray(y[sorted_indices], dtype=np.float64) e_sorted = np.asarray(e[sorted_indices], dtype=np.float64) color = colors.next() ax = plt.subplot(num_rows, num_columns, ind + 1) ax.set_xscale("log") ax.set_ylim((0.7, 0.9)) ax.errorbar(x_sorted, y_sorted, e_sorted, linestyle='--', lw=2, marker='x', color=color) ax.set_title('{} Gaussians in GMM'.format(k)) ax.set_xlabel('C') ax.set_ylabel('Accuracy') # Print information io.log('CODEBOOK {} '.format(k)) io.log('-------------') io.log('Mean accuracy: {}'.format(y.max())) io.log('Std accuracy: {}'.format(e[np.argmax(y)])) io.log('C: {}'.format(x[np.argmax(y)])) io.log() io.log('Timing') io.log('\tSIFT time: {:.2f} s'.format(feature_time)) io.log('\tPCA time: {:.2f} s'.format(pca_time)) io.log('\tGMM time: {:.2f} s'.format(gmm_time)) io.log('\tFisher time: {:.2f} s'.format(fisher_time)) io.log('\tScaler time: {:.2f} s'.format(scaler_time)) io.log('\tCV time: {:.2f} s'.format(crossvalidation_time)) io.log('\t_________________________') io.log('\tTOTAL TIME: {:.2f} s'.format(total_time)) io.log() plt.tight_layout() plt.show() plt.close()
def train(): best_accuracy = 0 best_params = {} cv_results = {} base_model = VGG16(weights='imagenet') # crop the model up to a certain layer model = Model(input=base_model.input, output=base_model.get_layer('block5_conv2').output) # Read the training set train_images_filenames = cPickle.load( open('./dataset/train_images_filenames.dat', 'r')) test_images_filenames = cPickle.load( open('./dataset/test_images_filenames.dat', 'r')) train_labels = cPickle.load(open('./dataset/train_labels.dat', 'r')) test_labels = cPickle.load(open('./dataset/test_labels.dat', 'r')) io.log('\nLoaded {} train images.'.format(len(train_images_filenames))) io.log('\nLoaded {} test images.'.format(len(test_images_filenames))) # read and process training images print 'Getting features from training images' start_feature = time.time() Train_descriptors = [] Train_label_per_descriptor = [] for i in range(len(train_images_filenames)): img = image.load_img(train_images_filenames[i], target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # get the features from images features = model.predict(x) features = features[0, :, :, :] descriptor = features.reshape(features.shape[0] * features.shape[1], features.shape[2]) Train_descriptors.append(descriptor) Train_label_per_descriptor.append(train_labels[i]) # Put all descriptors in a numpy array to compute PCA and GMM size_descriptors = Train_descriptors[0].shape[1] Desc = np.zeros( (np.sum([len(p) for p in Train_descriptors]), size_descriptors), dtype=np.uint8) startingpoint = 0 for i in range(len(Train_descriptors)): Desc[startingpoint:startingpoint + len(Train_descriptors[i])] = Train_descriptors[i] startingpoint += len(Train_descriptors[i]) feature_time = time.time() - start_feature io.log('Elapsed time: {:.2f} s'.format(feature_time)) for dim_red in pca_reduction: io.log('Applying PCA ... ') start_pca = time.time() reduction = np.int(dim_red * Desc.shape[1]) pca = decomposition.PCA(n_components=reduction) pca.fit(Desc) Desc_pca = np.float32(pca.transform(Desc)) pca_time = time.time() - start_pca io.log('Elapsed time: {:.2f} s'.format(pca_time)) for k in codebook_size: io.log('Creating GMM model (k = {})'.format(k)) start_gmm = time.time() gmm = ynumpy.gmm_learn(np.float32(Desc_pca), k) io.save_object(gmm, 'gmm_NN_pca_{}_k_{}'.format(reduction, k)) gmm_time = time.time() - start_gmm io.log('Elapsed time: {:.2f} s'.format(gmm_time)) io.log('Getting Fisher vectors from training set...') start_fisher = time.time() fisher = np.zeros((len(Train_descriptors), k * reduction * 2), dtype=np.float32) for i in xrange(len(Train_descriptors)): descriptor = Train_descriptors[i] descriptor = np.float32(pca.transform(descriptor)) fisher[i, :] = ynumpy.fisher(gmm, descriptor, include=['mu', 'sigma']) # L2 normalization - reshape to avoid deprecation warning, checked that the result is the same fisher[i, :] = preprocessing.normalize(fisher[i, :].reshape( 1, -1), norm='l2') fisher_time = time.time() - start_fisher io.log('Elapsed time: {:.2f} s'.format(fisher_time)) io.log('Scaling features...') start_scaler = time.time() stdSlr = StandardScaler().fit(fisher) D_scaled = stdSlr.transform(fisher) scaler_time = time.time() - start_scaler io.log('Elapsed time: {:.2f} s'.format(scaler_time)) io.log('Optimizing SVM hyperparameters...') start_crossvalidation = time.time() svm = SVC(kernel='precomputed') random_search = RandomizedSearchCV(svm, params_distribution, n_iter=n_iter, scoring='accuracy', refit=False, cv=3, verbose=1) # Precompute Gram matrix gram = kernels.intersection_kernel(D_scaled, D_scaled) random_search.fit(gram, train_labels) crossvalidation_time = time.time() - start_crossvalidation io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time)) # Convert MaskedArrays to ndarrays to avoid unpickling bugs results = random_search.cv_results_ results['param_C'] = results['param_C'].data # Appending all parameter-scores combinations cv_results.update({ (k): { 'cv_results': results, 'feature_time': feature_time, 'pca_time': pca_time, 'gmm_time': gmm_time, 'fisher_time': fisher_time, 'scaler_time': scaler_time, 'crossvalidation_time': crossvalidation_time, 'total_time': feature_time + pca_time + gmm_time + fisher_time + scaler_time + crossvalidation_time } }) io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) # Obtaining the parameters which yielded the best accuracy if random_search.best_score_ > best_accuracy: best_accuracy = random_search.best_score_ best_params = random_search.best_params_ best_params.update({'k': k, 'pca': dim_red}) io.log('-------------------------------\n') io.log('\nSaving best parameters...') io.save_object(best_params, 'best_params_intersection_svm_CNNfeatures', ignore=True) best_params_file = os.path.abspath( './ignore/best_params_intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(best_params_file)) io.log('\nSaving all cross-validation values...') io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) cv_results_file = os.path.abspath( './ignore/intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(cv_results_file)) io.log('\nBEST PARAMS') io.log('k={}, dim_red={}, C={} --> accuracy: {:.3f}'.format( best_params['k'], best_params['pca'], best_params['C'], best_accuracy))
def train(): best_accuracy = 0 best_params = {} cv_results = {} base_model = VGG16(weights='imagenet') # crop the model up to a certain layer model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) # Read the training set train_images_filenames = cPickle.load( open('./dataset/train_images_filenames.dat', 'r')) test_images_filenames = cPickle.load( open('./dataset/test_images_filenames.dat', 'r')) train_labels = cPickle.load(open('./dataset/train_labels.dat', 'r')) test_labels = cPickle.load(open('./dataset/test_labels.dat', 'r')) io.log('\nLoaded {} train images.'.format(len(train_images_filenames))) io.log('\nLoaded {} test images.'.format(len(test_images_filenames))) # read and process training images print 'Getting features from training images' start_feature = time.time() first = 1 for i in range(len(train_images_filenames)): img = image.load_img(train_images_filenames[i], target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # get the features from images features = model.predict(x) features = features[0, :] if first == 1: Desc = features first = 0 else: Desc = np.vstack((Desc, features)) feature_time = time.time() - start_feature io.log('Elapsed time: {:.2f} s'.format(feature_time)) io.log('Scaling features...') start_scaler = time.time() stdSlr = StandardScaler().fit(Desc) D_scaled = stdSlr.transform(Desc) scaler_time = time.time() - start_scaler io.log('Elapsed time: {:.2f} s'.format(scaler_time)) io.log('Optimizing SVM hyperparameters...') start_crossvalidation = time.time() svm = SVC(kernel='precomputed', probability=True) random_search = RandomizedSearchCV(svm, params_distribution, n_iter=n_iter, scoring='accuracy', refit=False, cv=3, verbose=1) # Precompute Gram matrix gram = kernels.intersection_kernel(D_scaled, D_scaled) random_search.fit(gram, train_labels) crossvalidation_time = time.time() - start_crossvalidation io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time)) # Convert MaskedArrays to ndarrays to avoid unpickling bugs results = random_search.cv_results_ results['param_C'] = results['param_C'].data # Appending all parameter-scores combinations cv_results.update({ 'cv_results': results, 'feature_time': feature_time, 'scaler_time': scaler_time, 'crossvalidation_time': crossvalidation_time, 'total_time': feature_time + scaler_time + crossvalidation_time }) io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) print('Best accuracy ' + str(random_search.best_score_)) # Obtaining the parameters which yielded the best accuracy if random_search.best_score_ > best_accuracy: best_accuracy = random_search.best_score_ best_params = random_search.best_params_ io.log('-------------------------------\n') io.log('\nSaving best parameters...') io.save_object(best_params, 'best_params_intersection_svm_CNNfeatures', ignore=True) best_params_file = os.path.abspath( './ignore/best_params_intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(best_params_file)) io.log('\nSaving all cross-validation values...') io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) cv_results_file = os.path.abspath( './ignore/intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(cv_results_file)) io.log('\nBEST PARAMS') io.log('C={} --> accuracy: {:.3f}'.format(best_params['C'], best_accuracy))
def train(): best_accuracy = 0 best_params = {} cv_results = {} """ SETTINGS """ settings.n_jobs = 1 # Read the training set train_images_filenames, train_labels = io.load_training_set() io.log('Loaded {} train images.'.format(len(train_images_filenames))) k = 64 io.log('Obtaining dense CNN features...') start_feature = time.time() try: D, L, I = io.load_object('train_CNN_descriptors', ignore=True), \ io.load_object('train_CNN_labels', ignore=True), \ io.load_object('train_CNN_indices', ignore=True) except IOError: # load VGG model base_model = VGG16(weights='imagenet') # io.save_object(base_model, 'base_model', ignore=True) # visualize topology in an image plot(base_model, to_file='modelVGG16.png', show_shapes=True, show_layer_names=True) # crop the model up to a certain layer model = Model(input=base_model.input, output=base_model.get_layer('block5_conv2').output) D, L, I = feature_extraction.parallel_CNN_features( train_images_filenames, train_labels, model, num_samples_class=-1, n_jobs=settings.n_jobs) io.save_object(D, 'train_CNN_descriptors', ignore=True) io.save_object(L, 'train_CNN_labels', ignore=True) io.save_object(I, 'train_CNN_indices', ignore=True) feature_time = time.time() - start_feature io.log('Elapsed time: {:.2f} s'.format(feature_time)) for dim_red in pca_reduction: io.log('Applying PCA ... ') start_pca = time.time() settings.pca_reduction = D.shape[1] * dim_red pca, D_pca = feature_extraction.pca(D) pca_time = time.time() - start_pca io.log('Elapsed time: {:.2f} s'.format(pca_time)) for k in codebook_size: io.log('Creating GMM model (k = {})'.format(k)) start_gmm = time.time() settings.codebook_size = k gmm = bovw.create_gmm( D_pca, 'gmm_{}_pca_{}_CNNfeature'.format(k, settings.pca_reduction)) gmm_time = time.time() - start_gmm io.log('Elapsed time: {:.2f} s'.format(gmm_time)) io.log('Getting Fisher vectors from training set...') start_fisher = time.time() fisher, labels = bovw.fisher_vectors(D_pca, L, I, gmm, normalization='l2') fisher_time = time.time() - start_fisher io.log('Elapsed time: {:.2f} s'.format(fisher_time)) io.log('Scaling features...') start_scaler = time.time() std_scaler = StandardScaler().fit(fisher) vis_words = std_scaler.transform(fisher) scaler_time = time.time() - start_scaler io.log('Elapsed time: {:.2f} s'.format(scaler_time)) io.log('Optimizing SVM hyperparameters...') start_crossvalidation = time.time() svm = SVC(kernel='precomputed') random_search = RandomizedSearchCV(svm, params_distribution, n_iter=n_iter, scoring='accuracy', n_jobs=settings.n_jobs, refit=False, cv=3, verbose=1) # Precompute Gram matrix gram = kernels.intersection_kernel(vis_words, vis_words) random_search.fit(gram, labels) crossvalidation_time = time.time() - start_crossvalidation io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time)) # Convert MaskedArrays to ndarrays to avoid unpickling bugs results = random_search.cv_results_ results['param_C'] = results['param_C'].data # Appending all parameter-scores combinations cv_results.update({ (k): { 'cv_results': results, 'feature_time': feature_time, 'pca_time': pca_time, 'gmm_time': gmm_time, 'fisher_time': fisher_time, 'scaler_time': scaler_time, 'crossvalidation_time': crossvalidation_time, 'total_time': feature_time + pca_time + gmm_time + fisher_time + scaler_time + crossvalidation_time } }) io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) # Obtaining the parameters which yielded the best accuracy if random_search.best_score_ > best_accuracy: best_accuracy = random_search.best_score_ best_params = random_search.best_params_ best_params.update({'k': k, 'pca': dim_red}) io.log('-------------------------------\n') io.log('\nSaving best parameters...') io.save_object(best_params, 'best_params_intersection_svm_CNNfeatures', ignore=True) best_params_file = os.path.abspath( './ignore/best_params_intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(best_params_file)) io.log('\nSaving all cross-validation values...') io.save_object(cv_results, 'intersection_svm_CNNfeatures', ignore=True) cv_results_file = os.path.abspath( './ignore/intersection_svm_CNNfeatures.pickle') io.log('Saved at {}'.format(cv_results_file)) io.log('\nBEST PARAMS') io.log('k={}, dim_red={}, C={} --> accuracy: {:.3f}'.format( best_params['k'], best_params['pca'], best_params['C'], best_accuracy))
C=0.0428307453111 pca_reduction = 52 # load VGG model base_model = VGG16(weights='imagenet') # crop the model up to a certain layer model = Model(input=base_model.input, output=base_model.get_layer('block5_conv2').output) # get train and test images train_images_filenames = cPickle.load(open('./dataset/train_images_filenames.dat', 'r')) test_images_filenames = cPickle.load(open('./dataset/test_images_filenames.dat', 'r')) train_labels = cPickle.load(open('./dataset/train_labels.dat', 'r')) test_labels = cPickle.load(open('./dataset/test_labels.dat', 'r')) io.log('\nLoaded {} train images.'.format(len(train_images_filenames))) io.log('\nLoaded {} test images.'.format(len(test_images_filenames))) # read and process training images print 'Getting features from training images' Train_descriptors = [] Train_label_per_descriptor = [] for i in range(len(train_images_filenames)): img = image.load_img(train_images_filenames[i], target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x)
def train(): best_accuracy = 0 best_params = {} cv_results = {} """ SETTINGS """ settings.n_jobs = 1 # Read the training set train_images_filenames, train_labels = io.load_training_set() io.log('Loaded {} train images.'.format(len(train_images_filenames))) # Parameter sweep for dense SIFT for ds in dense_sampling_density: io.log('Obtaining dense features with sampling parameter {}...'.format( ds)) start_sift = time.time() settings.dense_sampling_density = ds try: D, L, I = io.load_object('train_dense_descriptors_{}'.format(settings.dense_sampling_density), ignore=True), \ io.load_object('train_dense_labels_{}'.format(settings.dense_sampling_density), ignore=True), \ io.load_object('train_dense_indices_{}'.format(settings.dense_sampling_density), ignore=True) except IOError: D, L, I, _ = feature_extraction.parallel_dense( train_images_filenames, train_labels, num_samples_class=-1, n_jobs=settings.n_jobs) io.save_object(D, 'train_dense_descriptors_{}'.format( settings.dense_sampling_density), ignore=True) io.save_object(L, 'train_dense_labels_{}'.format( settings.dense_sampling_density), ignore=True) io.save_object(I, 'train_dense_indices_{}'.format( settings.dense_sampling_density), ignore=True) sift_time = time.time() - start_sift io.log('Elapsed time: {:.2f} s'.format(sift_time)) # Parameter sweep for PCA for dim_red in pca_reduction: io.log('Applying PCA (dim = {})...'.format(dim_red)) start_pca = time.time() settings.pca_reduction = dim_red pca, D_pca = feature_extraction.pca(D) pca_time = time.time() - start_pca io.log('Elapsed time: {:.2f} s'.format(pca_time)) # Parameter sweep for codebook size for k in codebook_size: io.log('Creating GMM model (k = {})'.format(k)) start_gmm = time.time() settings.codebook_size = k gmm = bovw.create_gmm( D_pca, 'gmm_{}_dense_{}_pca_{}'.format(k, ds, dim_red)) gmm_time = time.time() - start_gmm io.log('Elapsed time: {:.2f} s'.format(gmm_time)) io.log('Getting Fisher vectors from training set...') start_fisher = time.time() fisher, labels = bovw.fisher_vectors(D_pca, L, I, gmm, normalization='l2') fisher_time = time.time() - start_fisher io.log('Elapsed time: {:.2f} s'.format(fisher_time)) io.log('Scaling features...') start_scaler = time.time() std_scaler = StandardScaler().fit(fisher) vis_words = std_scaler.transform(fisher) scaler_time = time.time() - start_scaler io.log('Elapsed time: {:.2f} s'.format(scaler_time)) io.log('Optimizing SVM hyperparameters...') start_crossvalidation = time.time() svm = SVC(kernel='precomputed') random_search = RandomizedSearchCV(svm, params_distribution, n_iter=n_iter, scoring='accuracy', n_jobs=settings.n_jobs, refit=False, cv=3, verbose=1) # Precompute Gram matrix gram = kernels.intersection_kernel(vis_words, vis_words) random_search.fit(gram, labels) crossvalidation_time = time.time() - start_crossvalidation io.log('Elapsed time: {:.2f} s'.format(crossvalidation_time)) # Convert MaskedArrays to ndarrays to avoid unpickling bugs results = random_search.cv_results_ results['param_C'] = results['param_C'].data # Appending all parameter-scores combinations cv_results.update({ (k, dim_red, ds): { 'cv_results': results, 'sift_time': sift_time, 'pca_time': pca_time, 'gmm_time': gmm_time, 'fisher_time': fisher_time, 'scaler_time': scaler_time, 'crossvalidation_time': crossvalidation_time, 'total_time': sift_time + pca_time + gmm_time + fisher_time + scaler_time + crossvalidation_time } }) io.save_object( cv_results, 'intersection_svm_optimization_fisher_vectors_l2', ignore=True) # Obtaining the parameters which yielded the best accuracy if random_search.best_score_ > best_accuracy: best_accuracy = random_search.best_score_ best_params = random_search.best_params_ best_params.update({'k': k, 'pca': dim_red, 'ds': ds}) io.log('-------------------------------\n') io.log('\nSaving best parameters...') io.save_object( best_params, 'best_params_intersection_svm_optimization_fisher_vectors_l2', ignore=True) best_params_file = os.path.abspath( './ignore/best_params_intersection_svm_optimization_fisher_vectors_l2.pickle' ) io.log('Saved at {}'.format(best_params_file)) io.log('\nSaving all cross-validation values...') io.save_object(cv_results, 'intersection_svm_optimization_fisher_vectors_l2', ignore=True) cv_results_file = os.path.abspath( './ignore/intersection_svm_optimization_fisher_vectors_l2.pickle') io.log('Saved at {}'.format(cv_results_file)) io.log('\nBEST PARAMS') io.log('k={}, C={}, dim_red={}, dense_grid={} --> accuracy: {:.3f}'.format( best_params['k'], best_params['C'], best_params['pca'], best_params['ds'], best_accuracy))
def train(): best_accuracy = 0 best_params = {} cv_results = {} # load VGG model base_model = VGG16(weights='imagenet') # crop the model up to a certain layer model = Model(input=base_model.input, output=base_model.get_layer('block5_conv2').output) # aggregating features with max-pooling # inputs = Input(shape=[14, 14, 512]) # x = MaxPooling2D((2, 2), strides=(2, 2), name='max_pooling_layer')(inputs) # model_agg = Model(inputs, x, name='agg_features') # get train and test images train_images_filenames = cPickle.load( open('./dataset/train_images_filenames.dat', 'r')) train_labels = cPickle.load(open('./dataset/train_labels.dat', 'r')) io.log('\nLoaded {} train images.'.format(len(train_images_filenames))) # read and process training images print 'Getting features from training images' Train_descriptors = [] Train_label_per_descriptor = [] for i in range(len(train_images_filenames)): img = image.load_img(train_images_filenames[i], target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # get the features from images features_ = model.predict(x) features = features_[0, :, :, :] descriptor = features.reshape(features.shape[0] * features.shape[1], features.shape[2]) # aggregate features # max value (can be different filters) #descriptor_agg=descriptor.max(axis=1) # sum value (of all layers) #descriptor_agg=np.sum(descriptor,axis=1) # max value of just one filter energy = descriptor.max(axis=0) descriptor_agg = descriptor[:, np.argmax(energy)] descriptor_agg = np.reshape(descriptor_agg, [descriptor_agg.shape[0], 1]) Train_descriptors.append(descriptor_agg) Train_label_per_descriptor.append(train_labels[i]) # Put all descriptors in a numpy array to compute PCA and GMM size_descriptors = Train_descriptors[0].shape[1] #size_descriptors=1 Desc = np.zeros( (np.sum([len(p) for p in Train_descriptors]), size_descriptors), dtype=np.uint8) startingpoint = 0 for i in range(len(Train_descriptors)): Desc[startingpoint:startingpoint + len(Train_descriptors[i])] = Train_descriptors[i] startingpoint += len(Train_descriptors[i]) for k in codebook_size: print('Computing gmm with ' + str(k) + ' centroids') gmm = ynumpy.gmm_learn(np.float32(Desc), k) # io.save_object(gmm, 'gmm_NN_agg_features_max') # Compute the fisher vectors of the training images print('Computing fisher vectors') fisher = np.zeros((len(Train_descriptors), k * 1 * 2), dtype=np.float32) for i in xrange(len(Train_descriptors)): descriptor = Train_descriptors[i] # descriptor = np.float32(pca.transform(descriptor)) aux = ynumpy.fisher(gmm, descriptor, include=['mu', 'sigma']) fisher[i, :] = np.reshape(aux, [1, aux.shape[0]]) #fisher[i,:]=aux # L2 normalization - reshape to avoid deprecation warning, checked that the result is the same fisher[i, :] = preprocessing.normalize(fisher[i, :].reshape(1, -1), norm='l2') # CV in SVM training io.log('Scaling features...') std_scaler = StandardScaler().fit(fisher) vis_words = std_scaler.transform(fisher) io.log('Optimizing SVM hyperparameters...') svm = SVC(kernel='precomputed') random_search = RandomizedSearchCV(svm, params_distribution, n_iter=n_iter, scoring='accuracy', n_jobs=1, refit=False, cv=3, verbose=1) # Precompute Gram matrix gram = kernels.intersection_kernel(vis_words, vis_words) random_search.fit(gram, train_labels) # Convert MaskedArrays to ndarrays to avoid unpickling bugs results = random_search.cv_results_ results['param_C'] = results['param_C'].data # Appending all parameter-scores combinations cv_results.update({(k): { 'cv_results': results, }}) io.save_object(cv_results, 'intersection_svm_CNNfeatures_aggregate_energy', ignore=True) # Obtaining the parameters which yielded the best accuracy if random_search.best_score_ > best_accuracy: best_accuracy = random_search.best_score_ best_params = random_search.best_params_ best_params.update({'k': k}) io.log('-------------------------------\n') io.log('\nSaving best parameters...') io.save_object(best_params, 'best_params_intersection_svm_CNNfeatures_aggregate_energy', ignore=True) best_params_file = os.path.abspath( './ignore/best_params_intersection_svm_CNNfeatures_aggregate_energy.pickle' ) io.log('Saved at {}'.format(best_params_file)) io.log('\nSaving all cross-validation values...') io.save_object(cv_results, 'intersection_svm_CNNfeatures_aggregate_energy', ignore=True) cv_results_file = os.path.abspath( './ignore/intersection_svm_CNNfeatures_aggregate_energy.pickle') io.log('Saved at {}'.format(cv_results_file)) io.log('\nBEST PARAMS') io.log('k={}, C={} --> accuracy: {:.3f}'.format(best_params['k'], best_params['C'], best_accuracy))