cvg / Hierarchical-Localization

Visual localization made easy with hloc
Apache License 2.0
3.23k stars 599 forks source link

Reconstruction - Since hloc v1.3, largest model data is sometimes not selected #422

Open ricmatsui opened 2 months ago

ricmatsui commented 2 months ago

As part of run_reconstruction, the data for images.bin, cameras.bin, and points3D.bin for the largest reconstruction is supposed to be moved to sfm_dir. However, this does not happen reliably when COLMAP produces multiple models.

This is because the models in the numbered folders created by COLMAP (models/0, models/1, etc.) are not guaranteed to be in the same order as the reconstructions list returned by COLMAP.

COLMAP numbers the folders based on the count of points3D so that the folder 0 always has the model with the largest number of points and this is different than the order of the reconstructions list: https://github.com/colmap/colmap/blob/db14f5c59bdaa5239b804ff4623f2fb8e5bb2a46/src/colmap/scene/reconstruction_manager.cc#L60-L83

void ReconstructionManager::Write(const std::string& path) const {
  std::vector<std::pair<size_t, size_t>> recon_sizes(reconstructions_.size());
  for (size_t i = 0; i < reconstructions_.size(); ++i) {
    recon_sizes[i] = std::make_pair(i, reconstructions_[i]->NumPoints3D());
  }
  std::sort(recon_sizes.begin(),
            recon_sizes.end(),
            [](const std::pair<size_t, size_t>& first,
               const std::pair<size_t, size_t>& second) {
              return first.second > second.second;
            });

  for (size_t i = 0; i < reconstructions_.size(); ++i) {
    const std::string reconstruction_path = JoinPaths(path, std::to_string(i));
    CreateDirIfNotExists(reconstruction_path);
    reconstructions_[recon_sizes[i].first]->Write(reconstruction_path);
  }
}

This difference causes an issue in run_reconstruction where the data that is moved does not correspond to the model of the largest_index:

https://github.com/cvg/Hierarchical-Localization/blob/b21ff203ead5a3257df3b6806214ff605e0c01c5/hloc/reconstruction.py#L99-L103

Forcing largest_index to always be 0 resolves the issue, but is different behavior than before.

It looks like this issue was introduced as part of hloc 1.3 because before hloc 1.3, the logic involved iterating directly over the model folders https://github.com/cvg/Hierarchical-Localization/commit/55e6cde6d3b98cd97c3e48aef02f40e1f4fe3875#diff-1415a09ff92c2b9e2091340b072a454b63b9b7422a63f363dc6c2dcce7349769:

https://github.com/cvg/Hierarchical-Localization/blob/9bad6b46466fae34f1d6215c911e19b5a2e24dea/hloc/reconstruction.py#L81-L96