forked from datajoint/element-deeplabcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pretrained_model_run.py
More file actions
executable file
·1350 lines (1227 loc) · 53.2 KB
/
test_pretrained_model_run.py
File metadata and controls
executable file
·1350 lines (1227 loc) · 53.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""Simple script to test pretrained inference with a video file.
Usage:
1. Configure database (see below)
2. Put your video file(s) in ./test_videos/ directory
- In Docker: videos should be in ./data/ directory (mounted to /app/data)
3. Run: python test_pretrained_model_run.py [model_name]
- In Docker: make test-pretrained
Available models:
- superanimal_quadruped (default): For quadruped animals (mice, rats, etc.)
- superanimal_topviewmouse: For top-view mouse pose estimation
Examples:
python test_pretrained_model_run.py
python test_pretrained_model_run.py superanimal_quadruped
python test_pretrained_model_run.py superanimal_topviewmouse
Or set DLC_ROOT_DATA_DIR environment variable to point to your video directory.
Database Configuration:
The script will look for database configuration in this order:
1. dj_local_conf.json file in the project root
2. Environment variables: DJ_HOST, DJ_USER, DJ_PASS
3. Default DataJoint configuration
Example dj_local_conf.json:
{
"database.host": "localhost",
"database.user": "root",
"database.password": "your_password",
"database.port": 3306,
"custom": {
"database.prefix": "test_"
}
}
"""
import os
import sys
import importlib.util
import logging
import argparse
from pathlib import Path
import datajoint as dj
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
# Simple status printer for user-facing messages
class StatusPrinter:
"""Simple status printer for step-by-step progress."""
def __init__(self, total_steps=8):
self.total_steps = total_steps
self.current_step = 0
def step(self, message, status="info"):
"""Print a step message with status indicator."""
self.current_step += 1
icons = {
"info": "ℹ️",
"success": "✅",
"warning": "⚠️",
"error": "❌",
"skip": "⏭️",
}
icon = icons.get(status, "•")
print(f"\n[{self.current_step}/{self.total_steps}] {icon} {message}")
def sub(self, message, indent=3, icon=""):
"""Print a sub-message with indentation."""
prefix = f"{icon} " if icon else ""
print(" " * indent + prefix + message)
def header(self, title):
"""Print a section header."""
print("\n" + "=" * 60)
print(title)
print("=" * 60)
# Configure database connection
if Path("./dj_local_conf.json").exists():
dj.config.load("./dj_local_conf.json")
logger.info("✅ Loaded database configuration from dj_local_conf.json")
else:
logger.info("⚠️ No dj_local_conf.json found, using environment variables or defaults")
logger.info(" Set DJ_HOST, DJ_USER, DJ_PASS environment variables if needed")
# Update config from environment variables
dj.config.update(
{
"safemode": False,
"database.host": os.environ.get("DJ_HOST")
or dj.config.get("database.host", "localhost"),
"database.user": os.environ.get("DJ_USER")
or dj.config.get("database.user", "root"),
"database.password": os.environ.get("DJ_PASS")
or dj.config.get("database.password", ""),
}
)
# Set database prefix for tests
if "custom" not in dj.config:
dj.config["custom"] = {}
dj.config["custom"]["database.prefix"] = os.environ.get(
"DATABASE_PREFIX", dj.config["custom"].get("database.prefix", "test_")
)
# Detect docker & set DLC root data directory
is_docker = os.path.exists("/.dockerenv") or (
os.getcwd() == "/app" and os.path.exists("/app")
)
if is_docker:
# Prefer /app/test_videos (from project mount .:/app) since videos are in ./test_videos
test_videos_path = Path("/app/test_videos")
if test_videos_path.exists():
default_video_dir = "/app/test_videos"
else:
default_video_dir = "/app/data"
else:
default_video_dir = "./test_videos"
video_dir = Path(os.getenv("DLC_ROOT_DATA_DIR", default_video_dir))
if "dlc_root_data_dir" not in dj.config.get("custom", {}) or not dj.config[
"custom"
].get("dlc_root_data_dir"):
dj.config["custom"]["dlc_root_data_dir"] = str(video_dir.absolute())
logger.info(f"📁 Set DLC_ROOT_DATA_DIR to: {video_dir.absolute()}")
if is_docker:
logger.info("🐳 Running in Docker mode")
# Get the root directory for making relative paths (ensure it's absolute)
dlc_root_dir = Path(dj.config["custom"].get("dlc_root_data_dir", str(video_dir.absolute())))
if not dlc_root_dir.is_absolute():
dlc_root_dir = dlc_root_dir.resolve()
logger.info(f"📊 Database: {dj.config['database.host']} (prefix: {dj.config['custom']['database.prefix']})")
logger.info(f"📁 DLC Root: {dlc_root_dir}")
from element_deeplabcut import model
from tests import tutorial_pipeline as pipeline
def check_database_connection():
"""Verify database connection is working."""
try:
# Just ensure we can connect; no schema creation.
dj.conn()
return True
except Exception as e:
logger.error(f"\n❌ Database connection failed: {e}")
logger.error("\nPlease configure your database:")
logger.error(" 1. Create dj_local_conf.json with database credentials")
logger.error(" 2. Or set environment variables: DJ_HOST, DJ_USER, DJ_PASS")
logger.error(
" 3. Or ensure database is running (docker compose -f docker-compose-db.yaml up -d)"
)
return False
def main():
status = StatusPrinter(total_steps=8)
status.header("Testing Pretrained Model Inference with Video")
# 0. Check database connection
status.step("Checking database connection")
if not check_database_connection():
return # Exit gracefully
status.sub("Database connection successful", indent=3)
# 0.5. Clean up database (remove test data from previous runs)
status.step("Cleaning up database (removing test data from previous runs)")
cleanup_count = 0
# Delete PoseEstimation entries (and their parts)
pose_estimation_rel = pipeline.model.PoseEstimation
pose_keys = pose_estimation_rel.fetch("KEY")
pose_count = len(pose_keys)
if pose_count > 0:
pose_estimation_rel.delete()
cleanup_count += pose_count
status.sub(
f"Deleted {pose_count} PoseEstimation entry/entries", icon="🗑️", indent=3
)
# Delete PoseEstimationTask entries
task_rel = pipeline.model.PoseEstimationTask
task_keys = task_rel.fetch("KEY")
task_count = len(task_keys)
if task_count > 0:
task_rel.delete()
cleanup_count += task_count
status.sub(
f"Deleted {task_count} PoseEstimationTask entry/entries",
icon="🗑️",
indent=3,
)
# Delete test models (models with names starting with "test_")
test_models_rel = model.Model & "model_name LIKE 'test_%'"
test_model_keys = test_models_rel.fetch("KEY")
test_model_count = len(test_model_keys)
if test_model_count > 0:
test_models_rel.delete()
cleanup_count += test_model_count
status.sub(f"Deleted {test_model_count} test model(s)", icon="🗑️", indent=3)
# Delete test recordings (optional - comment out if you want to keep recordings)
# Example (fixed pattern):
# n_test_recordings = len(pipeline.model.VideoRecording & {"subject": "test1"})
# if n_test_recordings:
# (pipeline.model.VideoRecording & {"subject": "test1"}).delete()
# cleanup_count += n_test_recordings
# status.sub(f"Deleted {n_test_recordings} test recording(s)", icon="🗑️", indent=3)
if cleanup_count > 0:
status.sub(f"Total: {cleanup_count} entry/entries cleaned", icon="✅", indent=3)
else:
status.sub("No test data found to clean", icon="ℹ️", indent=3)
# 1. Find video files
status.step("Finding video files")
# Use same Docker detection logic as at the top
is_docker_local = os.path.exists("/.dockerenv") or (
os.getcwd() == "/app" and os.path.exists("/app")
)
# In Docker, prefer /app/test_videos (from project mount .:/app) since videos are in ./test_videos
if is_docker_local:
test_videos_path = Path("/app/test_videos")
if test_videos_path.exists():
default_video_dir_local = "/app/test_videos"
else:
default_video_dir_local = "/app/data"
else:
default_video_dir_local = "./test_videos"
video_dir_local = Path(os.getenv("DLC_ROOT_DATA_DIR", default_video_dir_local))
if not video_dir_local.exists():
status.sub(f"Video directory not found: {video_dir_local}", indent=3)
if is_docker_local:
status.sub("In Docker: Videos should be in ./test_videos/ on host", indent=3)
status.sub("(available at /app/test_videos via project mount)", indent=5)
return # Exit gracefully
video_files = list(video_dir_local.glob("*.mp4")) + list(
video_dir_local.glob("*.avi")
) + list(video_dir_local.glob("*.mov"))
if not video_files:
status.sub(f"No video files found in {video_dir_local}", indent=3)
status.sub("Supported formats: .mp4, .avi, .mov", indent=3)
if is_docker_local:
status.sub("In Docker: Videos should be in ./test_videos/ on host", indent=3)
status.sub("(available at /app/test_videos in container)", indent=5)
return # Exit gracefully
status.sub(f"Found {len(video_files)} video file(s):", indent=3)
for vf in video_files:
status.sub(f"- {vf.name}", indent=5)
# 2. Register pretrained model
# Get model name from command line or use default
pretrained_model_name = getattr(main, "pretrained_model_name", "superanimal_quadruped")
status.step(f"Registering pretrained model: {pretrained_model_name}")
model.PretrainedModel.populate_common_models([pretrained_model_name])
status.sub(f"Registered: {pretrained_model_name}", icon="✅")
# 3. Insert pretrained model instance
status.step("Inserting pretrained model instance")
# Use a model name that includes the pretrained model name for clarity
model_name = f"test_pretrained_model_run_{pretrained_model_name.replace('superanimal_', '')}"
# Check if model already exists and verify it's a pretrained model
if len(model.Model & {"model_name": model_name}):
existing_model = (model.Model & {"model_name": model_name}).fetch1()
config_template = existing_model.get("config_template", {})
is_pretrained = config_template.get("_pretrained_model_name") is not None
if is_pretrained:
existing_pretrained_name = config_template.get("_pretrained_model_name")
if existing_pretrained_name == pretrained_model_name:
status.sub(
f"Model '{model_name}' already exists (pretrained: {pretrained_model_name}), skipping insertion",
icon="✅",
)
else:
status.sub(
f"Model '{model_name}' exists but uses different pretrained model: {existing_pretrained_name}",
icon="⚠️",
)
status.sub(f"Expected: {pretrained_model_name}", indent=5)
status.sub(
"Deleting existing model and tasks, creating new one...", indent=5
)
# Delete any existing tasks that reference this model
if len(pipeline.model.PoseEstimationTask & {"model_name": model_name}):
(pipeline.model.PoseEstimationTask & {"model_name": model_name}).delete()
status.sub(
"Deleted existing PoseEstimationTask entries",
icon="✅",
indent=7,
)
# Delete the model
(model.Model & {"model_name": model_name}).delete()
# Insert the correct pretrained model
model.Model.insert_pretrained_model(
model_name=model_name,
pretrained_model_name=pretrained_model_name,
model_description=f"Test model using {pretrained_model_name}",
prompt=False,
)
status.sub(f"Re-inserted model: {model_name}", icon="✅")
else:
status.sub(
f"Model '{model_name}' exists but is a TRAINED model, not pretrained",
icon="⚠️",
)
status.sub(
"This script requires a PRETRAINED model. Deleting existing model and tasks...",
indent=5,
)
# Delete any existing tasks that reference this model
if len(pipeline.model.PoseEstimationTask & {"model_name": model_name}):
(pipeline.model.PoseEstimationTask & {"model_name": model_name}).delete()
status.sub(
"Deleted existing PoseEstimationTask entries",
icon="✅",
indent=7,
)
# Delete the model
(model.Model & {"model_name": model_name}).delete()
# Insert the correct pretrained model
model.Model.insert_pretrained_model(
model_name=model_name,
pretrained_model_name=pretrained_model_name,
model_description=f"Test model using {pretrained_model_name}",
prompt=False,
)
status.sub(f"Inserted pretrained model: {model_name}", icon="✅")
else:
try:
model.Model.insert_pretrained_model(
model_name=model_name,
pretrained_model_name=pretrained_model_name,
model_description=f"Test model using {pretrained_model_name}",
prompt=False,
)
status.sub(f"Inserted model: {model_name}", icon="✅")
except dj.errors.DuplicateError as e:
# If duplicate error occurs (e.g., same unique index), skip insertion
status.sub(
"Model with similar configuration already exists, skipping insertion",
icon="⚠️",
)
status.sub(f"Error: {str(e)[:100]}...", indent=5)
# 4. Setup test data (subject, session, recordings)
status.step("Setting up test data")
# Use shorter subject name (element-animal Subject table has limited varchar length)
base_key = {
"subject": "test1",
"session_datetime": "2024-01-01 12:00:00",
}
pipeline.subject.Subject.insert1(
{
"subject": "test1", # Short name to fit database column
"sex": "F",
"subject_birth_date": "2020-01-01",
"subject_description": "Test subject for video inference",
},
skip_duplicates=True,
)
pipeline.session.Session.insert1(
{
"subject": "test1",
"session_datetime": "2024-01-01 12:00:00",
},
skip_duplicates=True,
)
# Create a separate recording for each video file
recording_keys = []
for idx, video_file in enumerate(video_files):
recording_key = {
**base_key,
"recording_id": idx + 1, # Start from 1
}
recording_keys.append(recording_key)
# Insert or update recording and file entry
# Store file path relative to root directory
video_file_abs = Path(video_file).resolve()
dlc_root_dir_abs = Path(
dj.config["custom"].get("dlc_root_data_dir", str(video_dir_local.absolute()))
).resolve()
try:
relative_path = video_file_abs.relative_to(dlc_root_dir_abs)
except ValueError:
# If video_file is not under dlc_root_dir, use just the filename
# This handles the case where videos are in the root directory itself
relative_path = Path(video_file.name)
# Check if we need to update anything
existing_rec = pipeline.model.VideoRecording & recording_key
file_key = {**recording_key, "file_id": 0}
existing_file = pipeline.model.VideoRecording.File & file_key
needs_update = False
update_reason = []
if len(existing_rec):
existing_device = existing_rec.fetch1("device")
if existing_device != "Camera1":
needs_update = True
update_reason.append("device changed")
else:
needs_update = True
update_reason.append("recording doesn't exist")
if len(existing_file):
existing_path = existing_file.fetch1("file_path")
if existing_path != str(relative_path):
needs_update = True
update_reason.append("file path changed")
else:
needs_update = True
update_reason.append("file entry doesn't exist")
if needs_update:
# Get all file entries first (if they exist) to preserve them
all_files = []
if len(existing_rec):
# Fetch all file entries for this recording
file_keys = (
pipeline.model.VideoRecording.File & recording_key
).fetch("KEY", as_dict=True)
for fk in file_keys:
file_data = (
pipeline.model.VideoRecording.File & fk
).fetch1()
all_files.append(file_data)
# Delete the master record (which cascades to all Part entries)
(pipeline.model.VideoRecording & recording_key).delete()
# Re-insert the master record
pipeline.model.VideoRecording.insert1({**recording_key, "device": "Camera1"})
# Re-insert all file entries with updated path
file_updated = False
for file_entry in all_files:
if file_entry["file_id"] == 0:
# Update this file entry with new path
pipeline.model.VideoRecording.File.insert1(
{**recording_key, "file_id": 0, "file_path": str(relative_path)}
)
file_updated = True
else:
# Re-insert other file entries as-is
pipeline.model.VideoRecording.File.insert1(file_entry)
# If no file entry existed, insert it
if not file_updated:
pipeline.model.VideoRecording.File.insert1(
{**file_key, "file_path": str(relative_path)}
)
if len(update_reason) > 0:
status.sub(
f"Updated recording {recording_key['recording_id']} ({', '.join(update_reason)}): {video_file.name}",
icon="🔄",
indent=5,
)
else:
status.sub(
f"Created recording {recording_key['recording_id']} for {video_file.name}",
icon="✅",
indent=5,
)
else:
status.sub(
f"Recording {recording_key['recording_id']} already exists with correct data: {video_file.name}",
icon="✅",
indent=5,
)
status.sub(
f"Created {len(recording_keys)} separate recording(s)", icon="✅", indent=3
)
# 5. Extract video metadata
status.step("Extracting video metadata")
try:
pipeline.model.RecordingInfo.populate()
# Show info for all recordings
for rec_key in recording_keys:
rec_info = (pipeline.model.RecordingInfo & rec_key).fetch1()
status.sub(
f"Recording {rec_key['recording_id']}: {rec_info['px_width']}x{rec_info['px_height']}, "
f"{rec_info['nframes']} frames, {rec_info['fps']:.1f} fps",
icon="✅",
indent=5,
)
except ModuleNotFoundError as e:
if "cv2" in str(e):
status.sub(f"Error: {e}", icon="❌", indent=3)
status.sub(
"OpenCV (cv2) is required for video metadata extraction.", indent=3
)
status.sub("Install it with: pip install opencv-python", indent=5)
status.sub("Or: conda install -c conda-forge opencv", indent=5)
return # Exit gracefully
raise
# 6. Clean up any tasks that might be using wrong models (from other test scripts)
status.step("Cleaning up any conflicting tasks")
for rec_key in recording_keys:
# Find all tasks for this recording, regardless of model_name
all_tasks = (pipeline.model.PoseEstimationTask & rec_key).fetch("model_name")
for task_model_name in set(all_tasks):
if task_model_name != model_name:
status.sub(
f"Found task with different model '{task_model_name}' for recording {rec_key['recording_id']}",
icon="⚠️",
indent=3,
)
status.sub(
f"Deleting task (expected model: '{model_name}')...", indent=5
)
(pipeline.model.PoseEstimationTask & {**rec_key, "model_name": task_model_name}).delete()
status.sub("Task deleted", icon="✅", indent=7)
# 6. Create pose estimation tasks for each recording
status.step("Creating pose estimation tasks")
for rec_key in recording_keys:
# Check if results already exist
task_key = {**rec_key, "model_name": model_name}
output_dir = pipeline.model.PoseEstimationTask.infer_output_dir(
task_key, relative=False, mkdir=False
)
# Check if results exist - look for H5 files directly
results_exist = False
output_path = Path(output_dir)
if output_path.exists():
# Check for result files (H5, pickle, or JSON)
h5_files = list(output_path.glob("*.h5"))
pickle_files = list(output_path.glob("*.pickle"))
json_files = list(output_path.glob("*.json"))
if h5_files or pickle_files or json_files:
results_exist = True
status.sub(
f"Results found for recording {rec_key['recording_id']} in: {output_dir.name} "
f"({len(h5_files)} H5, {len(pickle_files)} pickle, {len(json_files)} JSON)",
icon="✅",
indent=5,
)
else:
status.sub(
f"No result files found for recording {rec_key['recording_id']} in: {output_dir.name}",
icon="⚠️",
indent=5,
)
else:
status.sub(
f"Output directory doesn't exist for recording {rec_key['recording_id']}: {output_dir.name}",
icon="⚠️",
indent=5,
)
# Generate task - it will auto-detect and set task_mode appropriately
# Always use "load" mode if results exist - never re-run inference
if results_exist:
task_mode = "load" # Use existing results - do NOT trigger inference
status.sub(
f"Results exist for recording {rec_key['recording_id']} - setting task_mode='load'",
icon="✅",
indent=5,
)
else:
task_mode = None # Auto-detect (will be "trigger" if no results)
status.sub(
f"No results found for recording {rec_key['recording_id']} - will auto-detect task_mode",
icon="ℹ️",
indent=5,
)
pipeline.model.PoseEstimationTask.generate(
rec_key,
model_name=model_name,
task_mode=task_mode,
analyze_videos_params={
"video_inference": {
"scale": 0.4, # Adjust if needed (0.3-0.5 recommended)
"batchsize": 8, # Pose estimation batch size (adjust based on GPU memory)
"detector_batch_size": getattr(main, "detector_batch_size", 4),
"device": (
f"cuda:{getattr(main, 'gpu', 0)}"
if getattr(main, "gpu", 0) >= 0
else "cpu"
),
}
},
)
status.sub(
f"Task created for recording {rec_key['recording_id']}",
icon="✅",
indent=5,
)
# 6.5. Update task modes if needed (in case results were created after task creation)
status.step("Checking and updating task modes")
for rec_key in recording_keys:
task_key = {**rec_key, "model_name": model_name}
output_dir = pipeline.model.PoseEstimationTask.infer_output_dir(
task_key, relative=False, mkdir=False
)
# Check if results exist
output_path = Path(output_dir)
results_exist = False
if output_path.exists():
h5_files = list(output_path.glob("*.h5"))
pickle_files = list(output_path.glob("*.pickle"))
json_files = list(output_path.glob("*.json"))
if h5_files or pickle_files or json_files:
results_exist = True
# Check current task mode
try:
current_task = (pipeline.model.PoseEstimationTask & task_key).fetch1()
current_mode = current_task.get("task_mode", "trigger")
# Update to "load" if results exist but task is in "trigger" mode
if results_exist and current_mode == "trigger":
pipeline.model.PoseEstimationTask.update1(
{**task_key, "task_mode": "load"}
)
status.sub(
f"Updated recording {rec_key['recording_id']} task_mode to 'load' (results exist)",
icon="✅",
indent=5,
)
elif not results_exist and current_mode == "load":
pipeline.model.PoseEstimationTask.update1(
{**task_key, "task_mode": "trigger"}
)
status.sub(
f"Updated recording {rec_key['recording_id']} task_mode to 'trigger' (no results)",
icon="⚠️",
indent=5,
)
else:
status.sub(
f"Recording {rec_key['recording_id']} task_mode is '{current_mode}' (correct)",
icon="ℹ️",
indent=5,
)
except Exception as e:
status.sub(
f"Could not check/update task for recording {rec_key['recording_id']}: {e}",
icon="⚠️",
indent=5,
)
# 7. Run inference or load existing results
status.step("Running inference or loading existing results")
# Check if DeepLabCut is available FIRST (before checking task modes)
# This import might print "Loading DLC..." so we do it early
deeplabcut_available = False
error_msg = None
# First check if PyTorch is available (needed for SuperAnimal)
try:
import torch
pytorch_available = True
pytorch_version = torch.__version__
except ImportError:
pytorch_available = False
pytorch_version = None
try:
import deeplabcut
deeplabcut_available = True
logger.info("DeepLabCut imported successfully")
# Verify it's actually usable by checking for a key function
if hasattr(deeplabcut, "video_inference_superanimal"):
logger.info("DeepLabCut has video_inference_superanimal function")
else:
logger.warning(
"DeepLabCut imported but video_inference_superanimal not found"
)
except (ImportError, TypeError, Exception) as e:
error_msg = str(e)
deeplabcut_available = False
logger.warning(f"DeepLabCut import failed: {e}")
import traceback
logger.debug(f"Traceback: {traceback.format_exc()}")
# Double-check task modes and results before proceeding
# This ensures we never trigger inference if results exist
all_in_load_mode = True
any_results_exist = False
for rec_key in recording_keys:
task_key = {**rec_key, "model_name": model_name}
try:
task_mode = (pipeline.model.PoseEstimationTask & task_key).fetch1(
"task_mode"
)
# Check if results actually exist for this task
output_dir = pipeline.model.PoseEstimationTask.infer_output_dir(
task_key, relative=False, mkdir=False
)
output_path = Path(output_dir)
results_exist = False
if output_path.exists():
h5_files = list(output_path.glob("*.h5"))
pickle_files = list(output_path.glob("*.pickle"))
json_files = list(output_path.glob("*.json"))
if h5_files or pickle_files or json_files:
results_exist = True
any_results_exist = True
# If results exist but task is in trigger mode, update it
if results_exist and task_mode == "trigger":
pipeline.model.PoseEstimationTask.update1(
{**task_key, "task_mode": "load"}
)
status.sub(
f"Updated recording {rec_key['recording_id']} to 'load' mode (results exist)",
icon="✅",
indent=3,
)
task_mode = "load"
if task_mode != "load":
all_in_load_mode = False
status.sub(
f"Recording {rec_key['recording_id']} is in '{task_mode}' mode",
icon="ℹ️",
indent=3,
)
except Exception as e:
# Task doesn't exist yet, so not in load mode
all_in_load_mode = False
status.sub(
f"Task for recording {rec_key['recording_id']} doesn't exist: {e}",
icon="⚠️",
indent=3,
)
break
# NEVER run inference if results exist - always use load mode
if any_results_exist:
status.sub("Results exist - will use 'load' mode (skipping inference)", icon="ℹ️", indent=3)
# Ensure all tasks with results are in load mode
for rec_key in recording_keys:
task_key = {**rec_key, "model_name": model_name}
try:
output_dir = pipeline.model.PoseEstimationTask.infer_output_dir(
task_key, relative=False, mkdir=False
)
output_path = Path(output_dir)
if output_path.exists():
h5_files = list(output_path.glob("*.h5"))
pickle_files = list(output_path.glob("*.pickle"))
json_files = list(output_path.glob("*.json"))
if h5_files or pickle_files or json_files:
# Force load mode if results exist
current_task = (
pipeline.model.PoseEstimationTask & task_key
).fetch1()
if current_task.get("task_mode") != "load":
pipeline.model.PoseEstimationTask.update1(
{**task_key, "task_mode": "load"}
)
except Exception as e:
logging.warning(f"Failed to set 'load' mode for recording {rec_key.get('recording_id', rec_key)}: {e}")
# Verify all tasks use the correct pretrained model before inference
status.step("Verifying model configuration")
for rec_key in recording_keys:
task_key = {**rec_key, "model_name": model_name}
if len(pipeline.model.PoseEstimationTask & task_key):
(pipeline.model.PoseEstimationTask & task_key).fetch1()
# Verify the model is actually a pretrained model
model_record = (model.Model & {"model_name": model_name}).fetch1()
config_template = model_record.get("config_template", {})
if config_template.get("_pretrained_model_name") is None:
status.sub(
f"ERROR: Task for recording {rec_key['recording_id']} references a TRAINED model, not pretrained!",
icon="❌",
indent=3,
)
status.sub("Deleting incorrect task...", indent=5)
(pipeline.model.PoseEstimationTask & task_key).delete()
status.sub(
"Task deleted. Please re-run the script to create correct tasks.",
icon="✅",
indent=5,
)
return # Exit gracefully
status.sub("All tasks verified to use pretrained model", icon="✅", indent=3)
if all_in_load_mode and deeplabcut_available:
status.step("Loading existing results")
status.sub("All tasks are in 'load' mode - will use existing results", icon="ℹ️", indent=3)
status.sub("Skipping inference step (results already exist)", icon="⚠️", indent=3)
try:
pipeline.model.PoseEstimation.populate()
# Check if there are any IndividualMapping entries (for multi-animal data)
individual_mappings = (
pipeline.model.PoseEstimation.IndividualMapping
& {"model_name": model_name}
)
num_mappings = len(individual_mappings)
if num_mappings > 0:
status.sub(
f"Results loaded successfully! ({num_mappings} individual mappings created in total)",
icon="✅",
indent=3,
)
else:
# Check if this is multi-animal data that should have mappings
individuals = (
pipeline.model.PoseEstimation.Individual
& {"model_name": model_name}
)
if len(individuals) > 0:
status.sub(
"Results loaded with warnings: Individual mappings could not be created",
icon="⚠️",
indent=3,
)
status.sub(
f"Found {len(individuals)} individual(s) but 0 mappings",
icon="ℹ️",
indent=4,
)
else:
status.sub(
"Results loaded successfully! (single-animal data)",
icon="✅",
indent=3,
)
except Exception as e:
status.sub(f"Error loading results: {e}", icon="❌", indent=3)
raise
return # Exit early if we're just loading
# Debug: print what we detected
status.sub(
f"DLC availability check: deeplabcut_available={deeplabcut_available}, error_msg={error_msg}",
indent=3,
)
# If DLC is not available, show error and exit
if not deeplabcut_available:
status.sub("DeepLabCut is not available - cannot run inference", icon="⚠️", indent=3)
# Check if deeplabcut package exists but has missing dependencies
if error_msg:
spec = importlib.util.find_spec("deeplabcut")
if spec is not None:
# Package exists but import failed - likely missing dependency
if "tensorflow" in error_msg.lower():
if pytorch_available:
status.sub(
f"PyTorch is available (version {pytorch_version})",
icon="✅",
indent=5,
)
status.sub(
"DeepLabCut is installed but TensorFlow is missing.",
icon="⚠️",
indent=3,
)
status.sub(
"DeepLabCut's __init__.py tries to import TensorFlow by default,",
indent=3,
)
status.sub(
"but SuperAnimal models only need PyTorch.", indent=3
)
status.sub("Workaround options:", indent=3)
status.sub(
"1. Install TensorFlow (even if unused): pip install tensorflow",
indent=5,
)
status.sub(
"2. Or try setting environment variable: export DLC_BACKEND='pytorch'",
indent=5,
)
status.sub(
"3. Or use DeepLabCut's PyTorch-only installation:",
indent=5,
)
status.sub(
" pip install --upgrade 'deeplabcut[superanimal]'",
indent=7,
)
elif "torch" in error_msg.lower() or "pytorch" in error_msg.lower():
status.sub(
"DeepLabCut is installed but PyTorch is missing.",
icon="⚠️",
indent=3,
)
status.sub(
"For SuperAnimal pretrained models, PyTorch is required.",
indent=3,
)
status.sub(
"To fix, install PyTorch: pip install torch", indent=5
)
elif "tensorpack" in error_msg.lower():
status.sub(
"DeepLabCut is installed but tensorpack is missing.",
icon="⚠️",
indent=3,
)
if pytorch_available:
status.sub(
f"PyTorch is available (version {pytorch_version})",
icon="✅",
indent=5,
)
status.sub(
"This is a dependency issue. To fix, reinstall DeepLabCut:",
indent=3,
)
status.sub("pip uninstall deeplabcut", indent=5)
status.sub(
"pip install 'deeplabcut[superanimal]==3.0.0rc13'",
indent=5,
)
elif "unsupported operand type(s) for |" in error_msg or "|:" in error_msg:
import sys
python_version = sys.version_info
status.sub(
"Python version incompatibility detected.",
icon="⚠️",
indent=3,
)
status.sub(
f"Current Python version: {python_version.major}.{python_version.minor}.{python_version.micro}",
indent=5,
)
status.sub(
"DeepLabCut 3.0.0rc13 requires Python 3.10 or higher",
indent=3,
)
status.sub(
"(it uses modern type hints like 'int | None' which require Python 3.10+)",
indent=5,
)
status.sub("To fix:", indent=3)
status.sub(
"1. Create a new conda environment with Python 3.10+:",
indent=5,
)
status.sub(
" conda create -n element-dlc python=3.10",
indent=7,
)
status.sub(
" conda activate element-dlc",
indent=7,
)
status.sub(
" conda env update -f environment.yml",
indent=7,
)
status.sub(
"2. Or upgrade your current environment:",
indent=5,
)
status.sub(