-
Notifications
You must be signed in to change notification settings - Fork 100
/
dialog.py
1372 lines (1116 loc) · 51.2 KB
/
dialog.py
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
"""
Dialogs for running training and/or inference in GUI.
"""
import cattr
import os
import shutil
import atexit
import tempfile
from pathlib import Path
import sleap
from sleap import Labels, Video
from sleap.gui.dialogs.filedialog import FileDialog
from sleap.gui.dialogs.formbuilder import YamlFormWidget
from sleap.gui.learning import runners, scopedkeydict, configs, datagen, receptivefield
from typing import Dict, List, Text, Optional, cast
from qtpy import QtWidgets, QtCore
import json
# List of fields which should show list of skeleton nodes
NODE_LIST_FIELDS = [
"data.instance_cropping.center_on_part",
"model.heads.centered_instance.anchor_part",
"model.heads.centroid.anchor_part",
"model.heads.multi_class_topdown.confmaps.anchor_part",
]
class LearningDialog(QtWidgets.QDialog):
"""
Dialog for running training and/or inference.
The dialog shows tabs for configuring the pipeline (
:py:class:`TrainingPipelineWidget`) and, depending on the pipeline, for
each specific model (:py:class:`TrainingEditorWidget`).
In training mode, the model hyperpameters are editable unless you're using
a trained model; they are read-only in inference mode.
Arguments:
mode: either "training" or "inference".
labels_filename: path to labels file, used for default location to
save models.
labels: the `Labels` object (can also be loaded from given filename)
skeleton: the `Skeleton` object (can also be taken from `Labels`), used
for list of nodes for (e.g.) selecting anchor node
"""
_handle_learning_finished = QtCore.Signal(int)
def __init__(
self,
mode: Text,
labels_filename: Text,
labels: Optional[Labels] = None,
skeleton: Optional["Skeleton"] = None,
*args,
**kwargs,
):
super(LearningDialog, self).__init__()
if labels is None:
labels = Labels.load_file(labels_filename)
if skeleton is None and labels.skeletons:
skeleton = labels.skeletons[0]
self.mode = mode
self.labels_filename = labels_filename
self.labels = labels
self.skeleton = skeleton
self._frame_selection = None
self.current_pipeline = ""
self.tabs: Dict[str, TrainingEditorWidget] = dict()
self.shown_tab_names = []
self._cfg_getter = configs.TrainingConfigsGetter.make_from_labels_filename(
labels_filename=self.labels_filename
)
# Layout for buttons
buttons = QtWidgets.QDialogButtonBox()
self.copy_button = buttons.addButton(
"Copy to clipboard", QtWidgets.QDialogButtonBox.ActionRole
)
self.save_button = buttons.addButton(
"Save configuration files...", QtWidgets.QDialogButtonBox.ActionRole
)
self.export_button = buttons.addButton(
"Export training job package...", QtWidgets.QDialogButtonBox.ActionRole
)
self.cancel_button = buttons.addButton(QtWidgets.QDialogButtonBox.Cancel)
self.run_button = buttons.addButton("Run", QtWidgets.QDialogButtonBox.ApplyRole)
self.copy_button.setToolTip("Copy configuration to the clipboard")
self.save_button.setToolTip("Save scripts and configuration to run pipeline.")
self.export_button.setToolTip(
"Export data, configuration, and scripts for remote training and inference."
)
self.run_button.setToolTip("Run pipeline locally (GPU recommended).")
buttons_layout = QtWidgets.QHBoxLayout()
buttons_layout.addWidget(buttons, alignment=QtCore.Qt.AlignTop)
buttons_layout_widget = QtWidgets.QWidget()
buttons_layout_widget.setLayout(buttons_layout)
self.pipeline_form_widget = TrainingPipelineWidget(mode=mode, skeleton=skeleton)
if mode == "training":
tab_label = "Training Pipeline"
elif mode == "inference":
# self.pipeline_form_widget = InferencePipelineWidget()
tab_label = "Inference Pipeline"
else:
raise ValueError(f"Invalid LearningDialog mode: {mode}")
self.tab_widget = QtWidgets.QTabWidget()
self.tab_widget.addTab(self.pipeline_form_widget, tab_label)
self.make_tabs()
self.message_widget = QtWidgets.QLabel("")
# Layout for entire dialog
content_widget = QtWidgets.QWidget()
content_layout = QtWidgets.QVBoxLayout(content_widget)
content_layout.addWidget(self.tab_widget)
content_layout.addWidget(self.message_widget)
content_layout.addWidget(buttons_layout_widget)
# Create the QScrollArea.
scroll_area = QtWidgets.QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_area.setWidget(content_widget)
scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(scroll_area)
self.adjust_initial_size()
# Default to most recently trained pipeline (if there is one)
self.set_default_pipeline_tab()
# Connect functions to update pipeline tabs when pipeline changes
self.pipeline_form_widget.updatePipeline.connect(self.set_pipeline)
self.pipeline_form_widget.emitPipeline()
self.connect_signals()
# Connect actions for buttons
self.copy_button.clicked.connect(self.copy)
self.save_button.clicked.connect(self.save)
self.export_button.clicked.connect(self.export_package)
self.cancel_button.clicked.connect(self.reject)
self.run_button.clicked.connect(self.run)
# Connect button for previewing the training data
if "_view_datagen" in self.pipeline_form_widget.buttons:
self.pipeline_form_widget.buttons["_view_datagen"].clicked.connect(
self.view_datagen
)
def adjust_initial_size(self):
# Get screen size
screen = QtWidgets.QDesktopWidget().screenGeometry()
max_width = 1860
max_height = 1150
margin = 0.10
# Calculate target width and height
target_width = min(screen.width() - screen.width() * margin, max_width)
target_height = min(screen.height() - screen.height() * margin, max_height)
# Set the dialog's dimensions
self.resize(target_width, target_height)
def update_file_lists(self):
self._cfg_getter.update()
for tab in self.tabs.values():
tab.update_file_list()
@staticmethod
def count_total_frames_for_selection_option(
videos_frames: Dict[Video, List[int]]
) -> int:
if not videos_frames:
return 0
count = 0
for frame_list in videos_frames.values():
# Check for [X, Y) range given as (X, -Y) tuple
if len(frame_list) == 2 and frame_list[1] < 0:
count += -frame_list[1] - frame_list[0]
elif frame_list != (0, 0):
count += len(frame_list)
return count
@property
def frame_selection(self) -> Dict[str, Dict[Video, List[int]]]:
"""
Returns dictionary with frames that user has selected for learning.
"""
return self._frame_selection
@frame_selection.setter
def frame_selection(self, frame_selection: Dict[str, Dict[Video, List[int]]]):
"""Sets options of frames on which to run learning."""
self._frame_selection = frame_selection
if "_predict_frames" in self.pipeline_form_widget.fields.keys():
prediction_options = []
total_random = 0
total_suggestions = 0
total_user = 0
random_video = 0
clip_length = 0
video_length = 0
all_videos_length = 0
# Determine which options are available given _frame_selection
if "random" in self._frame_selection:
total_random = self.count_total_frames_for_selection_option(
self._frame_selection["random"]
)
if "random_video" in self._frame_selection:
random_video = self.count_total_frames_for_selection_option(
self._frame_selection["random_video"]
)
if "suggestions" in self._frame_selection:
total_suggestions = self.count_total_frames_for_selection_option(
self._frame_selection["suggestions"]
)
if "user" in self._frame_selection:
total_user = self.count_total_frames_for_selection_option(
self._frame_selection["user"]
)
if "clip" in self._frame_selection:
clip_length = self.count_total_frames_for_selection_option(
self._frame_selection["clip"]
)
if "video" in self._frame_selection:
video_length = self.count_total_frames_for_selection_option(
self._frame_selection["video"]
)
if "all_videos" in self._frame_selection:
all_videos_length = self.count_total_frames_for_selection_option(
self._frame_selection["all_videos"]
)
# Build list of options
# Priority for default (lowest to highest):
# "nothing" (if training)
# "current frame" (if inference)
# "suggested frames" (if available)
# "selected clip" (if available)
if self.mode != "inference":
prediction_options.append("nothing")
prediction_options.append("current frame")
default_option = "nothing" if self.mode != "inference" else "current frame"
option = f"random frames ({total_random} total frames)"
prediction_options.append(option)
if random_video > 0:
option = f"random frames in current video ({random_video} frames)"
prediction_options.append(option)
if total_suggestions > 0:
option = f"suggested frames ({total_suggestions} total frames)"
prediction_options.append(option)
default_option = option
if total_user > 0:
option = f"user labeled frames ({total_user} total frames)"
prediction_options.append(option)
if clip_length > 0:
option = f"selected clip ({clip_length} frames)"
prediction_options.append(option)
default_option = option
prediction_options.append(f"entire current video ({video_length} frames)")
if len(self.labels.videos) > 1:
prediction_options.append(f"all videos ({all_videos_length} frames)")
self.pipeline_form_widget.fields["_predict_frames"].set_options(
prediction_options, default_option
)
def connect_signals(self):
self.pipeline_form_widget.valueChanged.connect(self.on_tab_data_change)
for head_name, tab in self.tabs.items():
tab.valueChanged.connect(lambda n=head_name: self.on_tab_data_change(n))
def disconnect_signals(self):
self.pipeline_form_widget.valueChanged.disconnect()
for head_name, tab in self.tabs.items():
tab.valueChanged.disconnect()
def make_tabs(self):
heads = (
"single_instance",
"centroid",
"centered_instance",
"multi_instance",
"multi_class_topdown",
"multi_class_bottomup",
)
video = self.labels.videos[0] if self.labels else None
for head_name in heads:
self.tabs[head_name] = TrainingEditorWidget(
video=video,
skeleton=self.skeleton,
head=head_name,
cfg_getter=self._cfg_getter,
require_trained=(self.mode == "inference"),
)
def adjust_data_to_update_other_tabs(self, source_data, updated_data=None):
if updated_data is None:
updated_data = source_data
anchor_part = None
set_anchor = False
if "model.heads.centroid.anchor_part" in source_data:
anchor_part = source_data["model.heads.centroid.anchor_part"]
set_anchor = True
elif "model.heads.centered_instance.anchor_part" in source_data:
anchor_part = source_data["model.heads.centered_instance.anchor_part"]
set_anchor = True
elif "model.heads.multi_class_topdown.confmaps.anchor_part" in source_data:
anchor_part = source_data[
"model.heads.multi_class_topdown.confmaps.anchor_part"
]
set_anchor = True
# Use None instead of empty string/list
anchor_part = anchor_part or None
if set_anchor:
updated_data["model.heads.centroid.anchor_part"] = anchor_part
updated_data["model.heads.centered_instance.anchor_part"] = anchor_part
updated_data[
"model.heads.multi_class_topdown.confmaps.anchor_part"
] = anchor_part
updated_data["data.instance_cropping.center_on_part"] = anchor_part
def update_tabs_from_pipeline(self, source_data):
self.adjust_data_to_update_other_tabs(source_data)
for tab in self.tabs.values():
tab.set_fields_from_key_val_dict(source_data)
def update_tabs_from_tab(self, source_data):
data_to_transfer = dict()
self.adjust_data_to_update_other_tabs(source_data, data_to_transfer)
if data_to_transfer:
for tab in self.tabs.values():
tab.set_fields_from_key_val_dict(data_to_transfer)
def on_tab_data_change(self, tab_name=None):
self.disconnect_signals()
if tab_name is None:
# Move data from pipeline tab to other tabs
source_data = self.pipeline_form_widget.get_form_data()
self.update_tabs_from_pipeline(source_data)
else:
# Get data from head-specific tab
source_data = self.tabs[tab_name].get_all_form_data()
self.update_tabs_from_tab(source_data)
# Update pipeline tab
self.pipeline_form_widget.set_form_data(source_data)
self._validate_pipeline()
self.connect_signals()
def get_most_recent_pipeline_trained(self) -> Text:
recent_cfg_info = self._cfg_getter.get_first()
if recent_cfg_info and recent_cfg_info.head_name:
if recent_cfg_info.head_name in ("multi_class_topdown",):
return "top-down-id"
if recent_cfg_info.head_name in ("centroid", "centered_instance"):
return "top-down"
if recent_cfg_info.head_name in ("multi_instance",):
return "bottom-up"
if recent_cfg_info.head_name in ("single_instance",):
return "single"
if recent_cfg_info.head_name in ("multi_class_bottomup",):
return "bottom-up-id"
return ""
def set_default_pipeline_tab(self):
recent_pipeline_name = self.get_most_recent_pipeline_trained()
if recent_pipeline_name:
self.pipeline_form_widget.current_pipeline = recent_pipeline_name
else:
# Set default based on detection of single- vs multi-animal project.
if self.labels.max_user_instances == 1:
self.pipeline_form_widget.current_pipeline = "single"
else:
self.pipeline_form_widget.current_pipeline = "top-down"
def add_tab(self, tab_name):
tab_labels = {
"single_instance": "Single Instance Model Configuration",
"centroid": "Centroid Model Configuration",
"centered_instance": "Centered Instance Model Configuration",
"multi_instance": "Bottom-Up Model Configuration",
"multi_class_topdown": "Top-Down-Id Model Configuration",
"multi_class_bottomup": "Bottom-Up-Id Model Configuration",
}
self.tab_widget.addTab(self.tabs[tab_name], tab_labels[tab_name])
self.shown_tab_names.append(tab_name)
def remove_tabs(self):
while self.tab_widget.count() > 1:
self.tab_widget.removeTab(1)
self.shown_tab_names = []
def set_pipeline(self, pipeline: str):
if pipeline != self.current_pipeline:
self.remove_tabs()
if pipeline == "top-down":
self.add_tab("centroid")
self.add_tab("centered_instance")
elif pipeline == "bottom-up":
self.add_tab("multi_instance")
elif pipeline == "top-down-id":
self.add_tab("centroid")
self.add_tab("multi_class_topdown")
elif pipeline == "bottom-up-id":
self.add_tab("multi_class_bottomup")
elif pipeline == "single":
self.add_tab("single_instance")
self.current_pipeline = pipeline
self._validate_pipeline()
def change_tab(self, tab_idx: int):
print(tab_idx)
def merge_pipeline_and_head_config_data(self, head_name, head_data, pipeline_data):
for key, val in pipeline_data.items():
# if key.starts_with("_"):
# continue
if key.startswith("model.heads."):
key_scope = key.split(".")
if key_scope[2] != head_name:
continue
head_data[key] = val
@staticmethod
def update_loaded_config(
loaded_cfg: configs.TrainingJobConfig, tab_cfg_key_val_dict: dict
) -> scopedkeydict.ScopedKeyDict:
"""Update a loaded preset config with values from the training editor.
Args:
loaded_cfg: A `TrainingJobConfig` that was loaded from a preset or previous
training run.
tab_cfg_key_val_dict: A dictionary with the values extracted from the training
editor GUI tab.
Returns:
A `ScopedKeyDict` with the loaded config values overriden by the corresponding
ones from the `tab_cfg_key_val_dict`.
"""
# Serialize training config
loaded_cfg_hierarchical: dict = cattr.unstructure(loaded_cfg)
# Clear backbone subfields since these will be set by the GUI
if (
"model" in loaded_cfg_hierarchical
and "backbone" in loaded_cfg_hierarchical["model"]
):
for k in loaded_cfg_hierarchical["model"]["backbone"]:
loaded_cfg_hierarchical["model"]["backbone"][k] = None
loaded_cfg_scoped: scopedkeydict.ScopedKeyDict = (
scopedkeydict.ScopedKeyDict.from_hierarchical_dict(loaded_cfg_hierarchical)
)
# Replace params exposed in GUI with values from GUI
for param, value in tab_cfg_key_val_dict.items():
loaded_cfg_scoped.key_val_dict[param] = value
return loaded_cfg_scoped
def get_every_head_config_data(
self, pipeline_form_data
) -> List[configs.ConfigFileInfo]:
cfg_info_list = []
# Copy relevant data into linked fields (i.e., anchor part).
self.adjust_data_to_update_other_tabs(pipeline_form_data)
for tab_name in self.shown_tab_names:
trained_cfg_info = self.tabs[tab_name].trained_config_info_to_use
if self.tabs[tab_name].use_trained and (trained_cfg_info is not None):
cfg_info_list.append(trained_cfg_info)
else:
# Get config data from GUI
tab_cfg_key_val_dict = self.tabs[tab_name].get_all_form_data()
self.merge_pipeline_and_head_config_data(
head_name=tab_name,
head_data=tab_cfg_key_val_dict,
pipeline_data=pipeline_form_data,
)
scopedkeydict.apply_cfg_transforms_to_key_val_dict(tab_cfg_key_val_dict)
if trained_cfg_info is None:
# Config could not be loaded, just use the values from the GUI
loaded_cfg_scoped: dict = tab_cfg_key_val_dict
else:
# Config was loaded, override with the values from the GUI
loaded_cfg_scoped = LearningDialog.update_loaded_config(
trained_cfg_info.config, tab_cfg_key_val_dict
)
# Deserialize merged dict to object
cfg = scopedkeydict.make_training_config_from_key_val_dict(
loaded_cfg_scoped
)
if len(self.labels.tracks) > 0:
# For multiclass topdown, the class vectors output stride
# should be the max stride.
backbone_name = scopedkeydict.find_backbone_name_from_key_val_dict(
tab_cfg_key_val_dict
)
max_stride = tab_cfg_key_val_dict[
f"model.backbone.{backbone_name}.max_stride"
]
# Classes should be added here to prevent value error in
# model since we don't add them in the training config yaml.
if cfg.model.heads.multi_class_bottomup is not None:
cfg.model.heads.multi_class_bottomup.class_maps.classes = [
t.name for t in self.labels.tracks
]
elif cfg.model.heads.multi_class_topdown is not None:
cfg.model.heads.multi_class_topdown.class_vectors.classes = [
t.name for t in self.labels.tracks
]
cfg.model.heads.multi_class_topdown.class_vectors.output_stride = (
max_stride
)
cfg_info = configs.ConfigFileInfo(config=cfg, head_name=tab_name)
cfg_info_list.append(cfg_info)
return cfg_info_list
def get_selected_frames_to_predict(
self, pipeline_form_data
) -> Dict[Video, List[int]]:
frames_to_predict = dict()
if self._frame_selection is not None:
predict_frames_choice = pipeline_form_data.get("_predict_frames", "")
if predict_frames_choice.startswith("current frame"):
frames_to_predict = self._frame_selection["frame"]
elif predict_frames_choice.startswith("random frames in current video"):
frames_to_predict = self._frame_selection["random_video"]
elif predict_frames_choice.startswith("random"):
frames_to_predict = self._frame_selection["random"]
elif predict_frames_choice.startswith("selected clip"):
frames_to_predict = self._frame_selection["clip"]
elif predict_frames_choice.startswith("suggested"):
frames_to_predict = self._frame_selection["suggestions"]
elif predict_frames_choice.startswith("entire current video"):
frames_to_predict = self._frame_selection["video"]
elif predict_frames_choice.startswith("all videos"):
frames_to_predict = self._frame_selection["all_videos"]
elif predict_frames_choice.startswith("user"):
frames_to_predict = self._frame_selection["user"]
return frames_to_predict
def get_items_for_inference(self, pipeline_form_data) -> runners.ItemsForInference:
predict_frames_choice = pipeline_form_data.get("_predict_frames", "")
frame_selection = self.get_selected_frames_to_predict(pipeline_form_data)
frame_count = self.count_total_frames_for_selection_option(frame_selection)
if predict_frames_choice.startswith("user"):
items_for_inference = runners.ItemsForInference(
items=[
runners.DatasetItemForInference(
labels_path=self.labels_filename, frame_filter="user"
)
],
total_frame_count=frame_count,
)
elif predict_frames_choice.startswith("suggested"):
items_for_inference = runners.ItemsForInference(
items=[
runners.DatasetItemForInference(
labels_path=self.labels_filename, frame_filter="suggested"
)
],
total_frame_count=frame_count,
)
else:
items_for_inference = runners.ItemsForInference.from_video_frames_dict(
video_frames_dict=frame_selection,
total_frame_count=frame_count,
labels_path=self.labels_filename,
labels=self.labels,
)
return items_for_inference
def _validate_pipeline(self):
can_run = True
message = ""
if self.mode == "inference":
# Make sure we have trained models for each required head.
untrained = [
tab_name
for tab_name in self.shown_tab_names
if not self.tabs[tab_name].has_trained_config_selected
]
if untrained:
can_run = False
message = (
"Cannot run inference with untrained models "
f"({', '.join(untrained)})."
)
# Make sure skeleton will be valid for bottom-up inference.
if self.mode == "training" and self.current_pipeline == "bottom-up":
skeleton = self.labels.skeletons[0]
if not skeleton.is_arborescence:
message += (
"Cannot run bottom-up pipeline when skeleton is not an "
"arborescence."
)
root_names = [n.name for n in skeleton.root_nodes]
over_max_in_degree = [n.name for n in skeleton.in_degree_over_one]
cycles = skeleton.cycles
if len(root_names) > 1:
message += (
f" There are multiple root nodes: {', '.join(root_names)} "
"(there should be exactly one node which is not a target)."
)
if over_max_in_degree:
message += (
" There are nodes which are target in multiple edges: "
f"{', '.join(over_max_in_degree)} (maximum in-degree should be "
"1).</li>"
)
if cycles:
cycle_strings = []
for cycle in cycles:
cycle_strings.append(
" –> ".join((node.name for node in cycle))
)
message += (
f" There are cycles in graph: {'; '.join(cycle_strings)}."
)
can_run = False
if not can_run and message:
message = f"<b>Unable to run:</b><br />{message}"
self.message_widget.setText(message)
self.run_button.setEnabled(can_run)
def view_datagen(self):
pipeline_form_data = self.pipeline_form_widget.get_form_data()
config_info_list = self.get_every_head_config_data(pipeline_form_data)
datagen.show_datagen_preview(self.labels, config_info_list)
self.hide()
def run(self):
"""Run with current dialog settings."""
pipeline_form_data = self.pipeline_form_widget.get_form_data()
items_for_inference = self.get_items_for_inference(pipeline_form_data)
config_info_list = self.get_every_head_config_data(pipeline_form_data)
# Close the dialog now that we have the data from it
self.accept()
# Run training/learning pipeline using the TrainingJobs
new_counts = runners.run_learning_pipeline(
labels_filename=self.labels_filename,
labels=self.labels,
config_info_list=config_info_list,
inference_params=pipeline_form_data,
items_for_inference=items_for_inference,
)
self._handle_learning_finished.emit(new_counts)
# count < 0 means there was an error and we didn't get any results.
if new_counts is not None and new_counts >= 0:
total_count = items_for_inference.total_frame_count
no_result_count = total_count - new_counts
message = (
f"Inference ran on {total_count} frames."
f"\n\nInstances were predicted on {new_counts} frames "
f"({no_result_count} frame{'s' if no_result_count != 1 else ''} with "
"no instances found)."
)
win = QtWidgets.QMessageBox(text=message)
win.setWindowTitle("Inference Results")
win.exec_()
def copy(self):
"""Copy scripts and configs to clipboard"""
# Get all info from dialog
pipeline_form_data = self.pipeline_form_widget.get_form_data()
config_info_list = self.get_every_head_config_data(pipeline_form_data)
pipeline_form_data = json.dumps(pipeline_form_data, indent=2)
# Format information for each tab in dialog
output = [pipeline_form_data]
for config_info in config_info_list:
config_info = config_info.config.to_json()
config_info = json.loads(config_info)
config_info = json.dumps(config_info, indent=2)
output.append(config_info)
output = "\n".join(output)
# Set the clipboard text
clipboard = QtWidgets.QApplication.clipboard()
clipboard.setText(output)
def save(
self, output_dir: Optional[str] = None, labels_filename: Optional[str] = None
):
"""Save scripts and configs to run pipeline."""
if output_dir is None:
labels_fn = Path(self.labels_filename)
models_dir = Path(labels_fn.parent, "models")
output_dir = FileDialog.openDir(
None,
dir=models_dir.as_posix(),
caption="Select directory to save scripts",
)
if not output_dir:
return
pipeline_form_data = self.pipeline_form_widget.get_form_data()
items_for_inference = self.get_items_for_inference(pipeline_form_data)
config_info_list = self.get_every_head_config_data(pipeline_form_data)
if labels_filename is None:
labels_filename = self.labels_filename
runners.write_pipeline_files(
output_dir=output_dir,
labels_filename=labels_filename,
config_info_list=config_info_list,
inference_params=pipeline_form_data,
items_for_inference=items_for_inference,
)
def export_package(self, output_path: Optional[str] = None, gui: bool = True):
"""Export training job package."""
# TODO: Warn if self.mode != "training"?
if output_path is None:
# Prompt for output path.
output_path, _ = FileDialog.save(
caption="Export Training Job Package...",
dir=f"{self.labels_filename}.training_job.zip",
filter="Training Job Package (*.zip)",
)
if len(output_path) == 0:
return
# Create temp dir before packaging.
tmp_dir = tempfile.TemporaryDirectory()
# Remove the temp dir when program exits in case something goes wrong.
# atexit.register(shutil.rmtree, tmp_dir.name, ignore_errors=True)
# Check if we need to include suggestions.
include_suggestions = False
items_for_inference = self.get_items_for_inference(
self.pipeline_form_widget.get_form_data()
)
for item in items_for_inference.items:
if (
isinstance(item, runners.DatasetItemForInference)
and item.frame_filter == "suggested"
):
include_suggestions = True
# Save dataset with images.
labels_pkg_filename = str(
Path(self.labels_filename).with_suffix(".pkg.slp").name
)
if gui:
ret = sleap.gui.commands.export_dataset_gui(
self.labels,
tmp_dir.name + "/" + labels_pkg_filename,
all_labeled=False,
suggested=include_suggestions,
)
if ret == "canceled":
# Quit if user canceled during export.
tmp_dir.cleanup()
return
else:
self.labels.save(
tmp_dir.name + "/" + labels_pkg_filename,
with_images=True,
embed_all_labeled=False,
embed_suggested=include_suggestions,
)
# Save config and scripts.
self.save(tmp_dir.name, labels_filename=labels_pkg_filename)
# Package everything.
shutil.make_archive(
base_name=str(Path(output_path).with_suffix("")),
format="zip",
root_dir=tmp_dir.name,
)
msg = f"Saved training job package to: {output_path}"
print(msg)
# Close training editor.
self.accept()
if gui:
msgBox = QtWidgets.QMessageBox(text=f"Created training job package.")
msgBox.setDetailedText(output_path)
msgBox.setWindowTitle("Training Job Package")
okButton = msgBox.addButton(QtWidgets.QMessageBox.Ok)
openFolderButton = msgBox.addButton(
"Open containing folder", QtWidgets.QMessageBox.ActionRole
)
colabButton = msgBox.addButton(
"Go to Colab", QtWidgets.QMessageBox.ActionRole
)
msgBox.exec_()
if msgBox.clickedButton() == openFolderButton:
sleap.gui.commands.open_file(str(Path(output_path).resolve().parent))
elif msgBox.clickedButton() == colabButton:
# TODO: Update this to more workflow-tailored notebook.
sleap.gui.commands.copy_to_clipboard(output_path)
sleap.gui.commands.open_website(
"https://colab.research.google.com/github/talmolab/sleap/blob/main/docs/notebooks/Training_and_inference_using_Google_Drive.ipynb"
)
tmp_dir.cleanup()
class TrainingPipelineWidget(QtWidgets.QWidget):
"""
Widget used in :py:class:`LearningDialog` for configuring pipeline.
"""
updatePipeline = QtCore.Signal(str)
valueChanged = QtCore.Signal()
def __init__(
self, mode: Text, skeleton: Optional["Skeleton"] = None, *args, **kwargs
):
super(TrainingPipelineWidget, self).__init__(*args, **kwargs)
self.form_widget = YamlFormWidget.from_name(
"pipeline_form", which_form=mode, title="Training Pipeline"
)
if hasattr(skeleton, "node_names"):
for field_name in NODE_LIST_FIELDS:
self.form_widget.set_field_options(
field_name,
skeleton.node_names,
)
# Connect actions for change to pipeline
self.pipeline_field = self.form_widget.form_layout.find_field("_pipeline")[0]
self.pipeline_field.valueChanged.connect(self.emitPipeline)
self.form_widget.form_layout.valueChanged.connect(self.valueChanged)
self.setLayout(self.form_widget.form_layout)
@property
def fields(self):
return self.form_widget.fields
@property
def buttons(self):
return self.form_widget.buttons
def set_message(self, message: Text):
self.form_widget.set_message()
def get_form_data(self):
return self.form_widget.get_form_data()
def set_form_data(self, data):
self.form_widget.set_form_data(data)
def emitPipeline(self):
val = self.current_pipeline
self.updatePipeline.emit(val)
@property
def current_pipeline(self):
pipeline_selected_label = self.pipeline_field.value()
if "top-down" in pipeline_selected_label:
if "id" not in pipeline_selected_label:
return "top-down"
else:
return "top-down-id"
if "bottom-up" in pipeline_selected_label:
if "id" not in pipeline_selected_label:
return "bottom-up"
else:
return "bottom-up-id"
if "single" in pipeline_selected_label:
return "single"
return ""
@current_pipeline.setter
def current_pipeline(self, val):
if val not in (
"top-down",
"bottom-up",
"single",
"top-down-id",
"bottom-up-id",
):
raise ValueError(f"Cannot set pipeline to {val}")
# Match short name to full pipeline name shown in menu
for full_option_name in self.pipeline_field.option_list:
if val in full_option_name:
val = full_option_name
break
self.pipeline_field.setValue(val)
self.emitPipeline()
class TrainingEditorWidget(QtWidgets.QWidget):
"""
Dialog for viewing and modifying training profiles (model hyperparameters).
Args:
video: `Video` to use for receptive field preview
skeleton: `Skeleton` to use for node option list
head: If given, then only show configs with specified head name
cfg_getter: Object to use for getting list of config files.
If given, then menu of config files will be shown so user can
either copy hyperameters from another profile/model, or use a model
that was already trained.
require_trained: If True, then only show configs that are trained,
and don't allow user to uncheck "use trained" setting. This is set
when :py:class:`LearningDialog` is in "inference" mode.