def execute(file, curr, rank): # Load sample image # file = utils.DATA_DIR + 'sample.jpg' img = image.load_img(file) # Resize scale = utils.IMG_SIZE / max(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), str(curr), "is processing...") img = img.resize(new_size) # Mean substraction x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = utils.preprocess_image(x) # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2]) regions = rmac_regions(Wmap, Hmap, 3) # print('Loading RMAC model...') model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions), rank) # Compute RMAC vector # print('Extracting RMAC from image...') RMAC = model.predict([x, np.expand_dims(regions, axis=0)]) return RMAC
def load_model(x, multi='parallel'): x = np.array(x) print('cur batch tensor shape', x.shape) x = utils.preprocess_image(x) # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(x.shape[2], x.shape[1]) #image_dim_ordering tf #Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2]) #image_dim_ordering th regions = rmac_regions(Wmap, Hmap, 3) print('Loading RMAC model...') print(x.shape[1], x.shape[2], x.shape[3], len(regions)) if PARALLEL: if multi == 'single': model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) return model, regions else: with tf.device('/cpu:0'): model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) parallel_model = multi_gpu_model(model, gpus=num_gpu) return parallel_model, regions else: model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) return model, regions
def ComputeRMAC(x): Wmap, Hmap = get_size_vgg_feat_map( x.shape[2], x.shape[1]) #Wmap, Hmap means extract from a root 6 times regions = rmac_regions(Wmap, Hmap, 10) # Load RMAC model model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) # Compute RMAC vector RMAC = model.predict([x, np.expand_dims(regions, axis=0)]) print(RMAC) RMAC = np.reshape(RMAC, (-1, 512)) print(RMAC.shape) return RMAC
def rmac_holidays(img): x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(x.shape[1], x.shape[2]) # warning can be ignored regions = rmac_regions(Wmap, Hmap, 3) print('Loading RMAC model...') model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) # Compute RMAC vector print('Extracting RMAC from image...') RMAC1 = model.predict([x, np.expand_dims(regions, axis=0)]) return RMAC1
def describe(img): K.clear_session() # Resize scale = utils.IMG_SIZE / max(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) img = img.resize(new_size) # Mean substraction x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = utils.preprocess_image(x) # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2]) regions = rmac_regions(Wmap, Hmap, 3) model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) # Compute RMAC vector RMAC = model.predict([x, np.expand_dims(regions, axis=0)]) return RMAC
# plt.draw() # plt.pause(5) # plt.close() # Mean substraction x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = utils.preprocess_image(x) # Show preprocessed image # y = np.squeeze(x, axis=0) # img = image.array_to_img(y) # plt.imshow(img) # plt.draw() # plt.pause(5) # plt.close() # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2]) regions = rmac_regions(Wmap, Hmap, 3) print('Loading RMAC model...') model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions)) # Compute RMAC vector print('Extracting RMAC from image...') RMAC = model.predict([x, np.expand_dims(regions, axis=0)]) print('RMAC size: %s' % RMAC.shape[1]) print(RMAC) print('Done!')
def infer(queries, _): test_path = DATASET_PATH + '/test/test_data' references = [ os.path.join(test_path, 'reference', path) for path in os.listdir(os.path.join(test_path, 'reference')) ] queries = [v.split('/')[-1].split('.')[0] for v in queries] references = [v.split('/')[-1].split('.')[0] for v in references] queries.sort() references.sort() # Load RMAC model Wmap, Hmap = get_size_vgg_feat_map(224, 224) regions = rmac_regions(Wmap, Hmap, 3) rmac_model = rmac(model, len(regions)) rmac_model.summary() test_datagen = ImageDataGenerator(dtype='float32') query_generator = test_datagen.flow_from_directory(directory=test_path, target_size=(224, 224), classes=['query'], color_mode="rgb", batch_size=1, class_mode=None, shuffle=False) query_vecs = rmac_model.predict_generator(multi_input( query_generator, regions), steps=len(query_generator), verbose=1) reference_generator = test_datagen.flow_from_directory( directory=test_path, target_size=(224, 224), classes=['reference'], color_mode="rgb", batch_size=1, class_mode=None, shuffle=False) reference_vecs = rmac_model.predict_generator( multi_input(reference_generator, regions), steps=len(reference_generator), verbose=1) # l2 normalization query_vecs = l2_normalize(query_vecs) reference_vecs = l2_normalize(reference_vecs) # Calculate cosine similarity sim_matrix = np.dot(query_vecs, reference_vecs.T) indices = np.argsort(sim_matrix, axis=1) indices = np.flip(indices, axis=1) retrieval_results = {} for (i, query) in enumerate(queries): ranked_list = [references[k] for k in indices[i]] ranked_list = ranked_list[:1000] retrieval_results[query] = ranked_list print('done') return list( zip(range(len(retrieval_results)), retrieval_results.items()))