示例#1
0
    def __init__(self, num_features=2000, do_tf_logging=False):
        print('Using LfNetFeature2D')
        self.lock = RLock()

        self.model_base_path = kLfNetBasePath
        self.model_path = kLfNetModelPath

        self.lfnet_config = build_lfnet_config()
        print_options(self.lfnet_config, 'LFNET CONFIG')

        self.num_features = num_features
        self.lfnet_config.top_k = self.num_features

        set_tf_logging(do_tf_logging)

        print('==> Loading pre-trained network.')
        # Build Networks
        tf.reset_default_graph()

        self.photo_ph = tf.placeholder(
            tf.float32,
            [1, None, None, 1])  # input grayscale image, normalized by 0~1
        is_training = tf.constant(False)  # Always False in testing

        self.ops = build_networks(self.lfnet_config, self.photo_ph,
                                  is_training)

        tf_config = tf.ConfigProto()
        tf_config.gpu_options.allow_growth = True
        self.session = tf.Session(config=tf_config)
        self.session.run(tf.global_variables_initializer())

        # load model
        saver = tf.train.Saver()
        print('Load trained models...')

        if os.path.isdir(self.lfnet_config.model):
            checkpoint = tf.train.latest_checkpoint(self.lfnet_config.model)
            model_dir = self.lfnet_config.model
        else:
            checkpoint = self.lfnet_config.model
            model_dir = os.path.dirname(self.lfnet_config.model)

        if checkpoint is not None:
            print('Checkpoint', os.path.basename(checkpoint))
            print("[{}] Resuming...".format(time.asctime()))
            saver.restore(self.session, checkpoint)
        else:
            raise ValueError('Cannot load model from {}'.format(model_dir))
        print('==> Successfully loaded pre-trained network.')

        self.pts = []
        self.kps = []
        self.des = []
        self.frame = None
        self.keypoint_size = 20.  # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint
示例#2
0
    def __init__(self,
                 num_features=2000,
                 n_sample=2048,              #  Maximum number of sampled keypoints per octave
                 dense_desc=False,           #  Whether to use dense descriptor model
                 model_type='pb',                  
                 do_tf_logging=False):  
        print('Using ContextDescFeature2D')   
        self.lock = RLock()
        self.model_base_path= config.cfg.root_folder + '/thirdparty/contextdesc/'
        
        set_tf_logging(do_tf_logging)
        
        self.num_features = num_features
        self.n_sample = n_sample
        self.model_type = model_type
        self.dense_desc = dense_desc
        self.quantize = ContextDescFeature2D.quantize
        
        self.loc_model_path = self.model_base_path + 'pretrained/contextdesc++'
        self.reg_model_path = self.model_base_path + 'pretrained/retrieval_model'
            
        if self.model_type == 'pb':
            reg_model_path = os.path.join(self.reg_model_path, 'reg.pb')
            loc_model_path = os.path.join(self.loc_model_path, 'loc.pb')
            aug_model_path = os.path.join(self.loc_model_path, 'aug.pb')
        elif self.model_type == 'ckpt':
            reg_model_path = os.path.join(self.reg_model_path, 'model.ckpt-550000')
            loc_model_path = os.path.join(self.loc_model_path, 'model.ckpt-400000')
            aug_model_path = os.path.join(self.loc_model_path, 'model.ckpt-400000')
        else:
            raise NotImplementedError
        
        self.keypoint_size = 10  # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint        

        self.pts = []
        self.kps = []        
        self.des = []
        self.scales = []
        self.scores = []        
        self.frame = None 
        
        print('==> Loading pre-trained network.')
        self.ref_model = RegModel(reg_model_path) #get_model('reg_model')(reg_model_path)  #RegModel(reg_model_path)
        self.loc_model = LocModel(loc_model_path, **{'sift_desc': False,             # compute or not SIFT descriptor (we do not need them here!)
                                                    'n_feature': self.num_features,                                                     
                                                    'n_sample': self.n_sample,
                                                    'peak_thld': 0.04,
                                                    'dense_desc': self.dense_desc,
                                                    'upright': False})       
        self.aug_model = AugModel(aug_model_path, **{'quantz': self.quantize})         
        print('==> Successfully loaded pre-trained network.')
