Skip to content

Huge inference time, GPU isn't used with maDLC and Pytorch #2975

Closed

Description

@tsievert

Is there an existing issue for this?

  • I have searched the existing issues

Operating System

Windows 11

DeepLabCut version

3.0.0rc8

What engine are you using?

pytorch

DeepLabCut mode

multi animal

Device type

RTX 5000 ADA

Bug description 🐛

I'm running my first maDLC model and I'm seeing terrible performance compared to the signle animal model.
It also seems as if the GPU is not properly engaged for maDLC.

On the same machine, analyzing a video with a single animal model has a speed of ~19it/sec and the resource monitor shows a stable activation of the GPU, but a maDLC model runs at 9sec/it (not a typo, it is really seconds per iteration) and the resource monitor shows only sporadic spikes in the GPU activity (see screenshot).

Image

I tried different batch sizes (from 1 to 8), but that does not seem to be an issue here.

Steps To Reproduce

  1. Activate single animal project and start video analysis to confirm GPU is properly activated
  2. Switch to maDLC project
  3. Choose video and start analysis
  4. Observe terminal output and Nvidia resource monitor to see low performance

Relevant log output

# I have a few lines of code to double check cuda and the GPU are properly recognized, so I'll just paste the output here

(DEEPLABCUT) C:\Windows\System32>python -c "import torch; print('Torch version:', torch.__version__)"
Torch version: 2.5.1

(DEEPLABCUT) C:\Windows\System32>python -c "import torch; print('CUDA available:', torch.cuda.is_available()); print('CUDA runtime:', torch.version.cuda); print('cuDNN version:', torch.backends.cudnn.version())"
CUDA available: True
CUDA runtime: 12.4
cuDNN version: 90100

(DEEPLABCUT) C:\Windows\System32>python -c "import deeplabcut; import torch; print('DLC GPU enabled:', torch.cuda.is_available())"
Loading DLC 3.0.0rc8...
DLC GPU enabled: True

Anything else?

I even tried the latest cuda version and the nightly build of pytorch that works with it, but same results

Code of Conduct

