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 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 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): 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 __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 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')
import tensorflow as tf from netvlad_tf.net_from_mat import netFromMat from netvlad_tf.nets import defaultCheckpoint tf.reset_default_graph() layers = netFromMat() saver = tf.train.Saver() sess = tf.Session() tf.global_variables_initializer().run(session=sess) saver.save(sess, defaultCheckpoint())
import cv2 import numpy as np import tensorflow as tf import netvlad_tf.net_from_mat as nfm import netvlad_tf.nets as nets from keras.models import Sequential import netvlad_keras import os # This script loads the tensorflow model and transforms it into a Keras model os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"] = "" checkpoint = nets.defaultCheckpoint() # start tensorflow session with tf.Session() as sess: # import graph saver = tf.train.import_meta_graph(checkpoint + ".meta") # load weights for graph saver.restore(sess, checkpoint) # get all global variables (including model variables) vars_global = tf.global_variables() # get their name and value and put them into dictionary sess.as_default() model_vars = {}