Skip to content

analyze videos error: ZeroDivisionError: division by zero ( BUCTD) #2989

Closed

Description

@HLH2000

Is there an existing issue for this?

  • I have searched the existing issues

Operating System

COLAB

DeepLabCut version

3.0.0rc8

What engine are you using?

pytorch

DeepLabCut mode

multi animal

Device type

Tesla T4

Bug description 🐛

When I try to analyze my multi-animal project using BUCTD models on Colab, I encounter an error during the video analysis step. I have verified that both networks were successfully trained by adjusting the shuffle number to 1 and 3, and by setting ctd_tracking=False. However, I am unsure if this error is related to previous issues (#1818, #2684).

Steps To Reproduce

BU_SHUFFLE = 1( net_type: resnet_50)
CTD_SHUFFLE = 3( net_type: ctd_prenet_cspnext_m)
config.txt

Relevant log output

deeplabcut.analyze_videos(
    config,
    videofile_path,
    shuffle=CTD_SHUFFLE,
    ctd_tracking=True,
    save_as_csv=True,
)


Analyzing videos with /content/drive/My Drive/LDx_v4m-HLH-2025-05-14/dlc-models-pytorch/iteration-1/LDx_v4mMay14-trainset80shuffle3/train/snapshot-best-070.pt
CTD tracking can only be used with batch size 1. Updating it.
Starting to analyze /content/drive/My Drive/LDx_v4m-HLH-2025-05-14/videos/mod3.mkv
Video metadata: 
  Overall # of frames:    969
  Duration of video [s]:  31.98
  fps:                    30.3
  resolution:             w=1920, h=1080

Running pose prediction with batch size 1

  4%|▍         | 37/969 [00:03<01:21, 11.49it/s]

---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-10-74060d4d252a> in <cell line: 0>()
----> 1 deeplabcut.analyze_videos(
      2     config,
      3     videofile_path,
      4     shuffle=CTD_SHUFFLE,
      5     ctd_tracking=True,

9 frames

/usr/local/lib/python3.11/dist-packages/deeplabcut/compat.py in analyze_videos(config, videos, videotype, shuffle, trainingsetindex, gputouse, save_as_csv, in_random_order, destfolder, batchsize, cropping, TFGPUinference, dynamic, modelprefix, robust_nframes, allow_growth, use_shelve, auto_track, n_tracks, animal_names, calibrate, identity_only, use_openvino, engine, **torch_kwargs)
    952                 torch_kwargs["batch_size"] = batchsize
    953 
--> 954         return analyze_videos(
    955             config,
    956             videos=videos,

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/apis/videos.py in analyze_videos(config, videos, videotype, shuffle, trainingsetindex, save_as_csv, in_random_order, snapshot_index, detector_snapshot_index, device, destfolder, batch_size, detector_batch_size, dynamic, ctd_conditions, ctd_tracking, top_down_dynamic, modelprefix, use_shelve, robust_nframes, transform, auto_track, n_tracks, animal_names, calibrate, identity_only, overwrite, cropping, save_as_df)
    543         else:
    544             runtime = [time.time()]
--> 545             predictions = video_inference(
    546                 video=video_iterator,
    547                 pose_runner=pose_runner,

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/apis/videos.py in video_inference(video, pose_runner, detector_runner, cropping, shelf_writer, robust_nframes)
    202         shelf_writer.open()
    203 
--> 204     predictions = pose_runner.inference(images=tqdm(video), shelf_writer=shelf_writer)
    205     if shelf_writer is not None:
    206         shelf_writer.close()

/usr/local/lib/python3.11/dist-packages/torch/utils/_contextlib.py in decorate_context(*args, **kwargs)
    114     def decorate_context(*args, **kwargs):
    115         with ctx_factory():
--> 116             return func(*args, **kwargs)
    117 
    118     return decorate_context

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/runners/inference.py in inference(self, images, shelf_writer)
    392         """
    393         if self.tracking:
--> 394             return self._ctd_tracking_inference(images, shelf_writer)
    395 
    396         results = []

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/runners/inference.py in _ctd_tracking_inference(self, images, shelf_writer)
    487         results = []
    488         for data in images:
--> 489             inputs, context = self._prepare_ctd_inputs(data)
    490             model_kwargs = context.pop("model_kwargs", {})
    491             predictions = self.predict(inputs, **model_kwargs)

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/runners/inference.py in _prepare_ctd_inputs(self, data)
    545             return torch.as_tensor(inputs), context
    546 
--> 547         inputs, context = self.preprocessor(inputs, context)
    548         return inputs, context
    549 

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/data/preprocessor.py in __call__(self, image, context)
    176     def __call__(self, image: Image, context: Context) -> tuple[Image, Context]:
    177         for preprocessor in self.components:
--> 178             image, context = preprocessor(image, context)
    179         return image, context
    180 

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/data/preprocessor.py in __call__(self, image, context)
    389         images, offsets, scales = [], [], []
    390         for bbox in context["bboxes"]:
--> 391             crop, offset, scale = top_down_crop(
    392                 image,
    393                 bbox,

/usr/local/lib/python3.11/dist-packages/deeplabcut/pose_estimation_pytorch/data/image.py in top_down_crop(image, bbox, output_size, margin, center_padding, crop_with_context)
    273     w, h = x2 - x1, y2 - y1
    274     if not crop_with_context:
--> 275         input_ratio = w / h
    276         output_ratio = out_w / out_h
    277         if input_ratio > output_ratio:  # h/w < h0/w0 => h' = w * h0/w0

ZeroDivisionError: division by zero

Anything else?

No response

Code of Conduct

Activity

  1. added
    bug
    CTD
    on May 21, 2025
  2. horsethey commented on May 22, 2025

    @horsethey

    Have been having a similar issue when trying to extract frames using kmeans clustering from more than one video at a time using DLC 3 on an M1 chip. I can extract frames from a video at a time, but when I select two videos or try to extract frames from another video after frames have been successfully extracted once, I get the following error:
    /opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/numpy/core/_methods.py:118: RuntimeWarning: invalid value encountered in reduce ret = umr_sum(arr, axis, dtype, out, keepdims, where=where) /opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/deeplabcut/utils/frameselectiontools.py:339: RuntimeWarning: invalid value encountered in subtract data = DATA - DATA.mean(axis=0) Traceback (most recent call last): File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/deeplabcut/gui/utils.py", line 37, in run self.outputs = self.func() File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/deeplabcut/generate_training_dataset/frame_extraction.py", line 383, in extract_frames frames2pick = frameselectiontools.KmeansbasedFrameselectioncv2( File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/deeplabcut/utils/frameselectiontools.py", line 345, in KmeansbasedFrameselectioncv2 kmeans.fit(data) File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/base.py", line 1389, in wrapper return fit_method(estimator, *args, **kwargs) File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py", line 2070, in fit X = validate_data( File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/utils/validation.py", line 2944, in validate_data out = check_array(X, input_name="X", **check_params) File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1107, in check_array _assert_all_finite( File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/utils/validation.py", line 120, in _assert_all_finite _assert_all_finite_element_wise( File "/opt/miniconda3/envs/deeplabcut/lib/python3.10/site-packages/sklearn/utils/validation.py", line 169, in _assert_all_finite_element_wise raise ValueError(msg_err) ValueError: Input X contains NaN. MiniBatchKMeans does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-values

  3. maximpavliv commented on May 23, 2025

    @maximpavliv
    Contributor

    Hi @HLH2000 ,

    Thanks for reporting this issue, good catch!

    I've identified the bug and submitted a fix in #2984, which should resolve the problem you describe.

    @horsethey: It looks like the issue you're experiencing is different from the one originally reported here. To keep things organized and easier to track, could you please open a separate issue with the details? That will help us address it more effectively without mixing contexts.

    Thanks!

  4. HLH2000 commented on May 28, 2025

    @HLH2000
    Author

    It works now, thank you.

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

Metadata

Metadata

Assignees

Labels

COLABCTDbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions