diff --git a/aima-core/src/main/java/aima/core/learning/deep/ConvLayer1D.java b/aima-core/src/main/java/aima/core/learning/deep/ConvLayer1D.java index afbc17ce7..fd83cfcbd 100644 --- a/aima-core/src/main/java/aima/core/learning/deep/ConvLayer1D.java +++ b/aima-core/src/main/java/aima/core/learning/deep/ConvLayer1D.java @@ -4,8 +4,8 @@ * Artificial Intelligence A Modern Approach (4th Edition): Section 21.3.
*
* A one-dimensional convolution layer: each channel of the input is convolved - * with the kernel of its unit, the output having the size of the input (the - * "same" mode of numpy). The kernels start as Gaussian kernels with standard + * with the kernel of its unit as in equation (21.8), the output having the size + * of the input. The kernels start as Gaussian kernels with standard * deviation 0.1, as in aima-python. * * @author Donato Meoli @@ -41,23 +41,28 @@ public double[][] forward(double[][] features) { } /** - * @return the convolution of x with k, of the size of the longer of the - * two and centered on the full convolution. + * The convolution of equation (21.8), z_i = sum_{j=1..l} k_j x_{j+i-(l+1)/2} + * for a kernel of length l. As in the book (footnote 4 of chapter 21), the + * kernel is not flipped, i.e., this is the cross-correlation of signal + * processing, and as in Figure 21.5 the input is padded with zeros, so that + * z is as long as x also when k is longer than x. For an even l, (l+1)/2 is + * rounded down: the kernel covers one element more to the right of x_i than + * to the left, as the "same" padding of the deep-learning libraries. + * + * @return the convolution of x with k, of the length of x. */ public static double[] convolve(double[] x, double[] k) { - int n = Math.max(x.length, k.length); - int offset = (Math.min(x.length, k.length) - 1) / 2; - double[] full = new double[x.length + k.length - 1]; - for (int t = 0; t < full.length; t++) { + int left = (k.length - 1) / 2; + double[] z = new double[x.length]; + for (int i = 0; i < x.length; i++) { for (int j = 0; j < k.length; j++) { - if (t - j >= 0 && t - j < x.length) { - full[t] += x[t - j] * k[j]; + int t = i + j - left; + if (t >= 0 && t < x.length) { + z[i] += k[j] * x[t]; } } } - double[] same = new double[n]; - System.arraycopy(full, offset, same, 0, n); - return same; + return z; } /** diff --git a/aima-core/src/main/java/aima/core/perception/ImageProcessing.java b/aima-core/src/main/java/aima/core/perception/ImageProcessing.java index 1bf49d311..eb249a03c 100644 --- a/aima-core/src/main/java/aima/core/perception/ImageProcessing.java +++ b/aima-core/src/main/java/aima/core/perception/ImageProcessing.java @@ -95,27 +95,33 @@ public static double[][] arrayNormalization(double[][] image, double rangeMin, d } /** + * The convolution of equation (21.8) along both dimensions, i.e., z_{i,j} = + * sum_{a,b} k_{a,b} x_{a+i-(p+1)/2, b+j-(q+1)/2} for a kernel of p rows and q + * columns, with the conventions of + * {@link aima.core.learning.deep.ConvLayer1D#convolve}: the kernel is not + * flipped, the image is padded with zeros, and the extra element of an even + * side of the kernel lies below and to the right. + * * @return the two-dimensional convolution of the image with the kernel, of - * the size of the image and centered on it (the "same" mode of - * scipy). + * the size of the image. */ public static double[][] convolve2D(double[][] image, double[][] kernel) { int m = image.length; int n = image[0].length; int p = kernel.length; int q = kernel[0].length; - int offsetRow = (p - 1) / 2; - int offsetCol = (q - 1) / 2; + int top = (p - 1) / 2; + int left = (q - 1) / 2; double[][] result = new double[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { double sum = 0; for (int a = 0; a < p; a++) { for (int b = 0; b < q; b++) { - int r = i + offsetRow - a; - int c = j + offsetCol - b; + int r = i + a - top; + int c = j + b - left; if (r >= 0 && r < m && c >= 0 && c < n) { - sum += image[r][c] * kernel[a][b]; + sum += kernel[a][b] * image[r][c]; } } } diff --git a/aima-core/src/test/java/aima/test/core/unit/learning/deep/DeepLearningTest.java b/aima-core/src/test/java/aima/test/core/unit/learning/deep/DeepLearningTest.java index ebf5dfc96..6da81aa38 100644 --- a/aima-core/src/test/java/aima/test/core/unit/learning/deep/DeepLearningTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/learning/deep/DeepLearningTest.java @@ -107,7 +107,7 @@ public void testSoftMax() { @Test public void testConvolutionPoolingAndNormalization() { - // the convolution of numpy in the "same" mode + // equation (21.8) with zero padding, on symmetric kernels Assert.assertArrayEquals(new double[] { 3, 6, 9, 12, 9 }, ConvLayer1D.convolve(new double[] { 1, 2, 3, 4, 5 }, new double[] { 1, 1, 1 }), 1e-9); Assert.assertArrayEquals(new double[] { 2, 4, 6 }, @@ -308,11 +308,12 @@ public void testConvolutionAndPoolingEdgeCases() { ConvLayer1D.convolve(new double[] { 1, 2, 3 }, new double[] { 3 }), 1e-12); Assert.assertArrayEquals(new double[] { 1, 3, 2 }, new MaxPoolingLayer1D(1).forward(new double[][] { { 1, 3, 2 } })[0], 0); - // the book does not say what a kernel longer than the input gives (the - // valid convolution of Figure 21.4 gives nothing): like numpy in the - // "same" mode, the result has the length of the kernel - Assert.assertArrayEquals(new double[] { 2, 4, 6 }, + // a kernel longer than the input: equation (21.8) with zero padding + // still gives one value for each input + Assert.assertArrayEquals(new double[] { 4 }, ConvLayer1D.convolve(new double[] { 2 }, new double[] { 1, 2, 3 }), 1e-12); + Assert.assertArrayEquals(new double[] { 26, 20, 14 }, + ConvLayer1D.convolve(new double[] { 1, 2, 3 }, new double[] { 1, 2, 3, 4, 5 }), 1e-12); // a pooling window longer than the input gives nothing Assert.assertEquals(0, new MaxPoolingLayer1D(4).forward(new double[][] { { 1, 3, 2 } })[0].length); // batch normalization of a constant input: every value is the shift beta @@ -324,6 +325,20 @@ public void testConvolutionAndPoolingEdgeCases() { Assert.assertArrayEquals(new double[] { 2, 2, 2 }, Arrays.copyOfRange(smoothed, 1, 4), 1e-12); } + @Test + public void testConvolutionWithAnAsymmetricKernel() { + // z_i = sum_j k_j x_{j+i-(l+1)/2} (equation 21.8): the kernel is not + // flipped + Assert.assertArrayEquals(new double[] { -2, -2, -2, 3 }, + ConvLayer1D.convolve(new double[] { 1, 2, 3, 4 }, new double[] { 1, 0, -1 }), 1e-12); + // an even kernel covers one element more to the right of x_i than to + // the left + Assert.assertArrayEquals(new double[] { -1, -1, -1, 4 }, + ConvLayer1D.convolve(new double[] { 1, 2, 3, 4 }, new double[] { 1, -1 }), 1e-12); + double[][] out = new ConvLayer1D(new double[][] { { 1, 0, -1 } }).forward(new double[][] { { 1, 2, 3, 4 } }); + Assert.assertArrayEquals(new double[] { -2, -2, -2, 3 }, out[0], 1e-12); + } + @Test public void testRecurrentNeuralNetworkOnShortSequences() { // on the empty sequence the hidden state stays z0 = 0, and the output diff --git a/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java b/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java index bdd4ed2f7..a66afb6b8 100644 --- a/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java @@ -55,15 +55,22 @@ public void testArrayNormalization() { @Test public void testConvolution() { - // the "same" mode of scipy.signal.convolve2d + // equation (21.8) along both dimensions, the kernel not flipped: the + // extra column of [1, -1] lies to the right, z_{i,j} = x_{i,j} - x_{i,j+1} double[][] c = ImageProcessing.convolve2D(new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }, new double[][] { { 1, -1 } }); - Assert.assertArrayEquals(new double[] { 1, 1, 1 }, c[0], DELTA); - Assert.assertArrayEquals(new double[] { 4, 1, 1 }, c[1], DELTA); + Assert.assertArrayEquals(new double[] { -1, -1, 3 }, c[0], DELTA); + Assert.assertArrayEquals(new double[] { -1, -1, 6 }, c[1], DELTA); + // an impulse gives the kernel rotated by 180 degrees double[][] box = ImageProcessing.convolve2D(new double[][] { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } }, new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }); - Assert.assertArrayEquals(new double[] { 1, 2, 3 }, box[0], DELTA); - Assert.assertArrayEquals(new double[] { 7, 8, 9 }, box[2], DELTA); + Assert.assertArrayEquals(new double[] { 9, 8, 7 }, box[0], DELTA); + Assert.assertArrayEquals(new double[] { 3, 2, 1 }, box[2], DELTA); + // a kernel larger than the image: the result has the size of the image + double[][] big = ImageProcessing.convolve2D(new double[][] { { 1, 2 } }, + new double[][] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }); + Assert.assertEquals(1, big.length); + Assert.assertArrayEquals(new double[] { 3, 3 }, big[0], DELTA); double[][] g = ImageProcessing.gaussianKernel2D(); double sum = 0; for (double[] row : g) { @@ -85,9 +92,11 @@ public void testEdgeDetectors() { Assert.assertEquals(0, values.first(), DELTA); Assert.assertEquals(255, values.last(), DELTA); // the rows inside are the same, and the response at the step - // differs from the one in the flat region + // differs from the one in the flat region (at column 2, the last + // before the step: the kernel [1, -1], not flipped, gives + // x_{i,2} - x_{i,3} there) Assert.assertArrayEquals(edges[2], edges[3], DELTA); - Assert.assertNotEquals(edges[2][0], edges[2][3], DELTA); + Assert.assertNotEquals(edges[2][0], edges[2][2], DELTA); } }