pytorch3d/tests/test_rendering_utils.py
Patrick Labatut 3c71ab64cc Remove shebang line when not strictly required
Summary: The shebang line `#!<path to interpreter>` is only required for Python scripts, so remove it on source files for class or function definitions. Additionally explicitly mark as executable the actual Python scripts in the codebase.

Reviewed By: nikhilaravi

Differential Revision: D20095778

fbshipit-source-id: d312599fba485e978a243292f88a180d71e1b55a
2020-03-12 10:39:44 -07:00

64 lines
2.1 KiB
Python

# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import unittest
import torch
from pytorch3d.renderer.utils import TensorProperties
from common_testing import TestCaseMixin
# Example class for testing
class TensorPropertiesTestClass(TensorProperties):
def __init__(self, x=None, y=None, device="cpu"):
super().__init__(device=device, x=x, y=y)
def clone(self):
other = TensorPropertiesTestClass()
return super().clone(other)
class TestTensorProperties(TestCaseMixin, unittest.TestCase):
def test_init(self):
example = TensorPropertiesTestClass(x=10.0, y=(100.0, 200.0))
# Check kwargs set as attributes + converted to tensors
self.assertTrue(torch.is_tensor(example.x))
self.assertTrue(torch.is_tensor(example.y))
# Check broadcasting
self.assertTrue(example.x.shape == (2,))
self.assertTrue(example.y.shape == (2,))
self.assertTrue(len(example) == 2)
def test_to(self):
# Check to method
example = TensorPropertiesTestClass(x=10.0, y=(100.0, 200.0))
device = torch.device("cuda:0")
new_example = example.to(device=device)
self.assertTrue(new_example.device == device)
def test_clone(self):
# Check clone method
example = TensorPropertiesTestClass(x=10.0, y=(100.0, 200.0))
new_example = example.clone()
self.assertSeparate(example.x, new_example.x)
self.assertSeparate(example.y, new_example.y)
def test_get_set(self):
# Test getitem returns an accessor which can be used to modify
# attributes at a particular index
example = TensorPropertiesTestClass(x=10.0, y=(100.0, 200.0, 300.0))
# update y1
example[1].y = 5.0
self.assertTrue(example.y[1] == 5.0)
# Get item and get value
ex0 = example[0]
self.assertTrue(ex0.y == 100.0)
def test_empty_input(self):
example = TensorPropertiesTestClass(x=(), y=())
self.assertTrue(len(example) == 0)
self.assertTrue(example.isempty())