Commit 04684075 authored by Julian Dierkes's avatar Julian Dierkes

added subimage command

parent 40df1af0
Pipeline #256460 failed with stages
in 11 minutes and 32 seconds
......@@ -54,6 +54,7 @@ public class MathCommandRegisterCPP extends MathCommandRegister {
registerMathCommand(new MathKMeansCommand());
registerMathCommand(new MathSqrtmCommand());
registerMathCommand(new MathSqrtmDiagCommand());
registerMathCommand(new MathSubImageCommand());
//for fixing some errors
registerMathCommand(new MathRowCommand());
......
......@@ -64,10 +64,6 @@ public class MathJoinCubeDimCommand extends MathCommand {
}
}
/**
* Implements a scaleCube function with syntax "scaleCube( CUBE , AXIS , NEW_X , NEW_Y )"
*
*/
private void convertExtendedJoinerImplementationArmadillo(String valueString, MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrint bluePrint) {
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setAccessStartSymbol("");
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setAccessEndSymbol("");
......
......@@ -130,7 +130,7 @@ public class MathScaleCubeCommand extends MathCommand {
" img = arma::reshape(img, img.n_rows, img.n_slices, img.n_cols);\n" +
" }\n" +
" \n" +
" arma::cube r_img = arma::cube(64,64, img.n_slices);\n" +
" arma::cube r_img = arma::cube(new_x, new_y, img.n_slices);\n" +
" for (int i = 0; i < img.n_slices; i++) \n" +
" {\n" +
" arma::mat cur_slice = img.slice(i);\n" +
......
package de.monticore.lang.monticar.generator.cpp.commands;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixAccessSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
public class MathSubImageCommand extends MathCommand{
private static final String SUBIMAGE_SYNTAX_EXTENDED = "subImage(CUBE, ROW_X , COL_X , ROW_Y , COL_Y , DIM )";
private static final String SUBIMAGE_METHOD_NAME = "subImage";
private static int subimageCommandCounter = 0;
public MathSubImageCommand() {
setMathCommandName("subImage");
}
@Override
public void convert(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
String backendName = MathConverter.curBackend.getBackendName();
if (backendName.equals("OctaveBackend")) {
convertUsingOctaveBackend(mathExpressionSymbol, bluePrint);
} else if (backendName.equals("ArmadilloBackend")) {
convertUsingArmadilloBackend(mathExpressionSymbol, bluePrint);
}
}
public void convertUsingOctaveBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
Log.error("Not implemented for Octave Backend");
}
public void convertUsingArmadilloBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) mathExpressionSymbol;
mathMatrixNameExpressionSymbol.setNameToAccess("");
String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, (BluePrintCPP) bluePrint);
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
BluePrintCPP bluePrintCPP = (BluePrintCPP) bluePrint;
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
if (mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().size() == 6) {
convertExtendedJoinerImplementationArmadillo(valueListString, mathMatrixNameExpressionSymbol, bluePrintCPP);
} else {
Log.error(String.format("No implementation found for subImage operation: \"%s\".", mathExpressionSymbol.getTextualRepresentation(), SUBIMAGE_SYNTAX_EXTENDED));
}
}
private void convertExtendedJoinerImplementationArmadillo(String valueString, MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrint bluePrint) {
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setAccessStartSymbol("");
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setAccessEndSymbol("");
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().clear();
// create method
Method calcSubImageMethod = getSubImageCalculationMethod(bluePrint);
// create code string
String code = calcSubImageMethod.getName() + valueString;
MathStringExpression codeExpr = new MathStringExpression(code, mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().add(new MathMatrixAccessSymbol(codeExpr));
// add method to bluePrint
bluePrint.addMethod(calcSubImageMethod);
}
private Method getSubImageCalculationMethod(BluePrint bluePrint) {
// create new method
Method method = getNewEmptyScalerCalculationMethod();
// parameters
Variable q = new Variable();
q.setName("q");
q.setVariableType(new VariableType("Cube", "cube", ""));
Variable row_x = new Variable();
row_x.setName("row_x");
row_x.setVariableType(new VariableType("Integer", "int", ""));
Variable col_x = new Variable();
col_x.setName("col_x");
col_x.setVariableType(new VariableType("Integer", "int", ""));
Variable row_y = new Variable();
row_y.setName("row_y");
row_y.setVariableType(new VariableType("Integer", "int", ""));
Variable col_y = new Variable();
col_y.setName("col_y");
col_y.setVariableType(new VariableType("Integer", "int", ""));
Variable dim = new Variable();
dim.setName("dim");
dim.setVariableType(new VariableType("Integer", "int", ""));
method.addParameter(q);
method.addParameter(row_x);
method.addParameter(col_x);
method.addParameter(row_y);
method.addParameter(col_y);
method.addParameter(dim);
// add instructions
method.addInstruction(methodBody());
return method;
}
private Method getNewEmptyScalerCalculationMethod() {
subimageCommandCounter++;
Method method = new Method();
method.setName(SUBIMAGE_METHOD_NAME + subimageCommandCounter);
method.setReturnTypeName("cube");
return method;
}
private Instruction methodBody() {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
return " if (dim == 0) \n" +
" q = arma::reshape(q, q.n_cols, q.n_slices, q.n_rows);\n" +
" else if(dim == 1) \n" +
" q = arma::reshape(q, q.n_rows, q.n_slices, q.n_cols);\n" +
" \n" +
" q = q.tube(row_x, col_x, row_y, col_y);\n" +
"\n" +
" if (dim == 0) \n" +
" q = arma::reshape(q, q.n_slices, q.n_rows, q.n_cols);\n" +
" else if (dim == 1)\n" +
" q = arma::reshape(q, q.n_rows, q.n_slices, q.n_cols);\n" +
"\n" +
" return q;\n";
}
@Override
public boolean isConnectInstruction() {
return false;
}
};
}
}
......@@ -148,6 +148,11 @@ public class ArmadilloFunctionTest extends AbstractSymtabTest {
testMathCommand("joinCubeDim");
}
@Test
public void SubImageCommand() throws IOException {
testMathCommand("subImage");
}
@Test
public void testDiagCommand() throws IOException {
testMathCommand("diag");
......
......@@ -22,7 +22,7 @@ cube scaleCube1(cube img, int depth_axis, int new_x, int new_y)
img = arma::reshape(img, img.n_rows, img.n_slices, img.n_cols);
}
arma::cube r_img = arma::cube(64,64, img.n_slices);
arma::cube r_img = arma::cube(new_x, new_y, img.n_slices);
for (int i = 0; i < img.n_slices; i++)
{
arma::mat cur_slice = img.slice(i);
......
#ifndef TEST_MATH_SUBIMAGECOMMANDTEST
#define TEST_MATH_SUBIMAGECOMMANDTEST
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
using namespace arma;
class test_math_subImageCommandTest{
public:
cube img_in;
cube img_out;
void init()
{
img_in = cube(1, 28, 28);
img_out = cube(1, 16, 16);
}
cube subImage1(cube q, int row_x, int col_x, int row_y, int col_y, int dim)
{
if (dim == 0)
q = arma::reshape(q, q.n_cols, q.n_slices, q.n_rows);
else if(dim == 1)
q = arma::reshape(q, q.n_rows, q.n_slices, q.n_cols);
q = q.tube(row_x, col_x, row_y, col_y);
if (dim == 0)
q = arma::reshape(q, q.n_slices, q.n_rows, q.n_cols);
else if (dim == 1)
q = arma::reshape(q, q.n_rows, q.n_slices, q.n_cols);
return q;
}
void execute()
{
img_out = subImage1(img_in, 0, 0, 15, 15, 0);
}
};
#endif
/* (c) https://github.com/MontiCore/monticore */
package test.math;
component SubImageCommandTest{
ports in Q(-1:1)^{1,28,28} img_in,
out Q(-1:1)^{1,16,16} img_out;
implementation Math{
img_out = subImage(img_in, 0, 0, 15, 15, 0);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment