@@ -449,9 +449,58 @@ def analyze_videos(
449449 print (f"Creating a TopDownDynamicCropper with configuration { top_down_dynamic } " )
450450 dynamic = TopDownDynamicCropper (** top_down_dynamic )
451451
452- snapshot = utils .get_model_snapshots (
453- snapshot_index , loader .model_folder , loader .pose_task
454- )[0 ]
452+ try :
453+ snapshot = utils .get_model_snapshots (
454+ snapshot_index , loader .model_folder , loader .pose_task
455+ )[0 ]
456+ except (ValueError , IndexError ) as e :
457+ print (f"Error loading snapshot with index { snapshot_index } : { e } " )
458+ print ("Attempting to find available snapshots..." )
459+
460+ # Try to get all available snapshots
461+ try :
462+ all_snapshots = utils .get_model_snapshots ("all" , loader .model_folder , loader .pose_task )
463+ if all_snapshots :
464+ # Try to find a "best" snapshot first
465+ best_snapshots = [s for s in all_snapshots if s .best ]
466+ if best_snapshots :
467+ snapshot = best_snapshots [0 ]
468+ print (f"Found and using best snapshot: { snapshot .path } " )
469+ else :
470+ # Use the last available snapshot
471+ snapshot = all_snapshots [- 1 ]
472+ print (f"No best snapshot found, using last available: { snapshot .path } " )
473+ else :
474+ raise FileNotFoundError (f"No snapshots found in { loader .model_folder } " )
475+ except Exception as fallback_error :
476+ raise FileNotFoundError (f"Failed to load any snapshots from { loader .model_folder } . Original error: { e } . Fallback error: { fallback_error } " )
477+
478+ # Additional validation for best snapshots
479+ if "best" in str (snapshot .path ) and not snapshot .path .exists ():
480+ print (f"Warning: Best snapshot path { snapshot .path } does not exist. Checking for alternative snapshots..." )
481+ # Try to find any available snapshot
482+ try :
483+ all_snapshots = utils .get_model_snapshots ("all" , loader .model_folder , loader .pose_task )
484+ if all_snapshots :
485+ # Try to find a different best snapshot
486+ best_snapshots = [s for s in all_snapshots if s .best and s .path .exists ()]
487+ if best_snapshots :
488+ snapshot = best_snapshots [0 ]
489+ print (f"Using alternative best snapshot: { snapshot .path } " )
490+ else :
491+ # Use the last available snapshot
492+ snapshot = all_snapshots [- 1 ]
493+ print (f"Using alternative snapshot: { snapshot .path } " )
494+ else :
495+ raise FileNotFoundError (f"No snapshots found in { loader .model_folder } " )
496+ except Exception as e :
497+ raise FileNotFoundError (f"Failed to find alternative snapshots: { e } " )
498+
499+ # Verify the snapshot file exists
500+ if not snapshot .path .exists ():
501+ raise FileNotFoundError (f"Snapshot file not found: { snapshot .path } " )
502+
503+ print (f"Successfully loaded snapshot: { snapshot .path } " )
455504
456505 # Load the BU model for the conditions provider
457506 cond_provider = None
@@ -497,9 +546,56 @@ def analyze_videos(
497546 if detector_batch_size is None :
498547 detector_batch_size = loader .project_cfg .get ("detector_batch_size" , 1 )
499548
500- detector_snapshot = utils .get_model_snapshots (
501- detector_snapshot_index , loader .model_folder , Task .DETECT
502- )[0 ]
549+ try :
550+ detector_snapshot = utils .get_model_snapshots (
551+ detector_snapshot_index , loader .model_folder , Task .DETECT
552+ )[0 ]
553+ except (ValueError , IndexError ) as e :
554+ print (f"Error loading detector snapshot with index { detector_snapshot_index } : { e } " )
555+ print ("Attempting to find available detector snapshots..." )
556+
557+ # Try to get all available detector snapshots
558+ try :
559+ all_detector_snapshots = utils .get_model_snapshots ("all" , loader .model_folder , Task .DETECT )
560+ if all_detector_snapshots :
561+ # Try to find a "best" detector snapshot first
562+ best_detector_snapshots = [s for s in all_detector_snapshots if s .best ]
563+ if best_detector_snapshots :
564+ detector_snapshot = best_detector_snapshots [0 ]
565+ print (f"Found and using best detector snapshot: { detector_snapshot .path } " )
566+ else :
567+ # Use the last available detector snapshot
568+ detector_snapshot = all_detector_snapshots [- 1 ]
569+ print (f"No best detector snapshot found, using last available: { detector_snapshot .path } " )
570+ else :
571+ raise FileNotFoundError (f"No detector snapshots found in { loader .model_folder } " )
572+ except Exception as fallback_error :
573+ raise FileNotFoundError (f"Failed to load any detector snapshots from { loader .model_folder } . Original error: { e } . Fallback error: { fallback_error } " )
574+
575+ # Additional validation for detector snapshots
576+ if "best" in str (detector_snapshot .path ) and not detector_snapshot .path .exists ():
577+ print (f"Warning: Best detector snapshot path { detector_snapshot .path } does not exist. Checking for alternative detector snapshots..." )
578+ try :
579+ all_detector_snapshots = utils .get_model_snapshots ("all" , loader .model_folder , Task .DETECT )
580+ if all_detector_snapshots :
581+ # Try to find a different best detector snapshot
582+ best_detector_snapshots = [s for s in all_detector_snapshots if s .best and s .path .exists ()]
583+ if best_detector_snapshots :
584+ detector_snapshot = best_detector_snapshots [0 ]
585+ print (f"Using alternative best detector snapshot: { detector_snapshot .path } " )
586+ else :
587+ # Use the last available detector snapshot
588+ detector_snapshot = all_detector_snapshots [- 1 ]
589+ print (f"Using alternative detector snapshot: { detector_snapshot .path } " )
590+ else :
591+ raise FileNotFoundError (f"No detector snapshots found in { loader .model_folder } " )
592+ except Exception as e :
593+ raise FileNotFoundError (f"Failed to find alternative detector snapshots: { e } " )
594+
595+ # Verify the detector snapshot file exists
596+ if not detector_snapshot .path .exists ():
597+ raise FileNotFoundError (f"Detector snapshot file not found: { detector_snapshot .path } " )
598+
503599 print (f" -> Using detector { detector_snapshot .path } " )
504600 detector_runner = utils .get_detector_inference_runner (
505601 model_config = loader .model_cfg ,
@@ -509,9 +605,12 @@ def analyze_videos(
509605 )
510606
511607 dlc_scorer = loader .scorer (snapshot , detector_snapshot )
608+ print (f"Using scorer: { dlc_scorer } " )
512609
513610 # Reading video and init variables
514611 videos = utils .list_videos_in_folder (videos , videotype , shuffle = in_random_order )
612+ h5_files_created = False # Track if any .h5 files were created
613+
515614 for video in videos :
516615 if destfolder is None :
517616 output_path = video .parent
@@ -583,6 +682,7 @@ def analyze_videos(
583682 output_prefix = output_prefix ,
584683 save_as_csv = save_as_csv ,
585684 )
685+ h5_files_created = True # .h5 file was created
586686
587687 if multi_animal :
588688 assemblies_path = output_path / f"{ output_prefix } _assemblies.pickle"
@@ -631,6 +731,7 @@ def analyze_videos(
631731 output_prefix = output_prefix + "_ctd" ,
632732 save_as_csv = save_as_csv ,
633733 )
734+ h5_files_created = True # .h5 file was created for CTD tracking
634735
635736 elif auto_track :
636737 convert_detections2tracklets (
@@ -654,14 +755,21 @@ def analyze_videos(
654755 destfolder = str (output_path ),
655756 save_as_csv = save_as_csv ,
656757 )
758+ h5_files_created = True # .h5 file was created by stitch_tracklets
657759
658- print (
659- "The videos are analyzed. Now your research can truly start!\n "
660- "You can create labeled videos with 'create_labeled_video'.\n "
661- "If the tracking is not satisfactory for some videos, consider expanding the "
662- "training set. You can use the function 'extract_outlier_frames' to extract a "
663- "few representative outlier frames.\n "
664- )
760+ if h5_files_created :
761+ print (
762+ "The videos are analyzed. Now your research can truly start!\n "
763+ "You can create labeled videos with 'create_labeled_video'.\n "
764+ "If the tracking is not satisfactory for some videos, consider expanding the "
765+ "training set. You can use the function 'extract_outlier_frames' to extract a "
766+ "few representative outlier frames.\n "
767+ )
768+ else :
769+ print (
770+ "No .h5 files were created during video analysis. Please check your code and "
771+ "ensure that the video inference and output generation are correct.\n "
772+ )
665773
666774 return dlc_scorer
667775
0 commit comments