def __init__(self): self.dim = rospy.get_param('~dim', 4096) self.scale = rospy.get_param('~scale', 1.0) rospy.loginfo("Parameter dim=%d", self.dim) rospy.loginfo("Parameter scale=%d", self.scale) tf.reset_default_graph() self.image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) self.net_out = nets.vgg16NetvladPca(self.image_batch) self.saver = tf.train.Saver() self.sess = tf.Session() self.saver.restore(self.sess, nets.defaultCheckpoint()) self.pub = rospy.Publisher('netvlad_descriptor', GlobalDescriptor, queue_size=1) self.bridge = CvBridge() self.image_sub = rospy.Subscriber("image", Image, self.callback, queue_size=1)
def test(): pub = rospy.Publisher('chatter', String, queue_size=10) rospy.init_node('talker', anonymous=True) rate = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): tf.reset_default_graph() image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) # images = [cv2.imread(file) for file in glob.glob("/Users/benjaminramtoula/Documents/Cours/POLYMTL/MISTLAB/SLAM/datasets/kitti/00_color/image_2/00000*.png")] # images = [cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for image in images] # print(images) inim = cv2.imread(nfm.exampleImgPath()) batch = np.expand_dims(inim, axis=0) # inim = cv2.imread(nfm.exampleImgPath()) # inim = cv2.cvtColor(inim, cv2.COLOR_BGR2RGB) # batch = [np.expand_dims(inim, axis=0) for inim in images] result = sess.run(net_out, feed_dict={image_batch: batch}) hello_str = "hello world %s" % rospy.get_time() rospy.loginfo(result) pub.publish(result) rate.sleep()
def gen_frozen_graph(): tf.reset_default_graph() image_batch = tf.placeholder(dtype=tf.float32, shape=placeholder_shape) net_out = nets.vgg16NetvladPca(image_batch) print('-------------------', net_out.name) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, nets.defaultCheckpoint()) batch = np.ones(input_shape, dtype=np.float32) result = sess.run(net_out, feed_dict={image_batch: batch}) with open("tf_python_checkpoint_output.txt", "w") as f: for r in result.flatten(): f.write("%.8f\n" % r) frozen_graph = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, output_node_names=output_node_name) frozen_graph = tf.compat.v1.graph_util.remove_training_nodes( frozen_graph) graph_io.write_graph(frozen_graph, '', output_graph_name, as_text=False) print('gen_frozen_graph done.')
def compute_map_features(ref_map_images): ref_desc=[] tf.reset_default_graph() image_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) for img in ref_map_images: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (640,480), interpolation=cv2.INTER_CUBIC) batch = np.expand_dims(img, axis=0) t1=time.time() desc = sess.run(net_out, feed_dict={image_batch: batch})#[0][0:1024] print('Encode Time: ', time.time()-t1) ref_desc.append(desc) print(desc.shape) return ref_desc
def __init__(self, is_grayscale=False): self.is_grayscale = is_grayscale if is_grayscale: self.tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 1]) else: self.tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) self.net_out = nets.vgg16NetvladPca(self.tf_batch) saver = tf.train.Saver() self.sess = tf.Session() saver.restore(self.sess, nets.defaultCheckpoint())
def getSessVars(): tf.reset_default_graph() image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) return sess, net_out, image_batch
def __init__(self, is_grayscale=False): self.is_grayscale = is_grayscale if is_grayscale: self.tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 1]) else: self.tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) self.net_out = nets.vgg16NetvladPca(self.tf_batch) saver = tf.train.Saver() self.sess = tf.Session() saver.restore(self.sess, nets.defaultCheckpoint())
def testVgg16NetvladPca(): tf.reset_default_graph() image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) saver.restore(sess, nets.defaultCheckpoint()) print('Initialization Finished!') overwrite = False data_name = "corridor" dataset_id = 2 data_path = f"/home/frog/Desktop/lifelongSLAM/Examples/RGB-D/data/{data_name}-1-package/{data_name}-1-{dataset_id}" out_put_path = data_path + "/vlad" if not os.path.exists(out_put_path): os.mkdir(out_put_path) data_names = open(os.path.join(data_path, "color.txt")) for i_data_name in data_names: i_time = i_data_name.strip('\n').split(' ')[0] i_name = i_data_name.strip('\n').split(' ')[-1] im2dl = os.path.join(data_path, i_name) if (os.path.isfile(im2dl) and np.all(cv2.imread(im2dl) != None)): start = time.time() dl2vlad = os.path.join(out_put_path, f"{i_time}.txt") if os.path.exists(dl2vlad) and not overwrite: continue img = cv2.imread(im2dl) if isinstance(img, type(np.nan)): continue #if img == None: # continue img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) batch = np.expand_dims(img, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: batch}) result = sess.run(net_out, feed_dict={image_batch: batch}) end = time.time() print('Took %f seconds' % (end - start)) #dl2vlad = "/home/wangrong/netvlad_tf_open-master/result.txt" #4096 np.savetxt(dl2vlad, result, fmt='%f')
def compute_query_desc(image_query): image_query=cv2.resize(image_query, (640,480), interpolation=cv2.INTER_CUBIC) tf.reset_default_graph() image_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) batch = np.expand_dims(image_query, axis=0) query_desc = sess.run(net_out, feed_dict={image_batch: batch})#[0][0:1024] print(query_desc.shape) return query_desc
def testVgg16NetvladPca(self): ''' Need example_stats.mat in matlab folder, which can be generated with get_example_stats.m. Also need translated checkpoint, can be generated with mat_to_checkpoint.py. ''' tf.reset_default_graph() image_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) inim = cv2.imread(nfm.exampleImgPath()) #print(nfm.exampleImgPath()) #/home/wangrong/netvlad_tf_open-master/example.jpg inim = cv2.cvtColor(inim, cv2.COLOR_BGR2RGB) batch = np.expand_dims(inim, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: batch}) t = time.time() result = sess.run(net_out, feed_dict={image_batch: batch}) #print(result.size) #4096 #print(result) #[[-0.00681984 -0.00276458 -0.00730957 ... 0.01022749 -0.00939475 #-0.0027039 ]] print('Took %f seconds' % (time.time() - t)) #%% Load Matlab results mat = scio.loadmat(nfm.exampleStatPath(), struct_as_record=False, squeeze_me=True) #print(nfm.exampleStatPath()) #/home/wangrong/netvlad_tf_open-master/matlab/example_stats.mat mat_outs = mat['outs'] #%% Compare final output out_diff = np.abs(mat_outs[-1] - result) self.assertLess(np.linalg.norm(out_diff), 0.0053) print('Error of final vector is %f' % np.linalg.norm(out_diff))
def testVgg16NetvladPca(self): tf.reset_default_graph() image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) rimg = cv2.imread('data/images/office_1.png') rimg = cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB) rbatch = np.expand_dims(rimg, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: rbatch}) rt = time.time() rresult = sess.run(net_out, feed_dict={image_batch: rbatch}) print('Took %f seconds' % (time.time() - rt)) qimg = cv2.imread('data/images/office_3.png') qimg = cv2.cvtColor(qimg, cv2.COLOR_BGR2RGB) qbatch = np.expand_dims(qimg, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: qbatch}) qt = time.time() qresult = sess.run(net_out, feed_dict={image_batch: qbatch}) print('Took %f seconds' % (time.time() - qt)) #%% Compare final output out_diff = np.abs(qresult - rresult) print('Image presentation distance: %f', np.linalg.norm(out_diff))
def testVgg16NetvladPca(self): ''' Need example_stats.mat in matlab folder, which can be generated with get_example_stats.m. Also need translated checkpoint, can be generated with mat_to_checkpoint.py. ''' tf.compat.v1.disable_eager_execution() tf.compat.v1.reset_default_graph() image_batch = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.compat.v1.train.Saver() sess = tf.compat.v1.Session() saver.restore(sess, nets.defaultCheckpoint()) inim = cv2.imread(nfm.exampleImgPath()) inim = cv2.cvtColor(inim, cv2.COLOR_BGR2RGB) batch = np.expand_dims(inim, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: batch}) t = time.time() result = sess.run(net_out, feed_dict={image_batch: batch}) print('Took %f seconds' % (time.time() - t)) #%% Load Matlab results mat = scio.loadmat(nfm.exampleStatPath(), struct_as_record=False, squeeze_me=True) mat_outs = mat['outs'] #%% Compare final output out_diff = np.abs(mat_outs[-1] - result) self.assertLess(np.linalg.norm(out_diff), 0.0053) print('Error of final vector is %f' % np.linalg.norm(out_diff))
def testVgg16NetvladPca(self): ''' Need example_stats.mat in matlab folder, which can be generated with get_example_stats.m. Also need translated checkpoint, can be generated with mat_to_checkpoint.py. ''' tf.reset_default_graph() image_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) inim = cv2.imread(nfm.exampleImgPath()) inim = cv2.cvtColor(inim, cv2.COLOR_BGR2RGB) batch = np.expand_dims(inim, axis=0) #%% Generate TF results for _ in range(2): sess.run(net_out, feed_dict={image_batch: batch}) t = time.time() result = sess.run(net_out, feed_dict={image_batch: batch}) print('Took %f seconds' % (time.time() - t)) #%% Load Matlab results mat = scio.loadmat(nfm.exampleStatPath(), struct_as_record=False, squeeze_me=True) mat_outs = mat['outs'] #%% Compare final output out_diff = np.abs(mat_outs[-1] - result) self.assertLess(np.linalg.norm(out_diff), 0.0053) print('Error of final vector is %f' % np.linalg.norm(out_diff))
import cv2 import numpy as np import tensorflow as tf import netvlad_tf.net_from_mat as nfm import netvlad_tf.nets as nets tf.compat.v1.reset_default_graph image_batch = tf.compat.v1.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) net_out = nets.vgg16NetvladPca(image_batch) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, nets.defaultCheckpoint()) inim = cv2.imread(nfm.exampleImgPath()) inim = cv2.cvtColor(inim, cv2.COLOR_BGR2RGB) batch = np.expand_dims(inim, axis=0) result = sess.run(net_out, feed_dict={image_batch: batch})
def __init__(self): self.images_l_queue = collections.deque() self.images_r_queue = collections.deque() self.images_rgb_queue = collections.deque() self.geometric_feats = collections.deque() self.images_rgb_kf = collections.deque() self.timestamps_kf = collections.deque() self.local_descriptors = [] self.nb_descriptors_already_sent = 0 self.received_descriptors = [] self.separators_found = collections.deque() self.local_kf_already_used = collections.deque() self.other_kf_already_used = collections.deque() self.frames_kept_pairs_ignored = collections.deque() self.nb_kf_skipped = 0 self.original_ids_of_kf = collections.deque() self.orig_id_last_img_in_q = 0 self.nb_kf_odom = 0 self.kf_ids_of_frames_kept = collections.deque() tf.reset_default_graph() self.image_batch = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3]) self.net_out = nets.vgg16NetvladPca(self.image_batch) saver = tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) saver.restore(self.sess, nets.defaultCheckpoint()) self.bridge = CvBridge() self.local_robot_id = rospy.get_param("local_robot_id") self.other_robot_id = rospy.get_param("other_robot_id") self.log_gps = rospy.get_param("log_gps") if self.log_gps: from dji_sdk.msg import GlobalPosition rospy.Subscriber("gps_topic", GlobalPosition, self.save_gps_queue) self.gps_data_queue = collections.deque() self.s_add_seps_pose_graph = rospy.ServiceProxy( 'add_separators_pose_graph', ReceiveSeparators) self.s_get_feats = rospy.ServiceProxy('get_features_and_descriptor', GetFeatsAndDesc) self.logs_location = rospy.get_param("logs_location") self.send_estimates_of_poses = rospy.get_param( "use_estimates_of_poses") if self.send_estimates_of_poses: self.s_get_pose_estimates = rospy.ServiceProxy( 'get_pose_estimates', PoseEstimates) # Read params self.netvlad_distance = rospy.get_param("netvlad_distance") self.netvlad_dimensions = rospy.get_param("netvlad_dimensions") self.netvlad_batch_size = rospy.get_param("netvlad_batch_size") self.netvlad_max_matches_nb = rospy.get_param("netvlad_max_matches_nb") self.number_of_kf_skipped = rospy.get_param("number_of_kf_skipped") # Log params to file with open( self.logs_location + 'params_' + str(self.local_robot_id) + '.txt', 'a') as file: file.write('netvlad_distance: ' + str(self.netvlad_distance) + '\nnetvlad_dimensions: ' + str(self.netvlad_dimensions) + '\nnetvlad_batch_size: ' + str(self.netvlad_batch_size) + '\nnetvlad_max_matches_nb: ' + str(self.netvlad_max_matches_nb) + '\nnumber_of_kf_skipped: ' + str(self.number_of_kf_skipped) + '\nseparators_min_inliers: ' + str(rospy.get_param("separators_min_inliers")) + '\n')
def extract_kapture_global(kapture_root, config, output_dir='', overwrite=False): logging.info('Extracting NetVLAD features with configuration:\n', config) # use kapture io to identify image paths and loop kdata = kapture_from_dir(kapture_root, matches_pairsfile_path=None, skip_list= [kapture.Matches, kapture.Points3d, kapture.Observations, kapture.Keypoints, kapture.Descriptors]) assert kdata.records_camera is not None export_dir = output_dir if output_dir else kapture_root # root of output directory for features os.makedirs(export_dir, exist_ok=True) image_list = [filename for _, _, filename in kapture.flatten(kdata.records_camera)] # resume extraction if some features exist try: # load features if there are any kdata.global_features = global_features_from_dir(export_dir, None) if kdata.global_features is not None and not overwrite: image_list = [name for name in image_list if name not in kdata.global_features] except FileNotFoundError: pass except: logging.exception("Error with importing existing global features.") # clear features first if overwriting if overwrite: delete_existing_kapture_files(export_dir, True, only=[kapture.GlobalFeatures]) if len(image_list) == 0: print('All features were already extracted') return else: print(f'Extracting NetVLAD features for {len(image_list)} images') # for the global descriptor type specification global_dtype = None if kdata.global_features is None else kdata.global_features.dtype global_dsize = None if kdata.global_features is None else kdata.global_features.dsize # setup network tf.reset_default_graph() if config['grayscale']: tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 1]) else: tf_batch = tf.placeholder( dtype=tf.float32, shape=[None, None, None, 3]) # load network and checkpoint net = nets.vgg16NetvladPca(tf_batch) saver = tf.train.Saver() sess = tf.Session() checkpoint = chkpt_path + '/' + config['checkpoint'] saver.restore(sess, checkpoint) for image_name in image_list: img_path = get_image_fullpath(kapture_root, image_name) if img_path.endswith('.txt'): args.images = open(img_path).read().splitlines() + args.images continue print(f"\nExtracting features for {img_path}") if config['grayscale']: image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) image = np.expand_dims( np.expand_dims(image, axis=0), axis=-1) else: image = cv2.imread(img_path, cv2.COLOR_BGR2RGB) image = np.expand_dims(image, axis=0) descriptor = sess.run(net, feed_dict={tf_batch: image})[:, :config['pca_dim']] descriptor = np.squeeze(descriptor) # write global descriptor type specification if global_dtype is None: global_dtype = descriptor.dtype global_dsize = len(descriptor) kdata.global_features = kapture.GlobalFeatures('netvlad', global_dtype, global_dsize) global_descriptors_config_abs_path = get_csv_fullpath(kapture.GlobalFeatures, export_dir) descriptors_to_file(global_descriptors_config_abs_path, kdata.global_features) else: assert kdata.global_features.type_name == "netvlad" assert kdata.global_features.dtype == descriptor.dtype assert kdata.global_features.dsize == len(descriptor) # get output paths global_descriptors_abs_path = get_global_features_fullpath(export_dir, image_name) image_global_features_to_file(global_descriptors_abs_path, descriptor) kdata.global_features.add(image_name) # sess.close() # close session before initializing again for next submap if not global_features_check_dir(kdata.global_features, export_dir): print('global feature extraction ended successfully but not all files were saved')