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

Method for image transformation matrix generation (more evualtion needed) #2057

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
80b5d0b
added test for transformation of linearized images
mark-p4 Jun 8, 2024
3288c00
added a dml file to calculate the image transformation matrix, the bu…
mark-p4 Jun 11, 2024
a616ba7
added a dml file img_transform_matrix.dml
mark-p4 Jul 18, 2024
5205e87
added a builtin for img_transform_matrix.dml
mark-p4 Jul 18, 2024
875c75f
added function img_transform_matrix, CP and Lop missing? maybe hop too?
mark-p4 Jul 19, 2024
36c8597
rewritten for img_transform_matrix.dml to be used
mark-p4 Jul 19, 2024
42a38a7
intermediate commit, added new test script, reimplemented function to…
mark-p4 Jul 26, 2024
9ae2b23
solid path to executing the necessary steps, throwing not implemented…
mark-p4 Jul 26, 2024
1ea9b08
added some multi return parameters
mark-p4 Jul 26, 2024
9a2a375
we made it to a state were the function itself can be implemented in …
mark-p4 Jul 26, 2024
9a4c921
first steps of implementation of the algorithm for the transformation…
mark-p4 Jul 27, 2024
e533dee
intermediate commit for transformation matrix implementation
mark-p4 Jul 27, 2024
ef83ba2
almost completed the transformation matrix implementation
mark-p4 Jul 27, 2024
36926ac
ctable is not working in any meaningful way
mark-p4 Jul 27, 2024
620e71e
image_transform_matrix.dml is now working, some testing and evaluatio…
mark-p4 Jul 28, 2024
bf120d2
cleanup of existing code for pull request
mark-p4 Jul 28, 2024
e35ea84
added approriate licences to new dml and java files
mark-p4 Jul 28, 2024
a2d0ee9
improved documentation based on automated tests
mark-p4 Jul 28, 2024
e3e06d3
disabled the transform linearized test for now, aim is to enable late…
mark-p4 Jul 28, 2024
ba4322c
changes to BuiltinImageTransformLinearizedTest.java and image_transfo…
mark-p4 Jul 28, 2024
3e2cb7d
various changes
mark-p4 Jul 29, 2024
fa7d7c1
added changes to tests and cleaned up LibMatrixIMGTransform
mark-p4 Jul 29, 2024
e32ca44
improved tests and looking into bug with the img_transform_linearized…
mark-p4 Jul 30, 2024
3ba7e55
everything seems to work, nice
mark-p4 Jul 30, 2024
e825ca1
added improvements based on feedback and cleaned up code
mark-p4 Jul 30, 2024
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
9 changes: 4 additions & 5 deletions scripts/builtin/img_transform_linearized.dml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# The Linearized Image Transform function applies an affine transformation to linearized images.
# Optionally resizes the image (without scaling).
# Uses nearest neighbor sampling.
# Is implemented as a builtin function as img_transform_matrix in image_transform_linearized.
#
# INPUT:
# -------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -75,17 +76,15 @@ m_img_transform_linearized = function(Matrix[Double] img_in, Integer out_w, Inte
index_vector = (orig_w *(iny-1) + inx) * ((0<inx) & (inx<=orig_w) & (0<iny) & (iny<=orig_h))
index_vector = t(index_vector)
xs = ((index_vector == 0)*(orig_w*orig_h +1)) + index_vector


ind= matrix(seq(1,ncol(xs),1),1,ncol(xs))
z = table(xs, ind)
if(min(index_vector) == 0){
ys=cbind(img_in, matrix(fill_value,nrow(img_in), 1))
}else{
ys = img_in
}

ind= matrix(seq(1,ncol(xs),1),1,ncol(xs))
z = table(xs, ind)
output = ys%*%z

