Skip to content

Commit

Permalink
Fix camera clone() with torch.save
Browse files Browse the repository at this point in the history
Summary:
User reported that cloned cameras fail to save. The error with latest PyTorch is

```
pickle.PicklingError: Can't pickle ~T_destination: attribute lookup T_destination on torch.nn.modules.module failed
```

This fixes it.

Reviewed By: btgraham

Differential Revision: D39692258

fbshipit-source-id: 75bbf3b8dfa0023dc28bf7d4cc253ca96e46a64d
  • Loading branch information
bottler authored and facebook-github-bot committed Sep 22, 2022
1 parent ce3fce4 commit efea540
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pytorch3d/renderer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import copy
import inspect
import warnings
from typing import Any, List, Optional, Tuple, Union
from typing import Any, List, Optional, Tuple, TypeVar, Union

import numpy as np
import torch
Expand Down Expand Up @@ -191,7 +191,7 @@ def clone(self, other) -> "TensorProperties":
"""
for k in dir(self):
v = getattr(self, k)
if inspect.ismethod(v) or k.startswith("__"):
if inspect.ismethod(v) or k.startswith("__") or type(v) is TypeVar:
continue
if torch.is_tensor(v):
v_clone = v.clone()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# SOFTWARE.

import math
import pickle
import typing
import unittest
from itertools import product
Expand Down Expand Up @@ -1333,6 +1334,11 @@ def test_getitem(self):
# Check in_ndc is handled correctly
self.assertEqual(cam._in_ndc, c0._in_ndc)

def test_clone_picklable(self):
camera = PerspectiveCameras()
pickle.dumps(camera)
pickle.dumps(camera.clone())


############################################################
# FishEye Camera #
Expand Down

0 comments on commit efea540

Please sign in to comment.