def vlad_vector_batch(input_path, output_path, vocabulary): vladL = [] file_names = list_all_files(input_path) for f in file_names: vec = vlad_vector(vector_path=input_path + f, vocabulary=vocabulary) vladL.append(vec) output_path = output_path + '_vlad.txt' savetxt(output_path, asarray(vladL), delimiter=",", fmt='%.4f')
def sift_image_batch(input_path, output_path, params=None): # Process all images in the input_path by SIFT, e.g. windows processing # Return those images whose SIFT vector is invalid, i.e. empty image_names = list_all_files(input_path, onlyImage=True) res_path_L = [] for img in image_names: res_path = sift_image(input_path=input_path, image_name=img, output_path=output_path, params=params) res_path_L.append(res_path) return res_path_L
def batch_all_images(input_image_path, annotation_path, output_parent_path, unit_ratio_list, overlap_ratio, target, target_pos_path, vladVector, target_count=20, pca=True, k=30, max_iter=30, preVLAD=False, voca_path=None, dataset_mode=False, overlap_threshold=0.5): global_sift_path = '%sglobal_sift.txt' % (output_parent_path) image_name_list = get_target_pos_names(input_path=target_pos_path, target=target, target_count=target_count) sift_path_L = [] if preVLAD: delete_file(file_path=global_sift_path) for image_name in image_name_list: metadata_path = '%s%s.xml' % (annotation_path, image_name) win_sift_path = batch_one_image_pre_VLAD(input_image_path, image_name, metadata_path, output_parent_path, unit_ratio_list, overlap_ratio) sift_path_L.append(win_sift_path) append_file(dest_file=global_sift_path, input_path=win_sift_path) print "----------pre-VLAD Done" else: all_dir = list_all_files(input_path=output_parent_path + 'windows/', onlyDir=True) for d in all_dir: sift_path_L.append("%s/windows/%s/temp_sift/" % (output_parent_path, d)) print "----------pre-VLAD is enabled" if voca_path is None or not isfile(voca_path): print "~~~~~~~Learning vocabulary by the sift vectors of all windows of all images" vector_matrix = None if pca: vector_matrix = pca_dataset(input_path=global_sift_path) vocabulary = learn_vocabulary(input_path=global_sift_path, k=k, max_iter=max_iter, single_file=True, vector_matrix=vector_matrix) save_matrix(v=vocabulary, output_path=voca_path) print "~~~~~~~Learning vocabulary done" elif vladVector: print "~~~~~~~Loading existing vocabulary" vocabulary = load_matrix(input_path=voca_path) if vladVector: for i in xrange(len(image_name_list)): image_name = image_name_list[i] output_path = "%swindows/%s/%s" % (output_parent_path, image_name, image_name) print "\t======Creating VLAD vectors" vlad_vector_batch(input_path=sift_path_L[i], output_path=output_path, vocabulary=vocabulary) print "\t======VLAD Done for", image_name else: print "##########No VLAD vector generated...." if dataset_mode: print "^^^^^^^^^^Generate data set for global windows and VLAD" global_X_path = output_parent_path + "global_X.txt" global_Y_path = output_parent_path + "global_Y.txt" delete_file(global_X_path) delete_file(global_Y_path) for img_name in image_name_list: img_window_path = "%s/windows/%s/%s_windows.txt" % (output_parent_path, img_name , img_name) img_vlad_path = "%s/windows/%s/%s_vlad.txt" % (output_parent_path, img_name , img_name) metadata_path = '%s%s.xml' % (annotation_path, img_name) batch_one_image_dataset(global_X_path, global_Y_path, img_window_path, img_vlad_path, metadata_path, target, overlap_threshold=overlap_threshold) print "\tData set done for", img_name print "....................All done"
def learn_vocabulary(k, max_iter, single_file, vector_matrix=None, input_path=None): if vector_matrix is None: if single_file: vector_matrix = read_feature_vector(input_path) else: file_names = list_all_files(input_path) vector_matrix = read_feature_vector(input_path + file_names[0]) for f in xrange(1, len(file_names)): vector = read_feature_vector(input_path + file_names[f]) if vector is not None: vector_matrix = append(vector_matrix, vector, axis=0) return mykmeans_plus(data=vector_matrix,k=k, max_iter=max_iter)[0]
def learn_vocabulary(k, max_iter, single_file, vector_matrix=None, input_path=None): if vector_matrix is None: if single_file: vector_matrix = read_feature_vector(input_path) else: file_names = list_all_files(input_path) vector_matrix = read_feature_vector(input_path + file_names[0]) for f in xrange(1, len(file_names)): vector = read_feature_vector(input_path + file_names[f]) if vector is not None: vector_matrix = append(vector_matrix, vector, axis=0) return mykmeans_plus(data=vector_matrix, k=k, max_iter=max_iter)[0]
def batch_all_images(input_image_path, annotation_path, output_parent_path, unit_ratio_list, overlap_ratio, target, target_pos_path, vladVector, target_count=20, pca=True, k=30, max_iter=30, preVLAD=False, voca_path=None, dataset_mode=False, overlap_threshold=0.5): global_sift_path = '%sglobal_sift.txt' % (output_parent_path) image_name_list = get_target_pos_names(input_path=target_pos_path, target=target, target_count=target_count) sift_path_L = [] if preVLAD: delete_file(file_path=global_sift_path) for image_name in image_name_list: metadata_path = '%s%s.xml' % (annotation_path, image_name) win_sift_path = batch_one_image_pre_VLAD(input_image_path, image_name, metadata_path, output_parent_path, unit_ratio_list, overlap_ratio) sift_path_L.append(win_sift_path) append_file(dest_file=global_sift_path, input_path=win_sift_path) print "----------pre-VLAD Done" else: all_dir = list_all_files(input_path=output_parent_path + 'windows/', onlyDir=True) for d in all_dir: sift_path_L.append("%s/windows/%s/temp_sift/" % (output_parent_path, d)) print "----------pre-VLAD is enabled" if voca_path is None or not isfile(voca_path): print "~~~~~~~Learning vocabulary by the sift vectors of all windows of all images" vector_matrix = None if pca: vector_matrix = pca_dataset(input_path=global_sift_path) vocabulary = learn_vocabulary(input_path=global_sift_path, k=k, max_iter=max_iter, single_file=True, vector_matrix=vector_matrix) save_matrix(v=vocabulary, output_path=voca_path) print "~~~~~~~Learning vocabulary done" elif vladVector: print "~~~~~~~Loading existing vocabulary" vocabulary = load_matrix(input_path=voca_path) if vladVector: for i in xrange(len(image_name_list)): image_name = image_name_list[i] output_path = "%swindows/%s/%s" % (output_parent_path, image_name, image_name) print "\t======Creating VLAD vectors" vlad_vector_batch(input_path=sift_path_L[i], output_path=output_path, vocabulary=vocabulary) print "\t======VLAD Done for", image_name else: print "##########No VLAD vector generated...." if dataset_mode: print "^^^^^^^^^^Generate data set for global windows and VLAD" global_X_path = output_parent_path + "global_X.txt" global_Y_path = output_parent_path + "global_Y.txt" delete_file(global_X_path) delete_file(global_Y_path) for img_name in image_name_list: img_window_path = "%s/windows/%s/%s_windows.txt" % ( output_parent_path, img_name, img_name) img_vlad_path = "%s/windows/%s/%s_vlad.txt" % (output_parent_path, img_name, img_name) metadata_path = '%s%s.xml' % (annotation_path, img_name) batch_one_image_dataset(global_X_path, global_Y_path, img_window_path, img_vlad_path, metadata_path, target, overlap_threshold=overlap_threshold) print "\tData set done for", img_name print "....................All done"