From 80b5d0b94719d2dc351f78719d19740237b13eaf Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sat, 8 Jun 2024 20:36:53 +0200 Subject: [PATCH 01/25] added test for transformation of linearized images --- .../BuiltinImageTransformLinearizedTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java new file mode 100644 index 00000000000..8739a6d1211 --- /dev/null +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -0,0 +1,114 @@ +package org.apache.sysds.test.functions.builtin.part1; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.apache.sysds.common.Types.ExecMode; +import org.apache.sysds.common.Types.ExecType; +import org.apache.sysds.runtime.matrix.data.MatrixValue; +import org.apache.sysds.test.AutomatedTestBase; +import org.apache.sysds.test.TestConfiguration; +import org.apache.sysds.test.TestUtils; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; + +@RunWith(Parameterized.class) +@net.jcip.annotations.NotThreadSafe + +public class BuiltinImageTransformLinearizedTest extends AutomatedTestBase { + private final static String TEST_NAME_LINEARIZED = "image_transform_linearized"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageTransformLinearizedTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + private final static double spSparse = 0.05; + private final static double spDense = 0.5; + + @Parameterized.Parameter(0) + public int rows; //number of linearized images + @Parameterized.Parameter(1) + public int width; + @Parameterized.Parameter(2) + public int height; + @Parameterized.Parameter(3) + public int out_w; + @Parameterized.Parameter(4) + public int out_h; + @Parameterized.Parameter(5) + public int a; + @Parameterized.Parameter(6) + public int b; + @Parameterized.Parameter(7) + public int c; + @Parameterized.Parameter(8) + public int d; + @Parameterized.Parameter(9) + public int e; + @Parameterized.Parameter(10) + public int f; + @Parameterized.Parameter(11) + public int fill_value; + @Parameterized.Parameter(12) + public int s_cols; + @Parameterized.Parameter(13) + public int s_rows; + + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] {{1,512, 512, 512, 512, 1,0,0,0,1,0,1, 512, 512}}); + } + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME_LINEARIZED, + new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[] {"B_x"})); + } + + @Test + public void testImageTranslateLinearized() { + runImageTranslateLinearizedTest(false, ExecType.CP); + } + + @Test + @Ignore + public void testImageTranslateLinearizedSP() { + runImageTranslateLinearizedTest(true, ExecType.SPARK); + } + + private void runImageTranslateLinearizedTest(boolean sparse, ExecType instType) { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try { + loadTestConfiguration(getTestConfiguration(TEST_NAME_LINEARIZED)); + + double sparsity = sparse ? spSparse : spDense; + String HOME = SCRIPT_DIR + TEST_DIR; + + fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; + programArgs = new String[] {"-nvargs", "in_file=" + input("A"), "width=" + width, "height=" + height, + "out_w=" + out_w, "out_h=" + out_h, "a=" + a, "b=" + b, "c=" + c, "d=" + d, "e=" + e, "f=" + f, + "fill_value=" + fill_value, "s_cols=" + s_cols, "s_rows=" + s_rows, + "out_file=" + output("B_x")}; + + double[][] A = getRandomMatrix(rows, height*width, 0, 255, sparsity, 7); + writeInputMatrixWithMTD("A", A, true); + + runTest(true, false, null, -1); + + //HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); + + //HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); + + //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); + + } + finally { + rtplatform = platformOld; + } + } +} From 3288c00627607d57eb8348019a09427d6977e5e7 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Tue, 11 Jun 2024 20:08:49 +0200 Subject: [PATCH 02/25] added a dml file to calculate the image transformation matrix, the builtin function name, and a simple test case to trace the namespace error from a function that is not yet fully implemented, also corrected wront initial test --- .../org/apache/sysds/common/Builtins.java | 1 + .../BuiltinImageTransformLinearizedTest.java | 10 +-- .../BuiltinImageTransformMatrixTest.java | 77 +++++++++++++++++++ .../builtin/image_transform_matrix.dml | 37 +++++++++ 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java create mode 100644 src/test/scripts/functions/builtin/image_transform_matrix.dml diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index a358b2e2bc9..93bead8f943 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -168,6 +168,7 @@ public enum Builtins { IMG_CROP_LINEARIZED("img_crop_linearized", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSFORM_LINEARIZED("img_transform_linearized", true), + IMG_TRANSLATE_MATRIX("img_translate_matrix", false), IMG_TRANSLATE("img_translate", true), IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), IMG_ROTATE("img_rotate", true), diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java index 8739a6d1211..39aae9b288c 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -69,17 +69,17 @@ public void setUp() { } @Test - public void testImageTranslateLinearized() { - runImageTranslateLinearizedTest(false, ExecType.CP); + public void testImageTransformLinearized() { + runImageTransformLinearizedTest(false, ExecType.CP); } @Test @Ignore - public void testImageTranslateLinearizedSP() { - runImageTranslateLinearizedTest(true, ExecType.SPARK); + public void testImageTransformLinearizedSP() { + runImageTransformLinearizedTest(true, ExecType.SPARK); } - private void runImageTranslateLinearizedTest(boolean sparse, ExecType instType) { + private void runImageTransformLinearizedTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java new file mode 100644 index 00000000000..345a7db5803 --- /dev/null +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java @@ -0,0 +1,77 @@ +package org.apache.sysds.test.functions.builtin.part1; + +import org.apache.sysds.test.AutomatedTestBase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.apache.sysds.common.Types.ExecMode; +import org.apache.sysds.common.Types.ExecType; +import org.apache.sysds.test.TestConfiguration; + +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +@net.jcip.annotations.NotThreadSafe + +public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { + private final static String TEST_NAME_LINEARIZED = "image_transform_matrix"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageTransformMatrixTest.class.getSimpleName() + "/"; + + @Parameterized.Parameter(0) + public double[][] transMat; + @Parameterized.Parameter(1) + public double[][] dimMat; + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] {{new double[][] {{1,0,0},{0,1,0},{0,0,1}}, new double[][] {{512, 512},{512,512}}}}); + } + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME_LINEARIZED, + new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[] {"B_x"})); + } + + @Test + public void testImageTransformMatrix() { + runImageTransformMatrixTest(ExecType.CP); + } + + private void runImageTransformMatrixTest(ExecType instType) { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try { + loadTestConfiguration(getTestConfiguration(TEST_NAME_LINEARIZED)); + + String HOME = SCRIPT_DIR + TEST_DIR; + + writeInputMatrixWithMTD("transMat", transMat, true); + writeInputMatrixWithMTD("dimMat", dimMat, true); + + fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; + programArgs = new String[]{"-nvargs", "transMat=" + input("transMat"), "dimMat=" + input("dimMat"), "out_file=" + output("B_x"), "--debug"}; + + //double[][] A = getRandomMatrix(rows, height*width, 0, 255, sparsity, 7); + + + runTest(true, false, null, -1); + + //HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); + + //HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); + + //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + rtplatform = platformOld; + } + } + + +} diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml new file mode 100644 index 00000000000..064e5ea6c34 --- /dev/null +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -0,0 +1,37 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +#------------------------------------------------------------- + +#get the transformation matrix (3x3) as an input +transformationMatrix = read($transMat) +print(toString(transformationMatrix)) + +#get a matrix with the original image dimensions and the target dimensions (2x2) +#-----original height----original width +#-----target height------target width +dimensionMatrix = read($dimMat) +print(toString(dimensionMatrix)) + +#function for calculating the matrix which will be matrix multiplied with +#the image for matrix multiplication +zMat = img_transform_matrix(transformationMatrix, dimensionMatrix) + +print(toString(transformationMatrix)) +write(zMat, $out_file) From a616ba7fe9f52a594fe2856c74db9eb5a22a0bd1 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Thu, 18 Jul 2024 13:03:01 +0200 Subject: [PATCH 03/25] added a dml file img_transform_matrix.dml --- scripts/builtin/img_transform_matrix.dml | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 scripts/builtin/img_transform_matrix.dml diff --git a/scripts/builtin/img_transform_matrix.dml b/scripts/builtin/img_transform_matrix.dml new file mode 100644 index 00000000000..5e3229f03c9 --- /dev/null +++ b/scripts/builtin/img_transform_matrix.dml @@ -0,0 +1,42 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +#------------------------------------------------------------- + +# Generates the z matrix for the new linearized image transformation function in order to apply +# affine transformation to linearized images. +# +# INPUT: +# ------------------------------------------------------------------------------------------- +# transMat 3x3 matrix for affine transformation (transformation Matrix) +# dimMat 2x2 matrix containing the original size of the image in the first row as follows +# [1,1] original height; [1,2] original width +# and the target size of the new image in the second row as follows +# [2,1] target height; [2,2] target width +# (dimensionMatrix) +# ------------------------------------------------------------------------------------------- +# OUTPUT: +# --------------------------------------------------------------------------------------- +# zMat transformation matrix to be multiplied with for the linearized image +# transformation function (arbitrary naming) +# --------------------------------------------------------------------------------------- + +m_img_transform_matrix = function(Matrix[Double] transMat, Matrix[Double] dimMat) return (Matrix[Double] zMat) { + zMat = transMat +} \ No newline at end of file From 5205e876ca083bdd8943256f1a11724f3656cd8c Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Thu, 18 Jul 2024 13:10:18 +0200 Subject: [PATCH 04/25] added a builtin for img_transform_matrix.dml --- src/main/java/org/apache/sysds/common/Builtins.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 93bead8f943..f36069f0f62 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -168,7 +168,8 @@ public enum Builtins { IMG_CROP_LINEARIZED("img_crop_linearized", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSFORM_LINEARIZED("img_transform_linearized", true), - IMG_TRANSLATE_MATRIX("img_translate_matrix", false), + IMG_TRANSFORM_MATRIX("img_transform_matrix", true), + IMG_TRANSLATE_MATRIX("img_translate_matrix", false), //TODO: remove unused builtin IMG_TRANSLATE("img_translate", true), IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), IMG_ROTATE("img_rotate", true), From 875c75f4884a89c76c2492cfaa4a0b0c36623f9a Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Fri, 19 Jul 2024 15:16:54 +0200 Subject: [PATCH 05/25] added function img_transform_matrix, CP and Lop missing? maybe hop too? --- src/main/java/org/apache/sysds/common/Builtins.java | 2 +- src/main/java/org/apache/sysds/common/Types.java | 5 ++++- .../sysds/parser/BuiltinFunctionExpression.java | 11 ++++++++++- .../java/org/apache/sysds/parser/DMLTranslator.java | 10 +++++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index f36069f0f62..43790348ecb 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -168,7 +168,7 @@ public enum Builtins { IMG_CROP_LINEARIZED("img_crop_linearized", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSFORM_LINEARIZED("img_transform_linearized", true), - IMG_TRANSFORM_MATRIX("img_transform_matrix", true), + IMG_TRANSFORM_MATRIX("img_transform_matrix", false), IMG_TRANSLATE_MATRIX("img_translate_matrix", false), //TODO: remove unused builtin IMG_TRANSLATE("img_translate", true), IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), diff --git a/src/main/java/org/apache/sysds/common/Types.java b/src/main/java/org/apache/sysds/common/Types.java index 30cd6bf5bd8..8bb4f69fac1 100644 --- a/src/main/java/org/apache/sysds/common/Types.java +++ b/src/main/java/org/apache/sysds/common/Types.java @@ -427,7 +427,8 @@ public enum OpOp2 { //fused ML-specific operators for performance MINUS_NZ(false), //sparse-safe minus: X-(mean*ppred(X,0,!=)) LOG_NZ(false), //sparse-safe log; ppred(X,0,"!=")*log(X,0.5) - MINUS1_MULT(false); //1-X*Y + MINUS1_MULT(false), //1-X*Y + IMG_TRANSFORM_MATRIX(false); private final boolean _validOuter; @@ -471,6 +472,7 @@ public String toString() { case FRAME_ROW_REPLICATE: return "freplicate"; case VALUE_SWAP: return "valueSwap"; case APPLY_SCHEMA: return "applySchema"; + case IMG_TRANSFORM_MATRIX: return "imgTransformMatrix"; default: return name().toLowerCase(); } } @@ -507,6 +509,7 @@ public static OpOp2 valueOfByOpcode(String opcode) { case "freplicate": return FRAME_ROW_REPLICATE; case "valueSwap": return VALUE_SWAP; case "applySchema": return APPLY_SCHEMA; + case "img_transform_matrix": return IMG_TRANSFORM_MATRIX; default: return valueOf(opcode.toUpperCase()); } } diff --git a/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java b/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java index ec9b4a4bbde..0521020c10d 100644 --- a/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java +++ b/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java @@ -1325,7 +1325,16 @@ else if( getOpCode() == Builtins.RBIND ) { output.setBlocksize(0); output.setValueType(ValueType.BOOLEAN); break; - + //implement the calculation of the transformation matrix for affine transformation of images + case IMG_TRANSFORM_MATRIX: + checkNumParameters(2); + checkMatrixParam(getFirstExpr()); + checkMatrixParam(getSecondExpr()); + output.setDataType(DataType.MATRIX); + output.setValueType(ValueType.FP64); + output.setDimensions(-1,-1); + output.setBlocksize(0); + break; // Contingency tables case TABLE: diff --git a/src/main/java/org/apache/sysds/parser/DMLTranslator.java b/src/main/java/org/apache/sysds/parser/DMLTranslator.java index 5ff351da4c0..bb17fff221a 100644 --- a/src/main/java/org/apache/sysds/parser/DMLTranslator.java +++ b/src/main/java/org/apache/sysds/parser/DMLTranslator.java @@ -2490,7 +2490,15 @@ else if ( sop.equalsIgnoreCase("!=") ) new NaryOp(target.getName(), target.getDataType(), target.getValueType(), appendOpN, processAllExpressions(source.getAllExpr(), hops)); break; - + case IMG_TRANSFORM_MATRIX: + currBuiltinOp = new BinaryOp(target.getName(), target.getDataType(), target.getValueType(), OpOp2.IMG_TRANSFORM_MATRIX , expr, expr2); +// switch (source.getOpCode()){ +// default: +// throw new ParseException(source.printErrorLocation() + +// "processBuiltinFunctionExpression():: Could not find Operation type for builtin function: " +// + source.getOpCode()); +// } + break; case TABLE: // Always a TertiaryOp is created for table(). From 36c8597e1953eb4cb932ed169e491616c4050a7d Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Fri, 19 Jul 2024 21:29:00 +0200 Subject: [PATCH 06/25] rewritten for img_transform_matrix.dml to be used --- scripts/builtin/img_transform_matrix.dml | 37 +++++++++++++++++-- .../org/apache/sysds/common/Builtins.java | 2 +- .../java/org/apache/sysds/common/Types.java | 2 +- .../BuiltinImageTransformMatrixTest.java | 2 +- .../builtin/image_transform_matrix.dml | 4 +- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/scripts/builtin/img_transform_matrix.dml b/scripts/builtin/img_transform_matrix.dml index 5e3229f03c9..db284a6bcd7 100644 --- a/scripts/builtin/img_transform_matrix.dml +++ b/scripts/builtin/img_transform_matrix.dml @@ -37,6 +37,37 @@ # transformation function (arbitrary naming) # --------------------------------------------------------------------------------------- -m_img_transform_matrix = function(Matrix[Double] transMat, Matrix[Double] dimMat) return (Matrix[Double] zMat) { - zMat = transMat -} \ No newline at end of file + m_img_transform_matrix = function(Matrix[Double] transMat, Matrix[Double] dimMat) return (Matrix[Double] zMat) { + orig_w = as.scalar(dimMat[1,2]) + orig_h = as.scalar(dimMat[1,1]) + + out_w = as.scalar(dimMat[2,2]) + out_h = as.scalar(dimMat[2,1]) + T_inv = inv(transMat) + + # coordinates of output pixel-centers linearized in row-major order + coords = matrix(1, rows=3, cols=out_w*out_h) + coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) + coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) + + # compute sampling pixel indices + coords = floor(T_inv %*% coords) + 1 + + inx = t(coords[1,]) + iny = t(coords[2,]) + + # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input + index_vector = (orig_w *(iny-1) + inx) * ((0 data() { - return Arrays.asList(new Object[][] {{new double[][] {{1,0,0},{0,1,0},{0,0,1}}, new double[][] {{512, 512},{512,512}}}}); + return Arrays.asList(new Object[][] {{new double[][] {{2,0,0},{0,1,0},{0,0,1}}, new double[][] {{10, 10},{15,15}}}}); } @Override diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml index 064e5ea6c34..ef00ec55a47 100644 --- a/src/test/scripts/functions/builtin/image_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -21,7 +21,7 @@ #get the transformation matrix (3x3) as an input transformationMatrix = read($transMat) -print(toString(transformationMatrix)) +#print(toString(transformationMatrix)) #get a matrix with the original image dimensions and the target dimensions (2x2) #-----original height----original width @@ -33,5 +33,5 @@ print(toString(dimensionMatrix)) #the image for matrix multiplication zMat = img_transform_matrix(transformationMatrix, dimensionMatrix) -print(toString(transformationMatrix)) +print(toString(zMat)) write(zMat, $out_file) From 42a38a799f11a9b2ae29bb3a5a50993a7b2696bd Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Fri, 26 Jul 2024 15:46:27 +0200 Subject: [PATCH 07/25] intermediate commit, added new test script, reimplemented function to have five inputs and no longer a matrix with dimensions inside. --- scripts/builtin/img_transform_matrix.dml | 18 +++-- scripts/builtin/img_transform_test.dml | 33 ++++++++ .../org/apache/sysds/common/Builtins.java | 5 +- .../java/org/apache/sysds/common/Types.java | 8 +- .../org/apache/sysds/hops/FunctionOp.java | 48 ++++++------ .../parser/BuiltinFunctionExpression.java | 78 ++++++++++++++++--- .../apache/sysds/parser/DMLTranslator.java | 7 +- .../instructions/CPInstructionParser.java | 1 + .../cp/MultiReturnBuiltinCPInstruction.java | 5 +- .../runtime/matrix/data/LibCommonsMath.java | 1 + .../builtin/image_transform_matrix.dml | 32 ++++++-- 11 files changed, 176 insertions(+), 60 deletions(-) create mode 100644 scripts/builtin/img_transform_test.dml diff --git a/scripts/builtin/img_transform_matrix.dml b/scripts/builtin/img_transform_matrix.dml index db284a6bcd7..484d6b129d3 100644 --- a/scripts/builtin/img_transform_matrix.dml +++ b/scripts/builtin/img_transform_matrix.dml @@ -35,17 +35,20 @@ # --------------------------------------------------------------------------------------- # zMat transformation matrix to be multiplied with for the linearized image # transformation function (arbitrary naming) +# isFillable returns a boolean which indicates if cells need to be filled (with fillvalue), + in this case the image is extended, otherwise the original image is used # --------------------------------------------------------------------------------------- - m_img_transform_matrix = function(Matrix[Double] transMat, Matrix[Double] dimMat) return (Matrix[Double] zMat) { - orig_w = as.scalar(dimMat[1,2]) - orig_h = as.scalar(dimMat[1,1]) + m_img_transform_matrix = function(Matrix[Double] transMat, Integer orig_w, Integer orig_h, Integer out_w, Integer out_h) + return (Matrix[Double] zMat, Boolean isFillable){ + #orig_w = as.scalar(dimMat[1,2]) + #orig_h = as.scalar(dimMat[1,1]) - out_w = as.scalar(dimMat[2,2]) - out_h = as.scalar(dimMat[2,1]) + #out_w = as.scalar(dimMat[2,2]) + #out_h = as.scalar(dimMat[2,1]) T_inv = inv(transMat) - # coordinates of output pixel-centers linearized in row-major order + ## coordinates of output pixel-centers linearized in row-major order coords = matrix(1, rows=3, cols=out_w*out_h) coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) @@ -69,5 +72,6 @@ ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) z = table(xs, ind) - zMat = z + zMat = transMat + isFillable = min(index_vector) == 0 } \ No newline at end of file diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml new file mode 100644 index 00000000000..1be776931c3 --- /dev/null +++ b/scripts/builtin/img_transform_test.dml @@ -0,0 +1,33 @@ +m_img_transform_test = function(Matrix[Double] transMat, Integer orig_w, Integer orig_h, Integer out_w, Integer out_h) + return (Matrix[Double] zMat, Boolean isFillable){ + #zMat = transMat + #isFillable = FALSE + T_inv = inv(transMat) + + ## coordinates of output pixel-centers linearized in row-major order + coords = matrix(1, rows=3, cols=out_w*out_h) + coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) + coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) + + # compute sampling pixel indices + coords = floor(T_inv %*% coords) + 1 + + inx = t(coords[1,]) + iny = t(coords[2,]) + + # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input + index_vector = (orig_w *(iny-1) + inx) * ((0 Date: Fri, 26 Jul 2024 16:28:01 +0200 Subject: [PATCH 08/25] solid path to executing the necessary steps, throwing not implemented exceptions to trace the necessary additions --- .../cp/MultiReturnBuiltinCPInstruction.java | 5 +++-- .../sysds/runtime/matrix/data/LibCommonsMath.java | 2 ++ .../runtime/matrix/data/LibMatrixIMGTransform.java | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java index 5499ad37f91..6256d771ab1 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java @@ -20,6 +20,7 @@ package org.apache.sysds.runtime.instructions.cp; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.NotImplementedException; @@ -138,8 +139,8 @@ else if(parts.length == 3 && opcode.equalsIgnoreCase("fft_linearized")) { return new MultiReturnBuiltinCPInstruction(null, null, outputs, opcode, str, threads); -// } else if (opcode.equalsIgnoreCase("img_transform_matrix")) { -// + } else if (opcode.equalsIgnoreCase("img_transform_matrix")) { + throw new NotImplementedException("Has yet to be done. Check number of inputs" + Arrays.toString(parts)); } else if ( opcode.equalsIgnoreCase("stft") ) { // one input and two outputs CPOperand in1 = new CPOperand(parts[1]); diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java index 2c1cb76f863..b3887c9f921 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java @@ -170,6 +170,8 @@ public static MatrixBlock[] multiReturnOperations(MatrixBlock in1, MatrixBlock i return computeIFFT_LINEARIZED(in1, in2, threads); case "rcm": return computeRCM(in1, in2); + case "img_transform_matrix": + return LibMatrixIMGTransform.transformationMatrix(in1, in2, threads); default: return null; } diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java new file mode 100644 index 00000000000..8a905129bce --- /dev/null +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -0,0 +1,14 @@ +package org.apache.sysds.runtime.matrix.data; + +import org.apache.commons.lang3.NotImplementedException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class LibMatrixIMGTransform { + + protected static final Log LOG = LogFactory.getLog(LibMatrixFourier.class.getName()); + + public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlock dimMat, int threads) { + throw new NotImplementedException(); + } +} From 1ea9b086c68d88d5f05e019b4bdd4f44f27a5582 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Fri, 26 Jul 2024 17:32:02 +0200 Subject: [PATCH 09/25] added some multi return parameters --- .../cp/MultiReturnBuiltinCPInstruction.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java index 6256d771ab1..f0621d5694c 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java @@ -140,7 +140,18 @@ else if(parts.length == 3 && opcode.equalsIgnoreCase("fft_linearized")) { return new MultiReturnBuiltinCPInstruction(null, null, outputs, opcode, str, threads); } else if (opcode.equalsIgnoreCase("img_transform_matrix")) { - throw new NotImplementedException("Has yet to be done. Check number of inputs" + Arrays.toString(parts)); + // 5 inputs and two outputs + CPOperand in1 = new CPOperand(parts[1]); //transformation matrix + CPOperand in2 = new CPOperand(parts[2]); //original width + CPOperand in3 = new CPOperand(parts[3]); //original height + CPOperand in4 = new CPOperand(parts[4]); //output width + CPOperand in5 = new CPOperand(parts[5]); //output height + + outputs.add(new CPOperand(parts[6], ValueType.FP64, DataType.MATRIX)); + outputs.add(new CPOperand(parts[7], ValueType.BOOLEAN, DataType.SCALAR)); + throw new NotImplementedException("Has yet to be done. Check number of inputs" + Arrays.toString(parts) + "; Number of parts: " + parts.length); + + } else if ( opcode.equalsIgnoreCase("stft") ) { // one input and two outputs CPOperand in1 = new CPOperand(parts[1]); From 9a2a3754b6d3977e781ae91f4b79989ae1b87940 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Fri, 26 Jul 2024 23:53:47 +0200 Subject: [PATCH 10/25] we made it to a state were the function itself can be implemented in java, accessible through MultiReturnComplexMatrixBuiltinCPInstruction and LibCommonsMath in its own class LibMatrixIMGTransform --- scripts/builtin/img_transform_matrix.dml | 4 +- scripts/builtin/img_transform_test.dml | 51 ++++++++++--------- .../parser/BuiltinFunctionExpression.java | 24 ++++----- .../instructions/CPInstructionParser.java | 2 +- .../cp/MultiReturnBuiltinCPInstruction.java | 12 ----- ...turnComplexMatrixBuiltinCPInstruction.java | 10 ++++ .../runtime/matrix/data/LibCommonsMath.java | 2 + .../matrix/data/LibMatrixIMGTransform.java | 6 ++- .../builtin/image_transform_matrix.dml | 18 +++---- 9 files changed, 67 insertions(+), 62 deletions(-) diff --git a/scripts/builtin/img_transform_matrix.dml b/scripts/builtin/img_transform_matrix.dml index 484d6b129d3..5ac02bfd7e7 100644 --- a/scripts/builtin/img_transform_matrix.dml +++ b/scripts/builtin/img_transform_matrix.dml @@ -40,7 +40,7 @@ # --------------------------------------------------------------------------------------- m_img_transform_matrix = function(Matrix[Double] transMat, Integer orig_w, Integer orig_h, Integer out_w, Integer out_h) - return (Matrix[Double] zMat, Boolean isFillable){ + return (Matrix[Double] zMat, Matrix[Double] isFillable){ #orig_w = as.scalar(dimMat[1,2]) #orig_h = as.scalar(dimMat[1,1]) @@ -73,5 +73,5 @@ ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) z = table(xs, ind) zMat = transMat - isFillable = min(index_vector) == 0 + isFillable = as.double(min(index_vector) == 0) } \ No newline at end of file diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml index 1be776931c3..d3cd2fafb80 100644 --- a/scripts/builtin/img_transform_test.dml +++ b/scripts/builtin/img_transform_test.dml @@ -1,33 +1,38 @@ -m_img_transform_test = function(Matrix[Double] transMat, Integer orig_w, Integer orig_h, Integer out_w, Integer out_h) - return (Matrix[Double] zMat, Boolean isFillable){ +m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensionMatrix) + return (Matrix[Double] zMat, Matrix[Double] isFillable){ #zMat = transMat #isFillable = FALSE T_inv = inv(transMat) - ## coordinates of output pixel-centers linearized in row-major order - coords = matrix(1, rows=3, cols=out_w*out_h) - coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) - coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) + orig_w = as.scalar(dimensionMatrix[1,1]) + orig_h = as.scalar(dimensionMatrix[1,2]) + out_w = as.scalar(dimensionMatrix[2,1]) + out_h = as.scalar(dimensionMatrix[2,2]) - # compute sampling pixel indices - coords = floor(T_inv %*% coords) + 1 + ## coordinates of output pixel-centers linearized in row-major order + coords = matrix(1, rows=3, cols=out_w*out_h) + coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) + coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) - inx = t(coords[1,]) - iny = t(coords[2,]) + # compute sampling pixel indices + coords = floor(T_inv %*% coords) + 1 - # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input - index_vector = (orig_w *(iny-1) + inx) * ((0 Date: Sat, 27 Jul 2024 14:24:52 +0200 Subject: [PATCH 11/25] first steps of implementation of the algorithm for the transformation matrix --- .../matrix/data/LibMatrixIMGTransform.java | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index 7fda510feac..daa5bc6db60 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -3,15 +3,85 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.sysds.runtime.functionobjects.Builtin; +import org.apache.sysds.runtime.functionobjects.Multiply; +import org.apache.sysds.runtime.matrix.operators.BinaryOperator; +import org.apache.sysds.runtime.matrix.operators.UnaryOperator; + +import java.util.Arrays; public class LibMatrixIMGTransform { protected static final Log LOG = LogFactory.getLog(LibMatrixFourier.class.getName()); + /** + * affine transformation matrix for calculated for a picture of original size and target dimensions + * see: https://en.wikipedia.org/wiki/Affine_transformation + * + * #orig_w = as.scalar(dimMat[1,2]) + * #orig_h = as.scalar(dimMat[1,1]) + * #out_w = as.scalar(dimMat[2,2]) + * #out_h = as.scalar(dimMat[2,1]) + * T_inv = inv(transMat) + * + * ## coordinates of output pixel-centers linearized in row-major order + * coords = matrix(1, rows=3, cols=out_w*out_h) + * coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) + * coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) + * # compute sampling pixel indices + * coords = floor(T_inv %*% coords) + 1 + * inx = t(coords[1,]) + * iny = t(coords[2,]) + * # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input + * index_vector = (orig_w *(iny-1) + inx) * ((0 Date: Sat, 27 Jul 2024 16:13:20 +0200 Subject: [PATCH 12/25] intermediate commit for transformation matrix implementation --- .../matrix/data/LibMatrixIMGTransform.java | 68 +++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index daa5bc6db60..f066488aca9 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -3,13 +3,15 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.sysds.api.mlcontext.Matrix; import org.apache.sysds.runtime.functionobjects.Builtin; -import org.apache.sysds.runtime.functionobjects.Multiply; +import org.apache.sysds.runtime.functionobjects.SwapIndex; +import org.apache.sysds.runtime.instructions.InstructionUtils; +import org.apache.sysds.runtime.matrix.operators.AggregateBinaryOperator; import org.apache.sysds.runtime.matrix.operators.BinaryOperator; +import org.apache.sysds.runtime.matrix.operators.ReorgOperator; import org.apache.sysds.runtime.matrix.operators.UnaryOperator; -import java.util.Arrays; - public class LibMatrixIMGTransform { protected static final Log LOG = LogFactory.getLog(LibMatrixFourier.class.getName()); @@ -73,10 +75,66 @@ public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlo MatrixBlock coords_mul; - op_mult = new BinaryOperator(Multiply.getMultiplyFnObject(), t_Inv, coords); + assert t_Inv != null; + AggregateBinaryOperator op_mul_agg = InstructionUtils.getMatMultOperator(threads); + UnaryOperator op_floor = new UnaryOperator(Builtin.getBuiltinFnObject(Builtin.BuiltinCode.FLOOR)); + BinaryOperator op_plus = InstructionUtils.parseExtendedBinaryOperator("+"); + coords_mul = t_Inv.aggregateBinaryOperations(t_Inv, coords, op_mul_agg); + coords_mul = coords_mul.unaryOperations(op_floor); + coords_mul = coords_mul.binaryOperationsInPlace(op_plus, new MatrixBlock(coords_mul.rlen, coords_mul.clen, 1.0)); + + ReorgOperator op_t = new ReorgOperator(SwapIndex.getSwapIndexFnObject(), threads); + // inx = t(coords[1,]) + MatrixBlock inx; + inx = coords_mul.slice(0,0); + inx = inx.reorgOperations(op_t, new MatrixBlock(), 0,0,inx.getNumColumns()); + // iny = t(coords[2,]) + MatrixBlock iny; + iny = coords_mul.slice(1,1); + iny = iny.reorgOperations(op_t, new MatrixBlock(), 0,0,iny.getNumColumns()); + //System.out.println(iny); + // # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input + // index_vector = (orig_w *(iny-1) + inx) * ((0"); + //BinaryOperator op_less = InstructionUtils.parseExtendedBinaryOperator("<"); + BinaryOperator op_less_equal = InstructionUtils.parseExtendedBinaryOperator("<="); + //BinaryOperator op_greater_equal = InstructionUtils.parseExtendedBinaryOperator(">="); + BinaryOperator op_and = InstructionUtils.parseExtendedBinaryOperator("&&"); + MatrixBlock helper_one; //(0 Date: Sat, 27 Jul 2024 16:40:42 +0200 Subject: [PATCH 13/25] almost completed the transformation matrix implementation --- .../matrix/data/LibMatrixIMGTransform.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index f066488aca9..1f54f06bec9 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -1,9 +1,7 @@ package org.apache.sysds.runtime.matrix.data; -import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.sysds.api.mlcontext.Matrix; import org.apache.sysds.runtime.functionobjects.Builtin; import org.apache.sysds.runtime.functionobjects.SwapIndex; import org.apache.sysds.runtime.instructions.InstructionUtils; @@ -119,6 +117,7 @@ public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlo second_term = helper_one.binaryOperations(op_and, helper_two); //(0 Date: Sat, 27 Jul 2024 20:51:48 +0200 Subject: [PATCH 14/25] ctable is not working in any meaningful way --- scripts/builtin/img_transform_test.dml | 15 +++++++ .../matrix/data/LibMatrixIMGTransform.java | 40 +++++++++++++++---- .../builtin/image_transform_matrix.dml | 5 ++- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml index d3cd2fafb80..37c3f91fd2a 100644 --- a/scripts/builtin/img_transform_test.dml +++ b/scripts/builtin/img_transform_test.dml @@ -20,10 +20,23 @@ m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensio inx = t(coords[1,]) iny = t(coords[2,]) + # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input index_vector = (orig_w *(iny-1) + inx) * ((0 Date: Sun, 28 Jul 2024 15:28:56 +0200 Subject: [PATCH 15/25] image_transform_matrix.dml is now working, some testing and evaluation needs to be done though --- scripts/builtin/img_transform_test.dml | 22 ++++---- .../matrix/data/LibMatrixIMGTransform.java | 51 +++++++++++++------ .../BuiltinImageTransformMatrixTest.java | 19 ++++++- .../builtin/image_transform_matrix.dml | 15 +++--- 4 files changed, 73 insertions(+), 34 deletions(-) diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml index 37c3f91fd2a..c02d47d300d 100644 --- a/scripts/builtin/img_transform_test.dml +++ b/scripts/builtin/img_transform_test.dml @@ -25,18 +25,18 @@ m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensio index_vector = (orig_w *(iny-1) + inx) * ((0 data() { - return Arrays.asList(new Object[][] {{new double[][] {{2,0,0},{0,1,0},{0,0,1}}, new double[][] {{10, 10},{15,15}}}}); + return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5},{t6, d6},{t7, d7},{t8, d8}}); } @Override diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml index 9d6b216dda1..03f1db1e948 100644 --- a/src/test/scripts/functions/builtin/image_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -30,11 +30,11 @@ dimensionMatrix = read($dimMat) # print(toString(dimensionMatrix)) -orig_w = as.scalar(dimensionMatrix[1,1]) -orig_h = as.scalar(dimensionMatrix[1,2]) -out_w = as.scalar(dimensionMatrix[2,1]) -out_h = as.scalar(dimensionMatrix[2,2]) -# print("orig_w: " + orig_w + "; orig_h: " + orig_h + "; out_w: " + out_w + "; out_h: " + out_h) +#orig_w = as.scalar(dimensionMatrix[1,1]) +#orig_h = as.scalar(dimensionMatrix[1,2]) +#out_w = as.scalar(dimensionMatrix[2,1]) +#out_h = as.scalar(dimensionMatrix[2,2]) +#print("orig_w: " + orig_w + "; orig_h: " + orig_h + "; out_w: " + out_w + "; out_h: " + out_h) input = matrix(transformationMatrix, rows=3, cols=3) builtin = TRUE @@ -46,9 +46,10 @@ if(builtin){ # function for calculating the matrix which will be matrix multiplied with # the image for matrix multiplication +isFillable = as.logical(as.scalar(isFillable)) - -print(toString(zMat)) +#print(toString(zMat)) print(nrow(zMat)) print(ncol(zMat)) +print("IsFillable: " + toString(isFillable)) write(zMat, $out_file) From bf120d2495993dba465011108ce3abcb1de2fcd4 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sun, 28 Jul 2024 16:19:26 +0200 Subject: [PATCH 16/25] cleanup of existing code for pull request --- .../org/apache/sysds/common/Builtins.java | 1 - .../java/org/apache/sysds/common/Types.java | 3 - .../org/apache/sysds/hops/FunctionOp.java | 3 - .../parser/BuiltinFunctionExpression.java | 58 ++------- .../apache/sysds/parser/DMLTranslator.java | 9 -- .../cp/MultiReturnBuiltinCPInstruction.java | 1 - ...turnComplexMatrixBuiltinCPInstruction.java | 2 +- .../runtime/matrix/data/LibCommonsMath.java | 3 +- .../matrix/data/LibMatrixIMGTransform.java | 117 ++++++++++-------- .../BuiltinImageTransformMatrixTest.java | 32 ++--- 10 files changed, 94 insertions(+), 135 deletions(-) diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 81bed12574b..359b7d3469b 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -170,7 +170,6 @@ public enum Builtins { IMG_TRANSFORM_LINEARIZED("img_transform_linearized", true), IMG_TRANSFORM_MATRIX("img_transform_matrix", false, ReturnType.MULTI_RETURN), IMG_TRANSFORM_TEST("img_transform_test", true), - //IMG_TRANSFORM_MATRIX("img_transform_matrix", true), //TODO: remove unused builtin IMG_TRANSLATE("img_translate", true), IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), IMG_ROTATE("img_rotate", true), diff --git a/src/main/java/org/apache/sysds/common/Types.java b/src/main/java/org/apache/sysds/common/Types.java index 44f38fcf5c2..30cd6bf5bd8 100644 --- a/src/main/java/org/apache/sysds/common/Types.java +++ b/src/main/java/org/apache/sysds/common/Types.java @@ -428,7 +428,6 @@ public enum OpOp2 { MINUS_NZ(false), //sparse-safe minus: X-(mean*ppred(X,0,!=)) LOG_NZ(false), //sparse-safe log; ppred(X,0,"!=")*log(X,0.5) MINUS1_MULT(false); //1-X*Y - //IMG_TRANSFORM_MATRIX(false); private final boolean _validOuter; @@ -472,7 +471,6 @@ public String toString() { case FRAME_ROW_REPLICATE: return "freplicate"; case VALUE_SWAP: return "valueSwap"; case APPLY_SCHEMA: return "applySchema"; - //case IMG_TRANSFORM_MATRIX: return "img_transform_matrix"; default: return name().toLowerCase(); } } @@ -509,7 +507,6 @@ public static OpOp2 valueOfByOpcode(String opcode) { case "freplicate": return FRAME_ROW_REPLICATE; case "valueSwap": return VALUE_SWAP; case "applySchema": return APPLY_SCHEMA; - //case "img_transform_matrix": return IMG_TRANSFORM_MATRIX; default: return valueOf(opcode.toUpperCase()); } } diff --git a/src/main/java/org/apache/sysds/hops/FunctionOp.java b/src/main/java/org/apache/sysds/hops/FunctionOp.java index 78982305701..3bf2ec1b939 100644 --- a/src/main/java/org/apache/sysds/hops/FunctionOp.java +++ b/src/main/java/org/apache/sysds/hops/FunctionOp.java @@ -293,9 +293,6 @@ else if ( getFunctionName().equalsIgnoreCase("fft_linearized") ) { else if ( getFunctionName().equalsIgnoreCase("ifft_linearized") ) { // 2 matrices of size same as the input return 2 * OptimizerUtils.estimateSizeExactSparsity(getInput().get(0).getDim1(), getInput().get(0).getDim2(), 1.0); -// } else if (getFunctionName().equalsIgnoreCase("img_transform_matrix")) { -// -// return OptimizerUtils.estimateSizeExactSparsity(getInput().get(4)., getInput().get(5).getDim2(), 1.0); } else if ( getFunctionName().equalsIgnoreCase("stft") ) { // 2 matrices of size same as the input return 2*OptimizerUtils.estimateSizeExactSparsity(getInput().get(0).getDim1(), getInput().get(0).getDim2(), 1.0); diff --git a/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java b/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java index d46fb0475d3..4d5cc13c24a 100644 --- a/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java +++ b/src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java @@ -675,66 +675,28 @@ else if(((ConstIdentifier) getThirdExpr().getOutput()) } //implement the calculation of the transformation matrix for affine transformation of images case IMG_TRANSFORM_MATRIX: - //transformation matrix + //transformation matrix must be 3x3 matrix Expression expressionOne_IMG = getFirstExpr(); - //original width + //dimension matrix must be a 2x2 matrix Expression expressionTwo_IMG = getSecondExpr(); checkMatrixFrameParam(expressionOne_IMG); checkMatrixFrameParam(expressionTwo_IMG); - if ((expressionOne_IMG.getOutput().getDim1() != expressionOne_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 3) { - raiseValidateError("The first argument to " + _opcode + " must be a square 3x3 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS); - } else if ((expressionTwo_IMG.getOutput().getDim1() != expressionTwo_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 2) { - raiseValidateError("The second argument to " + _opcode + " must be a square 2x2 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS); - } -// else if(expressionOne.getOutput() == null || expressionOne.getOutput().getDim1() == 0 || -// expressionOne.getOutput().getDim2() == 0) { -// raiseValidateError("The first argument to " + _opcode + " cannot be an empty matrix.", false, -// LanguageErrorCodes.INVALID_PARAMETERS); -// } -// else if(expressionTwo != null) { -// if(expressionTwo.getOutput() == null || expressionTwo.getOutput().getDim1() == 0 || -// expressionTwo.getOutput().getDim2() == 0) { -// raiseValidateError("The second argument to " + _opcode -// + " cannot be an empty matrix. Provide either only a real matrix or a filled real and imaginary one.", -// false, LanguageErrorCodes.INVALID_PARAMETERS); -// } -// } - -// checkNumParameters(expressionTwo != null ? 2 : 1); -// checkMatrixParam(expressionOne); -// if(expressionTwo != null && expressionOne != null) { -// checkMatrixParam(expressionTwo); -// if(expressionOne.getOutput().getDim1() != expressionTwo.getOutput().getDim1() || -// expressionOne.getOutput().getDim2() != expressionTwo.getOutput().getDim2()) -// raiseValidateError("The real and imaginary part of the provided matrix are of different dimensions.", -// false); -// else if(!isPowerOfTwo(expressionTwo.getOutput().getDim2())) { -// raiseValidateError( -// "This IFFT_LINEARIZED implementation is only defined for matrices with columns that are powers of 2.", -// false, LanguageErrorCodes.INVALID_PARAMETERS); -// } -// } -// else if(expressionOne != null) { -// if(!isPowerOfTwo(expressionOne.getOutput().getDim2())) { -// raiseValidateError( -// "This IFFT_LINEARIZED implementation is only defined for matrices with columns that are powers of 2.", -// false, LanguageErrorCodes.INVALID_PARAMETERS); -// } -// } + if ((expressionOne_IMG.getOutput().getDim1() != expressionOne_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 3) { + raiseValidateError("The first argument to " + _opcode + " must be a square 3x3 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS); + } else if ((expressionTwo_IMG.getOutput().getDim1() != expressionTwo_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 2) { + raiseValidateError("The second argument to " + _opcode + " must be a square 2x2 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS); + } DataIdentifier img_transfrom_matrix_Out1 = (DataIdentifier) getOutputs()[0]; DataIdentifier img_transfrom_matrix_Out2 = (DataIdentifier) getOutputs()[1]; + //describe the matrix characteristics type and value img_transfrom_matrix_Out1.setDataType(DataType.MATRIX); img_transfrom_matrix_Out1.setValueType(ValueType.FP64); - //img_transfrom_matrix_Out1.setDimensions(getThirdExpr().getOutput()._dim1, getFourthExpr().getOutput()._dim1); //TODO: get dimensions right, extract from second input matrix - //out1.setBlocksize(getFirstExpr().getOutput().getBlocksize()); - img_transfrom_matrix_Out2.setDataType(DataType.MATRIX); - img_transfrom_matrix_Out2.setValueType(ValueType.FP32); - //img_transfrom_matrix_Out2.setDimensions(1,1); - + img_transfrom_matrix_Out2.setValueType(ValueType.FP64); + //the output dimensions are not known beforehand and vary based on input values break; case REMOVE: { checkNumParameters(2); diff --git a/src/main/java/org/apache/sysds/parser/DMLTranslator.java b/src/main/java/org/apache/sysds/parser/DMLTranslator.java index 4bc866d53e1..4b8cde1d7a8 100644 --- a/src/main/java/org/apache/sysds/parser/DMLTranslator.java +++ b/src/main/java/org/apache/sysds/parser/DMLTranslator.java @@ -2491,15 +2491,6 @@ else if ( sop.equalsIgnoreCase("!=") ) new NaryOp(target.getName(), target.getDataType(), target.getValueType(), appendOpN, processAllExpressions(source.getAllExpr(), hops)); break; - //case IMG_TRANSFORM_MATRIX: - // currBuiltinOp = new BinaryOp(target.getName(), target.getDataType(), target.getValueType(), OpOp2.IMG_TRANSFORM_MATRIX , expr, expr2); -// switch (source.getOpCode()){ -// default: -// throw new ParseException(source.printErrorLocation() + -// "processBuiltinFunctionExpression():: Could not find Operation type for builtin function: " -// + source.getOpCode()); -// } - // break; case TABLE: // Always a TertiaryOp is created for table(). diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java index 45bdffcb076..4d2d2c91683 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnBuiltinCPInstruction.java @@ -20,7 +20,6 @@ package org.apache.sysds.runtime.instructions.cp; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.NotImplementedException; diff --git a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java index bc75f33c60a..e5571de1cd8 100644 --- a/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java +++ b/src/main/java/org/apache/sysds/runtime/instructions/cp/MultiReturnComplexMatrixBuiltinCPInstruction.java @@ -144,7 +144,7 @@ else if(parts.length == 8 && opcode.equalsIgnoreCase("stft")) { CPOperand in2 = new CPOperand(parts[2]); //dimension matrix [[orig_w, orig_h],[out_w, out_h]] outputs.add(new CPOperand(parts[3], ValueType.FP64, DataType.MATRIX)); - outputs.add(new CPOperand(parts[4], ValueType.BOOLEAN, DataType.SCALAR)); + outputs.add(new CPOperand(parts[4], ValueType.FP64, DataType.MATRIX)); int threads = Integer.parseInt(parts[5]); //throw new NotImplementedException("Has yet to be done. Check number of inputs" + Arrays.toString(parts) + "; Number of parts: " + parts.length); return new MultiReturnComplexMatrixBuiltinCPInstruction(null, in1, in2, outputs, opcode, str, threads); diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java index 0f49d1e49f8..a66a674fe94 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibCommonsMath.java @@ -23,6 +23,7 @@ import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.fft_linearized; import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.ifft; import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.ifft_linearized; +import static org.apache.sysds.runtime.matrix.data.LibMatrixIMGTransform.transformationMatrix; import java.util.HashMap; import java.util.Map; @@ -173,7 +174,7 @@ public static MatrixBlock[] multiReturnOperations(MatrixBlock in1, MatrixBlock i return computeRCM(in1, in2); case "img_transform_matrix": //throw new NotImplementedException("Hope we get here"); - return LibMatrixIMGTransform.transformationMatrix(in1, in2, threads); + return transformationMatrix(in1, in2, threads); default: return null; } diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index a041e50114a..aa2ebdf940f 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -13,40 +13,45 @@ public class LibMatrixIMGTransform { protected static final Log LOG = LogFactory.getLog(LibMatrixFourier.class.getName()); - /** - * affine transformation matrix for calculated for a picture of original size and target dimensions - * see: https://en.wikipedia.org/wiki/Affine_transformation - * - * #orig_w = as.scalar(dimMat[1,2]) - * #orig_h = as.scalar(dimMat[1,1]) - * #out_w = as.scalar(dimMat[2,2]) - * #out_h = as.scalar(dimMat[2,1]) - * T_inv = inv(transMat) - * - * ## coordinates of output pixel-centers linearized in row-major order - * coords = matrix(1, rows=3, cols=out_w*out_h) - * coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) - * coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) - * # compute sampling pixel indices - * coords = floor(T_inv %*% coords) + 1 - * inx = t(coords[1,]) - * iny = t(coords[2,]) - * # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input - * index_vector = (orig_w *(iny-1) + inx) * ((0 data() { From e35ea845c18459bdf8b2acb1587f647922141e6e Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sun, 28 Jul 2024 17:51:15 +0200 Subject: [PATCH 17/25] added approriate licences to new dml and java files --- scripts/builtin/img_transform_test.dml | 21 +++++++++++++++++ .../matrix/data/LibMatrixIMGTransform.java | 19 +++++++++++++++ .../BuiltinImageTransformLinearizedTest.java | 19 +++++++++++++++ .../BuiltinImageTransformMatrixTest.java | 23 ++++++++++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml index c02d47d300d..05706c5865c 100644 --- a/scripts/builtin/img_transform_test.dml +++ b/scripts/builtin/img_transform_test.dml @@ -1,3 +1,24 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +#------------------------------------------------------------- + m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensionMatrix) return (Matrix[Double] zMat, Matrix[Double] isFillable){ #zMat = transMat diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index aa2ebdf940f..cb8d709cc37 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.sysds.runtime.matrix.data; import org.apache.commons.logging.Log; diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java index 39aae9b288c..c90a57dbd38 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.sysds.test.functions.builtin.part1; import org.junit.Ignore; diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java index 23421ea5e94..e80cb9ee445 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.sysds.test.functions.builtin.part1; import org.apache.sysds.test.AutomatedTestBase; @@ -40,10 +59,12 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private static final double [][] d7 = new double[][] {{1920, 1080},{3840, 2160}}; private static final double [][] t8 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; private static final double [][] d8 = new double[][] {{3840, 2160},{1980, 1080}}; + private static final double [][] t9 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; + private static final double [][] d9 = new double[][] {{6000, 4000},{6000, 4000}}; @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5},{t6, d6},{t7, d7},{t8, d8}}); + return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5},{t6, d6},{t7, d7},{t8, d8},{t9, d9}}); } @Override From a2d0ee95e0e0998b7a8c290b391def51416c5de0 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sun, 28 Jul 2024 17:59:04 +0200 Subject: [PATCH 18/25] improved documentation based on automated tests --- .../apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index cb8d709cc37..cbb86ac4847 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -69,6 +69,8 @@ public class LibMatrixIMGTransform { * @param transMat affine 3x3 matrix for image transformations * @param dimMat 2x2 matrix with original and output image dimensions * @param threads number of threads for use in different methods + * + * @return array of two matrix blocks, 1st is the transformation matrix, 2nd a 1x1 matrix with 1 or 0 */ public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlock dimMat, int threads) { int orig_w = (int) dimMat.get(0,0); From e3e06d329734d7d3d74e7868c22cd0f56e0cde14 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sun, 28 Jul 2024 18:20:52 +0200 Subject: [PATCH 19/25] disabled the transform linearized test for now, aim is to enable later on or depricate the original dml file all together --- .../builtin/part1/BuiltinImageTransformLinearizedTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java index c90a57dbd38..7452b534f46 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -88,6 +88,7 @@ public void setUp() { } @Test + @Ignore public void testImageTransformLinearized() { runImageTransformLinearizedTest(false, ExecType.CP); } From ba4322cfae4650796c1f7f1a7c0768ead3bf21f6 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Sun, 28 Jul 2024 20:24:32 +0200 Subject: [PATCH 20/25] changes to BuiltinImageTransformLinearizedTest.java and image_transform_linearized.dml, img_transform_matrix.dml, img_transform_linearized.dml --- scripts/builtin/img_transform_linearized.dml | 3 +- scripts/builtin/img_transform_matrix.dml | 2 +- .../BuiltinImageTransformLinearizedTest.java | 42 +++++++++++++++- .../BuiltinImageTransformMatrixTest.java | 8 ++- .../builtin/image_transform_linearized.dml | 50 +++++++++++++++++-- 5 files changed, 94 insertions(+), 11 deletions(-) diff --git a/scripts/builtin/img_transform_linearized.dml b/scripts/builtin/img_transform_linearized.dml index 06867d61b2e..08c03d818e4 100644 --- a/scripts/builtin/img_transform_linearized.dml +++ b/scripts/builtin/img_transform_linearized.dml @@ -85,7 +85,8 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) z = table(xs, ind) output = ys%*%z - + print(nrow(img_in)) + print(ncol(img_in)) img_out = matrix(output, rows=nrow(img_in), cols=out_w*out_h) } } diff --git a/scripts/builtin/img_transform_matrix.dml b/scripts/builtin/img_transform_matrix.dml index 5ac02bfd7e7..7d2b2458c60 100644 --- a/scripts/builtin/img_transform_matrix.dml +++ b/scripts/builtin/img_transform_matrix.dml @@ -73,5 +73,5 @@ ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) z = table(xs, ind) zMat = transMat - isFillable = as.double(min(index_vector) == 0) + isFillable = as.logical(as.double(min(index_vector) == 0)) } \ No newline at end of file diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java index 7452b534f46..de13a8735b8 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -19,6 +19,7 @@ package org.apache.sysds.test.functions.builtin.part1; +import org.apache.spark.sql.catalyst.expressions.CheckOverflowInTableInsert; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -78,7 +79,7 @@ public class BuiltinImageTransformLinearizedTest extends AutomatedTestBase { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{1,512, 512, 512, 512, 1,0,0,0,1,0,1, 512, 512}}); + return Arrays.asList(new Object[][] {{1, 512, 512, 512, 512, 1,0,0,0,1,0,1, 512, 512}}); } @Override @@ -88,17 +89,23 @@ public void setUp() { } @Test - @Ignore public void testImageTransformLinearized() { runImageTransformLinearizedTest(false, ExecType.CP); } + @Test + public void testImageTransformLinearizedEmpty() { + runImageTransformLinearizedTestEmpty(false, ExecType.CP); + } + @Test @Ignore public void testImageTransformLinearizedSP() { runImageTransformLinearizedTest(true, ExecType.SPARK); } + + private void runImageTransformLinearizedTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); @@ -131,4 +138,35 @@ private void runImageTransformLinearizedTest(boolean sparse, ExecType instType) rtplatform = platformOld; } } + private void runImageTransformLinearizedTestEmpty(boolean sparse, ExecType instType) { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try { + loadTestConfiguration(getTestConfiguration(TEST_NAME_LINEARIZED)); + + double sparsity = sparse ? spSparse : spDense; + String HOME = SCRIPT_DIR + TEST_DIR; + + fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; + programArgs = new String[]{"-nvargs", "in_file=" + input("A"), "width=", "height=", + "out_w=", "out_h=", "a=", "b=", "c=", "d=", "e=", "f=", + "fill_value=", "s_cols=", "s_rows=", + "out_file=" + output("B_x")}; + + double[][] A = getRandomMatrix(rows, height * width, 0, 255, sparsity, 7); + writeInputMatrixWithMTD("A", A, true); + + runTest(true, false, null, -1); + + //HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); + + //HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); + + //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); + + } finally { + rtplatform = platformOld; + } + } } diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java index e80cb9ee445..4050d2e55b6 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java @@ -38,6 +38,7 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private final static String TEST_DIR = "functions/builtin/"; private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageTransformMatrixTest.class.getSimpleName() + "/"; + @Parameterized.Parameter(0) public double[][] transMat; @Parameterized.Parameter(1) @@ -60,11 +61,14 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private static final double [][] t8 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; private static final double [][] d8 = new double[][] {{3840, 2160},{1980, 1080}}; private static final double [][] t9 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; - private static final double [][] d9 = new double[][] {{6000, 4000},{6000, 4000}}; + private static final double [][] d9 = new double[][] {{5000, 3000},{5000, 3000}}; + private static final double [][] t10 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; + private static final double [][] d10 = new double[][] {{1, 3000},{1, 3000}}; @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5},{t6, d6},{t7, d7},{t8, d8},{t9, d9}}); + return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5}, + {t6, d6},{t7, d7},{t8, d8},{t9, d9},{t10, d10}}); } @Override diff --git a/src/test/scripts/functions/builtin/image_transform_linearized.dml b/src/test/scripts/functions/builtin/image_transform_linearized.dml index e53440b6a17..7826122bfce 100644 --- a/src/test/scripts/functions/builtin/image_transform_linearized.dml +++ b/src/test/scripts/functions/builtin/image_transform_linearized.dml @@ -24,9 +24,9 @@ width = ifdef($width, 512) height = ifdef($height, 512) out_w = ifdef($out_w, 512) out_h = ifdef($out_h, 512) -a = ifdef($a, 1) -b = ifdef($b, 0) -c = ifdef($c, 0) +a = ifdef($a, 2) +b = ifdef($b, 1) +c = ifdef($c, 1) d = ifdef($d, 0) e = ifdef($e, 1) f = ifdef($f, 0) @@ -34,7 +34,47 @@ fill_value = ifdef($fill_value, 0) s_cols = ifdef($s_cols, 512) s_rows = ifdef($s_rows, 512) -input = matrix(input, rows=height, cols=width) - +#inputMat = matrix(input, rows=height, cols=width) +print(nrow(input)) +print(ncol(input)) transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) +print(nrow(transformed)) +print(ncol(transformed)) + +#affineMat = matrix(0,rows=3, cols=3) +#affineMat[1,1] = a +#affineMat[1,2] = b +#affineMat[1,3] = c +#affineMat[2,1] = d +#affineMat[2,2] = e +#affineMat[2,3] = f +#affineMat[3,3] = 1 +#dimMat = matrix(0,rows=2, cols=2) +#dimMat[1,1] = width +#dimMat[1,2] = height +#dimMat[2,1] = out_w +#dimMat[2,2] = out_h + +#[zMat, isFillable] = img_transform_test(affineMat, dimMat) + +#print(nrow(zMat)) +#print(ncol(zMat)) +#print(toString(isFillable)) + +#if(min(as.logical(as.scalar(isFillable))){ +# ys=cbind(input, matrix(fill_value,nrow(input), 1)) +# }else{ +# ys = input +# } + + #ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) + #z = table(xs, ind) +#output = ys%*%zMat + +#img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) + + +#print(toString(affineMat)) +#print(toString(dimMat)) +#print(toString(transformed)) write(transformed, $out_file) From 3e2cb7d94b099185d8e2d3432b9c2d345367ea2f Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Mon, 29 Jul 2024 14:59:26 +0200 Subject: [PATCH 21/25] various changes --- .../matrix/data/LibMatrixIMGTransform.java | 45 ++++++++++++++++++- .../BuiltinImageTransformLinearizedTest.java | 7 ++- .../BuiltinImageTransformMatrixTest.java | 9 +++- .../builtin/image_transform_linearized.dml | 4 +- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java index cbb86ac4847..d90d977518c 100644 --- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java +++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java @@ -28,6 +28,8 @@ import org.apache.sysds.runtime.matrix.operators.*; import org.apache.sysds.runtime.util.DataConverter; +import static org.apache.commons.math3.util.FastMath.floor; + public class LibMatrixIMGTransform { protected static final Log LOG = LogFactory.getLog(LibMatrixFourier.class.getName()); @@ -73,6 +75,9 @@ public class LibMatrixIMGTransform { * @return array of two matrix blocks, 1st is the transformation matrix, 2nd a 1x1 matrix with 1 or 0 */ public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlock dimMat, int threads) { + //check the correctness of the input dimension matrix + isValidDimensionMatrix(dimMat); + int orig_w = (int) dimMat.get(0,0); int orig_h = (int) dimMat.get(0,1); int out_w = (int) dimMat.get(1,0); @@ -90,7 +95,7 @@ public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlo double [] coords3 = new double[out_w*out_h]; for(int i=0; i=1 && dimMat.get(0,1)>=1 && dimMat.get(1,0)>=1 && dimMat.get(1,1)>=1){ + //check if the double values of the dimension matrix are actually positive natural numbers + //i.e. that they can be cast to int without loss of information for matrix generation + if(!(dimMat.get(0,0)== floor(dimMat.get(0,0))) || !(dimMat.get(0,1) == floor(dimMat.get(0,1))) + || !(dimMat.get(1,0)== floor(dimMat.get(1,0))) || !(dimMat.get(1,1) == floor(dimMat.get(1,1)))){ + throw new RuntimeException("Image dimensions are not positive natural numbers! Check input and output image dimensions!"); + } + }else{ + throw new RuntimeException("Wrong values! Image dimensions cannot be zero or negative! Check input and output image dimensions!"); + } + } } diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java index de13a8735b8..70b39622335 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformLinearizedTest.java @@ -79,7 +79,7 @@ public class BuiltinImageTransformLinearizedTest extends AutomatedTestBase { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{1, 512, 512, 512, 512, 1,0,0,0,1,0,1, 512, 512}}); + return Arrays.asList(new Object[][] {{1, 512, 512, 512, 512, 2,0,0,0,1,0,1, 512, 512}}); } @Override @@ -117,7 +117,10 @@ private void runImageTransformLinearizedTest(boolean sparse, ExecType instType) String HOME = SCRIPT_DIR + TEST_DIR; fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; - programArgs = new String[] {"-nvargs", "in_file=" + input("A"), "width=" + width, "height=" + height, + programArgs = new String[] {"-nvargs", + "in_file=/home/mark-p4/data/pic1.csv", + //"in_file=" + input("A"), + "width=" + width, "height=" + height, "out_w=" + out_w, "out_h=" + out_h, "a=" + a, "b=" + b, "c=" + c, "d=" + d, "e=" + e, "f=" + f, "fill_value=" + fill_value, "s_cols=" + s_cols, "s_rows=" + s_rows, "out_file=" + output("B_x")}; diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java index 4050d2e55b6..45b2075162f 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java @@ -38,6 +38,12 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private final static String TEST_DIR = "functions/builtin/"; private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageTransformMatrixTest.class.getSimpleName() + "/"; + private enum TestType{ + TEST_WORKS, + TEST_FAILS, + COMPARE_TO_SCRIPT, + PERFORMANCE + } @Parameterized.Parameter(0) public double[][] transMat; @@ -63,7 +69,7 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private static final double [][] t9 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; private static final double [][] d9 = new double[][] {{5000, 3000},{5000, 3000}}; private static final double [][] t10 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; - private static final double [][] d10 = new double[][] {{1, 3000},{1, 3000}}; + private static final double [][] d10 = new double[][] {{10, 3000},{1, 3000}}; @Parameterized.Parameters public static Collection data() { @@ -82,6 +88,7 @@ public void testImageTransformMatrix() { runImageTransformMatrixTest(ExecType.CP); } + private void runImageTransformMatrixTest(ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); diff --git a/src/test/scripts/functions/builtin/image_transform_linearized.dml b/src/test/scripts/functions/builtin/image_transform_linearized.dml index 7826122bfce..53e6b804de7 100644 --- a/src/test/scripts/functions/builtin/image_transform_linearized.dml +++ b/src/test/scripts/functions/builtin/image_transform_linearized.dml @@ -34,10 +34,10 @@ fill_value = ifdef($fill_value, 0) s_cols = ifdef($s_cols, 512) s_rows = ifdef($s_rows, 512) -#inputMat = matrix(input, rows=height, cols=width) +inputMat = matrix(input, rows=1, cols=width*height) print(nrow(input)) print(ncol(input)) -transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) +transformed = img_transform_linearized(inputMat, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) print(nrow(transformed)) print(ncol(transformed)) From fa7d7c1c2e7a2f02e3f4c1d0deff2151c19e3eb6 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Mon, 29 Jul 2024 20:39:22 +0200 Subject: [PATCH 22/25] added changes to tests and cleaned up LibMatrixIMGTransform --- scripts/builtin/img_transform_test.dml | 24 +----- .../matrix/data/LibMatrixIMGTransform.java | 65 ++------------- .../BuiltinImageTransformMatrixTest.java | 79 +++++++++---------- .../builtin/image_transform_matrix.dml | 2 +- 4 files changed, 43 insertions(+), 127 deletions(-) diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml index 05706c5865c..1cc83f8a74f 100644 --- a/scripts/builtin/img_transform_test.dml +++ b/scripts/builtin/img_transform_test.dml @@ -41,34 +41,12 @@ m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensio inx = t(coords[1,]) iny = t(coords[2,]) - # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input index_vector = (orig_w *(iny-1) + inx) * ((0"); - //BinaryOperator op_less = InstructionUtils.parseExtendedBinaryOperator("<"); BinaryOperator op_less_equal = InstructionUtils.parseExtendedBinaryOperator("<="); - //BinaryOperator op_greater_equal = InstructionUtils.parseExtendedBinaryOperator(">="); BinaryOperator op_and = InstructionUtils.parseExtendedBinaryOperator("&&"); MatrixBlock helper_one; //(0 data() { - return Arrays.asList(new Object[][] {{t1, d1},{t2, d2},{t3, d3},{t4, d4},{t5, d5}, - {t6, d6},{t7, d7},{t8, d8},{t9, d9},{t10, d10}}); + return Arrays.asList(new Object[][] {{t1, d1, false},{t2, d2, false},{t3, d3, false},{t4, d4, false}, + {t5, d5,false},{t6, d6, false},{t7, d7,false},{t8, d8,false}, + {t9, d9,true},{t10,d10,true},{t11, d11,true},{t12, d12,true},{t13, d13,true}}); } @Override @@ -88,7 +90,6 @@ public void testImageTransformMatrix() { runImageTransformMatrixTest(ExecType.CP); } - private void runImageTransformMatrixTest(ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); @@ -104,16 +105,8 @@ private void runImageTransformMatrixTest(ExecType instType) { fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; programArgs = new String[]{"-nvargs", "transMat=" + input("transMat"), "dimMat=" + input("dimMat"), "out_file=" + output("B_x"), "--debug"}; - //double[][] A = getRandomMatrix(rows, height*width, 0, 255, sparsity, 7); - - - runTest(true, false, null, -1); - - //HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); - - //HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); - //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); + runTest(true, fails, null, -1); } catch (Exception e) { e.printStackTrace(); @@ -123,4 +116,4 @@ private void runImageTransformMatrixTest(ExecType instType) { } -} +} \ No newline at end of file diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml index 03f1db1e948..111cfe89327 100644 --- a/src/test/scripts/functions/builtin/image_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -24,7 +24,7 @@ transformationMatrix = read($transMat) #print(toString(transformationMatrix)) # get a matrix with the original image dimensions and the target dimensions (2x2) -# -----original width----original heigh +# -----original width----original height # -----target width------target height dimensionMatrix = read($dimMat) From e32ca44678a19597728dcf812a1ab5a3e639e7d6 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Tue, 30 Jul 2024 12:52:22 +0200 Subject: [PATCH 23/25] improved tests and looking into bug with the img_transform_linearized.dml script, where it works for certain tests but not for others --- scripts/builtin/img_transform_linearized.dml | 21 ++++-- .../BuiltinImageTransformLinearizedTest.java | 11 ++-- .../BuiltinImageTransformMatrixTest.java | 39 +++++++++-- .../BuiltinImageTransformLinTest.java | 7 +- .../builtin/image_transform_linearized.dml | 66 +++++++++---------- .../builtin/image_transform_matrix.dml | 3 + 6 files changed, 97 insertions(+), 50 deletions(-) diff --git a/scripts/builtin/img_transform_linearized.dml b/scripts/builtin/img_transform_linearized.dml index 08c03d818e4..83f36a9269a 100644 --- a/scripts/builtin/img_transform_linearized.dml +++ b/scripts/builtin/img_transform_linearized.dml @@ -75,18 +75,29 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte index_vector = (orig_w *(iny-1) + inx) * ((0 data() { - return Arrays.asList(new Object[][] {{1, 512, 512, 512, 512, 2,0,0,0,1,0,1, 512, 512}}); + return Arrays.asList(new Object[][] {{4, 512, 512, 512, 512, 2,0,0,0,1,0,0, 512, 512}}); } @Override @@ -118,9 +118,9 @@ private void runImageTransformLinearizedTest(boolean sparse, ExecType instType) fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; programArgs = new String[] {"-nvargs", - "in_file=/home/mark-p4/data/pic1.csv", - //"in_file=" + input("A"), - "width=" + width, "height=" + height, + //"in_file=/home/mark-p4/data/pic1.csv", + "in_file=" + input("A"), + "width=" + width*height, "height=" + rows, "out_w=" + out_w, "out_h=" + out_h, "a=" + a, "b=" + b, "c=" + c, "d=" + d, "e=" + e, "f=" + f, "fill_value=" + fill_value, "s_cols=" + s_cols, "s_rows=" + s_rows, "out_file=" + output("B_x")}; @@ -157,7 +157,8 @@ private void runImageTransformLinearizedTestEmpty(boolean sparse, ExecType instT "fill_value=", "s_cols=", "s_rows=", "out_file=" + output("B_x")}; - double[][] A = getRandomMatrix(rows, height * width, 0, 255, sparsity, 7); + int dims = width * height; + double[][] A = getRandomMatrix(rows, dims, 0, 255, sparsity, 7); writeInputMatrixWithMTD("A", A, true); runTest(true, false, null, -1); diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java index 2e67f67ff41..8ed152f2fe9 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageTransformMatrixTest.java @@ -19,8 +19,9 @@ package org.apache.sysds.test.functions.builtin.part1; -import org.apache.commons.lang3.NotImplementedException; +import org.apache.sysds.runtime.matrix.data.MatrixValue; import org.apache.sysds.test.AutomatedTestBase; +import org.apache.sysds.test.TestUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -72,6 +73,9 @@ public class BuiltinImageTransformMatrixTest extends AutomatedTestBase { private final static double [][] t13 = new double[][] {{2,0,0},{0,1,0},{0,0,1}}; private final static double [][] d13 = new double[][] {{0.10, 200},{200, 200}}; + public double internal = 1.0; + public boolean compareResults = false; + @Parameterized.Parameters public static Collection data() { return Arrays.asList(new Object[][] {{t1, d1, false},{t2, d2, false},{t3, d3, false},{t4, d4, false}, @@ -85,11 +89,28 @@ public void setUp() { new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[] {"B_x"})); } + //test for using the internal implementation only @Test public void testImageTransformMatrix() { runImageTransformMatrixTest(ExecType.CP); } + //test for using the script implementation only + @Test + public void testImageTransformMatrixScript() { + internal = 0; + runImageTransformMatrixTest(ExecType.CP); + } + + //test for comparing the script and internal implementations for correctness + //presumably due to caching it should not be used for benchmarks + @Test + public void testImageTransformMatrixCompare() { + internal = 1; + compareResults = true; + runImageTransformMatrixTest(ExecType.CP); + } + private void runImageTransformMatrixTest(ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); @@ -101,17 +122,27 @@ private void runImageTransformMatrixTest(ExecType instType) { writeInputMatrixWithMTD("transMat", transMat, true); writeInputMatrixWithMTD("dimMat", dimMat, true); + writeInputMatrixWithMTD("internal", new double[][] {{internal}}, true); fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; - programArgs = new String[]{"-nvargs", "transMat=" + input("transMat"), "dimMat=" + input("dimMat"), "out_file=" + output("B_x"), "--debug"}; - - + programArgs = new String[]{"-nvargs", "transMat=" + input("transMat"), "dimMat=" + input("dimMat"), "out_file=" + output("B_internal"),"internal=" + input("internal"), "--debug"}; runTest(true, fails, null, -1); + if (compareResults && !fails) { + internal = 0; + writeInputMatrixWithMTD("internal", new double[][] {{internal}}, true); + programArgs = new String[]{"-nvargs", "transMat=" + input("transMat"), "dimMat=" + input("dimMat"), "out_file=" + output("B_script"),"internal=" + input("internal"), "--debug"}; + runTest(true, fails, null, -1); + + HashMap internalfile = readDMLMatrixFromOutputDir("B_internal"); + HashMap scriptfile = readDMLMatrixFromOutputDir("B_script"); + TestUtils.compareMatrices(internalfile, scriptfile, 1e-10, "Stat-DML", "Stat-R"); + } } catch (Exception e) { e.printStackTrace(); } finally { rtplatform = platformOld; + internal = 1.0; } } diff --git a/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java b/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java index 0f83938578c..7bf80db31fa 100644 --- a/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java +++ b/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java @@ -62,8 +62,9 @@ public class BuiltinImageTransformLinTest extends AutomatedTestBase { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{16, 15, 50}, {32, 31, 100}, {64, 64, 200}, {127, 128, 100}, {256, 256, 200}, - {500, 135, 100}}); + return Arrays.asList(new Object[][] {{16, 15, 50}, {32, 31, 100}, {64, 64, 200}, + //{127, 128, 100}, {256, 256, 200}, {500, 135, 100} + }); } @Override @@ -104,7 +105,7 @@ public void testImageTransformLinMatrixSparseSP() { private void runImageTransformLinTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); - setOutputBuffering(true); + //setOutputBuffering(true); try { loadTestConfiguration(getTestConfiguration(TEST_NAME)); diff --git a/src/test/scripts/functions/builtin/image_transform_linearized.dml b/src/test/scripts/functions/builtin/image_transform_linearized.dml index 53e6b804de7..0c792bc0b44 100644 --- a/src/test/scripts/functions/builtin/image_transform_linearized.dml +++ b/src/test/scripts/functions/builtin/image_transform_linearized.dml @@ -34,47 +34,47 @@ fill_value = ifdef($fill_value, 0) s_cols = ifdef($s_cols, 512) s_rows = ifdef($s_rows, 512) -inputMat = matrix(input, rows=1, cols=width*height) -print(nrow(input)) -print(ncol(input)) -transformed = img_transform_linearized(inputMat, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) -print(nrow(transformed)) -print(ncol(transformed)) +#inputMat = matrix(input, rows=1, cols=width*height) +print("Nrow input matrix: " + nrow(input) + "; Ncol input matrix: " + ncol(input)) +#transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) +#print("Nrow transformed matrix: " + nrow(transformed) + "; Ncol transformed matrix: " + ncol(transformed)) -#affineMat = matrix(0,rows=3, cols=3) -#affineMat[1,1] = a -#affineMat[1,2] = b -#affineMat[1,3] = c -#affineMat[2,1] = d -#affineMat[2,2] = e -#affineMat[2,3] = f -#affineMat[3,3] = 1 -#dimMat = matrix(0,rows=2, cols=2) -#dimMat[1,1] = width -#dimMat[1,2] = height -#dimMat[2,1] = out_w -#dimMat[2,2] = out_h +affineMat = matrix(0,rows=3, cols=3) +affineMat[1,1] = a +affineMat[1,2] = b +affineMat[1,3] = c +affineMat[2,1] = d +affineMat[2,2] = e +affineMat[2,3] = f +affineMat[3,3] = 1 +dimMat = matrix(0,rows=2, cols=2) +dimMat[1,1] = width +dimMat[1,2] = height +dimMat[2,1] = out_w +dimMat[2,2] = out_h -#[zMat, isFillable] = img_transform_test(affineMat, dimMat) +[zMat, isFillable] = img_transform_test(affineMat, dimMat) -#print(nrow(zMat)) -#print(ncol(zMat)) -#print(toString(isFillable)) +print("zMat dimensions, nrow: " + nrow(zMat) + "; ncol: " + ncol(zMat)) +print(toString(isFillable)) -#if(min(as.logical(as.scalar(isFillable))){ -# ys=cbind(input, matrix(fill_value,nrow(input), 1)) -# }else{ -# ys = input -# } +if(as.logical(as.scalar(isFillable))){ + ys=cbind(input, matrix(fill_value,nrow(input), 1)) + print("Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) + }else{ + ys = input + print("No Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) + } - #ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) - #z = table(xs, ind) -#output = ys%*%zMat +#ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) +#z = table(xs, ind) +output = ys%*%zMat -#img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) +img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) #print(toString(affineMat)) #print(toString(dimMat)) #print(toString(transformed)) -write(transformed, $out_file) +#write(transformed, $out_file) +write(img_out, $out_file) \ No newline at end of file diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml index 111cfe89327..0278bdc1e8e 100644 --- a/src/test/scripts/functions/builtin/image_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -38,9 +38,12 @@ dimensionMatrix = read($dimMat) input = matrix(transformationMatrix, rows=3, cols=3) builtin = TRUE +builtin = as.logical(as.scalar(read($internal))) if(builtin){ + print("internal") [zMat, isFillable] = img_transform_matrix(input, dimensionMatrix) }else{ + print("script") [zMat, isFillable] = img_transform_test(input, dimensionMatrix) } From 3ba7e550eb0f495a422fa610b4928e12a401adea Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Tue, 30 Jul 2024 15:19:40 +0200 Subject: [PATCH 24/25] everything seems to work, nice --- scripts/builtin/img_transform_linearized.dml | 18 ++--- .../BuiltinImageTransformLinearizedTest.java | 2 +- .../BuiltinImageTransformLinTest.java | 10 ++- .../builtin/image_transform_linearized.dml | 69 +++++++++---------- 4 files changed, 46 insertions(+), 53 deletions(-) diff --git a/scripts/builtin/img_transform_linearized.dml b/scripts/builtin/img_transform_linearized.dml index 83f36a9269a..32774a33eb3 100644 --- a/scripts/builtin/img_transform_linearized.dml +++ b/scripts/builtin/img_transform_linearized.dml @@ -75,25 +75,25 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte index_vector = (orig_w *(iny-1) + inx) * ((0 data() { - return Arrays.asList(new Object[][] {{4, 512, 512, 512, 512, 2,0,0,0,1,0,0, 512, 512}}); + return Arrays.asList(new Object[][] {{4, 512, 512, 512, 512, 2,1,0,0,1,0,0, 512, 512}}); } @Override diff --git a/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java b/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java index 7bf80db31fa..4c3445fe641 100644 --- a/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java +++ b/src/test/java/org/apache/sysds/test/functions/pipelines/BuiltinImageTransformLinTest.java @@ -62,9 +62,7 @@ public class BuiltinImageTransformLinTest extends AutomatedTestBase { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][] {{16, 15, 50}, {32, 31, 100}, {64, 64, 200}, - //{127, 128, 100}, {256, 256, 200}, {500, 135, 100} - }); + return Arrays.asList(new Object[][] {{16, 15, 50}, {32, 31, 100}, {64, 64, 200},{127, 128, 100}, {256, 256, 200}, {500, 135, 100}}); } @Override @@ -105,7 +103,7 @@ public void testImageTransformLinMatrixSparseSP() { private void runImageTransformLinTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); - //setOutputBuffering(true); + setOutputBuffering(true); try { loadTestConfiguration(getTestConfiguration(TEST_NAME)); @@ -114,13 +112,13 @@ private void runImageTransformLinTest(boolean sparse, ExecType instType) { String HOME = SCRIPT_DIR + TEST_DIR; fullDMLScriptName = HOME + TEST_NAME + ".dml"; programArgs = new String[] {"-nvargs", "in_file=" + input("A"), "out_file=" + output("B"), - "width=" + s_cols * s_rows, "height=" + n_imgs, "out_w=" + s_cols, "out_h=" + s_rows * 1.2, "a=" + a, + "width=" + s_cols * s_rows, "height=" + n_imgs, "out_w=" + s_cols, "out_h=" + Math.floor(s_rows * 1.2), "a=" + a, "b=" + b, "c=" + c, "d=" + d, "e=" + e, "f=" + f, "fill_value=" + fill_value, "s_cols=" + s_cols, "s_rows=" + s_rows}; fullRScriptName = HOME + TEST_NAME + ".R"; rCmd = "Rscript" + " " + fullRScriptName + " " + inputDir() + " " + expectedDir() + " " + s_cols * s_rows + " " - + n_imgs + " " + s_cols + " " + (s_rows * 1.2) + " " + a + " " + b + " " + c + " " + d + " " + e + " " + f + + n_imgs + " " + s_cols + " " + Math.floor(s_rows * 1.2) + " " + a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + fill_value + " " + s_cols + " " + s_rows; // generate actual dataset diff --git a/src/test/scripts/functions/builtin/image_transform_linearized.dml b/src/test/scripts/functions/builtin/image_transform_linearized.dml index 0c792bc0b44..d8e07cea0b6 100644 --- a/src/test/scripts/functions/builtin/image_transform_linearized.dml +++ b/src/test/scripts/functions/builtin/image_transform_linearized.dml @@ -34,47 +34,42 @@ fill_value = ifdef($fill_value, 0) s_cols = ifdef($s_cols, 512) s_rows = ifdef($s_rows, 512) -#inputMat = matrix(input, rows=1, cols=width*height) -print("Nrow input matrix: " + nrow(input) + "; Ncol input matrix: " + ncol(input)) -#transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) -#print("Nrow transformed matrix: " + nrow(transformed) + "; Ncol transformed matrix: " + ncol(transformed)) +if(FALSE){ + input = matrix(input, rows=height, cols=width) + print("Nrow input matrix: " + nrow(input) + "; Ncol input matrix: " + ncol(input)) + transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) + #transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) + #print("Nrow transformed matrix: " + nrow(transformed) + "; Ncol transformed matrix: " + ncol(transformed)) + write(transformed, $out_file) +}else{ + affineMat = matrix(0,rows=3, cols=3) + affineMat[1,1] = a + affineMat[1,2] = b + affineMat[1,3] = c + affineMat[2,1] = d + affineMat[2,2] = e + affineMat[2,3] = f + affineMat[3,3] = 1 + dimMat = matrix(0,rows=2, cols=2) + dimMat[1,1] = s_cols + dimMat[1,2] = s_rows + dimMat[2,1] = out_w + dimMat[2,2] = out_h -affineMat = matrix(0,rows=3, cols=3) -affineMat[1,1] = a -affineMat[1,2] = b -affineMat[1,3] = c -affineMat[2,1] = d -affineMat[2,2] = e -affineMat[2,3] = f -affineMat[3,3] = 1 -dimMat = matrix(0,rows=2, cols=2) -dimMat[1,1] = width -dimMat[1,2] = height -dimMat[2,1] = out_w -dimMat[2,2] = out_h + [zMat, isFillable] = img_transform_matrix(affineMat, dimMat) -[zMat, isFillable] = img_transform_test(affineMat, dimMat) + print("zMat dimensions, nrow: " + nrow(zMat) + "; ncol: " + ncol(zMat)) -print("zMat dimensions, nrow: " + nrow(zMat) + "; ncol: " + ncol(zMat)) -print(toString(isFillable)) - -if(as.logical(as.scalar(isFillable))){ - ys=cbind(input, matrix(fill_value,nrow(input), 1)) - print("Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) + if(as.logical(as.scalar(isFillable))){ + ys=cbind(input, matrix(fill_value,nrow(input), 1)) + print("Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) }else{ - ys = input - print("No Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) + ys = input + print("No Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) } + output = ys%*%zMat -#ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) -#z = table(xs, ind) -output = ys%*%zMat - -img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) - + img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) + write(img_out, $out_file) -#print(toString(affineMat)) -#print(toString(dimMat)) -#print(toString(transformed)) -#write(transformed, $out_file) -write(img_out, $out_file) \ No newline at end of file +} From e825ca1a59b623550d2a4d597a71a5317e1fe736 Mon Sep 17 00:00:00 2001 From: mark-p4 Date: Tue, 30 Jul 2024 16:12:25 +0200 Subject: [PATCH 25/25] added improvements based on feedback and cleaned up code --- scripts/builtin/img_transform_linearized.dml | 15 +--- scripts/builtin/img_transform_test.dml | 52 ------------- .../org/apache/sysds/hops/FunctionOp.java | 2 +- .../parser/BuiltinFunctionExpression.java | 1 - .../cp/MultiReturnBuiltinCPInstruction.java | 4 +- ...turnComplexMatrixBuiltinCPInstruction.java | 3 +- .../runtime/matrix/data/LibCommonsMath.java | 1 - .../matrix/data/LibMatrixIMGTransform.java | 78 ++++++++----------- .../BuiltinImageTransformLinearizedTest.java | 19 ----- .../builtin/image_transform_linearized.dml | 35 ++++++--- .../builtin/image_transform_matrix.dml | 19 +---- .../functions/builtin/img_transform_test.dml | 55 ++++++------- 12 files changed, 91 insertions(+), 193 deletions(-) delete mode 100644 scripts/builtin/img_transform_test.dml rename scripts/builtin/img_transform_matrix.dml => src/test/scripts/functions/builtin/img_transform_test.dml (58%) diff --git a/scripts/builtin/img_transform_linearized.dml b/scripts/builtin/img_transform_linearized.dml index 32774a33eb3..296e5e513c7 100644 --- a/scripts/builtin/img_transform_linearized.dml +++ b/scripts/builtin/img_transform_linearized.dml @@ -22,6 +22,7 @@ # The Linearized Image Transform function applies an affine transformation to linearized images. # Optionally resizes the image (without scaling). # Uses nearest neighbor sampling. +# Is implemented as a builtin function as img_transform_matrix in image_transform_linearized. # # INPUT: # ------------------------------------------------------------------------------------------- @@ -76,28 +77,14 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte index_vector = t(index_vector) xs = ((index_vector == 0)*(orig_w*orig_h +1)) + index_vector - # print("IMG_IN nrow: " + nrow(img_in) "; ncol: " + ncol(img_in)) - # print("Nrow xs: " + nrow(xs) + "; Ncol xs: " + ncol(xs)) - ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) - # print("Nrow ind: " + nrow(ind) + "; Ncol ind: " + ncol(ind)) z = table(xs, ind) - # print("Nrow z: " + nrow(z) + "; Ncol z: " + ncol(z)) - # print("Min of index_vector: " + min(index_vector)) if(min(index_vector) == 0){ - # print("Nrow Img: " + nrow(img_in) + "; Ncol Img: " + ncol(img_in)) ys=cbind(img_in, matrix(fill_value,nrow(img_in), 1)) - # print("CBind Nrow ys: " + nrow(ys) + "; Ncol ys: " + ncol(ys)) }else{ - ys = img_in - # print("No CBind Nrow ys: " + nrow(ys) + "; Ncol ys: " + ncol(ys)) } - - - output = ys%*%z - img_out = matrix(output, rows=nrow(img_in), cols=out_w*out_h) } } diff --git a/scripts/builtin/img_transform_test.dml b/scripts/builtin/img_transform_test.dml deleted file mode 100644 index 1cc83f8a74f..00000000000 --- a/scripts/builtin/img_transform_test.dml +++ /dev/null @@ -1,52 +0,0 @@ -#------------------------------------------------------------- -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# -#------------------------------------------------------------- - -m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensionMatrix) - return (Matrix[Double] zMat, Matrix[Double] isFillable){ - #zMat = transMat - #isFillable = FALSE - T_inv = inv(transMat) - - orig_w = as.scalar(dimensionMatrix[1,1]) - orig_h = as.scalar(dimensionMatrix[1,2]) - out_w = as.scalar(dimensionMatrix[2,1]) - out_h = as.scalar(dimensionMatrix[2,2]) - - ## coordinates of output pixel-centers linearized in row-major order - coords = matrix(1, rows=3, cols=out_w*out_h) - coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) - coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) - - # compute sampling pixel indices - coords = floor(T_inv %*% coords) + 1 - - inx = t(coords[1,]) - iny = t(coords[2,]) - - # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input - index_vector = (orig_w *(iny-1) + inx) * ((0 dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); - - //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); - } finally { rtplatform = platformOld; @@ -163,12 +150,6 @@ private void runImageTransformLinearizedTestEmpty(boolean sparse, ExecType instT runTest(true, false, null, -1); - //HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); - - //HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); - - //TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); - } finally { rtplatform = platformOld; } diff --git a/src/test/scripts/functions/builtin/image_transform_linearized.dml b/src/test/scripts/functions/builtin/image_transform_linearized.dml index d8e07cea0b6..2dbbd2590e1 100644 --- a/src/test/scripts/functions/builtin/image_transform_linearized.dml +++ b/src/test/scripts/functions/builtin/image_transform_linearized.dml @@ -18,6 +18,29 @@ # under the License. # #------------------------------------------------------------- +# INPUT: +# -input: linearized picture with pixel_width*pixel_height columns +# and number of pictures as rows (one image per row) +# -width: pixel_width*pixel_height of the original images +# -height: number ob images +# -out_w: width of the output images +# -out_h: height of the output images +# -a: entry at [0,0] of the affine matrix +# -b: entry at [0,1] of the affine matrix +# -c: entry at [0,2] of the affine matrix +# -d: entry at [1,0] of the affine matrix +# -e: entry at [1,1] of the affine matrix +# -f: entry at [1,2] of the affine matrix +# -fill_value colorvalue for empty spaces +# after transformation +# -s_cols: original number of pixel width +# before linearization +# -s_rows: original number of pixel height +# before linearization +#------------------------------------------------------------- +# OUTPUT: +# -img_out: transformed image +#------------------------------------------------------------- input = read($in_file) width = ifdef($width, 512) @@ -34,13 +57,12 @@ fill_value = ifdef($fill_value, 0) s_cols = ifdef($s_cols, 512) s_rows = ifdef($s_rows, 512) +#method using dml scripts if(FALSE){ input = matrix(input, rows=height, cols=width) - print("Nrow input matrix: " + nrow(input) + "; Ncol input matrix: " + ncol(input)) transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) - #transformed = img_transform_linearized(input, out_w, out_h, a, b, c, d, e, f, fill_value, s_cols, s_rows) - #print("Nrow transformed matrix: " + nrow(transformed) + "; Ncol transformed matrix: " + ncol(transformed)) write(transformed, $out_file) +#method using builtin function }else{ affineMat = matrix(0,rows=3, cols=3) affineMat[1,1] = a @@ -58,18 +80,13 @@ if(FALSE){ [zMat, isFillable] = img_transform_matrix(affineMat, dimMat) - print("zMat dimensions, nrow: " + nrow(zMat) + "; ncol: " + ncol(zMat)) - if(as.logical(as.scalar(isFillable))){ ys=cbind(input, matrix(fill_value,nrow(input), 1)) - print("Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) }else{ ys = input - print("No Cbind ys nrow: " + nrow(ys) + "; ncol: " + ncol(ys)) } - output = ys%*%zMat + output = ys%*%zMat img_out = matrix(output, rows=nrow(input), cols=out_w*out_h) write(img_out, $out_file) - } diff --git a/src/test/scripts/functions/builtin/image_transform_matrix.dml b/src/test/scripts/functions/builtin/image_transform_matrix.dml index 0278bdc1e8e..c66463bcc2a 100644 --- a/src/test/scripts/functions/builtin/image_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/image_transform_matrix.dml @@ -21,38 +21,27 @@ #get the transformation matrix (3x3) as an input transformationMatrix = read($transMat) -#print(toString(transformationMatrix)) # get a matrix with the original image dimensions and the target dimensions (2x2) # -----original width----original height # -----target width------target height dimensionMatrix = read($dimMat) -# print(toString(dimensionMatrix)) - -#orig_w = as.scalar(dimensionMatrix[1,1]) -#orig_h = as.scalar(dimensionMatrix[1,2]) -#out_w = as.scalar(dimensionMatrix[2,1]) -#out_h = as.scalar(dimensionMatrix[2,2]) -#print("orig_w: " + orig_w + "; orig_h: " + orig_h + "; out_w: " + out_w + "; out_h: " + out_h) input = matrix(transformationMatrix, rows=3, cols=3) +# by setting the boolean the calculation of the transformation matrix can +# be done internally or via script builtin = TRUE builtin = as.logical(as.scalar(read($internal))) if(builtin){ - print("internal") + # internal execution [zMat, isFillable] = img_transform_matrix(input, dimensionMatrix) }else{ - print("script") + # execution via script [zMat, isFillable] = img_transform_test(input, dimensionMatrix) } # function for calculating the matrix which will be matrix multiplied with # the image for matrix multiplication isFillable = as.logical(as.scalar(isFillable)) - -#print(toString(zMat)) -print(nrow(zMat)) -print(ncol(zMat)) -print("IsFillable: " + toString(isFillable)) write(zMat, $out_file) diff --git a/scripts/builtin/img_transform_matrix.dml b/src/test/scripts/functions/builtin/img_transform_test.dml similarity index 58% rename from scripts/builtin/img_transform_matrix.dml rename to src/test/scripts/functions/builtin/img_transform_test.dml index 7d2b2458c60..bcccfd3a0e4 100644 --- a/scripts/builtin/img_transform_matrix.dml +++ b/src/test/scripts/functions/builtin/img_transform_test.dml @@ -18,7 +18,6 @@ # under the License. # #------------------------------------------------------------- - # Generates the z matrix for the new linearized image transformation function in order to apply # affine transformation to linearized images. # @@ -36,42 +35,36 @@ # zMat transformation matrix to be multiplied with for the linearized image # transformation function (arbitrary naming) # isFillable returns a boolean which indicates if cells need to be filled (with fillvalue), - in this case the image is extended, otherwise the original image is used +# in this case the image is extended, otherwise the original image is used # --------------------------------------------------------------------------------------- - m_img_transform_matrix = function(Matrix[Double] transMat, Integer orig_w, Integer orig_h, Integer out_w, Integer out_h) - return (Matrix[Double] zMat, Matrix[Double] isFillable){ - #orig_w = as.scalar(dimMat[1,2]) - #orig_h = as.scalar(dimMat[1,1]) - #out_w = as.scalar(dimMat[2,2]) - #out_h = as.scalar(dimMat[2,1]) - T_inv = inv(transMat) +m_img_transform_test = function(Matrix[Double] transMat, Matrix[Double] dimensionMatrix) + return (Matrix[Double] zMat, Matrix[Double] isFillable){ + T_inv = inv(transMat) - ## coordinates of output pixel-centers linearized in row-major order - coords = matrix(1, rows=3, cols=out_w*out_h) - coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) - coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) + orig_w = as.scalar(dimensionMatrix[1,1]) + orig_h = as.scalar(dimensionMatrix[1,2]) + out_w = as.scalar(dimensionMatrix[2,1]) + out_h = as.scalar(dimensionMatrix[2,2]) - # compute sampling pixel indices - coords = floor(T_inv %*% coords) + 1 + ## coordinates of output pixel-centers linearized in row-major order + coords = matrix(1, rows=3, cols=out_w*out_h) + coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) + coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) - inx = t(coords[1,]) - iny = t(coords[2,]) + # compute sampling pixel indices + coords = floor(T_inv %*% coords) + 1 - # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input - index_vector = (orig_w *(iny-1) + inx) * ((0