From 10eaa3b1e051d68846615c04c1579489e021f8c8 Mon Sep 17 00:00:00 2001 From: Selin Kahvecioglu Date: Thu, 17 Aug 2023 20:06:50 +0200 Subject: [PATCH 1/6] crop_linearized.dml draft --- scripts/builtin/img_crop_linearized.dml | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 scripts/builtin/img_crop_linearized.dml diff --git a/scripts/builtin/img_crop_linearized.dml b/scripts/builtin/img_crop_linearized.dml new file mode 100644 index 00000000000..1b864a37f50 --- /dev/null +++ b/scripts/builtin/img_crop_linearized.dml @@ -0,0 +1,69 @@ +#------------------------------------------------------------- +# +# 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-function is an image data augmentation function. It cuts out a subregion of an image. +# +# INPUT: +# ---------------------------------------------------------------------------------------- +# img_in Input images as linearized 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_rows, Integer s_cols) return (Matrix[Double] img_out) { + + orig_w = s_cols + orig_h = s_rows + + 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) + + # Repeat the linear mask for each image (row) + full_mask = matrix(linear_mask, rows=n_images, cols=orig_w * orig_h) + + # Apply mask by element-wise multiplication + masked_img = (img_in + 1) * full_mask + + cropped_img = removeEmpty(target=masked_img, margin="cols", select=matrix(1, rows=1, cols=orig_w * orig_h)) + + img_out = matrix(cropped_img - 1, n_images, w * h) + } +} \ No newline at end of file From 0fe69a7a3fcf4e2ff64bc0137e8cd68792cae1d1 Mon Sep 17 00:00:00 2001 From: slnkahveci <76944633+slnkahveci@users.noreply.github.com> Date: Sat, 2 Sep 2023 17:02:17 +0000 Subject: [PATCH 2/6] croplin row-col issue --- scripts/builtin/img_crop_linearized.dml | 21 ++- .../org/apache/sysds/common/Builtins.java | 1 + .../part1/BuiltinImageCropLinTest.java | 120 ++++++++++++++++++ .../functions/builtin/image_crop_linearized.R | 77 +++++++++++ .../builtin/image_crop_linearized.dml | 38 ++++++ 5 files changed, 244 insertions(+), 13 deletions(-) create mode 100644 src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java create mode 100644 src/test/scripts/functions/builtin/image_crop_linearized.R create mode 100644 src/test/scripts/functions/builtin/image_crop_linearized.dml diff --git a/scripts/builtin/img_crop_linearized.dml b/scripts/builtin/img_crop_linearized.dml index 1b864a37f50..a056e1d651b 100644 --- a/scripts/builtin/img_crop_linearized.dml +++ b/scripts/builtin/img_crop_linearized.dml @@ -19,11 +19,11 @@ # #------------------------------------------------------------- -# The img_crop-function is an image data augmentation function. It cuts out a subregion of an image. +# The img_crop_linearized cuts out a rectangular section of multiple linearized images. # # INPUT: # ---------------------------------------------------------------------------------------- -# img_in Input images as linearized 2D matrix +# 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 @@ -35,11 +35,13 @@ # 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_rows, Integer s_cols) return (Matrix[Double] img_out) { +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 @@ -55,15 +57,8 @@ m_img_crop_linearized = function(Matrix[Double] img_in, Integer w, Integer h, In mask[start_h:end_h, start_w:end_w] = temp_mask linear_mask = matrix(mask, rows=1, cols=orig_w * orig_h) - - # Repeat the linear mask for each image (row) - full_mask = matrix(linear_mask, rows=n_images, cols=orig_w * orig_h) - # Apply mask by element-wise multiplication - masked_img = (img_in + 1) * full_mask - - cropped_img = removeEmpty(target=masked_img, margin="cols", select=matrix(1, rows=1, cols=orig_w * orig_h)) - - img_out = matrix(cropped_img - 1, n_images, w * 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) } -} \ No newline at end of file +} + diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 883e57aa22f..052481a97e0 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -157,6 +157,7 @@ public enum Builtins { IMG_MIRROR("img_mirror", true), IMG_BRIGHTNESS("img_brightness", true), IMG_CROP("img_crop", true), + IMG_CROP_LINEARIZED("img_crop_linearized", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSLATE("img_translate", true), IMG_ROTATE("img_rotate", true), diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java new file mode 100644 index 00000000000..c181dfb057d --- /dev/null +++ b/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 dmlfile = readDMLMatrixFromOutputDir("B"); + HashMap rfile = readRMatrixFromExpectedDir("B"); + TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); + + } + finally { + rtplatform = platformOld; + } + + + + } + +} \ No newline at end of file diff --git a/src/test/scripts/functions/builtin/image_crop_linearized.R b/src/test/scripts/functions/builtin/image_crop_linearized.R new file mode 100644 index 00000000000..11da023d08f --- /dev/null +++ b/src/test/scripts/functions/builtin/image_crop_linearized.R @@ -0,0 +1,77 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- +args = commandArgs(TRUE) +options(digits=22) +library("Matrix") + +# wrapper for image_crop that takes in 2d matrix of linearized images and returns a 2d matrix of linearized cropped images +image_crop = function(img_in, w, h, x_offset, y_offset) { + orig_w = ncol(img_in) + orig_h = nrow(img_in) + + start_h = (ceiling((orig_h - h) / 2)) + y_offset + end_h = (start_h + h - 1) + start_w = (ceiling((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, orig_h, orig_w) + temp_mask = matrix(1, h , w ) + mask[start_h:end_h, start_w:end_w] = temp_mask + mask = matrix(mask, 1, orig_w * orig_h) + img_out = input[start_h:end_h, start_w:end_w] + } + + return(img_out) +} + +image_crop_linearized = function(img_in, new_w, new_h , s_cols, s_rows, x_offset, y_offset) { + n_imgs = nrow(img_in) + + img_out = matrix(0, n_imgs, new_w * new_h) + + + for (i in 1:n_imgs) { + row_in = matrix(img_in[i], s_rows, s_cols) + + cropped_img = image_crop(row_in, new_w, new_h, x_offset, y_offset) + #cropped_img = image_crop(img_in, w, h, x_offset, y_offset) + + img_out[i] = matrix(cropped_img, 1, new_w * new_h) + } + + return(img_out) +} + + +input = as.matrix(readMM(paste(args[1], "A.mtx", sep=""))) + +input = matrix(input, as.integer(args[4]), as.integer(args[5])) +new_w = as.integer(args[10]) +new_h = as.integer(args[11]) +print(paste("R New w/h=",new_w,"/",new_h)) +crop2 = image_crop_linearized(input, new_w, new_h, as.integer(args[6]), as.integer(args[7]), as.integer(args[8]), as.integer(args[9])); +writeMM(as(crop2, "CsparseMatrix"), paste(args[2], "B", sep="")) diff --git a/src/test/scripts/functions/builtin/image_crop_linearized.dml b/src/test/scripts/functions/builtin/image_crop_linearized.dml new file mode 100644 index 00000000000..435870ba7a7 --- /dev/null +++ b/src/test/scripts/functions/builtin/image_crop_linearized.dml @@ -0,0 +1,38 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +input = read($in_file); + +x_offset = ifdef($x_offset, 0) +y_offset = ifdef($y_offset, 0) +cols = ifdef($cols, 512) +rows = ifdef($rows, 512) +s_cols = ifdef($s_cols, 512) +s_rows = ifdef($s_rows, 512) +new_w = ifdef($new_w, 512) +new_h = ifdef($new_h, 512) + +input = matrix(input, rows=rows, cols=cols) + +print("DML New w/h=" + new_w + "/" + new_h) + +crop2 = img_crop_linearized(input, new_w, new_h, x_offset, y_offset, s_cols, s_rows) +write(crop2, $out_file); From cb941a3fe309f1fd1a9d27838259828db8517868 Mon Sep 17 00:00:00 2001 From: slnkahveci <76944633+slnkahveci@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:51:42 +0000 Subject: [PATCH 3/6] removed removeEmpty, added parameterized tests --- scripts/builtin/img_crop_linearized.dml | 34 ++-- .../part1/BuiltinImageCropLinTest.java | 150 +++++++++++------- .../functions/builtin/image_crop_linearized.R | 77 --------- .../builtin/image_crop_linearized.dml | 2 - 4 files changed, 117 insertions(+), 146 deletions(-) delete mode 100644 src/test/scripts/functions/builtin/image_crop_linearized.R diff --git a/scripts/builtin/img_crop_linearized.dml b/scripts/builtin/img_crop_linearized.dml index a056e1d651b..e6848775f8a 100644 --- a/scripts/builtin/img_crop_linearized.dml +++ b/scripts/builtin/img_crop_linearized.dml @@ -23,19 +23,22 @@ # # 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 +# 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 offset for the center of the crop region +# y_offset The vertical offset for the center of the crop region +# s_cols Width of a single image +# s_rows Height of a single image # ---------------------------------------------------------------------------------------- # # OUTPUT: # -------------------------------------------------------------------------------------------------- -# img_out Cropped images as linearized 2D matrix +# 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) { +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 @@ -52,13 +55,18 @@ m_img_crop_linearized = function(Matrix[Double] img_in, Integer w, Integer h, In 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) + img_out = matrix(0, rows=nrows , cols= w*h ) + + parfor (i in start_h: end_h, check=0){ + start_idx = (i-1) * s_cols + start_w + end_idx = (i-1) * s_cols + end_w + + start_idw = (i-start_h) * w +1 + end_idw = (i-start_h) * w + w + + img_out[, start_idw:end_idw] = matrix(img_in[, start_idx:end_idx], rows=nrows, cols=w) + } } } diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java index c181dfb057d..aca709eb75d 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.sysds.test.functions.builtin.part1; import org.junit.Test; @@ -25,96 +26,137 @@ import org.apache.sysds.test.AutomatedTestBase; import org.apache.sysds.test.TestConfiguration; import org.apache.sysds.test.TestUtils; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import java.util.HashMap; +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +@net.jcip.annotations.NotThreadSafe + +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 + BuiltinImageCropLinTest.class.getSimpleName() + "/"; + + private final static double eps = 1e-10; + private final static double spSparse = 0.1; + private final static double spDense = 0.9; + + @Parameterized.Parameter(0) + public int s_cols; + @Parameterized.Parameter(1) + public int s_rows; + @Parameterized.Parameter(2) + public int rows; + @Parameterized.Parameter(3) + public int x_offset; + @Parameterized.Parameter(4) + public int y_offset; + @Parameterized.Parameter(5) + public double size; + + public int cols; + public int new_w; + public int new_h; + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] { + { 10, 12, 20, 2, 3, 0.5 }, + { 12, 12, 40, 5, 5, 0.4 }, + { 32, 32, 200, 13, 10, 0.2 }, + { 31, 33, 200, 7, 10, 0.2 }, + { 64, 64, 50, 2, 0, 0.8 }, + { 125, 123, 32, 7, 37, 0.3 }, + { 128, 128, 83, 23, 14, 0.123 }, + { 256, 50, 2, 0, 0, 0.8 } + }); + } -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 + @Override public void setUp() { - addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); + cols = s_cols * s_rows; + new_w = (int) Math.floor(s_cols * size); + new_h = (int) Math.floor(s_rows * size); + addTestConfiguration(TEST_NAME, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] { "B" })); } @Test - public void testImageCropMatrixDenseCP() {runImageCropLinTest(false, ExecType.CP); + public void testImageCropMatrixDenseCP() { + runImageCropLinTest(false, ExecType.CP); } @Test - public void testImageCropMatrixSparseCP() {runImageCropLinTest(true, ExecType.CP); + public void testImageCropMatrixSparseCP() { + runImageCropLinTest(true, ExecType.CP); } @Test - public void testImageCropMatrixDenseSP() {runImageCropLinTest(false, ExecType.SPARK); + public void testImageCropMatrixDenseSP() { + runImageCropLinTest(false, ExecType.SPARK); } @Test - public void testImageCropMatrixSparseSP() {runImageCropLinTest(false,ExecType.SPARK); + public void testImageCropMatrixSparseSP() { + runImageCropLinTest(false, ExecType.SPARK); } - private void runImageCropLinTest (boolean sparse, ExecType instType) - { + private void runImageCropLinTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); - try{ + 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 + 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 }; + // print the command + System.out.println("COMMAND:" + fullDMLScriptName + " " + String.join(" ", programArgs)); + + // generate actual dataset double[][] A = getRandomMatrix(rows, cols, 0, 255, sparsity, 7); writeInputMatrixWithMTD("A", A, true); + // crop functionality in java + double[][] ref = new double[rows][new_h * new_w]; + int start_h = (int) Math.ceil((double) (s_rows - new_h) / 2) + y_offset; + int start_w = (int) Math.ceil((double) (s_cols - new_w) / 2) + x_offset; + if(s_cols == 64){ + System.out.println("start_h: " + start_h + ", start_w: " + start_w); + } + + for (int i = 0; i < rows; i++) { + if(start_w == 0 && start_h == 0){ + ref[i]=A[i]; + }else{ + for (int j = 0; j < new_h * new_w; j++) { + int ja = ((j / new_w) + start_h - 1) * s_cols + (j % new_w) + start_w - 1; + ref[i][j] = A[i][ja]; + } + } + } + + writeInputMatrixWithMTD("ref", ref, true); + runTest(true, false, null, -1); - runRScript(true); HashMap dmlfile = readDMLMatrixFromOutputDir("B"); - HashMap rfile = readRMatrixFromExpectedDir("B"); - TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); + double[][] dml_res = TestUtils.convertHashMapToDoubleArray(dmlfile, rows, + (new_h * new_w)); + TestUtils.compareMatrices(ref, dml_res, eps, "Java vs. DML"); - } - finally { + } finally { rtplatform = platformOld; } - - } } \ No newline at end of file diff --git a/src/test/scripts/functions/builtin/image_crop_linearized.R b/src/test/scripts/functions/builtin/image_crop_linearized.R deleted file mode 100644 index 11da023d08f..00000000000 --- a/src/test/scripts/functions/builtin/image_crop_linearized.R +++ /dev/null @@ -1,77 +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. -# -#------------------------------------------------------------- -args = commandArgs(TRUE) -options(digits=22) -library("Matrix") - -# wrapper for image_crop that takes in 2d matrix of linearized images and returns a 2d matrix of linearized cropped images -image_crop = function(img_in, w, h, x_offset, y_offset) { - orig_w = ncol(img_in) - orig_h = nrow(img_in) - - start_h = (ceiling((orig_h - h) / 2)) + y_offset - end_h = (start_h + h - 1) - start_w = (ceiling((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, orig_h, orig_w) - temp_mask = matrix(1, h , w ) - mask[start_h:end_h, start_w:end_w] = temp_mask - mask = matrix(mask, 1, orig_w * orig_h) - img_out = input[start_h:end_h, start_w:end_w] - } - - return(img_out) -} - -image_crop_linearized = function(img_in, new_w, new_h , s_cols, s_rows, x_offset, y_offset) { - n_imgs = nrow(img_in) - - img_out = matrix(0, n_imgs, new_w * new_h) - - - for (i in 1:n_imgs) { - row_in = matrix(img_in[i], s_rows, s_cols) - - cropped_img = image_crop(row_in, new_w, new_h, x_offset, y_offset) - #cropped_img = image_crop(img_in, w, h, x_offset, y_offset) - - img_out[i] = matrix(cropped_img, 1, new_w * new_h) - } - - return(img_out) -} - - -input = as.matrix(readMM(paste(args[1], "A.mtx", sep=""))) - -input = matrix(input, as.integer(args[4]), as.integer(args[5])) -new_w = as.integer(args[10]) -new_h = as.integer(args[11]) -print(paste("R New w/h=",new_w,"/",new_h)) -crop2 = image_crop_linearized(input, new_w, new_h, as.integer(args[6]), as.integer(args[7]), as.integer(args[8]), as.integer(args[9])); -writeMM(as(crop2, "CsparseMatrix"), paste(args[2], "B", sep="")) diff --git a/src/test/scripts/functions/builtin/image_crop_linearized.dml b/src/test/scripts/functions/builtin/image_crop_linearized.dml index 435870ba7a7..6cfe7b3ba82 100644 --- a/src/test/scripts/functions/builtin/image_crop_linearized.dml +++ b/src/test/scripts/functions/builtin/image_crop_linearized.dml @@ -32,7 +32,5 @@ new_h = ifdef($new_h, 512) input = matrix(input, rows=rows, cols=cols) -print("DML New w/h=" + new_w + "/" + new_h) - crop2 = img_crop_linearized(input, new_w, new_h, x_offset, y_offset, s_cols, s_rows) write(crop2, $out_file); From 19c7de96cad4a5d6c8df4c1c1b41ea610978c68b Mon Sep 17 00:00:00 2001 From: slnkahveci <76944633+slnkahveci@users.noreply.github.com> Date: Wed, 20 Sep 2023 10:14:34 +0000 Subject: [PATCH 4/6] back to removeempty --- scripts/builtin/img_crop_linearized.dml | 19 +++++++------------ .../part1/BuiltinImageCropLinTest.java | 15 +++++++-------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/scripts/builtin/img_crop_linearized.dml b/scripts/builtin/img_crop_linearized.dml index e6848775f8a..378025fe240 100644 --- a/scripts/builtin/img_crop_linearized.dml +++ b/scripts/builtin/img_crop_linearized.dml @@ -26,8 +26,8 @@ # 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 offset for the center of the crop region -# y_offset The vertical offset for the center of the crop region +# 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 # s_cols Width of a single image # s_rows Height of a single image # ---------------------------------------------------------------------------------------- @@ -55,18 +55,13 @@ m_img_crop_linearized = function(Matrix[Double] img_in, Integer w, Integer h, In 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 - img_out = matrix(0, rows=nrows , cols= w*h ) + linear_mask = matrix(mask, rows=1, cols=orig_w * orig_h) - parfor (i in start_h: end_h, check=0){ - start_idx = (i-1) * s_cols + start_w - end_idx = (i-1) * s_cols + end_w - - start_idw = (i-start_h) * w +1 - end_idw = (i-start_h) * w + w - - img_out[, start_idw:end_idw] = matrix(img_in[, start_idx:end_idx], rows=nrows, cols=w) - } + img_out = matrix(removeEmpty(target=(matrix(img_in+1, nrow(img_in), ncol(img_in))), margin="cols", select=linear_mask) - 1, nrows, w * h) } } diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java index aca709eb75d..f650ca71956 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java @@ -68,11 +68,12 @@ public static Collection data() { { 10, 12, 20, 2, 3, 0.5 }, { 12, 12, 40, 5, 5, 0.4 }, { 32, 32, 200, 13, 10, 0.2 }, - { 31, 33, 200, 7, 10, 0.2 }, + { 31, 33, 200, 7, 10, 0.2 }, { 64, 64, 50, 2, 0, 0.8 }, { 125, 123, 32, 7, 37, 0.3 }, { 128, 128, 83, 23, 14, 0.123 }, - { 256, 50, 2, 0, 0, 0.8 } + { 256, 50, 2, 0, 0, 0.8 }, + { 256, 255, 2, 0, 0, 0.8 } }); } @@ -118,8 +119,6 @@ private void runImageCropLinTest(boolean sparse, ExecType instType) { "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 }; - // print the command - System.out.println("COMMAND:" + fullDMLScriptName + " " + String.join(" ", programArgs)); // generate actual dataset double[][] A = getRandomMatrix(rows, cols, 0, 255, sparsity, 7); @@ -129,14 +128,14 @@ private void runImageCropLinTest(boolean sparse, ExecType instType) { double[][] ref = new double[rows][new_h * new_w]; int start_h = (int) Math.ceil((double) (s_rows - new_h) / 2) + y_offset; int start_w = (int) Math.ceil((double) (s_cols - new_w) / 2) + x_offset; - if(s_cols == 64){ + if (s_cols == 64) { System.out.println("start_h: " + start_h + ", start_w: " + start_w); } for (int i = 0; i < rows; i++) { - if(start_w == 0 && start_h == 0){ - ref[i]=A[i]; - }else{ + if (start_w == 0 && start_h == 0) { + ref[i] = A[i]; + } else { for (int j = 0; j < new_h * new_w; j++) { int ja = ((j / new_w) + start_h - 1) * s_cols + (j % new_w) + start_w - 1; ref[i][j] = A[i][ja]; From 3ed79d0fef5b11c188be28c658e831be6020111c Mon Sep 17 00:00:00 2001 From: slnkahveci <76944633+slnkahveci@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:20:29 +0200 Subject: [PATCH 5/6] offset description --- scripts/builtin/img_crop_linearized.dml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/builtin/img_crop_linearized.dml b/scripts/builtin/img_crop_linearized.dml index 378025fe240..b2c2c03fd18 100644 --- a/scripts/builtin/img_crop_linearized.dml +++ b/scripts/builtin/img_crop_linearized.dml @@ -26,8 +26,8 @@ # 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 +# x_offset The horizontal offset for the center of the crop region +# y_offset The vertical offset for the center of the crop region # s_cols Width of a single image # s_rows Height of a single image # ---------------------------------------------------------------------------------------- From 70cabbd390dd33e14e7900de51eac136d0475346 Mon Sep 17 00:00:00 2001 From: slnkahveci <76944633+slnkahveci@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:08:36 +0200 Subject: [PATCH 6/6] Update BuiltinImageCropLinTest.java --- .../builtin/part1/BuiltinImageCropLinTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java index f650ca71956..4f6e8c20e66 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageCropLinTest.java @@ -73,7 +73,8 @@ public static Collection data() { { 125, 123, 32, 7, 37, 0.3 }, { 128, 128, 83, 23, 14, 0.123 }, { 256, 50, 2, 0, 0, 0.8 }, - { 256, 255, 2, 0, 0, 0.8 } + { 256, 255, 2, 0, 0, 0.8 }, + { 512, 300, 47, 6, 7, 0.7 } }); } @@ -102,7 +103,7 @@ public void testImageCropMatrixDenseSP() { @Test public void testImageCropMatrixSparseSP() { - runImageCropLinTest(false, ExecType.SPARK); + runImageCropLinTest(true, ExecType.SPARK); } private void runImageCropLinTest(boolean sparse, ExecType instType) { @@ -128,9 +129,6 @@ private void runImageCropLinTest(boolean sparse, ExecType instType) { double[][] ref = new double[rows][new_h * new_w]; int start_h = (int) Math.ceil((double) (s_rows - new_h) / 2) + y_offset; int start_w = (int) Math.ceil((double) (s_cols - new_w) / 2) + x_offset; - if (s_cols == 64) { - System.out.println("start_h: " + start_h + ", start_w: " + start_w); - } for (int i = 0; i < rows; i++) { if (start_w == 0 && start_h == 0) { @@ -158,4 +156,4 @@ private void runImageCropLinTest(boolean sparse, ExecType instType) { } -} \ No newline at end of file +}