def build_index_and_search_through_it(images_features, file_index): # Decide whether to do only image search or hybrid search if not features_from_new_model_boolean: # This is pure image search image_index = vector_search.index_features(images_features, dims=4096) search_key = get_index(input_image, file_index) results = vector_search.search_index_by_key(search_key, image_index, file_index) print(results) else: word_vectors = vector_search.load_glove_vectors(glove_path) # If we are searching for tags for an image if not input_word: # Work on a single image instead of indexing search_key = get_index(input_image, file_index) word_index, word_mapping = vector_search.build_word_index( word_vectors) results = vector_search.search_index_by_value( images_features[search_key], word_index, word_mapping) # If we are using words to search through our images else: image_index = vector_search.index_features(images_features, dims=300) results = vector_search.search_index_by_value( word_vectors[input_word], image_index, file_index) print(results)
def build_index_and_search_through_it(images_features, file_index): # Decide whether to do only image search or hybrid search if not features_from_new_model_boolean: # This is pure image search image_index = vector_search.index_features(images_features, dims=4096) search_key = get_index(input_image, file_index) results = vector_search.search_index_by_key(search_key, image_index, file_index) print(results) st.write('\n'.join('- `%s`' % elt for elt in results)) demo.show_top_n(9, results, search_by_img=False) else: word_vectors = vector_search.load_glove_vectors(glove_path) # If we are searching for tags for an image if not input_word: # Work on a single image instead of indexing search_key = get_index(input_image, file_index) word_index, word_mapping = vector_search.build_word_index(word_vectors) results = vector_search.search_index_by_value(images_features[search_key], word_index, word_mapping) for result in results: st.write(result) image_index = vector_search.index_features(images_features, dims=300) results = vector_search.search_index_by_value(word_vectors[result[1]], image_index, file_index) st.write('\n'.join('- `%s`' % elt for elt in results)) demo.show_top_n(9, results, search_by_img=False) # If we are using words to search through our images else: image_index = vector_search.index_features(images_features, dims=300) st.write(word_vectors[input_word]) results = vector_search.search_index_by_value(word_vectors[input_word], image_index, file_index) st.write('\n'.join('- `%s`' % elt for elt in results)) demo.show_top_n(9, results, search_by_img=False) print(results)
def index_images(folder, features_path, mapping_path, model, features_from_new_model_boolean, glove_path): print ("Now indexing images...") # Use word vectors if leveraging the new model if features_from_new_model_boolean: word_vectors=vector_search.load_glove_vectors(glove_path) else: word_vectors=[] # Use utiliy function _, _, paths = load_paired_img_wrd( folder=folder, word_vectors=word_vectors, use_word_vectors=features_from_new_model_boolean) images_features, file_index = vector_search.generate_features(paths, model) vector_search.save_features(features_path, images_features, mapping_path, file_index) return images_features, file_index
def load_images_vectors_paths(glove_model_path, data_path): word_vectors = vector_search.load_glove_vectors(glove_model_path) images, vectors, image_paths = load_paired_img_wrd(data_path, word_vectors) return images, vectors, image_paths, word_vectors
help='number of epochs to train on', default=50) return par if __name__ == "__main__": parser = build_parser() options = parser.parse_args() model_save_path = options.model_save_path checkpoint_path = options.checkpoint_path glove_path = options.glove_path dataset_path = options.dataset_path num_epochs = options.num_epochs word_vectors = vector_search.load_glove_vectors(glove_path) images, vectors, image_paths = load_paired_img_wrd(dataset_path, word_vectors) x, y = shuffle(images, vectors, random_state=2) X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2) checkpointer = ModelCheckpoint(filepath=checkpoint_path, verbose=1, save_best_only=True) custom_model = vector_search.setup_custon_model() custom_model.fit(X_train, y_train, validation_data=(X_test, y_test),
st.header("Dataset") st.subheader("Loading the data") st.write(""" Let's start by loading our dataset, which consists of a total of a **1000 images**, divided in **20 classes** with 50 images for each. This dataset can be found [here](http://vision.cs.uiuc.edu/pascal-sentences/). *Credit to Cyrus Rashtchian, Peter Young, Micah Hodosh, and Julia Hockenmaier.* In addition, we load [GloVe](https://nlp.stanford.edu/projects/glove/) vectors pre-trained on Wikipedia, which we will use when we incorporate text. """) with st.echo(): word_vectors = vector_search.load_glove_vectors("models/glove.6B") images, vectors, image_paths = load_paired_img_wrd( 'dataset', word_vectors) st.write("Here is what our load function looks like:") show_source(load_paired_img_wrd) all_labels = [fold.split("/")[1] for fold in image_paths] st.write("Here is a list of our classes:", '\n'.join('`%s`' % elt for elt in sorted(list(set(all_labels))))) st.write( "We now have a tensor of images of size %s, of word vectors of size %s, and a list of corresponding file " "paths." % (images.shape, vectors.shape)) st.subheader("Visualizing the data") st.write( "Let's see what our data looks like, here is one example image from each class"