train_bow.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. args = parser.parse_args()
  16. ds = Dataset(args.dataset_dir)
  17. session = ds.create_session(args.session_name)
  18. save_dir = f"./bow_train_NoBackup/{session.name}"
  19. # Lapse DSIFT descriptors
  20. lapse_dscs_file = os.path.join(save_dir, "lapse_dscs.npy")
  21. dictionary_file = os.path.join(save_dir, f"bow_dict_{args.clusters}.npy")
  22. train_feat_file = os.path.join(save_dir, f"bow_train_{args.clusters}.npy")
  23. if os.path.isfile(lapse_dscs_file):
  24. if os.path.isfile(dictionary_file):
  25. # if dictionary file already exists, we don't need the lapse descriptors
  26. print(f"{lapse_dscs_file} already exists, skipping lapse descriptor extraction...")
  27. else:
  28. print(f"{lapse_dscs_file} already exists, loading lapse descriptor from file...")
  29. lapse_dscs = np.load(lapse_dscs_file)
  30. else:
  31. # Step 1 - extract dense SIFT descriptors
  32. print("Extracting lapse descriptors...")
  33. lapse_dscs = extract_descriptors(list(session.generate_lapse_images()))
  34. os.makedirs(save_dir, exist_ok=True)
  35. np.save(lapse_dscs_file, lapse_dscs)
  36. # BOW dictionary
  37. if os.path.isfile(dictionary_file):
  38. print(f"{dictionary_file} already exists, loading BOW dictionary from file...")
  39. dictionary = np.load(dictionary_file)
  40. else:
  41. # Step 2 - create BOW dictionary from Lapse SIFT descriptors
  42. print(f"Creating BOW vocabulary with {args.clusters} clusters...")
  43. dictionary = generate_dictionary_from_descriptors(lapse_dscs, args.clusters)
  44. np.save(dictionary_file, dictionary)
  45. # Extract Lapse BOW features using vocabulary (train data)
  46. if os.path.isfile(train_feat_file):
  47. print(f"{train_feat_file} already exists, skipping lapse BOW feature extraction...")
  48. else:
  49. # Step 3 - calculate training data (BOW features of Lapse images)
  50. print(f"Extracting BOW features from Lapse images...")
  51. features = list(generate_bow_features(list(session.generate_lapse_images()), dictionary))
  52. np.save(train_feat_file, features)
  53. print("Complete!")
  54. if __name__ == "__main__":
  55. main()