def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) source_path = "db" source_paths = np.array( list( filter(lambda path: os.path.splitext(path)[1] in ['.jpg', '.jpeg'], np.array(os.listdir(source_path))))) # ================================================ # Read images and convert them to feature vectors # ================================================ imgs, filename_heads, X = [], [], [] for f in [sys.argv[1]]: # Process filename filename = os.path.splitext(f) # filename in directory head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue # Read image file img = image.load_img(f, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images # =========================== # Find k-nearest images to each image # =========================== n_neighbours = 3 pre_X = np.load('feature_matrix.npy') knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(pre_X) n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] for ind_query in range(n_imgs): # Find top-k closest image feature vectors to each vector distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) print(json.dumps(source_paths[indices].flatten().tolist())) print(distances)
def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ print("Loading pre-trained model...") caffe.set_mode_cpu() model = Model(caffe_root) model.model_init() # ================================================ # Read images and convert them to feature vectors # ================================================ inputdata_shape = model.get_input_datashape() transformer = get_transformer(inputdata_shape, caffe_root) # print('inputdata shape',inputdata_shape) imgs, filename_heads, X = [], [], [] path = "../db" print("Reading images from '{}' directory...\n".format(path)) for f in os.listdir(path): # Process filename filename = os.path.splitext(f) # filename in directory filename_full = os.path.join(path,f) # full path filename head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue print('read', head) # Read image file image = caffe.io.load_image(filename_full) transformed_image = transformer.preprocess('data', image) imgs.append(np.array(image)) # image filename_heads.append(head) # filename head # Pre-process for model input features = model.get_feature(transformed_image) # features X.append(features.copy()) # append feature extractor # all X are the same.... X = np.array(X) # feature vectors imgs = np.array(imgs) # images print("imgs.shape = {}".format(imgs.shape)) print("X_features.shape = {}\n".format(X.shape)) # =========================== # Find k-nearest images to each image # =========================== n_neighbours = 5 + 1 # +1 as itself is most similar knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(X) # ================================================== # Plot recommendations for each image in database # ================================================== output_rec_dir = os.path.join("../output", "rec") if not os.path.exists(output_rec_dir): os.makedirs(output_rec_dir) n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] for ind_query in range(n_imgs): # Find top-k closest image feature vectors to each vector print("[{}/{}] Plotting similar image recommendations for: {}".format(ind_query+1, n_imgs, filename_heads[ind_query])) distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) # Plot recommendations rec_filename = os.path.join(output_rec_dir, "{}_rec.png".format(filename_heads[ind_query])) x_query_plot = imgs[ind_query].reshape((-1, ypixels, xpixels, 3)) x_answer_plot = imgs[indices].reshape((-1, ypixels, xpixels, 3)) plot_query_answer(x_query=x_query_plot, x_answer=x_answer_plot[1:], # remove itself filename=rec_filename) # =========================== # Plot tSNE # =========================== output_tsne_dir = os.path.join("../output") if not os.path.exists(output_tsne_dir): os.makedirs(output_tsne_dir) tsne_filename = os.path.join(output_tsne_dir, "tsne.png") print("Plotting tSNE to {}...".format(tsne_filename)) plot_tsne(imgs, X, tsne_filename)
def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ print("Loading VGG19 pre-trained model...") base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) # ================================================ # Read images and convert them to feature vectors # ================================================ imgs, filename_heads, X = [], [], [] path = "db" print("Reading images from '{}' directory...".format(path)) for f in os.listdir(path): # Process filename filename = os.path.splitext(f) # filename in directory filename_full = os.path.join(path, f) # full path filename head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue # Read image file img = image.load_img(filename_full, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images print("imgs.shape = {}".format(imgs.shape)) print("X_features.shape = {}".format(X.shape)) # =========================== # Find k-nearest images to each image # =========================== n_neighbours = 5 + 1 # +1 as itself is most similar knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(X) # ================================================== # Plot recommendations for each image in database # ================================================== output_rec_dir = os.path.join("output", "rec") if not os.path.exists(output_rec_dir): os.makedirs(output_rec_dir) n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] for ind_query in range(n_imgs): # Find top-k closest image feature vectors to each vector print("[{}/{}] Plotting similar image recommendations for: {}".format( ind_query + 1, n_imgs, filename_heads[ind_query])) distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) # Plot recommendations rec_filename = os.path.join( output_rec_dir, "{}_rec.png".format(filename_heads[ind_query])) x_query_plot = imgs[ind_query].reshape((-1, ypixels, xpixels, 3)) x_answer_plot = imgs[indices].reshape((-1, ypixels, xpixels, 3)) plot_query_answer( x_query=x_query_plot, x_answer=x_answer_plot[1:], # remove itself filename=rec_filename) # =========================== # Plot tSNE # =========================== output_tsne_dir = os.path.join("output") if not os.path.exists(output_tsne_dir): os.makedirs(output_tsne_dir) tsne_filename = os.path.join(output_tsne_dir, "tsne.png") print("Plotting tSNE to {}...".format(tsne_filename)) plot_tsne(imgs, X, tsne_filename)
def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ print("Loading VGG19 pre-trained model...") base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) # ================================================ # Read images and convert them to feature vectors # ================================================ imgs, filename_heads, X = [], [], [] path = "db" print("Reading images from '{}' directory...\n".format(path)) for f in os.listdir(path): # Process filename filename = os.path.splitext(f) # filename in directory filename_full = os.path.join(path,f) # full path filename head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue # Read image file img = image.load_img(filename_full, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images print("imgs.shape = {}".format(imgs.shape)) print("X_features.shape = {}\n".format(X.shape)) # =========================== # Find k-nearest images to each image # =========================== n_neighbours = 5 + 1 # +1 as itself is most similar knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(X) # ================================================== # Plot recommendations for each image in database # ================================================== output_rec_dir = os.path.join("output", "rec") if not os.path.exists(output_rec_dir): os.makedirs(output_rec_dir) n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] for ind_query in range(n_imgs): # Find top-k closest image feature vectors to each vector print("[{}/{}] Plotting similar image recommendations for: {}".format(ind_query+1, n_imgs, filename_heads[ind_query])) distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) # Plot recommendations rec_filename = os.path.join(output_rec_dir, "{}_rec.png".format(filename_heads[ind_query])) x_query_plot = imgs[ind_query].reshape((-1, ypixels, xpixels, 3)) x_answer_plot = imgs[indices].reshape((-1, ypixels, xpixels, 3)) plot_query_answer(x_query=x_query_plot, x_answer=x_answer_plot[1:], # remove itself filename=rec_filename) # =========================== # Plot tSNE # =========================== output_tsne_dir = os.path.join("output") if not os.path.exists(output_tsne_dir): os.makedirs(output_tsne_dir) tsne_filename = os.path.join(output_tsne_dir, "tsne.png") print("Plotting tSNE to {}...".format(tsne_filename)) plot_tsne(imgs, X, tsne_filename)
def get_recommendations(): print("Loading the VGG19 model") base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) # Read images and convert them to feature vectors imgs, filename_heads, X = [], [], [] path = os.path.join("data", "final") print("Reading the images from '{}' directory...\n".format(path)) for f in os.listdir(path): # Process filename filename = os.path.splitext(f) # filename in directory filename_full = os.path.join(path, f) # full path filename head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue # Read image file img = image.load_img(filename_full, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images print("imgs.shape = {}".format(imgs.shape)) print("X_features.shape = {}\n".format(X.shape)) # Find k-nearest images to each image n_neighbours = 6 + 1 # +1 as itself is most similar knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(X) # return the recommendations for each painting in the form of a dictionary output_rec_dir = os.path.join("output", "recommendations") n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] recommendations = {} for ind_query in range(n_imgs): # Find k closest image feature vectors to each vector print("[{}/{}] finding your recommendations for painting {}".format( ind_query + 1, n_imgs, filename_heads[ind_query])) distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) indices = indices[0][ 1:] #remove the first painting (as it's obviously the closet one) wildcard = np.array([ random.randrange(1, n_imgs) for _ in range(3) ]) #(add 3 random paintings in the next suggestion) indices = np.concatenate((indices, wildcard)) indices = [filename_heads[index] for index in indices] recommendations.update({filename_heads[ind_query]: indices}) return recommendations