def store_instances(layers, compression): conn = psycopg2.connect(dbname=utils.dbname, user=utils.user, password=utils.password, host=utils.host) cur = conn.cursor() start_time = time.clock() for layer in layers: # CREATE THE TABLE table_name = create_table_name(compression, layer) insert_command = "INSERT INTO " + table_name + " (file, class," values_sql = "VALUES(%s,%s," dimensions = utils.get_dimension_options(layer, compression) if len(dimensions) == 0: print 'POSSIBLE ERROR: No dimensions loaded for ', layer, ' with ', compression continue for dim in dimensions: insert_command += create_feature_name(dim) + "," values_sql += "%s," values_sql = values_sql[:-1] + ");" insert_command = insert_command[:-1] + ") " + values_sql print insert_command # INSERT DATA INTO TABLE # load the data X, ids = utils.load_instance_features(layer) scalar = utils.load_scalar(layer) X = scalar.transform(X) transforms = [] # apply the compression algorithm for dim in dimensions: compressor = utils.load_compressor(layer, dim, compression) transforms.append(compressor.transform(X)) value = [] for i in range(X.shape[0]): file_name = ids[i] value = [file_name, -1] for X_prime in transforms: value.append(X_prime[i, :].tolist()) cur.execute(insert_command, value) conn.commit() cur.close() conn.close() print 'Total Time : ', time.clock() - start_time
import sql import utils import os import numpy as np import caffe image_file = 'ubot5_1.JPG' layer = 'pool5' dimension = 256 compression = 'pca' k = 10 compressor = utils.load_compressor(layer=layer, dimension=dimension, compression=compression) scalar = utils.load_scalar(layer=layer) net, params, blobs = utils.load_network() input_image = caffe.io.load_image(os.path.join(utils.instances_dir, image_file)) # predict takes any number of images, and formats them for the Caffe net automatically prediction = net.predict([input_image], oversample=False) feat = net.blobs[layer].data[0].ravel() feat = scalar.transform(feat) comp_feat = compressor.transform(feat).ravel() results = sql.query_top_k(k=k,
labels = [] print len(ids) for i in range(len(ids)): labels.append(int(ids[i].split('_')[0].split('n')[1])) labels = matlab.double(labels) #======== Start MATLAB ============ print 'start matlab' eng = matlab.engine.start_matlab() for n_com in n_components: scalar = utils.load_scalar(layer) compressor = utils.load_compressor(layer=layer, dimension=n_com, compression=c_type) X = scalar.transform(X0) comp_X = compressor.transform(X) #comp_X = comp_X[0:500,:] #===== Convert ndarray to list so that we can pass the variable to matalb function ========= comp_X = comp_X.tolist() comp_X = matlab.double(comp_X) print '===============================start tsne in python ===================================' print layer, n_com, c_type result = eng.tsne_traning_python(comp_X, labels, dimensions, layer, n_com, c_type)
# load the test set test_files = utils.load_test_set() test_files = test_files[:N] net, params, blobs = utils.load_network() # validation labels are the test set labels test_labels = utils.load_test_class_labels() labels = utils.load_english_labels() dist_mat = utils.load_distance_matrix(distance_matrix_layer) # start matlab engine eng = matlab.engine.start_matlab() query_compressor = utils.load_compressor(layer='fc7', dimension=256, compression='pca') # initialize results data object results = {} for c_type in compression_types: results[c_type] = {} for layer in feature_layers: results[c_type][layer] = {} for n_components in pca_dimensions: results[c_type][layer][n_components] = {'similarity_dist': [], 'avg_time': []} for c_type in compression_types: for layer in feature_layers: scalar = utils.load_scalar(layer=layer)
def store_feature(layers, compression): conn = psycopg2.connect(dbname=utils.dbname, user=utils.user, password=utils.password, host=utils.host) cur = conn.cursor() start_time = time.clock() train_labels = utils.load_train_class_labels() for layer in layers: # CREATE THE TABLE table_name = create_table_name(compression, layer) cur.execute("DROP TABLE IF EXISTS " + table_name + ";") table_command = "CREATE TABLE " + table_name + " (id serial PRIMARY KEY, file text, class integer, " insert_command = "INSERT INTO " + table_name + " (file, class," values_sql = "VALUES(%s,%s," dimensions = utils.get_dimension_options(layer, compression) if len(dimensions) == 0: print 'POSSIBLE ERROR: No dimensions loaded for ', layer, ' with ', compression continue for dim in dimensions: table_command += create_feature_column(dim) insert_command += create_feature_name(dim) + "," values_sql += "%s," table_command = table_command[:-1] + ");" values_sql = values_sql[:-1] + ");" insert_command = insert_command[:-1] + ") " + values_sql print table_command print insert_command cur.execute(table_command) # INSERT DATA INTO TABLE # load the data X, imagenet_ids = utils.load_feature_layer(layer) scalar = utils.load_scalar(layer) X = scalar.transform(X) transforms = [] # apply the compression algorithm for dim in dimensions: compressor = utils.load_compressor(layer, dim, compression) transforms.append(compressor.transform(X)) value = [] for i in range(X.shape[0]): file_name = imagenet_ids[i] value = [file_name, train_labels[file_name]] for X_prime in transforms: value.append(X_prime[i, :].tolist()) cur.execute(insert_command, value) conn.commit() cur.close() conn.close() print 'Done Creating Tables' print 'Total Time : ', time.clock() - start_time
def store_tsne_feature(layers, compression, tsne_dim): conn = psycopg2.connect(dbname=utils.dbname, user=utils.user, password=utils.password, host=utils.host) cur = conn.cursor() start_time = time.clock() eng = matlab.engine.start_matlab() train_labels = utils.load_train_class_labels() for layer in layers: # CREATE THE TABLE table_name = create_table_name(compression, layer) cur.execute("DROP TABLE IF EXISTS " + table_name + ";") table_command = "CREATE TABLE " + table_name + " (id serial PRIMARY KEY, file text, class integer, " insert_command = "INSERT INTO " + table_name + " (file, class," values_sql = "VALUES(%s,%s," if compression == 'tsne': dimensions = [64] #[64, 128, 256] else: dimensions = utils.get_dimension_options(layer, compression) if len(dimensions) == 0: print 'POSSIBLE ERROR: No dimensions loaded for ', layer, ' with ', compression continue for dim in dimensions: table_command += create_feature_column(dim) insert_command += create_feature_name(dim) + "," values_sql += "%s," table_command = table_command[:-1] + ");" values_sql = values_sql[:-1] + ");" insert_command = insert_command[:-1] + ") " + values_sql #print table_command #print insert_command cur.execute(table_command) # INSERT DATA INTO TABLE # load the data X, imagenet_ids = utils.load_feature_layer(layer) scalar = utils.load_scalar(layer) X = scalar.transform(X) X = X[keep_idxs] imagenet_ids = np.asarray(imagenet_ids, dtype=np.object) imagenet_ids = imagenet_ids[keep_idxs] transforms = [] # apply the compression algorithm for dim in dimensions: if compression == 'tsne': print 'tsne' compressor = utils.load_compressor(layer, dim, 'pca') #utils.plot_tsne_features('fc7',64) comp_X = compressor.transform(X) comp_X = comp_X.tolist() comp_X = matlab.double(comp_X) comp_X = eng.tsne_testing_python(comp_X, tsne_dim, layer, dim, 'pca') comp_X = np.array(comp_X) print comp_X transforms.append(comp_X) else: compressor = utils.load_compressor(layer, dim, compression) transforms.append(compressor.transform(X)) value = [] for i in range(X.shape[0]): file_name = imagenet_ids[i] value = [file_name, train_labels[file_name]] for X_prime in transforms: value.append(X_prime[i, :].tolist()) cur.execute(insert_command, value) conn.commit() cur.close() conn.close() eng.exit() print 'Done Creating Tables' print 'Total Time : ', time.clock() - start_time
# load the test set test_files = utils.load_test_set() test_files = test_files[:N] net, params, blobs = utils.load_network() # validation labels are the test set labels test_labels = utils.load_test_class_labels() labels = utils.load_english_labels() dist_mat = utils.load_distance_matrix(distance_matrix_layer) # start matlab engine eng = matlab.engine.start_matlab() query_compressor = utils.load_compressor(layer='fc7', dimension=256, compression='pca') # initialize results data object results = {} for c_type in compression_types: results[c_type] = {} for layer in feature_layers: results[c_type][layer] = {} for n_components in pca_dimensions: results[c_type][layer][n_components] = { 'similarity_dist': [], 'avg_time': [] } for c_type in compression_types:
for layer in feature_layers: results[c_type][layer] = {} for n_components in dimensions: results[c_type][layer][n_components] = {'similarity_dist': [], 'avg_time': []} for c_type in compression_types: for layer in feature_layers: scalar = utils.load_scalar(layer=layer) for n_components in dimensions: succs = 0 # how many times we retrieved at least 1 image of the true class hits = 0 # how many images of the true class we retrieved in total compressor = utils.load_compressor(layer=layer, dimension=n_components, compression=c_type) count = 0 for t_files in utils.batch_gen(test_files, batch_size=batch_size): images = [] for t_file in t_files: image_path = os.path.join(utils.test_dir, t_file) images.append(caffe.io.load_image(image_path)) # predict takes any number of images, and formats them for the Caffe net automatically prediction = net.predict(images, oversample=False) for i in range(batch_size):