|
1 | 1 | from typing import TYPE_CHECKING, Literal, cast
|
2 | 2 |
|
3 | 3 | from numpy import convolve as numpy_convolve
|
| 4 | +from scipy.signal import convolve2d as scipy_convolve2d |
4 | 5 |
|
5 | 6 | from pytensor.graph import Apply, Op
|
6 | 7 | from pytensor.scalar.basic import upcast
|
7 | 8 | from pytensor.tensor.basic import as_tensor_variable, join, zeros
|
8 | 9 | from pytensor.tensor.blockwise import Blockwise
|
9 | 10 | from pytensor.tensor.math import maximum, minimum
|
10 |
| -from pytensor.tensor.type import vector |
| 11 | +from pytensor.tensor.type import matrix, vector |
11 | 12 | from pytensor.tensor.variable import TensorVariable
|
12 | 13 |
|
13 | 14 |
|
@@ -131,3 +132,116 @@ def convolve1d(
|
131 | 132 | mode = "valid"
|
132 | 133 |
|
133 | 134 | return cast(TensorVariable, Blockwise(Convolve1d(mode=mode))(in1, in2))
|
| 135 | + |
| 136 | + |
| 137 | +class Convolve2D(Op): |
| 138 | + __props__ = ("mode", "boundary", "fillvalue") |
| 139 | + gufunc_signature = "(n,m),(k,l)->(o,p)" |
| 140 | + |
| 141 | + def __init__( |
| 142 | + self, |
| 143 | + mode: Literal["full", "valid", "same"] = "full", |
| 144 | + boundary: Literal["fill", "wrap", "symm"] = "fill", |
| 145 | + fillvalue: float | int = 0, |
| 146 | + ): |
| 147 | + if mode not in ("full", "valid", "same"): |
| 148 | + raise ValueError(f"Invalid mode: {mode}") |
| 149 | + if boundary not in ("fill", "wrap", "symm"): |
| 150 | + raise ValueError(f"Invalid boundary: {boundary}") |
| 151 | + |
| 152 | + self.mode = mode |
| 153 | + self.boundary = boundary |
| 154 | + self.fillvalue = fillvalue |
| 155 | + |
| 156 | + def make_node(self, in1, in2): |
| 157 | + in1, in2 = map(as_tensor_variable, (in1, in2)) |
| 158 | + |
| 159 | + assert in1.ndim == 2 |
| 160 | + assert in2.ndim == 2 |
| 161 | + |
| 162 | + dtype = upcast(in1.dtype, in2.dtype) |
| 163 | + |
| 164 | + n, m = in1.type.shape |
| 165 | + k, l = in2.type.shape |
| 166 | + |
| 167 | + if any(x is None for x in (n, m, k, l)): |
| 168 | + out_shape = (None, None) |
| 169 | + elif self.mode == "full": |
| 170 | + out_shape = (n + k - 1, m + l - 1) |
| 171 | + elif self.mode == "valid": |
| 172 | + out_shape = (n - k + 1, m - l + 1) |
| 173 | + else: # mode == "same" |
| 174 | + out_shape = (n, m) |
| 175 | + |
| 176 | + out = matrix(dtype=dtype, shape=out_shape) |
| 177 | + return Apply(self, [in1, in2], [out]) |
| 178 | + |
| 179 | + def perform(self, node, inputs, outputs): |
| 180 | + in1, in2 = inputs |
| 181 | + outputs[0][0] = scipy_convolve2d( |
| 182 | + in1, in2, mode=self.mode, boundary=self.boundary, fillvalue=self.fillvalue |
| 183 | + ) |
| 184 | + |
| 185 | + def infer_shape(self, fgraph, node, shapes): |
| 186 | + in1_shape, in2_shape = shapes |
| 187 | + n, m = in1_shape |
| 188 | + k, l = in2_shape |
| 189 | + |
| 190 | + if self.mode == "full": |
| 191 | + shape = (n + k - 1, m + l - 1) |
| 192 | + elif self.mode == "valid": |
| 193 | + shape = ( |
| 194 | + maximum(n, k) - minimum(n, k) + 1, |
| 195 | + maximum(m, l) - minimum(m, l) + 1, |
| 196 | + ) |
| 197 | + else: # self.mode == 'same': |
| 198 | + shape = (n, m) |
| 199 | + |
| 200 | + return [shape] |
| 201 | + |
| 202 | + def L_op(self, inputs, outputs, output_grads): |
| 203 | + raise NotImplementedError |
| 204 | + |
| 205 | + |
| 206 | +def convolve2d( |
| 207 | + in1: "TensorLike", |
| 208 | + in2: "TensorLike", |
| 209 | + mode: Literal["full", "valid", "same"] = "full", |
| 210 | + boundary: Literal["fill", "wrap", "symm"] = "fill", |
| 211 | + fillvalue: float | int = 0, |
| 212 | +) -> TensorVariable: |
| 213 | + """Convolve two two-dimensional arrays. |
| 214 | +
|
| 215 | + Convolve in1 and in2, with the output size determined by the mode argument. |
| 216 | +
|
| 217 | + Parameters |
| 218 | + ---------- |
| 219 | + in1 : (..., N, M) tensor_like |
| 220 | + First input. |
| 221 | + in2 : (..., K, L) tensor_like |
| 222 | + Second input. |
| 223 | + mode : {'full', 'valid', 'same'}, optional |
| 224 | + A string indicating the size of the output: |
| 225 | + - 'full': The output is the full discrete linear convolution of the inputs, with shape (..., N+K-1, M+L-1). |
| 226 | + - 'valid': The output consists only of elements that do not rely on zero-padding, with shape (..., max(N, K) - min(N, K) + 1, max(M, L) - min(M, L) + 1). |
| 227 | + - 'same': The output is the same size as in1, centered with respect to the 'full' output. |
| 228 | + boundary : {'fill', 'wrap', 'symm'}, optional |
| 229 | + A string indicating how to handle boundaries: |
| 230 | + - 'fill': Pads the input arrays with fillvalue. |
| 231 | + - 'wrap': Circularly wraps the input arrays. |
| 232 | + - 'symm': Symmetrically reflects the input arrays. |
| 233 | + fillvalue : float or int, optional |
| 234 | + The value to use for padding when boundary is 'fill'. Default is 0. |
| 235 | + Returns |
| 236 | + ------- |
| 237 | + out: tensor_variable |
| 238 | + The discrete linear convolution of in1 with in2. |
| 239 | +
|
| 240 | + """ |
| 241 | + in1 = as_tensor_variable(in1) |
| 242 | + in2 = as_tensor_variable(in2) |
| 243 | + |
| 244 | + blockwise_convolve = Blockwise( |
| 245 | + Convolve2D(mode=mode, boundary=boundary, fillvalue=fillvalue) |
| 246 | + ) |
| 247 | + return cast(TensorVariable, blockwise_convolve(in1, in2)) |
0 commit comments