-
Notifications
You must be signed in to change notification settings - Fork 542
[SYSTEMDS-3545] img_mirror_linearized #1903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dc2543e
Added img_mirror_linearized built-in function
terzioglub 648f4a5
updated img_mirror_linearized
terzioglub a74eae9
Add image_mirror_linearized.dml for testing
terzioglub 3b6c2fe
Added BuiltinImageMirrorLinearizedTest.java
terzioglub c6aafef
Added img_mirror_linearized to Builtins.java
terzioglub 08958a7
Resolved comments and issues
terzioglub 2ce5e9a
line formatting corrected
terzioglub b8d5379
license header added
terzioglub 53ccb9a
add translate
terzioglub 1445565
Revert "add translate"
terzioglub 87c266b
Tests updated
terzioglub c8837ca
some optimizations
terzioglub 614321d
parfor added
terzioglub 2839644
redundant parts removed
terzioglub 5e49d59
spark test added
terzioglub 92b24e2
ignore flag to spark test
terzioglub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #------------------------------------------------------------- | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # | ||
| #------------------------------------------------------------- | ||
|
|
||
| # This function has the same functionality with img_mirror but it handles multiple images at | ||
| # the same time. Each row of the input and output matrix represents a linearized image/matrix | ||
| # It flips an image on the X (horizontal) or Y (vertical) axis. | ||
| # INPUT: | ||
| # ----------------------------------------------------------------------------------------- | ||
| # img_matrix Input matrix/image (every row represents a linearized matrix/image) | ||
| # horizontal_axis flip either in X or Y axis | ||
| # original_rows number of rows in the original 2-D images | ||
| # original_cols number of cols in the original 2-D images | ||
| # ----------------------------------------------------------------------------------------- | ||
| # | ||
| # OUTPUT: | ||
| # ----------------------------------------------------------------------------------------- | ||
| # R Output matrix/image (every row represents a linearized matrix/image) | ||
| # ----------------------------------------------------------------------------------------- | ||
|
|
||
| m_img_mirror_linearized = function(matrix[double] img_matrix, Boolean horizontal_axis, | ||
| Integer original_rows, Integer original_cols) return (matrix[double] R) { | ||
| n = ncol(img_matrix); | ||
| R = matrix(0, rows=nrow(img_matrix), cols=n); | ||
| rows = original_rows; | ||
| cols = original_cols; | ||
|
|
||
| if (horizontal_axis) { | ||
| parfor (i in seq(1, (rows %/% 2) * cols, cols),check=0) { | ||
| 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 { | ||
| offset = 1; | ||
| while (offset <= n) { | ||
| end = min(n, offset + cols - 1); | ||
| reversed_sub_matrix = matrix(0, rows=nrow(img_matrix), cols=cols); | ||
| idx = 1; | ||
| for (j in offset:end) { | ||
| reversed_sub_matrix[, cols - idx + 1] = img_matrix[, j]; | ||
| idx = idx + 1; | ||
| } | ||
| R[, offset:end] = reversed_sub_matrix; | ||
| offset = end + 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
.../java/org/apache/sysds/test/functions/builtin/part1/BuiltinImageMirrorLinearizedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.sysds.test.functions.builtin.part1; | ||
|
|
||
| import org.junit.Ignore; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import org.apache.sysds.common.Types.ExecMode; | ||
| import org.apache.sysds.common.Types.ExecType; | ||
| import org.apache.sysds.runtime.matrix.data.MatrixValue; | ||
| import org.apache.sysds.test.AutomatedTestBase; | ||
| import org.apache.sysds.test.TestConfiguration; | ||
| import org.apache.sysds.test.TestUtils; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| @net.jcip.annotations.NotThreadSafe | ||
| public class BuiltinImageMirrorLinearizedTest extends AutomatedTestBase { | ||
|
|
||
| private final static String TEST_NAME_LINEARIZED = "image_mirror_linearized"; | ||
| 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 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<Object[]> 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() { | ||
| 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 testImageMirrorLinearized() { | ||
| runImageMirrorLinearizedTest(false, ExecType.CP); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add SP and Hybrid. |
||
| } | ||
|
|
||
| @Test | ||
| public void testImageMirrorLinearizedSparse() { | ||
| runImageMirrorLinearizedTest(true, ExecType.CP); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore | ||
| public void testImageMirrorLinearizedSP() { | ||
| runImageMirrorLinearizedTest(false, ExecType.SPARK); | ||
| } | ||
|
|
||
| private void runImageMirrorLinearizedTest(boolean sparse, ExecType instType) { | ||
| ExecMode platformOld = setExecMode(instType); | ||
| disableOutAndExpectedDeletion(); | ||
|
|
||
| try { | ||
| loadTestConfiguration(getTestConfiguration(TEST_NAME_LINEARIZED)); | ||
|
|
||
| double sparsity = sparse ? spSparse : spDense; | ||
| String HOME = SCRIPT_DIR + TEST_DIR; | ||
|
|
||
| fullDMLScriptName = HOME + TEST_NAME_LINEARIZED + ".dml"; | ||
| programArgs = new String[]{"-nvargs", | ||
| "in_file=" + input("A"), | ||
| "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<MatrixValue.CellIndex, Double> dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x"); | ||
| HashMap<MatrixValue.CellIndex, Double> dmlfileLinearizedY = readDMLMatrixFromOutputDir("B_y"); | ||
|
|
||
| HashMap<MatrixValue.CellIndex, Double> dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape"); | ||
| HashMap<MatrixValue.CellIndex, Double> dmlfileY = readDMLMatrixFromOutputDir("B_y_reshape"); | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
src/test/scripts/functions/builtin/image_mirror_linearized.dml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #------------------------------------------------------------- | ||
| # | ||
| # 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_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); | ||
| 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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.