Activity

  1. tsievert commented on May 1, 2025

    @tsievert
    Author

    I have now trained a minimal TF model, and when I analyze the same video file with the TF model, I get good GPU engagement and 25-30it/sec.

    So it seems as if the problem is pytorch exclusive

  2. changed the title [-]Low performance with maDLC and pytorch[/-] [+]Low performance with maDLC and pytorch (maDLC TF works fine)[/+] on May 1, 2025
  3. maximpavliv commented on May 5, 2025

    @maximpavliv
    Contributor

    Hello,

    By "terrible performance" you mean the model doesn't detect the poses well? Okay you're reporting two different issues then.
    Let's first focus on the speed and GPU engagement issue. Then we'll address the model performance issue once this is solved.

    Could you please share the contents of your config.yaml and pytorch_config.yaml please (maDLC project, pytorch model, the one that doesn't work properly)?

  4. added
    more-data-requested-by-us
    on May 5, 2025
  5. tsievert commented on May 5, 2025

    @tsievert
    Author

    Sorry, bad phrasing on my end. I was referring to the slow speed of the analysis and not the quality of the analysis.

    config.yaml

    # Project definitions (do not edit)
    Task: pig_overhead
    scorer: TS
    date: Mar12
    multianimalproject: true
    identity: false
    
    
    # Project path (change when moving around)
    project_path: E:\pig_overhead-TS-2025-03-12
    
    
    # Default DeepLabCut engine to use for shuffle creation (either pytorch or tensorflow)
    engine: pytorch
    
    
    # Annotation data set configuration (and individual video cropping parameters)
    video_sets:
      H:\30\C30.20200323_000002_1.mp4:
        crop: 0, 1280, 0, 720
      H:\30\C30.20200324_000003_1.mp4:
        crop: 0, 1280, 0, 720
      H:\30\C30.20200324_150542_1.mp4:
        crop: 0, 1280, 0, 720
      H:\30\C30.20200325_000000_1.mp4:
        crop: 0, 1280, 0, 720
    individuals:
    - piglet1
    - piglet2
    - piglet3
    - piglet4
    - piglet5
    - piglet6
    - piglet7
    - piglet8
    - piglet9
    - piglet10
    - piglet11
    - piglet12
    - piglet13
    - piglet14
    - piglet15
    - piglet16
    - piglet17
    uniquebodyparts:
    - water
    - feeder_tl
    - feeder_tr
    - feeder_bl
    - feeder_br
    - sow_snout
    - sow_left_ear_base
    - sow_left_ear_tip
    - sow_right_ear_base
    - sow_right_ear_tip
    - sow_left_eye
    - sow_right_eye
    - sow_mid_head
    - sow_neck
    - sow_mid_back
    - sow_pig_center
    - sow_mid_backend
    - sow_mid_backend2
    - sow_mid_backend3
    - sow_tail_base
    - sow_tail1
    - sow_tail2
    - sow_tail_end
    - sow_left_shoulder
    - sow_right_shoulder
    - sow_left_flank
    - sow_right_flank
    - sow_left_hip
    - sow_right_hip
    - sow_mid_belly
    - sow_left_front_hoof
    - sow_right_front_hoof
    - sow_left_hind_hoof
    - sow_right_hind_hoof
    multianimalbodyparts:
    - snout
    - left_ear_base
    - left_ear_tip
    - right_ear_base
    - right_ear_tip
    - left_eye
    - right_eye
    - mid_head
    - neck
    - mid_back
    - pig_center
    - mid_backend
    - mid_backend2
    - mid_backend3
    - tail_base
    - tail1
    - tail2
    - tail_end
    - left_shoulder
    - right_shoulder
    - left_flank
    - right_flank
    - left_hip
    - right_hip
    - mid_belly
    - left_front_hoof
    - right_front_hoof
    - left_hind_hoof
    - right_hind_hoof
    bodyparts: MULTI!
    
    
    # Fraction of video to start/stop when extracting frames for labeling/refinement
    start: 0
    stop: 1
    numframes2pick: 10
    
    
    # Plotting configuration
    skeleton:
    - - bodypart1
      - bodypart2
    - - bodypart2
      - bodypart3
    - - bodypart1
      - bodypart3
    skeleton_color: black
    pcutoff: 0.6
    dotsize: 5
    alphavalue: 0.7
    colormap: rainbow
    
    
    # Training,Evaluation and Analysis configuration
    TrainingFraction:
    - 0.95
    iteration: 0
    default_net_type: dlcrnet_ms5
    default_augmenter: multi-animal-imgaug
    default_track_method: ellipse
    snapshotindex: -1
    detector_snapshotindex: -1
    batch_size: 8
    
    
    # Cropping Parameters (for analysis and outlier frame detection)
    cropping: false
    #if cropping is true for analysis, then set the values here:
    x1: 0
    x2: 640
    y1: 277
    y2: 624
    
    
    # Refinement configuration (parameters from annotation dataset configuration also relevant in this stage)
    corner2move2:
    - 50
    - 50
    move2corner: true
    
    
    # Conversion tables to fine-tune SuperAnimal weights
    SuperAnimalConversionTables:
    detector_batch_size: 1
    

    pytorch_config.yaml

    data:
      colormode: RGB
      inference:
        normalize_images: true
      train:
        affine:
          p: 0.5
          rotation: 30
          scaling:
          - 0.5
          - 1.25
          translation: 0
        covering: false
        crop_sampling:
          width: 448
          height: 448
          max_shift: 0.1
          method: hybrid
        gaussian_noise: 12.75
        hist_eq: false
        motion_blur: false
        normalize_images: true
    device: auto
    metadata:
      project_path: E:\pig_overhead-TS-2025-03-12
      pose_config_path: 
        E:\pig_overhead-TS-2025-03-12\dlc-models-pytorch\iteration-0\pig_overheadMar12-trainset95shuffle1\train\pytorch_config.yaml
      bodyparts:
      - snout
      - left_ear_base
      - left_ear_tip
      - right_ear_base
      - right_ear_tip
      - left_eye
      - right_eye
      - mid_head
      - neck
      - mid_back
      - pig_center
      - mid_backend
      - mid_backend2
      - mid_backend3
      - tail_base
      - tail1
      - tail2
      - tail_end
      - left_shoulder
      - right_shoulder
      - left_flank
      - right_flank
      - left_hip
      - right_hip
      - mid_belly
      - left_front_hoof
      - right_front_hoof
      - left_hind_hoof
      - right_hind_hoof
      unique_bodyparts:
      - water
      - feeder_tl
      - feeder_tr
      - feeder_bl
      - feeder_br
      - sow_snout
      - sow_left_ear_base
      - sow_left_ear_tip
      - sow_right_ear_base
      - sow_right_ear_tip
      - sow_left_eye
      - sow_right_eye
      - sow_mid_head
      - sow_neck
      - sow_mid_back
      - sow_pig_center
      - sow_mid_backend
      - sow_mid_backend2
      - sow_mid_backend3
      - sow_tail_base
      - sow_tail1
      - sow_tail2
      - sow_tail_end
      - sow_left_shoulder
      - sow_right_shoulder
      - sow_left_flank
      - sow_right_flank
      - sow_left_hip
      - sow_right_hip
      - sow_mid_belly
      - sow_left_front_hoof
      - sow_right_front_hoof
      - sow_left_hind_hoof
      - sow_right_hind_hoof
      individuals:
      - piglet1
      - piglet2
      - piglet3
      - piglet4
      - piglet5
      - piglet6
      - piglet7
      - piglet8
      - piglet9
      - piglet10
      - piglet11
      - piglet12
      - piglet13
      - piglet14
      - piglet15
      - piglet16
      - piglet17
      with_identity: false
    method: bu
    model:
      backbone:
        type: ResNet
        model_name: resnet50_gn
        output_stride: 16
        freeze_bn_stats: false
        freeze_bn_weights: false
      backbone_output_channels: 2048
      heads:
        bodypart:
          type: DLCRNetHead
          predictor:
            type: PartAffinityFieldPredictor
            num_animals: 17
            num_multibodyparts: 29
            num_uniquebodyparts: 0
            nms_radius: 5
            sigma: 1.0
            locref_stdev: 7.2801
            min_affinity: 0.05
            graph: &id001
            - - 10
              - 14
            - - 6
              - 10
            - - 10
              - 21
            - - 0
              - 10
            - - 4
              - 10
            - - 10
              - 15
            - - 10
              - 20
            - - 0
              - 14
            - - 11
              - 14
            - - 7
              - 14
            - - 14
              - 19
            - - 14
              - 18
            - - 14
              - 21
            - - 1
              - 24
            - - 0
              - 1
            - - 1
              - 21
            - - 1
              - 15
            - - 1
              - 27
            - - 1
              - 26
            - - 1
              - 13
            - - 1
              - 8
            - - 1
              - 25
            - - 1
              - 11
            - - 23
              - 24
            - - 18
              - 24
            - - 12
              - 24
            - - 2
              - 24
            - - 0
              - 18
            - - 0
              - 12
            - - 0
              - 26
            - - 0
              - 2
            - - 0
              - 8
            - - 8
              - 18
            - - 18
              - 22
            - - 2
              - 18
            - - 18
              - 27
            - - 13
              - 18
            - - 3
              - 18
            - - 9
              - 18
            - - 5
              - 18
            - - 18
              - 23
            - - 6
              - 13
            - - 4
              - 6
            - - 6
              - 23
            - - 7
              - 13
            - - 13
              - 22
            - - 15
              - 23
            - - 5
              - 23
            - - 23
              - 27
            - - 9
              - 15
            - - 9
              - 12
            - - 9
              - 28
            - - 9
              - 27
            - - 9
              - 19
            - - 11
              - 15
            - - 15
              - 19
            - - 15
              - 20
            - - 15
              - 28
            - - 15
              - 25
            - - 8
              - 19
            - - 5
              - 12
            - - 5
              - 28
            - - 2
              - 5
            - - 4
              - 5
            - - 3
              - 5
            - - 12
              - 28
            - - 12
              - 19
            - - 12
              - 16
            - - 17
              - 26
            - - 17
              - 19
            - - 17
              - 28
            - - 11
              - 17
            - - 17
              - 25
            - - 17
              - 20
            - - 2
              - 17
            - - 26
              - 28
            - - 3
              - 19
            - - 19
              - 28
            - - 25
              - 28
            - - 7
              - 22
            - - 22
              - 25
            - - 21
              - 22
            - - 3
              - 25
            - - 4
              - 27
            - - 4
              - 21
            - - 20
              - 21
            - - 3
              - 21
            edges_to_keep:
            - 0
            - 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
            apply_sigmoid: true
            clip_scores: false
          target_generator:
            type: SequentialGenerator
            generators:
            - type: HeatmapPlateauGenerator
              num_heatmaps: 29
              pos_dist_thresh: 17
              heatmap_mode: KEYPOINT
              gradient_masking: false
              generate_locref: true
              locref_std: 7.2801
            - type: PartAffinityFieldGenerator
              graph: *id001
              width: 20
          criterion:
            heatmap:
              type: WeightedBCECriterion
              weight: 1.0
            locref:
              type: WeightedHuberCriterion
              weight: 0.05
            paf:
              type: WeightedHuberCriterion
              weight: 0.1
          heatmap_config:
            channels:
            - 2048
            - 29
            kernel_size:
            - 3
            strides:
            - 2
          locref_config:
            channels:
            - 2048
            - 58
            kernel_size:
            - 3
            strides:
            - 2
          paf_config:
            channels:
            - 2048
            - 174
            kernel_size:
            - 3
            strides:
            - 2
          num_stages: 5
        unique_bodypart:
          type: HeatmapHead
          weight_init: normal
          predictor:
            type: HeatmapPredictor
            apply_sigmoid: false
            clip_scores: true
            location_refinement: true
            locref_std: 7.2801
          target_generator:
            type: HeatmapGaussianGenerator
            num_heatmaps: 34
            pos_dist_thresh: 17
            heatmap_mode: KEYPOINT
            gradient_masking: false
            generate_locref: true
            locref_std: 7.2801
            label_keypoint_key: keypoints_unique
          criterion:
            heatmap:
              type: WeightedMSECriterion
              weight: 1.0
            locref:
              type: WeightedHuberCriterion
              weight: 0.05
          heatmap_config:
            channels:
            - 2048
            - 34
            kernel_size:
            - 3
            strides:
            - 2
          locref_config:
            channels:
            - 2048
            - 68
            kernel_size:
            - 3
            strides:
            - 2
    net_type: resnet_50
    runner:
      type: PoseTrainingRunner
      gpus:
      key_metric: test.mAP
      key_metric_asc: true
      eval_interval: 10
      optimizer:
        type: AdamW
        params:
          lr: 0.001
      scheduler:
        type: LRListScheduler
        params:
          lr_list:
          - - 0.0001
          - - 1e-05
          milestones:
          - 90
          - 120
      snapshots:
        max_snapshots: 5
        save_epochs: 25
        save_optimizer_state: false
    train_settings:
      batch_size: 8
      dataloader_workers: 0
      dataloader_pin_memory: false
      display_iters: 500
      epochs: 200
      seed: 42
    
  6. removed
    more-data-requested-by-us
    on May 5, 2025
  7. maximpavliv commented on May 7, 2025

    @maximpavliv
    Contributor

    Hello @tsievert,

    Thank you very much for the detailed explanations.

    I indeed can reproduce this issue when using your config files.
    I also noticed that the evalutation stage (during training) shows the same behavior (GPU isn't used, and it takes a huge amount of time). Interestingly, the training process itself does use the GPU correctly, and is executed quite fast! What I also noticed that, during evaluation and video analysis, the RAM is getting crammed.

    What seems special with your configs is that you have a lot of individuals (17), each with 29 bodyparts, plus also 34 unique bodyparts; I guess this is what brings out a potential issue in the processing pipeline.

    At this point I don't know what is to blame, I'll continue investigating this.

    You say that when using TensorFlow as engine - the inference time is decent and the GPU is properly used? Do you have as many individuals, bodyparts and unique bodyparts in your Tensorflow project too?

  8. changed the title [-]Low performance with maDLC and pytorch (maDLC TF works fine)[/-] [+]Huge inference time, GPU isn't used with maDLC and Pytorch[/+] on May 7, 2025
  9. tsievert commented on May 7, 2025

    @tsievert
    Author

    Hey,
    great that you can reproduce the issue!
    My tensorflow model was trained on the same data as the pytorch model, and uses the same bodyparts, unique bodyparts, and number of individuals.
    After having letting it run now for a while, I noticed that the initial analysis speed drops down to 1.12it/s after 25h of analyzing the same video file.
    Still faster than the 9s/it I got with pytorch though

  10. maximpavliv commented on May 8, 2025

    @maximpavliv
    Contributor

    Hey, I tried a few experiments:

    • Same config as yours but without the unique bodyparts

    • Same config as yours but half the number of individuals (8 instead of 17).

    In both these cases the analysis time looks acceptable.

    Then I tried with the original config again (17 individuals and unique bodyparts), the analysis takes ages (actually it seems to get stuck in my case).

    Do you observe something similar on your side too?

  11. AlexEMG commented on May 9, 2025

    @AlexEMG
    Member

    @tsievert -- please note that you use a bottom-up, two-stage models. The model predicts detections & costs, then the poses are assembled. See : https://www.nature.com/articles/s41592-022-01443-0

    The assembly is mostly based on the CPU, the computational costs scales in the number of detections (which is large when you have many individuals). Now, if your pose estimation is not well trained, then you get even more detections & poor costs, which will make the assembly step very slow (and most of the wall time will be spend on the CPU). Seems like in TF it works, how do the two models compare quantitatively (performance)?

    So here are a few ideas:

    • try out DEKR, which does not rely on assembly methods.
    • make sure the pose estimation model doesn't give additional detections
    • consider pruning the graph for assembly (see Nature Methods paper)
  12. tsievert commented on May 15, 2025

    @tsievert
    Author

    Hey @AlexEMG, thanks for chiming in!
    Here the performance of the two models

    pytorch

    Column1 Column2
    %Training dataset 0.95
    Shuffle number 2
    Training epochs 170
    Detector epochs (TD only) -1
    pcutoff 0.6
    train rmse 31.52
    train rmse_pcutoff 12.97
    train mAP 28.51
    train mAR 32.2
    test rmse 41.01
    test rmse_pcutoff 13.7
    test mAP 23.48
    test mAR 24.78

    TensorFlow

    Column1 Column2
      0
    Training iterations: 200000
    %Training dataset 95
    Shuffle number 1
    Train error(px) 4.55
    Test error(px) 14.71
    p-cutoff used 0.6
    Train error with p-cutoff 3.18
    Test error with p-cutoff 13.3

    I will look into DEKR and train up a model to test it, and I will also follow @maximpavliv advice and reduce the unique body parts (I can't reduce individuals unfortunately).

  13. tsievert commented on May 21, 2025

    @tsievert
    Author

    The DEKR model runs with 10-14 it/through the video file, but when it comes to saving the h5, it errors out.

    INFO:console:E:\pig_overhead-TS-2025-03-12\videos\C31.20200324_150542_1.mp4
    INFO:console:Loading From E:\pig_overhead-TS-2025-03-12\videos\C31.20200324_150542_1DLC_DekrW32_pig_overheadMar12shuffle3_snapshot_290.h5
    100%|████████████████████████████████████████████████████████████████████████| 481250/481250 [05:57<00:00, 1347.68it/s]
    INFO:console:The tracklets were created (i.e., under the hood deeplabcut.convert_detections2tracklets was run). Now you can 'refine_tracklets' in the GUI, or run 'deeplabcut.stitch_tracklets'.
    INFO:console:Processing...
    INFO:console:
    INFO:console:E:\pig_overhead-TS-2025-03-12\videos\C31.20200324_150542_1.mp4
    C:\Users\au783787\AppData\Local\anaconda3\envs\DLC_PY\lib\site-packages\deeplabcut\core\trackingutils.py:46: RuntimeWarning: invalid value encountered in scalar divide
      return wh / (
    

    The GUI still shows the progress bar moving, but I don't see any GPU or CPU activity.
    In the folder I see four pickle files associated with the video, but no H5 file.

  14. MMathisLab commented on May 23, 2025

    @MMathisLab
    Member

    Perhaps back to an installation issue; given your CUDA version & pytorch version -- how did you install it?

    CUDA runtime: 12.4

    for example, you need to be sure you install the correct pytorch for your CUDA version:

    # GPU version of pytorch for CUDA 11.4
    conda install pytorch cudatoolkit=11.4 -c pytorch
  15. tsievert commented on May 28, 2025

    @tsievert
    Author

    I have since the beginning of the issue reinstalled DLC in a new environment (because it did not like switching between TF and pytorch).
    I now have a global installation of CUDA 12.8 (confirmed by nvidia-smi) and inside my environment I have reinstalled torch according to the official instruction pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128.
    My environment also returns 2.7.0+cu128 for the torch version.
    So far, when I had a mismatch between my cuda and TF or pytorch versions, the model completely refused to run on a GPU and fell back to CPU only, but I've never had it fail to assemble the h5 before.

  16. deruyter92 commented on Dec 8, 2025

    @deruyter92
    Collaborator

    Hi @tsievert, not sure if this was solved already. Are you still experiencing this issue with version 3.0.0rc13? I was just reading into this discussion and happy to assist in case this problem persists.

  17. tsievert commented on Dec 11, 2025

    @tsievert
    Author

    Hey @deruyter92 , thanks for checking in!
    I think the issue was related to #3108 in the end. At the moment I am running maDLC with this branch https://github.com/DeepLabCut/DeepLabCut/tree/maxim/fix_shelf_writer without any issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions