Skip to content

TypeError: can't assign a numpy.float32 to a torch.FloatTensor #3512

Closed

Description

@anthonyattacks

Is there an existing issue for this?

  • I have searched the existing issues

Operating System

ubuntu 24.04 desktop, vs code insiders, conda, numpy 2.4

DeepLabCut version

Name Version Build Channel

deeplabcut 3.0.0rc14 pypi_0 pypi
napari-deeplabcut 0.3.1.1 pypi_0 pypi

What engine are you using?

pytorch

DeepLabCut mode

multi animal

Device type

GeForce RTX 4070 Ti; cuda version 13.2, driver 595.84; 13th Gen Intel(R) Core(TM) i9-13900K

Bug description 🐛

DLC hits corrupted frames (unavoidable across 600,000 videos with our hardware), attempts to assign NaN. pytorch catches it / it violates strict type checking.

Fix:
/deeplabcut/pose_estimation_pytorch/models/predictors/paf_predictor.py
line 199: poses[i, j, :, 4] = assembly.affinity.item()

added .item(), extracts item directly. pose prediction then functions as expected.

Steps To Reproduce

load video with corrupted frame --> it'll end up a NaN during deeplabcut.analyze_videos pass.

Relevant log output

lost it when i fixed the bug (see above, fix is in description)

Anything else?

No response

Code of Conduct

Activity

  1. anthonyattacks commented on Sep 13, 2026

    @anthonyattacks
    Author

    Correction to the fix, apparently. The NaN gets pushed downstream

    File ~/miniconda3/envs/finchesPy/lib/python3.12/site-packages/deeplabcut/utils/make_labeled_video.py:1309, in create_video_with_all_detections(config, videos, videotype, shuffle, trainingsetindex, displayedbodyparts, cropping, destfolder, modelprefix, confidence_to_alpha, plot_bboxes, **kwargs)
    1307 print("Creating labeled video for ", video_name)
    1308 h5file = full_pickle.replace("_full.pickle", ".h5")
    -> 1309 data, metadata = auxfun_multianimal.LoadFullMultiAnimalData(h5file)
    1310 data = dict(
    1311 data
    1312 ) # Cast to dict (making a copy) so items can safely be popped
    1314 x1, y1 = 0, 0

    File ~/miniconda3/envs/finchesPy/lib/python3.12/site-packages/deeplabcut/utils/auxfun_multianimal.py:221, in LoadFullMultiAnimalData(dataname)
    219 data = pickle.load(handle)
    220 except (pickle.UnpicklingError, FileNotFoundError):
    --> 221 data = shelve.open(data_file, flag="r")
    222 with open(data_file.replace("_full.", "_meta."), "rb") as handle:
    223 metadata = pickle.load(handle)

    File ~/miniconda3/envs/finchesPy/lib/python3.12/shelve.py:243, in open(filename, flag, protocol, writeback)
    230 def open(filename, flag='c', protocol=None, writeback=False):
    231 """Open a persistent dictionary for reading and writing.
    232
    233 The filename parameter is the base filename for the underlying
    (...) 240 See the module's doc string for an overview of the interface.
    241 """
    --> 243 return DbfilenameShelf(filename, flag, protocol, writeback)

    File ~/miniconda3/envs/finchesPy/lib/python3.12/shelve.py:227, in DbfilenameShelf.init(self, filename, flag, protocol, writeback)
    225 def init(self, filename, flag='c', protocol=None, writeback=False):
    226 import dbm
    --> 227 Shelf.init(self, dbm.open(filename, flag), protocol, writeback)

    File ~/miniconda3/envs/finchesPy/lib/python3.12/dbm/init.py:85, in open(file, flag, mode)
    83 mod = _defaultmod
    84 else:
    ---> 85 raise error[0]("db file doesn't exist; "
    86 "use 'c' or 'n' flag to create a new db")
    87 elif result == "":
    88 # db type cannot be determined
    89 raise error[0]("db type could not be determined")

    error: db file doesn't exist; use 'c' or 'n' flag to create a new db

    proposed solution: edit /deeplabcut/pose_estimation_pytorch/data/postprocessor.py;

    on line 542, between the for loop and the heatmap_indices variable, insert to cast nans to 0.0

        for pred_idx, individual_keypoints in enumerate(pose):
            individual_keypoints = np.nan_to_num(individual_keypoints, nan=0.0)
            heatmap_indices = np.rint(individual_keypoints).astype(int)
    

    -----can fully confirm, with the above two fixes to the source, problem is solved. Recommend adjusting source to match.

  2. anthonyattacks commented on Sep 13, 2026

    @anthonyattacks
    Author

    whoops, didn't mean to close (impatient clicking, saturday night)

  3. C-Achard commented on Sep 14, 2026

    @C-Achard
    Member

    Hello @anthonyattacks,

    Thanks for the report, this was fixed recently in #3508 (see 5cf10c9).

    We will ideally include it in the next release.

    Thanks and best,
    Cyril

  4. linked a pull request that will close this issueFollow-up fixes for numpy2 compatibility #3508on Sep 14, 2026
  5. added theissue type on Sep 14, 2026
  6. added
    bug
    on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpytorch

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions