From dc2543e0092f2b4acb032f151fd3faba5c8ad3b5 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Fri, 8 Sep 2023 12:53:15 +0200 Subject: [PATCH 01/16] Added img_mirror_linearized built-in function --- scripts/builtin/img_mirror_linearized.dml | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts/builtin/img_mirror_linearized.dml diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml new file mode 100644 index 00000000000..318ad406734 --- /dev/null +++ b/scripts/builtin/img_mirror_linearized.dml @@ -0,0 +1,46 @@ +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); #initialize the output_matrix + 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)) { + offset = 1; + while(offset <= n) { + # Get the sub-matrix + end = min(n, offset + cols - 1); + # Reverse the sub-matrix columns + reversed_sub_matrix = matrix(0, rows=1, cols=cols); + idx = 1; + for (j in offset:end) { + reversed_sub_matrix[1, cols - idx + 1] = img_matrix[i, j]; + idx = idx + 1; + } + R[i, offset:end] = reversed_sub_matrix; + offset = end + 1; + } + } +} +} + + From 648f4a56bef5d2d4297d8bd7094d1a938636232a Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Fri, 8 Sep 2023 13:11:30 +0200 Subject: [PATCH 02/16] updated img_mirror_linearized --- scripts/builtin/img_mirror_linearized.dml | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml index 318ad406734..d43e4996ef6 100644 --- a/scripts/builtin/img_mirror_linearized.dml +++ b/scripts/builtin/img_mirror_linearized.dml @@ -1,3 +1,40 @@ +#------------------------------------------------------------- +# +# 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: +# ---------------------------------------------------------------------------------------------------------------------- +# img_out 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); #initialize the output_matrix From a74eae90d2f4534e647254ca1899f696bba6f486 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 21:13:28 +0200 Subject: [PATCH 03/16] Add image_mirror_linearized.dml for testing --- .../builtin/image_mirror_linearized.dml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/scripts/functions/builtin/image_mirror_linearized.dml diff --git a/src/test/scripts/functions/builtin/image_mirror_linearized.dml b/src/test/scripts/functions/builtin/image_mirror_linearized.dml new file mode 100644 index 00000000000..58c940df100 --- /dev/null +++ b/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); + + + + \ No newline at end of file From 3b6c2fe2cecabfb1a62b084860107516bacf516f Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 21:17:43 +0200 Subject: [PATCH 04/16] Added BuiltinImageMirrorLinearizedTest.java --- .../BuiltinImageMirrorLinearizedTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java new file mode 100644 index 00000000000..e810a6b7892 --- /dev/null +++ b/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 dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x_reshape"); + HashMap 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") + }; + + runTest(true, false, null, -1); + + HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x"); + HashMap dmlfileY = readDMLMatrixFromOutputDir("B_y"); + + // Compare matrices + TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); + TestUtils.compareMatrices(dmlfileLinearizedY, dmlfileY, eps, "Stat-DML-LinearizedY", "Stat-DML-Y"); + + + } finally { + rtplatform = platformOld; + } + } +} From c6aafefe201c216295f7a5614070961a06cd2035 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 21:25:51 +0200 Subject: [PATCH 05/16] Added img_mirror_linearized to Builtins.java --- src/main/java/org/apache/sysds/common/Builtins.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 2243eeb963a..5541c53bc55 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -155,6 +155,7 @@ public enum Builtins { HYPERBAND("hyperband", true), IFELSE("ifelse", false), IMG_MIRROR("img_mirror", true), + IMG_MIRROR_LINEARIZED("img_mirror_linearized", true), IMG_BRIGHTNESS("img_brightness", true), IMG_CROP("img_crop", true), IMG_TRANSFORM("img_transform", true), From 08958a7c62a5b304def486ffb5ab58d914370ab1 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 21:42:17 +0200 Subject: [PATCH 06/16] Resolved comments and issues --- scripts/builtin/img_mirror_linearized.dml | 54 +++++++++++------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml index d43e4996ef6..e46de84a01b 100644 --- a/scripts/builtin/img_mirror_linearized.dml +++ b/scripts/builtin/img_mirror_linearized.dml @@ -31,40 +31,36 @@ # ----------------------------------------------------------------------------------------- # # OUTPUT: -# ---------------------------------------------------------------------------------------------------------------------- -# img_out Output matrix/image (every row represents a linearized matrix/image) -# ---------------------------------------------------------------------------------------------------------------------- +# ----------------------------------------------------------------------------------------- +# 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); #initialize the output_matrix - rows =original_rows - cols= original_cols +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; + 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)) { + for (i in 1:nrow(img_matrix)) { offset = 1; - while(offset <= n) { - # Get the sub-matrix + while (offset <= n) { + # Get the sub-matrix end = min(n, offset + cols - 1); # Reverse the sub-matrix columns reversed_sub_matrix = matrix(0, rows=1, cols=cols); @@ -75,9 +71,11 @@ m_img_mirror_linearized = function(matrix[double] img_matrix, Boolean horizontal } R[i, offset:end] = reversed_sub_matrix; offset = end + 1; - } + } + } } } -} + + From 2ce5e9af146709b9398b6a8b9d00b317bb8feef8 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 21:53:42 +0200 Subject: [PATCH 07/16] line formatting corrected --- scripts/builtin/img_mirror_linearized.dml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml index e46de84a01b..626684937e0 100644 --- a/scripts/builtin/img_mirror_linearized.dml +++ b/scripts/builtin/img_mirror_linearized.dml @@ -35,7 +35,8 @@ # 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) { +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; From b8d53795ecee9ad3b61b31cc2c0b65208068ea01 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Mon, 11 Sep 2023 22:08:42 +0200 Subject: [PATCH 08/16] license header added --- .../BuiltinImageMirrorLinearizedTest.java | 19 ++++++++++++++- .../builtin/image_mirror_linearized.dml | 23 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java index e810a6b7892..2b1bbc0839b 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java @@ -1,4 +1,21 @@ - +/* + * 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; diff --git a/src/test/scripts/functions/builtin/image_mirror_linearized.dml b/src/test/scripts/functions/builtin/image_mirror_linearized.dml index 58c940df100..38308a4f28a 100644 --- a/src/test/scripts/functions/builtin/image_mirror_linearized.dml +++ b/src/test/scripts/functions/builtin/image_mirror_linearized.dml @@ -1,5 +1,26 @@ -input = read($in_file); +#------------------------------------------------------------- +# +# 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); n = nrow(input); m = ncol(input); From 53ccb9a4a7904b6e29356f732afff051c035b6f3 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Tue, 12 Sep 2023 17:08:23 +0200 Subject: [PATCH 09/16] add translate --- scripts/builtin/deneme.dml | 9 +++ scripts/builtin/img_translate_linearized.dml | 73 +++++++++++++++++++ .../org/apache/sysds/common/Builtins.java | 1 + 3 files changed, 83 insertions(+) create mode 100644 scripts/builtin/deneme.dml create mode 100644 scripts/builtin/img_translate_linearized.dml diff --git a/scripts/builtin/deneme.dml b/scripts/builtin/deneme.dml new file mode 100644 index 00000000000..a0c987d10ea --- /dev/null +++ b/scripts/builtin/deneme.dml @@ -0,0 +1,9 @@ + +M = matrix("1 2 3 4 5 6 7 8 9", rows=1, cols=9) + + + +A_linearized = img_translate_linearized(M, 0, 0, 4, 4, 11, 3, 3) + + +write(A_linearized, "output.txt", format='csv') \ No newline at end of file diff --git a/scripts/builtin/img_translate_linearized.dml b/scripts/builtin/img_translate_linearized.dml new file mode 100644 index 00000000000..3b7e501800c --- /dev/null +++ b/scripts/builtin/img_translate_linearized.dml @@ -0,0 +1,73 @@ +#------------------------------------------------------------- +# +# 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_translate but it handles multiple images at +# the same time. Each row of the input and output matrix represents a linearized image/matrix +# It translates the image and Optionally resizes the image (without scaling). +# INPUT: +# ---------------------------------------------------------------------------------------------- +# img_in_linearized Input matrix/image (every row represents a linearized matrix/image) +# offset_x The distance to move the image in x direction +# offset_y The distance to move the image in y direction +# out_w Width of the output image +# out_h Height of the output image +# fill_value the background of the image +# original_w width of the original 2D images +# original_h height of the original 2D images +# -------------------------------------------------------------------------------------------- +# +# OUTPUT: +# -------------------------------------------------------------------------------------------- +# img_out_linearized Output matrix/image (every row represents a linearized matrix/image) +# -------------------------------------------------------------------------------------------- + +m_img_translate_linearized = function(Matrix[Double] img_in_linearized, +Double offset_x, Double offset_y, Integer out_w, Integer out_h, Double fill_value, +Integer original_w, Integer original_h) return (Matrix[Double] img_out_linearized) { + + num_images = nrow(img_in_linearized) + w = original_w + h = original_h + # Round offsets to nearest integer + offset_x = round(offset_x) + offset_y = round(offset_y) + img_out_linearized = matrix(fill_value, rows=num_images, cols=out_w * out_h) + for (i in 1:num_images) { + img_row = img_in_linearized[i,] + + for (y in 1:out_h) { + for (x in 1:out_w) { + src_x = x - offset_x + src_y = y - offset_y + + # Validate indices to be in the valid range + if (src_x >= 1 & src_x <= w & src_y >= 1 & src_y <= h) { + src_index = (src_y - 1) * w + src_x + dest_index = (y - 1) * out_w + x + + if (src_index >= 1 & src_index <= ncol(img_row) & dest_index >= 1 & dest_index <= ncol(img_out_linearized)) { + img_out_linearized[i, dest_index] = img_row[1, src_index] + } + } + } + } + } +} diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 5541c53bc55..0436bd265f3 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_MIRROR_LINEARIZED("img_mirror_linearized", true), IMG_BRIGHTNESS("img_brightness", true), + IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), IMG_CROP("img_crop", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSLATE("img_translate", true), From 144556568f55916fb4022825590deebc8bd0b6d3 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Tue, 12 Sep 2023 19:17:57 +0200 Subject: [PATCH 10/16] Revert "add translate" This reverts commit 53ccb9a4a7904b6e29356f732afff051c035b6f3. git push origin img_mirror_linearized_feature --- scripts/builtin/deneme.dml | 9 --- scripts/builtin/img_translate_linearized.dml | 73 ------------------- .../org/apache/sysds/common/Builtins.java | 1 - 3 files changed, 83 deletions(-) delete mode 100644 scripts/builtin/deneme.dml delete mode 100644 scripts/builtin/img_translate_linearized.dml diff --git a/scripts/builtin/deneme.dml b/scripts/builtin/deneme.dml deleted file mode 100644 index a0c987d10ea..00000000000 --- a/scripts/builtin/deneme.dml +++ /dev/null @@ -1,9 +0,0 @@ - -M = matrix("1 2 3 4 5 6 7 8 9", rows=1, cols=9) - - - -A_linearized = img_translate_linearized(M, 0, 0, 4, 4, 11, 3, 3) - - -write(A_linearized, "output.txt", format='csv') \ No newline at end of file diff --git a/scripts/builtin/img_translate_linearized.dml b/scripts/builtin/img_translate_linearized.dml deleted file mode 100644 index 3b7e501800c..00000000000 --- a/scripts/builtin/img_translate_linearized.dml +++ /dev/null @@ -1,73 +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. -# -#------------------------------------------------------------- - -# This function has the same functionality with img_translate but it handles multiple images at -# the same time. Each row of the input and output matrix represents a linearized image/matrix -# It translates the image and Optionally resizes the image (without scaling). -# INPUT: -# ---------------------------------------------------------------------------------------------- -# img_in_linearized Input matrix/image (every row represents a linearized matrix/image) -# offset_x The distance to move the image in x direction -# offset_y The distance to move the image in y direction -# out_w Width of the output image -# out_h Height of the output image -# fill_value the background of the image -# original_w width of the original 2D images -# original_h height of the original 2D images -# -------------------------------------------------------------------------------------------- -# -# OUTPUT: -# -------------------------------------------------------------------------------------------- -# img_out_linearized Output matrix/image (every row represents a linearized matrix/image) -# -------------------------------------------------------------------------------------------- - -m_img_translate_linearized = function(Matrix[Double] img_in_linearized, -Double offset_x, Double offset_y, Integer out_w, Integer out_h, Double fill_value, -Integer original_w, Integer original_h) return (Matrix[Double] img_out_linearized) { - - num_images = nrow(img_in_linearized) - w = original_w - h = original_h - # Round offsets to nearest integer - offset_x = round(offset_x) - offset_y = round(offset_y) - img_out_linearized = matrix(fill_value, rows=num_images, cols=out_w * out_h) - for (i in 1:num_images) { - img_row = img_in_linearized[i,] - - for (y in 1:out_h) { - for (x in 1:out_w) { - src_x = x - offset_x - src_y = y - offset_y - - # Validate indices to be in the valid range - if (src_x >= 1 & src_x <= w & src_y >= 1 & src_y <= h) { - src_index = (src_y - 1) * w + src_x - dest_index = (y - 1) * out_w + x - - if (src_index >= 1 & src_index <= ncol(img_row) & dest_index >= 1 & dest_index <= ncol(img_out_linearized)) { - img_out_linearized[i, dest_index] = img_row[1, src_index] - } - } - } - } - } -} diff --git a/src/main/java/org/apache/sysds/common/Builtins.java b/src/main/java/org/apache/sysds/common/Builtins.java index 0436bd265f3..5541c53bc55 100644 --- a/src/main/java/org/apache/sysds/common/Builtins.java +++ b/src/main/java/org/apache/sysds/common/Builtins.java @@ -157,7 +157,6 @@ public enum Builtins { IMG_MIRROR("img_mirror", true), IMG_MIRROR_LINEARIZED("img_mirror_linearized", true), IMG_BRIGHTNESS("img_brightness", true), - IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true), IMG_CROP("img_crop", true), IMG_TRANSFORM("img_transform", true), IMG_TRANSLATE("img_translate", true), From 87c266b6d8a723972f3d4a22ef5ba507cccf65fb Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Wed, 13 Sep 2023 19:40:33 +0200 Subject: [PATCH 11/16] Tests updated --- .../BuiltinImageMirrorLinearizedTest.java | 78 ++++++++++++------- .../builtin/image_mirror_linearized.dml | 59 +++++++++----- 2 files changed, 90 insertions(+), 47 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java index 2b1bbc0839b..8fce2a5adcd 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java @@ -19,6 +19,8 @@ package org.apache.sysds.test.functions.builtin.part1; 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; @@ -26,28 +28,53 @@ 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 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; + private final static double eps = 1e-10; + private final static double spSparse = 0.05; + private final static double spDense = 0.5; + + @Parameterized.Parameter() + public int img_rows; + @Parameterized.Parameter(1) + public int img_cols; + @Parameterized.Parameter(2) + public int rows; // number of images + public int cols; // Initialized based on img_rows * img_cols + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] { + {12, 12, 4}, + {13, 11, 5}, + {64, 64, 32}, + {256, 256, 5}, + {256, 253, 5}, + {1024, 1024, 5}, + {1024, 1048, 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"})); + cols = img_rows * img_cols; + addTestConfiguration(TEST_NAME_LINEARIZED, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[]{"B_x", "B_y"})); } @Test - public void testImageMirrorLinearizedDenseCP() { + public void testImageMirrorLinearized() { runImageMirrorLinearizedTest(false, ExecType.CP); } @@ -57,39 +84,34 @@ private void runImageMirrorLinearizedTest(boolean sparse, ExecType instType) { 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") + "in_file=" + input("A"), + "x_out_reshape_file=" + output("B_x_reshape"), + "y_out_reshape_file=" + output("B_y_reshape"), + "x_out_file=" + output("B_x"), + "y_out_file=" + output("B_y"), + "img_rows=" + img_rows, + "img_cols=" + img_cols }; + double[][] A = getRandomMatrix(rows, cols, 0, 255, sparsity, 7); writeInputMatrixWithMTD("A", A, true); runTest(true, false, null, -1); - HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x_reshape"); - HashMap 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") - }; - - runTest(true, false, null, -1); - - HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x"); - HashMap dmlfileY = readDMLMatrixFromOutputDir("B_y"); + HashMap dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); + HashMap dmlfileLinearizedY = readDMLMatrixFromOutputDir("B_y"); + + HashMap dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); + HashMap dmlfileY = readDMLMatrixFromOutputDir("B_y_reshape"); - // Compare matrices TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X"); TestUtils.compareMatrices(dmlfileLinearizedY, dmlfileY, eps, "Stat-DML-LinearizedY", "Stat-DML-Y"); - } finally { rtplatform = platformOld; diff --git a/src/test/scripts/functions/builtin/image_mirror_linearized.dml b/src/test/scripts/functions/builtin/image_mirror_linearized.dml index 38308a4f28a..74703396ecc 100644 --- a/src/test/scripts/functions/builtin/image_mirror_linearized.dml +++ b/src/test/scripts/functions/builtin/image_mirror_linearized.dml @@ -19,22 +19,43 @@ # #------------------------------------------------------------- - -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); - - - - \ No newline at end of file +# Beginning of the image_mirror_batched code +input_batched = read($in_file); +num_images = nrow(input_batched); +m_batched = ncol(input_batched); + +img_rows = $img_rows; +img_cols = $img_cols; + +img_out_flattened_x = matrix(0, rows=num_images, cols=m_batched); +img_out_flattened_y = matrix(0, rows=num_images, cols=m_batched); +input_matrix = matrix(input_batched, rows=num_images, cols=m_batched); + +for(i in 1:num_images) { + image_i = matrix(input_matrix[i,], rows=img_rows, cols=img_cols); + + img_out_x = img_mirror(image_i, TRUE); + img_out_y = img_mirror(image_i, FALSE); + + img_out_flattened_x[i,] = matrix(img_out_x, rows=1, cols=m_batched); + img_out_flattened_y[i,] = matrix(img_out_y, rows=1, cols=m_batched); +} + +write(img_out_flattened_x, $x_out_reshape_file); +write(img_out_flattened_y, $y_out_reshape_file); + +# Beginning of the image_mirror_linearized code +input_linearized = read($in_file); +n_linearized = nrow(input_linearized); +m_linearized = ncol(input_linearized); + +img_rows_linearized = $img_rows; +img_cols_linearized = $img_cols; + +image_i_linearized = matrix(input_linearized, rows=n_linearized, cols=m_linearized); + +img_out_x_linearized = img_mirror_linearized(image_i_linearized, TRUE, img_rows_linearized, img_cols_linearized); +img_out_y_linearized = img_mirror_linearized(image_i_linearized, FALSE, img_rows_linearized, img_cols_linearized); + +write(img_out_x_linearized, $x_out_file); +write(img_out_y_linearized, $y_out_file); From c8837cab535c7a03df541eb3da65959ba5f925a2 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Thu, 21 Sep 2023 02:56:33 +0200 Subject: [PATCH 12/16] some optimizations --- scripts/builtin/img_mirror_linearized.dml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml index 626684937e0..bcab3d7146e 100644 --- a/scripts/builtin/img_mirror_linearized.dml +++ b/scripts/builtin/img_mirror_linearized.dml @@ -58,22 +58,18 @@ Integer original_rows, Integer original_cols) return (matrix[double] R) { } } else { - for (i in 1:nrow(img_matrix)) { offset = 1; while (offset <= n) { - # Get the sub-matrix end = min(n, offset + cols - 1); - # Reverse the sub-matrix columns - reversed_sub_matrix = matrix(0, rows=1, cols=cols); + reversed_sub_matrix = matrix(0, rows=nrow(img_matrix), cols=cols); idx = 1; for (j in offset:end) { - reversed_sub_matrix[1, cols - idx + 1] = img_matrix[i, j]; + reversed_sub_matrix[, cols - idx + 1] = img_matrix[, j]; idx = idx + 1; } - R[i, offset:end] = reversed_sub_matrix; + R[, offset:end] = reversed_sub_matrix; offset = end + 1; } - } } } From 614321dcf5ec37dd2614dfd2243ba77fac7e9e5b Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Thu, 21 Sep 2023 15:24:39 +0200 Subject: [PATCH 13/16] parfor added --- scripts/builtin/img_mirror_linearized.dml | 2 +- .../builtin/part1/BuiltinImageMirrorLinearizedTest.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/builtin/img_mirror_linearized.dml b/scripts/builtin/img_mirror_linearized.dml index bcab3d7146e..3a2a7049b77 100644 --- a/scripts/builtin/img_mirror_linearized.dml +++ b/scripts/builtin/img_mirror_linearized.dml @@ -43,7 +43,7 @@ Integer original_rows, Integer original_cols) return (matrix[double] R) { cols = original_cols; if (horizontal_axis) { - for (i in seq(1, (rows %/% 2) * cols, cols)) { + parfor (i in seq(1, (rows %/% 2) * cols, cols),check=0) { start = i; end = i + cols - 1; mirrorStart = (n - end) + 1; diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java index 8fce2a5adcd..2c8aec6f050 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java @@ -77,6 +77,11 @@ public void setUp() { public void testImageMirrorLinearized() { runImageMirrorLinearizedTest(false, ExecType.CP); } + + @Test + public void testImageMirrorLinearizedSparse() { + runImageMirrorLinearizedTest(true, ExecType.CP); + } private void runImageMirrorLinearizedTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); From 28396445afb9d4890ee7527ce3755a7abe67d615 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Thu, 21 Sep 2023 19:15:26 +0200 Subject: [PATCH 14/16] redundant parts removed --- .../builtin/image_mirror_linearized.dml | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/src/test/scripts/functions/builtin/image_mirror_linearized.dml b/src/test/scripts/functions/builtin/image_mirror_linearized.dml index 74703396ecc..3501cade9d8 100644 --- a/src/test/scripts/functions/builtin/image_mirror_linearized.dml +++ b/src/test/scripts/functions/builtin/image_mirror_linearized.dml @@ -19,43 +19,25 @@ # #------------------------------------------------------------- -# Beginning of the image_mirror_batched code input_batched = read($in_file); num_images = nrow(input_batched); m_batched = ncol(input_batched); - img_rows = $img_rows; img_cols = $img_cols; - img_out_flattened_x = matrix(0, rows=num_images, cols=m_batched); img_out_flattened_y = matrix(0, rows=num_images, cols=m_batched); input_matrix = matrix(input_batched, rows=num_images, cols=m_batched); - for(i in 1:num_images) { image_i = matrix(input_matrix[i,], rows=img_rows, cols=img_cols); - img_out_x = img_mirror(image_i, TRUE); img_out_y = img_mirror(image_i, FALSE); - img_out_flattened_x[i,] = matrix(img_out_x, rows=1, cols=m_batched); img_out_flattened_y[i,] = matrix(img_out_y, rows=1, cols=m_batched); } - write(img_out_flattened_x, $x_out_reshape_file); write(img_out_flattened_y, $y_out_reshape_file); - -# Beginning of the image_mirror_linearized code -input_linearized = read($in_file); -n_linearized = nrow(input_linearized); -m_linearized = ncol(input_linearized); - -img_rows_linearized = $img_rows; -img_cols_linearized = $img_cols; - -image_i_linearized = matrix(input_linearized, rows=n_linearized, cols=m_linearized); - -img_out_x_linearized = img_mirror_linearized(image_i_linearized, TRUE, img_rows_linearized, img_cols_linearized); -img_out_y_linearized = img_mirror_linearized(image_i_linearized, FALSE, img_rows_linearized, img_cols_linearized); - +imgs = matrix(input_batched, rows=num_images, cols=m_batched); +img_out_x_linearized = img_mirror_linearized(imgs, TRUE, img_rows, img_cols); +img_out_y_linearized = img_mirror_linearized(imgs, FALSE, img_rows, img_cols); write(img_out_x_linearized, $x_out_file); write(img_out_y_linearized, $y_out_file); From 5e49d59de54c814d7af8b27847d53d15276b4f2f Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Thu, 21 Sep 2023 22:14:00 +0200 Subject: [PATCH 15/16] spark test added --- .../part1/BuiltinImageMirrorLinearizedTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java index 2c8aec6f050..6300acf0d93 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java @@ -57,13 +57,10 @@ public static Collection data() { {12, 12, 4}, {13, 11, 5}, {64, 64, 32}, - {256, 256, 5}, + {256, 256, 5}, {256, 253, 5}, {1024, 1024, 5}, {1024, 1048, 5} - - - }); } @@ -83,6 +80,11 @@ public void testImageMirrorLinearizedSparse() { runImageMirrorLinearizedTest(true, ExecType.CP); } + @Test + public void testImageMirrorLinearizedSP() { + runImageMirrorLinearizedTest(false, ExecType.SPARK); + } + private void runImageMirrorLinearizedTest(boolean sparse, ExecType instType) { ExecMode platformOld = setExecMode(instType); disableOutAndExpectedDeletion(); From 92b24e25d3f2a9a6c8b2ee9a4d457452bedd55e0 Mon Sep 17 00:00:00 2001 From: baristerzioglu Date: Fri, 22 Sep 2023 20:24:20 +0200 Subject: [PATCH 16/16] ignore flag to spark test --- .../builtin/part1/BuiltinImageMirrorLinearizedTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java index 6300acf0d93..ea449254305 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java @@ -18,6 +18,7 @@ */ 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; @@ -81,6 +82,7 @@ public void testImageMirrorLinearizedSparse() { } @Test + @Ignore public void testImageMirrorLinearizedSP() { runImageMirrorLinearizedTest(false, ExecType.SPARK); }