img_out = matrix(output, rows=nrow(img_in), cols=out_w*out_h)
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/apache/sysds/common/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public enum Builtins {
IMG_CROP_LINEARIZED("img_crop_linearized", true),
IMG_TRANSFORM("img_transform", true),
IMG_TRANSFORM_LINEARIZED("img_transform_linearized", true),
IMG_TRANSFORM_MATRIX("img_transform_matrix", false, ReturnType.MULTI_RETURN),
IMG_TRANSFORM_TEST("img_transform_test", true),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the test function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be removed after benchmarking

IMG_TRANSLATE("img_translate", true),
IMG_TRANSLATE_LINEARIZED("img_translate_linearized", true),
IMG_ROTATE("img_rotate", true),
Expand Down
43 changes: 20 additions & 23 deletions src/main/java/org/apache/sysds/hops/FunctionOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,39 +187,37 @@
// upper-triangular and lower-triangular matrices
long outputH = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 0.5);
long outputR = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 0.5);
return outputH+outputR;
}
else if ( getFunctionName().equalsIgnoreCase("lu") ) {
return outputH + outputR;
} else if (getFunctionName().equalsIgnoreCase("lu")) {
// upper-triangular and lower-triangular matrices
long outputP = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0/getOutputs().get(1).getDim2());
long outputP = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0 / getOutputs().get(1).getDim2());
long outputL = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 0.5);
long outputU = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 0.5);
return outputL+outputU+outputP;
}
else if ( getFunctionName().equalsIgnoreCase("eigen") ) {
return outputL + outputU + outputP;
} else if (getFunctionName().equalsIgnoreCase("eigen")) {
long outputVectors = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputValues = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), 1, 1.0);
return outputVectors+outputValues;
}
else if ( getFunctionName().equalsIgnoreCase("fft") ) {
return outputVectors + outputValues;
} else if (getFunctionName().equalsIgnoreCase("fft")) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputIm = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0);
return outputRe+outputIm;
}
else if ( getFunctionName().equalsIgnoreCase("ifft") ) {
return outputRe + outputIm;

Check warning on line 204 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L204

Added line #L204 was not covered by tests
} else if (getFunctionName().equalsIgnoreCase("ifft")) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputIm = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0);
return outputRe+outputIm;
}
else if ( getFunctionName().equalsIgnoreCase("fft_linearized") ) {
return outputRe + outputIm;

Check warning on line 208 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L208

Added line #L208 was not covered by tests
} else if (getFunctionName().equalsIgnoreCase("fft_linearized")) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputIm = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0);
return outputRe+outputIm;
}
else if ( getFunctionName().equalsIgnoreCase("ifft_linearized") ) {
return outputRe + outputIm;

Check warning on line 212 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L212

Added line #L212 was not covered by tests
} else if (getFunctionName().equalsIgnoreCase("ifft_linearized")) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputIm = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0);
return outputRe+outputIm;
return outputRe + outputIm;

Check warning on line 216 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L216

Added line #L216 was not covered by tests
} else if ( getFunctionName().equalsIgnoreCase("img_transform_matrix")) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
long outputIm = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(1).getDim1(), getOutputs().get(1).getDim2(), 1.0);
return outputRe + outputIm;

Check warning on line 220 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L218-L220

