train_bow.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Approach 3: Local features
  2. # This script is used for generating a BOW vocabulary using
  3. # densely sampeled SIFT features on Lapse images.
  4. # See eval_bow.py for evaluation.
  5. import argparse
  6. import os
  7. import numpy as np
  8. from py.Dataset import Dataset
  9. from py.LocalFeatures import extract_descriptors, generate_dictionary_from_descriptors, generate_bow_features
  10. def main():
  11. parser = argparse.ArgumentParser(description="BOW train script")
  12. parser.add_argument("dataset_dir", type=str, help="Directory of the dataset containing all session folders")
  13. parser.add_argument("session_name", type=str, help="Name of the session to use for Lapse images (e.g. marten_01)")
  14. parser.add_argument("--clusters", type=int, help="Number of clusters / BOW vocabulary size", default=1024)
  15. parser.add_argument("--step_size", type=int, help="DSIFT keypoint step size. Smaller step size = more keypoints.", default=30)
  16. args = parser.parse_args()
  17. ds = Dataset(args.dataset_dir)
  18. session = ds.create_session(args.session_name)
  19. save_dir = f"./bow_train_NoBackup/{session.name}"
  20. # Lapse DSIFT descriptors
  21. lapse_dscs_file = os.path.join(save_dir, f"lapse_dscs_{args.step_size}.npy")
  22. dictionary_file = os.path.join(save_dir, f"bow_dict_{args.step_size}_{args.clusters}.npy")
  23. train_feat_file = os.path.join(save_dir, f"bow_train_{args.step_size}_{args.clusters}.npy")
  24. if os.path.isfile(lapse_dscs_file):
  25. if os.path.isfile(dictionary_file):
  26. # if dictionary file already exists, we don't need the lapse descriptors
  27. print(f"{lapse_dscs_file} already exists, skipping lapse descriptor extraction...")
  28. else:
  29. print(f"{lapse_dscs_file} already exists, loading lapse descriptor from file...")
  30. lapse_dscs = np.load(lapse_dscs_file)
  31. else:
  32. # Step 1 - extract dense SIFT descriptors
  33. print("Extracting lapse descriptors...")
  34. lapse_dscs = extract_descriptors(list(session.generate_lapse_images()), kp_step=args.step_size)
  35. os.makedirs(save_dir, exist_ok=True)
  36. np.save(lapse_dscs_file, lapse_dscs)
  37. # BOW dictionary
  38. if os.path.isfile(dictionary_file):
  39. print(f"{dictionary_file} already exists, loading BOW dictionary from file...")
  40. dictionary = np.load(dictionary_file)
  41. else:
  42. # Step 2 - create BOW dictionary from Lapse SIFT descriptors
  43. print(f"Creating BOW vocabulary with {args.clusters} clusters...")
  44. dictionary = generate_dictionary_from_descriptors(lapse_dscs, args.clusters)
  45. np.save(dictionary_file, dictionary)
  46. # Extract Lapse BOW features using vocabulary (train data)
  47. if os.path.isfile(train_feat_file):
  48. print(f"{train_feat_file} already exists, skipping lapse BOW feature extraction...")
  49. else:
  50. # Step 3 - calculate training data (BOW features of Lapse images)
  51. print(f"Extracting BOW features from Lapse images...")
  52. features = [feat for _, feat in generate_bow_features(list(session.generate_lapse_images()), dictionary, kp_step=args.step_size)]
  53. np.save(train_feat_file, features)
  54. print("Complete!")
  55. if __name__ == "__main__":
  56. main()