示例#3
0
    def __init__(self,
                 num_features=1000,
                 score_threshold=100,
                 do_tf_logging=False):
        print('Using DelfFeature2D')
        self.lock = RLock()

        set_tf_logging(do_tf_logging)

        # Parse DelfConfig proto.
        self.delf_config = delf_config_pb2.DelfConfig()
        with tf.gfile.FastGFile(delf_config_file, 'r') as f:
            text_format.Merge(f.read(), self.delf_config)
        self.delf_config.model_path = delf_model_path
        self.delf_config.delf_local_config.pca_parameters.mean_path = delf_mean_path
        self.delf_config.delf_local_config.pca_parameters.projection_matrix_path = delf_projection_matrix_path
        self.delf_config.delf_local_config.max_feature_num = num_features
        self.delf_config.delf_local_config.score_threshold = score_threshold
        print('DELF CONFIG\n:', self.delf_config)

        self.keypoint_size = 30  # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint

        self.image_scales = list(self.delf_config.image_scales)
        #print('image scales: ',self.image_scales)
        try:
            self.scale_factor = self.image_scales[1] / self.image_scales[0]
        except:
            self.scale_factor = np.sqrt(
                2)  # according to default config and the paper
        #print('scale_factor: ',self.scale_factor)
        #self.image_levels = np.round(-np.log(self.image_scales)/np.log(self.scale_factor)).astype(np.int32)
        #print('image levels: ',self.image_levels)

        self.session = None

        self.pts = []
        self.kps = []
        self.des = []
        self.scales = []
        self.scores = []
        self.frame = None

        print('==> Loading pre-trained network.')
        self.load_model()
        print('==> Successfully loaded pre-trained network.')
示例#4
0
    def __init__(self, net_name, do_tf_logging=True, flagCS=False):
        set_tf_logging(do_tf_logging)

        # from https://kobkrit.com/using-allow-growth-memory-option-in-tensorflow-and-keras-dc8c8081bc96
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
        #config.log_device_placement = True     # to log device placement (on which device the operation ran)
        sess = tf.Session(config=config)
        set_session(
            sess
        )  # set this TensorFlow session as the default session for Keras

        model, model_cen, pix_mean, pix_mean_cen = build_L2_net(net_name)
        self.flagCS = flagCS
        self.model = model
        self.model_cen = model_cen
        self.pix_mean = pix_mean
        self.pix_mean_cen = pix_mean_cen
示例#5
0
    def __init__(
            self,
            num_features=2000,
            num_levels=5,  # The number of downsample levels in the pyramid.
            scale_factor=2,  # The scale factor to extract patches before descriptor.
            scale_factor_levels=np.sqrt(
                2),  # The scale factor between the pyramid levels.
            do_cuda=True,
            do_tf_logging=False):
        print('Using KeyNetDescFeature2D')
        self.lock = RLock()
        self.model_base_path = config.cfg.root_folder + '/thirdparty/keynet/'

        set_tf_logging(do_tf_logging)

        self.do_cuda = do_cuda & torch.cuda.is_available()
        print('cuda:', self.do_cuda)
        device = torch.device("cuda:0" if self.do_cuda else "cpu")

        self.session = None

        self.keypoint_size = 8  # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint

        self.pts = []
        self.kps = []
        self.des = []
        self.scales = []
        self.scores = []
        self.frame = None

        keynet_config = build_keynet_config(self.model_base_path)
        self.keynet_config = keynet_config
        keynet_config.num_points = num_features
        keynet_config.pyramid_levels = num_levels
        keynet_config.scale_factor = scale_factor
        keynet_config.scale_factor_levels = scale_factor_levels

        print_options(self.keynet_config, 'KEYNET CONFIG')

        print('==> Loading pre-trained network.')
        self.load_model()
        print('==> Successfully loaded pre-trained network.')
示例#6
0
    def __init__(self, do_tf_logging=False):
        print('Using GeodescFeature2D')
        # mag_factor is how many times the original keypoint scale
        # is enlarged to generate a patch from a keypoint
        self.mag_factor = 3

        # inference batch size
        self.batch_size = 512
        self.process_all = True  # process all the patches at once

        self.model_base_path = config.cfg.root_folder + '/thirdparty/geodesc/'
        self.model_path = self.model_base_path + 'model/geodesc.pb'

        set_tf_logging(do_tf_logging)

        print('==> Loading pre-trained network.')
        # create deep feature extractor.
        self.graph = load_frozen_model(self.model_path, print_nodes=False)
        #sess = tf.Session(graph=graph)
        print('==> Successfully loaded pre-trained network.')