Added lines #L218 - L220 were not covered by tests
}
else if ( getFunctionName().equalsIgnoreCase("stft") ) {
long outputRe = OptimizerUtils.estimateSizeExactSparsity(getOutputs().get(0).getDim1(), getOutputs().get(0).getDim2(), 1.0);
Expand Down Expand Up @@ -294,9 +292,8 @@
}
else if ( getFunctionName().equalsIgnoreCase("ifft_linearized") ) {
// 2 matrices of size same as the input
return 2*OptimizerUtils.estimateSizeExactSparsity(getInput().get(0).getDim1(), getInput().get(0).getDim2(), 1.0);
}
else if ( getFunctionName().equalsIgnoreCase("stft") ) {
return 2 * OptimizerUtils.estimateSizeExactSparsity(getInput().get(0).getDim1(), getInput().get(0).getDim2(), 1.0);

Check warning on line 295 in src/main/java/org/apache/sysds/hops/FunctionOp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/hops/FunctionOp.java#L295

Added line #L295 was not covered by tests
} else if ( getFunctionName().equalsIgnoreCase("stft") ) {
// 2 matrices of size same as the input
return 2*OptimizerUtils.estimateSizeExactSparsity(getInput().get(0).getDim1(), getInput().get(0).getDim2(), 1.0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,31 @@

break;
}
//implement the calculation of the transformation matrix for affine transformation of images
case IMG_TRANSFORM_MATRIX:
//transformation matrix must be 3x3 matrix
Expression expressionOne_IMG = getFirstExpr();
//dimension matrix must be a 2x2 matrix
Expression expressionTwo_IMG = getSecondExpr();
checkMatrixFrameParam(expressionOne_IMG);
checkMatrixFrameParam(expressionTwo_IMG);

if ((expressionOne_IMG.getOutput().getDim1() != expressionOne_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 3) {
raiseValidateError("The first argument to " + _opcode + " must be a square 3x3 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS);

Check warning on line 685 in src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java#L685

Added line #L685 was not covered by tests
} else if ((expressionTwo_IMG.getOutput().getDim1() != expressionTwo_IMG.getOutput().getDim2()) && expressionOne_IMG.getOutput().getDim1() != 2) {
raiseValidateError("The second argument to " + _opcode + " must be a square 2x2 matrix.", false, LanguageErrorCodes.INVALID_PARAMETERS);

Check warning on line 687 in src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/apache/sysds/parser/BuiltinFunctionExpression.java#L687

Added line #L687 was not covered by tests
}

DataIdentifier img_transfrom_matrix_Out1 = (DataIdentifier) getOutputs()[0];
DataIdentifier img_transfrom_matrix_Out2 = (DataIdentifier) getOutputs()[1];

//describe the matrix characteristics type and value
img_transfrom_matrix_Out1.setDataType(DataType.MATRIX);
img_transfrom_matrix_Out1.setValueType(ValueType.FP64);
img_transfrom_matrix_Out2.setDataType(DataType.MATRIX);
img_transfrom_matrix_Out2.setValueType(ValueType.FP64);
//the output dimensions are not known beforehand and vary based on input values
break;
case REMOVE: {
checkNumParameters(2);
checkListParam(getFirstExpr());
Expand Down Expand Up @@ -1325,7 +1350,6 @@
output.setBlocksize(0);
output.setValueType(ValueType.BOOLEAN);
break;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid this formatting.

// Contingency tables
case TABLE:

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/sysds/parser/DMLTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,7 @@ private Hop processMultipleReturnBuiltinFunctionExpression(BuiltinFunctionExpres
case IFFT:
case FFT_LINEARIZED:
case IFFT_LINEARIZED:
case IMG_TRANSFORM_MATRIX:
case STFT:
case LSTM:
case LSTM_BACKWARD:
Expand Down Expand Up @@ -2490,7 +2491,6 @@ else if ( sop.equalsIgnoreCase("!=") )
new NaryOp(target.getName(), target.getDataType(), target.getValueType(), appendOpN,
processAllExpressions(source.getAllExpr(), hops));
break;

case TABLE:

// Always a TertiaryOp is created for table().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public class CPInstructionParser extends InstructionParser {
String2CPInstructionType.put( "ifft", CPType.MultiReturnComplexMatrixBuiltin);
String2CPInstructionType.put( "fft_linearized", CPType.MultiReturnBuiltin);
String2CPInstructionType.put( "ifft_linearized", CPType.MultiReturnComplexMatrixBuiltin);
String2CPInstructionType.put( "img_transform_matrix", CPType.MultiReturnComplexMatrixBuiltin);
String2CPInstructionType.put( "stft", CPType.MultiReturnComplexMatrixBuiltin);
String2CPInstructionType.put( "svd", CPType.MultiReturnBuiltin);
String2CPInstructionType.put( "rcm", CPType.MultiReturnComplexMatrixBuiltin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ else if(parts.length == 8 && opcode.equalsIgnoreCase("stft")) {

return new MultiReturnComplexMatrixBuiltinCPInstruction(null, in1, in2, windowSize, overlap, outputs, opcode,
str, threads);
// 2 inputs and two outputs
} else if (opcode.equalsIgnoreCase("img_transform_matrix")) {
CPOperand in1 = new CPOperand(parts[1]); //transformation matrix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move comments to line above.

CPOperand in2 = new CPOperand(parts[2]); //dimension matrix [[orig_w, orig_h],[out_w, out_h]]

outputs.add(new CPOperand(parts[3], ValueType.FP64, DataType.MATRIX));
outputs.add(new CPOperand(parts[4], ValueType.FP64, DataType.MATRIX));
int threads = Integer.parseInt(parts[5]);
return new MultiReturnComplexMatrixBuiltinCPInstruction(null, in1, in2, outputs, opcode, str, threads);
}
else if ( opcode.equalsIgnoreCase("rcm") ) {
CPOperand in1 = new CPOperand(parts[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.fft_linearized;
import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.ifft;
import static org.apache.sysds.runtime.matrix.data.LibMatrixFourier.ifft_linearized;
import static org.apache.sysds.runtime.matrix.data.LibMatrixIMGTransform.transformationMatrix;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.math3.exception.MaxCountExceededException;
Expand Down Expand Up @@ -91,6 +93,7 @@ public static boolean isSupportedMultiReturnOperation( String opcode ) {
case "fft_linearized":
case "ifft":
case "ifft_linearized":
case "img_transform_matrix":
case "lu":
case "qr":
case "rcm":
Expand Down Expand Up @@ -169,6 +172,8 @@ public static MatrixBlock[] multiReturnOperations(MatrixBlock in1, MatrixBlock i
return computeIFFT_LINEARIZED(in1, in2, threads);
case "rcm":
return computeRCM(in1, in2);
case "img_transform_matrix":
return transformationMatrix(in1, in2, threads);
default:
return null;
}
Expand Down
Loading
Loading