Baunsgaard commented on code in PR #1883: URL: https://github.com/apache/systemds/pull/1883#discussion_r1325671384
########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.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 BuiltinImageCropLinTest extends AutomatedTestBase +{ + private final static String TEST_NAME = "image_crop_linearized"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageCropTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + + private final static int s_cols = 27; + private final static int s_rows = 8; + private final static int rows = s_cols * s_rows; + private final static int cols = 13; + private final static double spSparse = 0.1; + private final static double spDense = 0.9; + private final static int x_offset = 1; + private final static int y_offset = 2; + private final static float size = 0.6f; + + + + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); + } + + @Test + public void testImageCropMatrixDenseCP() {runImageCropLinTest(false, ExecType.CP); Review Comment: fix formatting, here the internals of the method should be on the next line. ########## scripts/builtin/img_crop_linearized.dml: ########## @@ -0,0 +1,64 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +# The img_crop_linearized cuts out a rectangular section of multiple linearized images. +# +# INPUT: +# ---------------------------------------------------------------------------------------- +# img_in Linearized input images as 2D matrix +# w The width of the subregion required +# h The height of the subregion required +# x_offset The horizontal coordinate in the image to begin the crop operation +# y_offset The vertical coordinate in the image to begin the crop operation +# ---------------------------------------------------------------------------------------- +# +# OUTPUT: +# -------------------------------------------------------------------------------------------------- +# img_out Cropped images as linearized 2D matrix +# -------------------------------------------------------------------------------------------------- + +m_img_crop_linearized = function(Matrix[Double] img_in, Integer w, Integer h, Integer x_offset, Integer y_offset, Integer s_cols, Integer s_rows) return (Matrix[Double] img_out) { + + orig_w = s_cols + orig_h = s_rows + + nrows = nrow(img_in) # number of images + + start_h = (ceil((orig_h - h) / 2)) + y_offset + end_h = (start_h + h - 1) + start_w = (ceil((orig_w - w) / 2)) + x_offset + end_w = (start_w + w - 1) + + if((start_h < 0) | (end_h > orig_h) | (start_w < 0) | (end_w > orig_w)) { + print("Offset out of bounds! Returning input.") + img_out = img_in + } + else { + mask = matrix(0, rows=orig_h, cols=orig_w) + temp_mask = matrix(1, rows=h , cols=w ) + mask[start_h:end_h, start_w:end_w] = temp_mask + + linear_mask = matrix(mask, rows=1, cols=orig_w * orig_h) + + img_out = matrix(removeEmpty(target=(matrix(img_in+1, nrow(img_in), ncol(img_in))), margin="cols", select=linear_mask) - 1, nrows, w * h) Review Comment: I'm not sure, but i think using remove empty is the wrong choice here. I suggest you use slicing instead to slice out specific columns of pixels instead. But i could be wrong. ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.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 BuiltinImageCropLinTest extends AutomatedTestBase +{ + private final static String TEST_NAME = "image_crop_linearized"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageCropTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + + private final static int s_cols = 27; + private final static int s_rows = 8; + private final static int rows = s_cols * s_rows; + private final static int cols = 13; + private final static double spSparse = 0.1; + private final static double spDense = 0.9; + private final static int x_offset = 1; + private final static int y_offset = 2; + private final static float size = 0.6f; + + + + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); + } + + @Test + public void testImageCropMatrixDenseCP() {runImageCropLinTest(false, ExecType.CP); + } + + @Test + public void testImageCropMatrixSparseCP() {runImageCropLinTest(true, ExecType.CP); + } + + @Test + public void testImageCropMatrixDenseSP() {runImageCropLinTest(false, ExecType.SPARK); + } + + @Test + public void testImageCropMatrixSparseSP() {runImageCropLinTest(false,ExecType.SPARK); + } + + private void runImageCropLinTest (boolean sparse, ExecType instType) + { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try{ + loadTestConfiguration(getTestConfiguration(TEST_NAME)); + double sparsity = sparse ? spSparse : spDense; + + int new_w = (int) Math.floor(s_cols * size); + int new_h = (int) Math.floor(s_rows * size); + //int new_w = 20; + //int new_h = 5; + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + TEST_NAME + ".dml"; + programArgs = new String[]{"-nvargs", + "in_file=" + input("A"), "out_file=" + output("B"), + "x_offset=" + x_offset, "y_offset=" + y_offset, "cols=" + cols, "rows=" + rows, + "s_cols=" + s_cols, "s_rows=" + s_rows, "new_w=" + new_w, "new_h=" + new_h}; + + + fullRScriptName = HOME + TEST_NAME + ".R"; + rCmd = "Rscript" + " " + fullRScriptName + " " + inputDir() + " " + expectedDir() + + " " + size + " " + cols + " " + rows + " " + s_cols + " " + s_rows + " " + x_offset + " " + y_offset + + " " + new_w + " " + new_h; + + //generate actual dataset + double[][] A = getRandomMatrix(rows, cols, 0, 255, sparsity, 7); + writeInputMatrixWithMTD("A", A, true); + + runTest(true, false, null, -1); + runRScript(true); + + HashMap<MatrixValue.CellIndex, Double> dmlfile = readDMLMatrixFromOutputDir("B"); + HashMap<MatrixValue.CellIndex, Double> rfile = readRMatrixFromExpectedDir("B"); + TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); + + } + finally { + rtplatform = platformOld; + } + + + + } + +} Review Comment: new line in the end. ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.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 BuiltinImageCropLinTest extends AutomatedTestBase +{ + private final static String TEST_NAME = "image_crop_linearized"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageCropTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + + private final static int s_cols = 27; + private final static int s_rows = 8; + private final static int rows = s_cols * s_rows; + private final static int cols = 13; + private final static double spSparse = 0.1; + private final static double spDense = 0.9; Review Comment: here change the input cases to a parameterized list. line in #1903 ########## src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.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 BuiltinImageCropLinTest extends AutomatedTestBase +{ + private final static String TEST_NAME = "image_crop_linearized"; + private final static String TEST_DIR = "functions/builtin/"; + private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageCropTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + + private final static int s_cols = 27; + private final static int s_rows = 8; + private final static int rows = s_cols * s_rows; + private final static int cols = 13; + private final static double spSparse = 0.1; + private final static double spDense = 0.9; + private final static int x_offset = 1; + private final static int y_offset = 2; + private final static float size = 0.6f; + + + + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); + } + + @Test + public void testImageCropMatrixDenseCP() {runImageCropLinTest(false, ExecType.CP); + } + + @Test + public void testImageCropMatrixSparseCP() {runImageCropLinTest(true, ExecType.CP); + } + + @Test + public void testImageCropMatrixDenseSP() {runImageCropLinTest(false, ExecType.SPARK); + } + + @Test + public void testImageCropMatrixSparseSP() {runImageCropLinTest(false,ExecType.SPARK); + } + + private void runImageCropLinTest (boolean sparse, ExecType instType) + { + ExecMode platformOld = setExecMode(instType); + disableOutAndExpectedDeletion(); + + try{ + loadTestConfiguration(getTestConfiguration(TEST_NAME)); + double sparsity = sparse ? spSparse : spDense; + + int new_w = (int) Math.floor(s_cols * size); + int new_h = (int) Math.floor(s_rows * size); + //int new_w = 20; + //int new_h = 5; + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + TEST_NAME + ".dml"; + programArgs = new String[]{"-nvargs", + "in_file=" + input("A"), "out_file=" + output("B"), + "x_offset=" + x_offset, "y_offset=" + y_offset, "cols=" + cols, "rows=" + rows, + "s_cols=" + s_cols, "s_rows=" + s_rows, "new_w=" + new_w, "new_h=" + new_h}; + + + fullRScriptName = HOME + TEST_NAME + ".R"; + rCmd = "Rscript" + " " + fullRScriptName + " " + inputDir() + " " + expectedDir() + + " " + size + " " + cols + " " + rows + " " + s_cols + " " + s_rows + " " + x_offset + " " + y_offset + + " " + new_w + " " + new_h; Review Comment: I would prefer if we did not use R to verify instead we verify by knowing the output and calculating it right here. The crop should be simple to make with a few slice instructions in this code. -- 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]
