Loop detector class for detecting loop closures in image sequences
| 72 | |
| 73 | |
| 74 | class LoopDetector: |
| 75 | """Loop detector class for detecting loop closures in image sequences""" |
| 76 | |
| 77 | def __init__(self, image_dir, output="loop_closures.txt", config=None): |
| 78 | """Initialize the loop detector |
| 79 | |
| 80 | Args: |
| 81 | image_dir: Directory path containing images |
| 82 | ckpt_path: Model checkpoint path |
| 83 | image_size: Image resize dimensions [height width] |
| 84 | batch_size: Batch size for processing |
| 85 | similarity_threshold: Similarity threshold for loop closure |
| 86 | top_k: Number of nearest neighbors to check for each image |
| 87 | use_nms: Whether to use Non-Maximum Suppression (NMS) filtering |
| 88 | nms_threshold: NMS threshold for minimum frame difference between loop pairs |
| 89 | output: Output file path |
| 90 | """ |
| 91 | self.config = config |
| 92 | self.image_dir = image_dir |
| 93 | self.ckpt_path = self.config["Weights"]["SALAD"] |
| 94 | self.image_size = self.config["Loop"]["SALAD"]["image_size"] |
| 95 | self.batch_size = self.config["Loop"]["SALAD"]["batch_size"] |
| 96 | self.similarity_threshold = self.config["Loop"]["SALAD"]["similarity_threshold"] |
| 97 | self.top_k = self.config["Loop"]["SALAD"]["top_k"] |
| 98 | self.use_nms = self.config["Loop"]["SALAD"]["use_nms"] |
| 99 | self.nms_threshold = self.config["Loop"]["SALAD"]["nms_threshold"] |
| 100 | self.output = output |
| 101 | |
| 102 | self.model = None |
| 103 | self.device = None |
| 104 | self.image_paths = None |
| 105 | self.descriptors = None |
| 106 | self.loop_closures = None |
| 107 | |
| 108 | def _input_transform(self, image_size=None): |
| 109 | """Create image transformation function""" |
| 110 | MEAN = [0.485, 0.456, 0.406] |
| 111 | STD = [0.229, 0.224, 0.225] |
| 112 | if image_size: |
| 113 | return T.Compose( |
| 114 | [ |
| 115 | T.Resize(image_size, interpolation=T.InterpolationMode.BILINEAR), |
| 116 | T.ToTensor(), |
| 117 | T.Normalize(mean=MEAN, std=STD), |
| 118 | ] |
| 119 | ) |
| 120 | else: |
| 121 | return T.Compose([T.ToTensor(), T.Normalize(mean=MEAN, std=STD)]) |
| 122 | |
| 123 | def load_model(self): |
| 124 | """Load model""" |
| 125 | model = VPRModel( |
| 126 | backbone_arch="dinov2_vitb14", |
| 127 | backbone_config={ |
| 128 | "num_trainable_blocks": 4, |
| 129 | "return_token": True, |
| 130 | "norm_layer": True, |
| 131 | }, |