Baunsgaard commented on code in PR #1903: URL: https://github.com/apache/systemds/pull/1903#discussion_r1322014291
########## scripts/builtin/img_mirror_linearized.dml: ########## @@ -0,0 +1,82 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +# This function has the same functionality with img_mirror but it handles multiple images at +# the same time. Each row of the input and output matrix represents a linearized image/matrix +# It flips an image on the X (horizontal) or Y (vertical) axis. +# INPUT: +# ----------------------------------------------------------------------------------------- +# img_matrix Input matrix/image (every row represents a linearized matrix/image) +# horizontal_axis flip either in X or Y axis +# original_rows number of rows in the original 2-D images +# original_cols number of cols in the original 2-D images +# ----------------------------------------------------------------------------------------- +# +# OUTPUT: +# ----------------------------------------------------------------------------------------- +# R Output matrix/image (every row represents a linearized matrix/image) +# ----------------------------------------------------------------------------------------- + +m_img_mirror_linearized = function(matrix[double] img_matrix, Boolean horizontal_axis, +Integer original_rows, Integer original_cols) return (matrix[double] R) { + n = ncol(img_matrix); + R = matrix(0, rows=nrow(img_matrix), cols=n); + rows = original_rows; + cols = original_cols; + + if (horizontal_axis) { + for (i in seq(1, (rows %/% 2) * cols, cols)) { Review Comment: i would love to know if we can use a parfor here, ParFor simply parallelize the forloop, it does not work in some cases if the same variable is assigned to. but in the case of mirror i think it should. ########## scripts/builtin/img_mirror_linearized.dml: ########## @@ -0,0 +1,82 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +# This function has the same functionality with img_mirror but it handles multiple images at +# the same time. Each row of the input and output matrix represents a linearized image/matrix +# It flips an image on the X (horizontal) or Y (vertical) axis. +# INPUT: +# ----------------------------------------------------------------------------------------- +# img_matrix Input matrix/image (every row represents a linearized matrix/image) +# horizontal_axis flip either in X or Y axis +# original_rows number of rows in the original 2-D images +# original_cols number of cols in the original 2-D images +# ----------------------------------------------------------------------------------------- +# +# OUTPUT: +# ----------------------------------------------------------------------------------------- +# R Output matrix/image (every row represents a linearized matrix/image) +# ----------------------------------------------------------------------------------------- + +m_img_mirror_linearized = function(matrix[double] img_matrix, Boolean horizontal_axis, +Integer original_rows, Integer original_cols) return (matrix[double] R) { + n = ncol(img_matrix); + R = matrix(0, rows=nrow(img_matrix), cols=n); + rows = original_rows; + cols = original_cols; + + if (horizontal_axis) { + for (i in seq(1, (rows %/% 2) * cols, cols)) { + start = i; + end = i + cols - 1; + mirrorStart = (n - end) + 1; + mirrorEnd = (n - start) + 1; + R[, start:end] = img_matrix[, mirrorStart:mirrorEnd]; + R[, mirrorStart:mirrorEnd] = img_matrix[, start:end]; + } + if (rows %% 2 == 1) { + midStart = ((rows %/% 2)) * cols + 1; + midEnd = midStart + cols - 1; + R[, midStart:midEnd] = img_matrix[, midStart:midEnd]; + } + } + else { + for (i in 1:nrow(img_matrix)) { Review Comment: similarly parfor ########## src/test/scripts/functions/builtin/image_mirror_linearized.dml: ########## @@ -0,0 +1,19 @@ +input = read($in_file); + +n = nrow(input); +m = ncol(input); + +input_flattened_row = matrix(input, rows=1, cols=n*m); + + +img_out_flattened_x = img_mirror_linearized(input_flattened_row, TRUE, n, m); +img_out_flattened_y = img_mirror_linearized(input_flattened_row, FALSE, n, m); +img_out_flattened_reshape_xflip = matrix( img_out_flattened_x[1,], rows=n ,cols=m) +img_out_flattened_reshape_yflip = matrix( img_out_flattened_y[1,], rows=n ,cols=m) Review Comment: you could also just use the input, in this case ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java: ########## @@ -0,0 +1,81 @@ + +package org.apache.sysds.test.functions.builtin.part1; + +import org.junit.Test; +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.HashMap; + +public class BuiltinImageMirrorLinearizedTest extends AutomatedTestBase { + private final static String TEST_NAME_LINEARIZED = "image_mirror_linearized"; + private final static String TEST_NAME = "image_mirror"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageMirrorLinearizedTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + private final static int rows = 64; + private final static int cols = 64; + private final static double spSparse = 0.05; + private final static double spDense = 0.5; + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME_LINEARIZED, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[]{"B"})); + addTestConfiguration(TEST_NAME, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[]{"B"})); + } + + @Test + public void testImageMirrorLinearizedDenseCP() { Review Comment: add an SP and Hybrid case ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java: ########## @@ -0,0 +1,81 @@ + +package org.apache.sysds.test.functions.builtin.part1; + +import org.junit.Test; +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.HashMap; + +public class BuiltinImageMirrorLinearizedTest extends AutomatedTestBase { + private final static String TEST_NAME_LINEARIZED = "image_mirror_linearized"; + private final static String TEST_NAME = "image_mirror"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageMirrorLinearizedTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + private final static int rows = 64; + private final static int cols = 64; + private final static double spSparse = 0.05; + private final static double spDense = 0.5; + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME_LINEARIZED, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[]{"B"})); + addTestConfiguration(TEST_NAME, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[]{"B"})); + } + + @Test + public void testImageMirrorLinearizedDenseCP() { + runImageMirrorLinearizedTest(false, ExecType.CP); + } + + private void runImageMirrorLinearizedTest(boolean sparse, ExecType instType) { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try { + loadTestConfiguration(getTestConfiguration(TEST_NAME_LINEARIZED)); + loadTestConfiguration(getTestConfiguration(TEST_NAME)); + + double sparsity = sparse ? spSparse : spDense; + String HOME = SCRIPT_DIR + TEST_DIR; + + // For image_mirror_linearized + fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; + programArgs = new String[]{"-nvargs", + "in_file=" + input("A"), "x_out_reshape_file=" + output("B_x_reshape"), "y_out_reshape_file=" + output("B_y_reshape") + }; + double[][] A = getRandomMatrix(rows, cols, 0, 255, sparsity, 7); + writeInputMatrixWithMTD("A", A, true); + + runTest(true, false, null, -1); + + HashMap<MatrixValue.CellIndex, Double> dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x_reshape"); + HashMap<MatrixValue.CellIndex, Double> dmlfileLinearizedY = readDMLMatrixFromOutputDir("B_y_reshape"); + + // For image_mirror + fullDMLScriptName = HOME + TEST_NAME + ".dml"; + programArgs = new String[]{"-nvargs", + "in_file=" + input("A"), "x_out_file=" + output("B_x"), "y_out_file=" + output("B_y") Review Comment: maybe give multiple random matrices input, as different images. ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java: ########## @@ -0,0 +1,81 @@ + +package org.apache.sysds.test.functions.builtin.part1; + +import org.junit.Test; +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.HashMap; + +public class BuiltinImageMirrorLinearizedTest extends AutomatedTestBase { + private final static String TEST_NAME_LINEARIZED = "image_mirror_linearized"; + private final static String TEST_NAME = "image_mirror"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageMirrorLinearizedTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + private final static int rows = 64; + private final static int cols = 64; Review Comment: This is very small, and same number of rows and cols, please add tests cases for uneven images. ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java: ########## @@ -0,0 +1,81 @@ + Review Comment: license ########## src/test/scripts/functions/builtin/image_mirror_linearized.dml: ########## @@ -0,0 +1,19 @@ +input = read($in_file); + +n = nrow(input); +m = ncol(input); + +input_flattened_row = matrix(input, rows=1, cols=n*m); + + +img_out_flattened_x = img_mirror_linearized(input_flattened_row, TRUE, n, m); +img_out_flattened_y = img_mirror_linearized(input_flattened_row, FALSE, n, m); +img_out_flattened_reshape_xflip = matrix( img_out_flattened_x[1,], rows=n ,cols=m) +img_out_flattened_reshape_yflip = matrix( img_out_flattened_y[1,], rows=n ,cols=m) + +write(img_out_flattened_reshape_xflip , $x_out_reshape_file); +write(img_out_flattened_reshape_yflip ,$y_out_reshape_file); + + + + Review Comment: Github is complaining about the end of this file.please fix it to only have 1 extra empty line in the end of the file. ########## src/test/scripts/functions/builtin/image_mirror_linearized.dml: ########## @@ -0,0 +1,19 @@ +input = read($in_file); + +n = nrow(input); +m = ncol(input); + +input_flattened_row = matrix(input, rows=1, cols=n*m); + + +img_out_flattened_x = img_mirror_linearized(input_flattened_row, TRUE, n, m); +img_out_flattened_y = img_mirror_linearized(input_flattened_row, FALSE, n, m); +img_out_flattened_reshape_xflip = matrix( img_out_flattened_x[1,], rows=n ,cols=m) +img_out_flattened_reshape_yflip = matrix( img_out_flattened_y[1,], rows=n ,cols=m) + +write(img_out_flattened_reshape_xflip , $x_out_reshape_file); +write(img_out_flattened_reshape_yflip ,$y_out_reshape_file); Review Comment: please fix formatting, to be consistent with spaces. ########## src/test/scripts/functions/builtin/image_mirror_linearized.dml: ########## @@ -0,0 +1,19 @@ +input = read($in_file); + +n = nrow(input); +m = ncol(input); + +input_flattened_row = matrix(input, rows=1, cols=n*m); Review Comment: i would like to see a test that use multiple images, not just one. ########## src/test/scripts/functions/builtin/image_mirror_linearized.dml: ########## @@ -0,0 +1,19 @@ +input = read($in_file); Review Comment: license -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
