torch-geopooling 1.0.1__cp310-cp310-macosx_11_0_arm64.whl → 1.1.1__cp310-cp310-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of torch-geopooling might be problematic. Click here for more details.

Binary file
@@ -11,14 +11,17 @@ namespace torch_geopooling {
11
11
 
12
12
  PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
13
13
  {
14
- m.def("quad_pool2d", &quad_pool2d);
15
- m.def("quad_pool2d_backward", &quad_pool2d_backward);
14
+ m.def("avg_quad_pool2d", &avg_quad_pool2d);
15
+ m.def("avg_quad_pool2d_backward", &avg_quad_pool2d_backward);
16
16
 
17
17
  m.def("max_quad_pool2d", &max_quad_pool2d);
18
18
  m.def("max_quad_pool2d_backward", &max_quad_pool2d_backward);
19
19
 
20
- m.def("avg_quad_pool2d", &avg_quad_pool2d);
21
- m.def("avg_quad_pool2d_backward", &avg_quad_pool2d_backward);
20
+ m.def("quad_pool2d", &quad_pool2d);
21
+ m.def("quad_pool2d_backward", &quad_pool2d_backward);
22
+
23
+ m.def("embedding2d", &embedding2d);
24
+ m.def("embedding2d_backward", &embedding2d_backward);
22
25
  }
23
26
 
24
27
 
@@ -1,3 +1,3 @@
1
1
  from typing import Final
2
2
 
3
- __version__: Final[str] = "1.0.1"
3
+ __version__: Final[str] = "1.1.1"
@@ -0,0 +1,2 @@
1
+ from torch_geopooling.functional.embedding import * # noqa
2
+ from torch_geopooling.functional.pooling import * # noqa
@@ -0,0 +1,85 @@
1
+ # Copyright (C) 2024, Yakau Bubnou
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+
16
+ from typing import Optional, Tuple
17
+
18
+ from torch import Tensor, autograd
19
+ from torch.autograd.function import FunctionCtx
20
+
21
+ import torch_geopooling._C as _C
22
+ from torch_geopooling.tiling import ExteriorTuple
23
+
24
+
25
+ __all__ = ["embedding2d"]
26
+
27
+
28
+ class Function(autograd.Function):
29
+ @staticmethod
30
+ def forward(
31
+ input: Tensor,
32
+ weight: Tensor,
33
+ padding: Tuple[int, int],
34
+ exterior: ExteriorTuple,
35
+ ) -> Tensor:
36
+ return _C.embedding2d(input, weight, padding, exterior)
37
+
38
+ @staticmethod
39
+ def setup_context(ctx: FunctionCtx, inputs: Tuple, outputs: Tuple) -> None:
40
+ input, weight, padding, exterior = inputs
41
+
42
+ ctx.save_for_backward(input, weight)
43
+ ctx.padding = padding
44
+ ctx.exterior = exterior
45
+
46
+ @staticmethod
47
+ def backward(ctx: FunctionCtx, grad: Tensor) -> Tuple[Optional[Tensor], ...]:
48
+ input, weight = ctx.saved_tensors
49
+ grad_weight = _C.embedding2d_backward(grad, input, weight, ctx.padding, ctx.exterior)
50
+ return None, grad_weight, None, None
51
+
52
+
53
+ def embedding2d(
54
+ input: Tensor,
55
+ weight: Tensor,
56
+ *,
57
+ padding: Tuple[int, int] = (0, 0),
58
+ exterior: ExteriorTuple,
59
+ ) -> Tensor:
60
+ """
61
+ Retrieves spatial embeddings from a fixed-size lookup table based on 2D coordinates.
62
+
63
+ This function accepts a list of (x, y) coordinates and retrieves the corresponding
64
+ spatial embeddings from a provided embedding matrix. The embeddings are selected
65
+ based on the input coordinates, with an optional padding to include neighboring cells.
66
+ See :class:`torch_geopooling.nn.Embedding2d` for more details.
67
+
68
+ Args:
69
+ input: A list of 2D coordinates where each coordinate is represented as a tuple (x, y),
70
+ where x is the longitude and y is the latitude.
71
+ weight: A 3D tensor representing the embedding matrix. The first dimension corresponds to
72
+ the maximum possible bucket for the x coordinate, the second dimension corresponds to
73
+ the maximum possible bucket for the y coordinate, and the third dimension corresponds
74
+ to the embedding size.
75
+ padding: The size of the neighborhood to query. Default is 0, meaning only the embedding
76
+ for the exact input coordinate is retrieved.
77
+ exterior: The geometric boundary of the learning space, specified as a tuple (X, Y, W, H),
78
+ where X and Y represent the origin, and W and H represent the width and height of the
79
+ space, respectively.
80
+
81
+ Returns:
82
+ Tensor: The retrieved spatial embeddings corresponding to the input coordinates.
83
+ """
84
+
85
+ return Function.apply(input, weight, padding, exterior)
@@ -0,0 +1,31 @@
1
+ # Copyright (C) 2024, Yakau Bubnou
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+
16
+ import torch
17
+ from torch_geopooling.functional.embedding import embedding2d
18
+
19
+
20
+ def test_embedding2d() -> None:
21
+ input = torch.rand((100, 2), dtype=torch.float64) * 10.0
22
+ weight = torch.rand((1024, 1024, 3), dtype=torch.float64)
23
+
24
+ result = embedding2d(
25
+ input,
26
+ weight,
27
+ padding=(3, 2),
28
+ exterior=(-10.0, -10.0, 20.0, 20.0),
29
+ )
30
+
31
+ assert result.size() == torch.Size([100, 7, 5, 3])
@@ -16,7 +16,7 @@
16
16
  import pytest
17
17
  import torch
18
18
 
19
- from torch_geopooling.functional import (
19
+ from torch_geopooling.functional.pooling import (
20
20
  AdaptiveFunction,
21
21
  adaptive_quad_pool2d,
22
22
  adaptive_avg_quad_pool2d,
@@ -0,0 +1,2 @@
1
+ from torch_geopooling.nn.embedding import * # noqa
2
+ from torch_geopooling.nn.pooling import * # noqa
@@ -0,0 +1,90 @@
1
+ # Copyright (C) 2024, Yakau Bubnou
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+
16
+ from typing import Union, Tuple, cast
17
+
18
+ import torch
19
+ from torch import Tensor, nn
20
+
21
+ from torch_geopooling import functional as F
22
+ from torch_geopooling.tiling import Exterior, ExteriorTuple
23
+
24
+
25
+ __all__ = [
26
+ "Embedding2d",
27
+ ]
28
+
29
+
30
+ _Exterior = Union[Exterior, ExteriorTuple]
31
+
32
+
33
+ class Embedding2d(nn.Module):
34
+ """
35
+ Retrieves spatial embeddings from a fixed-size lookup table based on 2D coordinates.
36
+
37
+ This module accepts a tensor of (x, y) coordinates and retrieves the corresponding
38
+ spatial embeddings from a provided embedding matrix. The embeddings are selected
39
+ based on the input coordinates, with an optional padding to include neighboring cells.
40
+
41
+ Args:
42
+ manifold: The size of the 2-dimensional embedding in a form (W, H, N), where
43
+ W is a width, H is a height, and N is a feature dimension of the embedding.
44
+ padding: The size of the neighborhood to query. Default is 0, meaning only the embedding
45
+ for the exact input coordinate is retrieved.
46
+ exterior: The geometric boundary of the learning space, specified as a tuple (X, Y, W, H),
47
+ where X and Y represent the origin, and W and H represent the width and height of the
48
+ space, respectively.
49
+
50
+ Shape:
51
+ - Input: :math:`(*, 2)`, where 2 comprises x and y coordinates.
52
+ - Output: :math:`(*, X_{out}, Y_{out}, N)`, where * is the input shape, \
53
+ :math:`N = \\text{manifold[2]}`, and
54
+
55
+ :math:`X_{out} = \\text{padding}[0] \\times 2 + 1`
56
+
57
+ :math:`Y_{out} = \\text{padding}[1] \\times 2 + 1`
58
+
59
+ Examples:
60
+
61
+ >>> # Create an embedding of EPSG:4326 rectangle into 1024x1024 embedding
62
+ >>> # with 3 features in each cell.
63
+ >>> embedding = nn.Embedding2d(
64
+ ... (1024, 1024, 3),
65
+ ... exterior=(-180.0, -90.0, 360.0, 180.0),
66
+ ... padding=(2, 2),
67
+ ... )
68
+ >>> input = torch.rand((100, 2), dtype=torch.float64) * 60.0
69
+ >>> output = embedding(input)
70
+ """
71
+
72
+ def __init__(
73
+ self,
74
+ manifold: Tuple[int, int, int],
75
+ exterior: _Exterior,
76
+ padding: Tuple[int, int] = (0, 0),
77
+ ) -> None:
78
+ super().__init__()
79
+ self.manifold = manifold
80
+ self.exterior = cast(ExteriorTuple, tuple(map(float, exterior)))
81
+ self.padding = padding
82
+
83
+ self.weight = nn.Parameter(torch.empty(manifold, dtype=torch.float64))
84
+ nn.init.zeros_(self.weight)
85
+
86
+ def extra_repr(self) -> str:
87
+ return "{manifold}, exterior={exterior}, padding={padding}".format(**self.__dict__)
88
+
89
+ def forward(self, input: Tensor) -> Tensor:
90
+ return F.embedding2d(input, self.weight, exterior=self.exterior, padding=self.padding)
@@ -0,0 +1,48 @@
1
+ # Copyright (C) 2024, Yakau Bubnou
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+
16
+ import pytest
17
+ import torch
18
+ from torch import nn
19
+ from torch.optim import SGD
20
+
21
+ from torch_geopooling.nn.embedding import Embedding2d
22
+
23
+
24
+ def test_embedding2d_optimize() -> None:
25
+ embedding = Embedding2d(
26
+ (2, 2, 1),
27
+ padding=(0, 0),
28
+ exterior=(-180.0, -90.0, 360.0, 180.0),
29
+ )
30
+
31
+ x_true = torch.tensor(
32
+ [[90.0, 45.0], [90.0, -45.0], [-90.0, -45.0], [-90.0, 45.0]], dtype=torch.float64
33
+ )
34
+ y_true = torch.tensor([[10.0], [20.0], [30.0], [40.0]], dtype=torch.float64)
35
+
36
+ optim = SGD(embedding.parameters(), lr=0.1)
37
+ loss_fn = nn.L1Loss()
38
+
39
+ for i in range(10000):
40
+ optim.zero_grad()
41
+
42
+ y_pred = embedding(x_true)
43
+ loss = loss_fn(y_pred[:, 0, 0, :], y_true)
44
+ loss.backward()
45
+
46
+ optim.step()
47
+
48
+ assert pytest.approx(0.0, abs=1e-1) == loss.detach().item()
@@ -22,7 +22,7 @@ from torch import nn
22
22
  from torch.optim import SGD
23
23
  from torch.nn import L1Loss
24
24
 
25
- from torch_geopooling.nn import (
25
+ from torch_geopooling.nn.pooling import (
26
26
  AdaptiveAvgQuadPool2d,
27
27
  AdaptiveMaxQuadPool2d,
28
28
  AdaptiveQuadPool2d,
File without changes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: torch-geopooling
3
- Version: 1.0.1
3
+ Version: 1.1.1
4
4
  Summary: The geospatial pooling modules for neural networks in PyTorch
5
5
  Author-email: Yakau Bubnou <girokompass@gmail.com>
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -700,10 +700,10 @@ Requires-Python: >=3.9
700
700
  Description-Content-Type: text/markdown
701
701
  License-File: LICENSE
702
702
  Requires-Dist: shapely>=2.0.0
703
- Requires-Dist: torch<2.3.0,>=2.2.0
703
+ Requires-Dist: torch<2.4.0,>=2.3.0
704
704
  Provides-Extra: test
705
705
  Requires-Dist: pytest>=8.0.0; extra == "test"
706
- Requires-Dist: numpy>=1.4.0; extra == "test"
706
+ Requires-Dist: numpy<2.0.0,>=1.4.0; extra == "test"
707
707
 
708
708
  # Torch Geopooling - The geospatial pooling library for PyTorch
709
709
 
@@ -768,6 +768,16 @@ input = torch.DoubleTensor(200, 2).uniform_(0.0, 10.0)
768
768
  output = pool(input)
769
769
  ```
770
770
 
771
+ Using 2-dimensional embedding module for learning data on sphere:
772
+ ```py
773
+ import torch
774
+ from torch_geopooling.nn import Embedding2d
775
+
776
+ embedding = Embedding2d((16, 16, 2), padding=(3, 3), exterior=(-100, 100, 200.0, 200.0))
777
+ input = torch.DoubleTensor(1024, 2).normal_(5.0, 1.0)
778
+ output = embedding(input)
779
+ ```
780
+
771
781
  ## License
772
782
 
773
783
  The Torch Geopooling is distributed under GPLv3 license. See the [LICENSE](LICENSE) file for full
@@ -0,0 +1,24 @@
1
+ torch_geopooling/_C.cpython-310-darwin.so,sha256=cMmJ2pMFhTE74Q3n1F9hID9witvCpx65es-QDpLKlPM,562264
2
+ torch_geopooling/__init__.py,sha256=gocbEzjrMP-BkhotjSAi8QMuhKTEaRaMwBD6JC2R9is,60
3
+ torch_geopooling/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ torch_geopooling/return_types.py,sha256=QhIKf_qxpppzuZzUrT-VacfbvZ6zKmSRu-WZOqL8wuE,898
5
+ torch_geopooling/tiling.py,sha256=f1g0Gf__5zIOafZzDkwO62t9OqMDMBEYVtM2HXB5X0Y,2553
6
+ torch_geopooling/transforms.py,sha256=NHPBgx1L4-1745xNO_PvLuvbMH6DWi2rs1ViuRyBQAY,3635
7
+ torch_geopooling/transforms_test.py,sha256=Cqp4VVObBVrc8HQ7uxkpe1EsQ5NxXrCI77BiUqngLXw,1196
8
+ torch_geopooling/__bind__/python_module.cc,sha256=LLI363riung_4LI5ugN7Pm93iuoHDRF7n4Y4RlkzLYk,675
9
+ torch_geopooling/functional/__init__.py,sha256=BCcS22yPWITm37M-EPD-PK6WrS7grikDR8uZdoO8sZ4,118
10
+ torch_geopooling/functional/embedding.py,sha256=ER0KB-G7SoCsWJuNEqXRC_XcLdhqaCpn5OJPpUF9pLc,3337
11
+ torch_geopooling/functional/embedding_test.py,sha256=B5a1MP0rynX01a1RmmTJHZxMi4Y68cQoblw-kQawHJM,1094
12
+ torch_geopooling/functional/pooling.py,sha256=MuJfPW_RC4slgzWBO2ngQIBTzlZXeW7DBqV3ooIOSw4,12060
13
+ torch_geopooling/functional/pooling_test.py,sha256=qBT_WVbwyANolwg7-rE7KimF-bAEDB44guBl4OQuQhE,3005
14
+ torch_geopooling/nn/__init__.py,sha256=fWrhfQu0_KzW7VUesk31Sy3n3Om2c5g5dGA3vrLcCg4,102
15
+ torch_geopooling/nn/embedding.py,sha256=3uF0zbtql90FUpRNKJK_oUcdL8_J6mrOpPm-zTJWlX4,3350
16
+ torch_geopooling/nn/embedding_test.py,sha256=msUeidL9Xkf3AKCPGQH9SJAhfkS7dyqYRzsH88Pt1uY,1505
17
+ torch_geopooling/nn/pooling.py,sha256=_NnGB45dWTZy4MQokFfmJL3as-EPVU3liwOuAmWbv1Y,14219
18
+ torch_geopooling/nn/pooling_test.py,sha256=AqnkpaQ6LcQxh5bWkuDuBAJ2XiBc-C2zVuUp5hynzBM,4794
19
+ torch_geopooling-1.1.1.dist-info/LICENSE,sha256=jLc4eyvG8hqxH4AdKAjK2DwXoP68qjHiGfq8eP5ubBI,35069
20
+ torch_geopooling-1.1.1.dist-info/METADATA,sha256=20wqd_pqv0QO2bIkgG078qmqt3DXXy2Uxk1xrKIKpp8,44675
21
+ torch_geopooling-1.1.1.dist-info/WHEEL,sha256=ouErvQ11jObv_Zc_3angaSrUe1BdXm5BVGaL2Y171WY,110
22
+ torch_geopooling-1.1.1.dist-info/dependency_links.txt,sha256=JqLDcYHtEaQB51V72n3gAJvRd36bpoPk9qgTbot-Lx4,37
23
+ torch_geopooling-1.1.1.dist-info/top_level.txt,sha256=3geTL2nsLvybdtr1psWIE6h63B1LuyIIyWWv0rDafTk,17
24
+ torch_geopooling-1.1.1.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- torch_geopooling/_C.cpython-310-darwin.so,sha256=O__bz8PpcIS9IOUEMfhrDToA2bM-HU8bCpxmvSV_EBI,517944
2
- torch_geopooling/__init__.py,sha256=g3zAIxXO6CdrT0zoTlfM3K86LeTWJcgIsqPTqYmhBMQ,60
3
- torch_geopooling/functional.py,sha256=MuJfPW_RC4slgzWBO2ngQIBTzlZXeW7DBqV3ooIOSw4,12060
4
- torch_geopooling/functional_test.py,sha256=Sfv6bSe4DIB-y1xYVwavZoyIdu0bwARGdr8woOx7F7U,2997
5
- torch_geopooling/nn.py,sha256=_NnGB45dWTZy4MQokFfmJL3as-EPVU3liwOuAmWbv1Y,14219
6
- torch_geopooling/nn_test.py,sha256=zKoyK1f9SUJGhR8pt4wvocmVICoA6nPWPeUMvKKbwY8,4786
7
- torch_geopooling/return_types.py,sha256=QhIKf_qxpppzuZzUrT-VacfbvZ6zKmSRu-WZOqL8wuE,898
8
- torch_geopooling/tiling.py,sha256=f1g0Gf__5zIOafZzDkwO62t9OqMDMBEYVtM2HXB5X0Y,2553
9
- torch_geopooling/transforms.py,sha256=NHPBgx1L4-1745xNO_PvLuvbMH6DWi2rs1ViuRyBQAY,3635
10
- torch_geopooling/transforms_test.py,sha256=Cqp4VVObBVrc8HQ7uxkpe1EsQ5NxXrCI77BiUqngLXw,1196
11
- torch_geopooling/__bind__/python_module.cc,sha256=5K04tRT62XTVsZhY2EE4r4bBTz4mA4IwEkw5adPzo58,576
12
- torch_geopooling-1.0.1.dist-info/LICENSE,sha256=jLc4eyvG8hqxH4AdKAjK2DwXoP68qjHiGfq8eP5ubBI,35069
13
- torch_geopooling-1.0.1.dist-info/METADATA,sha256=CgOBHIw38QEgF4TIAVvnVYWz-RlPgW_60AbdJkK_J-E,44364
14
- torch_geopooling-1.0.1.dist-info/WHEEL,sha256=ouErvQ11jObv_Zc_3angaSrUe1BdXm5BVGaL2Y171WY,110
15
- torch_geopooling-1.0.1.dist-info/dependency_links.txt,sha256=JqLDcYHtEaQB51V72n3gAJvRd36bpoPk9qgTbot-Lx4,37
16
- torch_geopooling-1.0.1.dist-info/top_level.txt,sha256=3geTL2nsLvybdtr1psWIE6h63B1LuyIIyWWv0rDafTk,17
17
- torch_geopooling-1.0.1.dist-info/RECORD,,
File without changes