Skip to content
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

[SYSTEMDS-3545] Linearized Img Sample Shear & Rotate #1965

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions scripts/builtin/img_rotate_linearized.dml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#-------------------------------------------------------------
#
# 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 Linearized Image Rotate function rotates the linearized input images counter-clockwise around the center.
# Uses nearest neighbor sampling.
#
# INPUT:
# -----------------------------------------------------------------------------------------------
# img_in Linearized input images as 2D matrix with top left corner at [1, 1]
# radians The value by which to rotate in radian.
# fill_value The background color revealed by the rotation
# -----------------------------------------------------------------------------------------------
#
# OUTPUT:
# ---------------------------------------------------------------------------------------------
# img_out Output images in linearized form as 2D matrix with top left corner at [1, 1]
# ---------------------------------------------------------------------------------------------

m_img_rotate_linearized = function(Matrix[Double] img_in, Double radians, Double fill_value, Integer s_cols, Integer s_rows) return (Matrix[Double] img_out) {
# Translation matrix for moving the origin to the center of the image
t1 = matrix("1 0 0 0 1 0 0 0 1", rows=3, cols=3)
t1[1, 3] = -s_cols / 2
t1[2, 3] = -s_rows / 2

# Translation matrix for moving the origin back to the top left corner
t2 = matrix("1 0 0 0 1 0 0 0 1", rows=3, cols=3)
t2[1, 3] = s_cols / 2
t2[2, 3] = s_rows / 2

# The rotation matrix around the origin
rot = matrix("1 0 0 0 1 0 0 0 1", rows=3, cols=3)
c = cos(radians)
s = sin(radians)
rot[1, 1] = c
rot[1, 2] = s
rot[2, 1] = -s
rot[2, 2] = c

# Combined transformation matrix
m = t2 %*% rot %*% t1

# Transform image
img_out = img_transform_linearized(img_in, s_cols, s_rows, as.scalar(m[1,1]), as.scalar(m[1,2]), as.scalar(m[1,3]), as.scalar(m[2,1]), as.scalar(m[2,2]), as.scalar(m[2,3]), fill_value, s_cols, s_rows)
}
48 changes: 48 additions & 0 deletions scripts/builtin/img_sample_pairing_linearized.dml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#-------------------------------------------------------------
#
# 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 image sample pairing function blends two images together.
#
# INPUT:
# -------------------------------------------------------------------------------------------
# img_in1 input matrix/image (every row is a linearized image)
# img_in2 Second input image (one image represented as a single row linearized matrix)
# weight The weight given to the second image.
# 0 means only img_in1, 1 means only img_in2 will be visible
# -------------------------------------------------------------------------------------------
#
# OUTPUT:
# --------------------------------------------------------------------------------------------
# img_out Output image
# --------------------------------------------------------------------------------------------

m_img_sample_pairing_linearized= function(Matrix[Double] img_in1, Matrix[Double] img_in2, Double weight) return (Matrix[Double] img_out) {
if (weight < 0 | 1 < weight) {
print("Invalid weight. Set weight to 0.5")
weight = 0.5
}
num_images= nrow(img_in1)
img_out = matrix (0 ,rows=nrow(img_in1),cols=ncol(img_in2))
parfor(i in 1:num_images) {
img_out[i,] = (1 - weight) * img_in1[i,]+ weight * img_in2
}

}
40 changes: 40 additions & 0 deletions scripts/builtin/img_shear_linearized.dml
Original file line number Diff line number Diff line change
@@ -0,0 +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 applies a shearing transformation to linearized images.
# Uses nearest neighbor sampling.
#
# INPUT:
# ---------------------------------------------------------------------------------------------
# img_in Linearized input images as 2D matrix with top left corner at [1, 1]
# shear_x Shearing factor for horizontal shearing
# shear_y Shearing factor for vertical shearing
# fill_value The background color revealed by the shearing
# ---------------------------------------------------------------------------------------------
#
# OUTPUT:
# ------------------------------------------------------------------------------------------
# img_out Output images in linearized form as 2D matrix with top left corner at [1, 1]
# ------------------------------------------------------------------------------------------

m_img_shear_linearized = function(Matrix[Double] img_in, Double shear_x, Double shear_y, Double fill_value, Integer s_cols, Integer s_rows) return (Matrix[Double] img_out) {
img_out = img_transform_linearized(img_in, s_cols, s_rows, 1, shear_x, 0, shear_y, 1, 0, fill_value, s_cols, s_rows)
}
3 changes: 0 additions & 3 deletions scripts/builtin/img_transform_linearized.dml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte
# compute sampling pixel indices
coords = floor(T_inv %*% coords) + 1

# fill output image
img_out = matrix(fill_value, rows=nrow(img_in), cols=out_w*out_h)

inx = t(coords[1,])
iny = t(coords[2,])

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/apache/sysds/common/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ public enum Builtins {
IMG_TRANSLATE("img_translate", true),
IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true),
IMG_ROTATE("img_rotate", true),
IMG_ROTATE_LINEARIZED("img_rotate_linearized", true),
IMG_SHEAR("img_shear", true),
IMG_SHEAR_LINEARIZED("img_shear_linearized", true),
IMG_CUTOUT("img_cutout", true),
IMG_CUTOUT_LINEARIZED("img_cutout_linearized", true),
IMG_SAMPLE_PAIRING("img_sample_pairing", true),
IMG_SAMPLE_PAIRING_LINEARIZED("img_sample_pairing_linearized", true),
IMG_INVERT("img_invert", true),
IMG_INVERT_LINEARIZED("img_invert_linearized", true),
IMG_POSTERIZE("img_posterize", true),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 BuiltinImageSamplePairingLinearizedTest extends AutomatedTestBase {

private final static String TEST_NAME_LINEARIZED = "image_sample_pairing_linearized";
private final static String TEST_DIR = "functions/builtin/";
private final static String TEST_CLASS_DIR = TEST_DIR + BuiltinImageSamplePairingLinearizedTest.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 value;

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{10},
{-5},
{3}
});
}

@Override
public void setUp() {

addTestConfiguration(TEST_NAME_LINEARIZED, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME_LINEARIZED, new String[]{"B_x"}));
}

@Test
public void testImageSamplePairingLinearized() {
runImageSamplePairingLinearizedTest(false, ExecType.CP);
}

private void runImageSamplePairingLinearizedTest(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"),
"in_file_second=" + input("secondMatrix"),
"x_out_reshape_file=" + output("B_x_reshape"),
"x_out_file=" + output("B_x"),
"value=" +value
};

double[][] A = getRandomMatrix(100,50, 0, 255, sparsity, 7);
double[][] secondMatrix = getRandomMatrix(1,50, 0, 255, sparsity, 7);
writeInputMatrixWithMTD("A", A, true);
writeInputMatrixWithMTD("secondMatrix", secondMatrix, true);
runTest(true, false, null, -1);

HashMap<MatrixValue.CellIndex, Double> dmlfileLinearizedX = readDMLMatrixFromOutputDir("B_x");

HashMap<MatrixValue.CellIndex, Double> dmlfileX = readDMLMatrixFromOutputDir("B_x_reshape");

TestUtils.compareMatrices(dmlfileLinearizedX, dmlfileX, eps, "Stat-DML-LinearizedX", "Stat-DML-X");


} finally {
rtplatform = platformOld;
}
}
}
Loading
Loading