def __call__(self, data): if data.pos.size(-1) == 2: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) matrix = [[cos, sin], [-sin, cos]] else: m1, m2, m3 = torch.eye(3), torch.eye(3), torch.eye(3) if 0 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m1 = torch.tensor([[1, 0, 0], [0, cos, sin], [0, -sin, cos]]) if 1 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m2 = torch.tensor([[cos, 0, -sin], [0, 1, 0], [sin, 0, cos]]) if 2 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m3 = torch.tensor([[cos, sin, 0], [-sin, cos, 0], [0, 0, 1]]) matrix = torch.mm(torch.mm(m1, m2), m3) # LinearTransformation only rotates `.pos`; need to rotate `.cell` too. data_rotated = LinearTransformation(matrix)(data) if hasattr(data_rotated, "cell"): data_rotated.cell = torch.matmul(data_rotated.cell, matrix) return ( data_rotated, matrix, torch.inverse(matrix), )
def test_cartesian(): matrix = torch.tensor([[2, 0], [0, 2]], dtype=torch.float) transform = LinearTransformation(matrix) out = 'LinearTransformation([[2.0, 0.0], [0.0, 2.0]])' assert transform.__repr__() == out pos = torch.tensor([[-1, 1], [-3, 0], [2, -1]], dtype=torch.float) data = Data(pos=pos) out = transform(data).pos.tolist() assert out == [[-2, 2], [-6, 0], [4, -2]]
def test_cartesian(): matrix = torch.tensor([[2, 0], [0, 2]], dtype=torch.float) transform = LinearTransformation(matrix) assert transform.__repr__() == ('LinearTransformation(' '[[2.0, 0.0], [0.0, 2.0]])') pos = torch.Tensor([[-1, 1], [-3, 0], [2, -1]]) data = Data(pos=pos) data = transform(data) assert len(data) == 1 assert data.pos.tolist() == [[-2, 2], [-6, 0], [4, -2]]
def __call__(self, data): if data.pos.size(-1) == 2: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) matrix = [[cos, sin], [-sin, cos]] else: m1, m2, m3 = torch.eye(3), torch.eye(3), torch.eye(3) if 0 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m1 = torch.tensor([[1, 0, 0], [0, cos, sin], [0, -sin, cos]]) if 1 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m2 = torch.tensor([[cos, 0, -sin], [0, 1, 0], [sin, 0, cos]]) if 2 in self.axes: degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) m3 = torch.tensor([[cos, sin, 0], [-sin, cos, 0], [0, 0, 1]]) matrix = torch.mm(torch.mm(m1, m2), m3) return ( LinearTransformation(matrix)(data), matrix, torch.inverse(matrix), )
def __call__(self, data): dim = data.pos.size(-1) matrix = data.pos.new_empty(dim, dim).uniform_(-self.shear, self.shear) eye = torch.arange(dim, dtype=torch.long) matrix[eye, eye] = 1 return LinearTransformation(matrix)(data)
def __call__(self, data): degree = math.pi * random.uniform(*self.degrees) / 180.0 sin, cos = math.sin(degree), math.cos(degree) if data.pos.size(1) == 2: matrix = [[cos, sin], [-sin, cos]] else: if self.axis == 0: matrix = [[1, 0, 0], [0, cos, sin], [0, -sin, cos]] elif self.axis == 1: matrix = [[cos, 0, -sin], [0, 1, 0], [sin, 0, cos]] else: matrix = [[cos, sin, 0], [-sin, cos, 0], [0, 0, 1]] return LinearTransformation(torch.tensor(matrix))(data)
def test_linear_transformation(): matrix = torch.tensor([[2, 0], [0, 2]], dtype=torch.float) transform = LinearTransformation(matrix) assert str(transform) == ('LinearTransformation(\n' '[[2. 0.]\n' ' [0. 2.]]\n' ')') pos = torch.Tensor([[-1, 1], [-3, 0], [2, -1]]) data = Data(pos=pos) data = transform(data) assert len(data) == 1 assert data.pos.tolist() == [[-2, 2], [-6, 0], [4, -2]]
def generate_validation_dataset(self): val_set = ShapeNet( ShapeNetGenerator.PATH, categories=self.categories, include_normals=False, split='val', transform=LinearTransformation( torch.tensor([ [2.0, 0, 0], [0, 2.0, 0], [0, 0, 2.0] ]) ) ) self.val_size = len(val_set) return val_set