Commit c873a881 authored by Julian Dierkes's avatar Julian Dierkes
Browse files

changed transConv to UpConv

parent 930d3130
Pipeline #264722 failed with stages
......@@ -173,32 +173,17 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
}
//output type function for transposed convolution
protected static List<ArchTypeSymbol> computeTransConvOutputShape(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
protected static List<ArchTypeSymbol> computeUpConvOutputShape(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
String borderModeSetting = method.getStringValue(AllPredefinedLayers.TRANSPADDING_NAME).get();
if (borderModeSetting.equals(AllPredefinedLayers.PADDING_SAME)) {
return computeTransConvOutputShapeWithSamePadding(inputType, method, channels);
return computeUpConvOutputShapeWithSamePadding(inputType, method, channels);
} else if (borderModeSetting.equals(AllPredefinedLayers.PADDING_VALID)) {
return computeTransConvOutputShapeWithValidPadding(inputType, method, channels);
return computeUpConvOutputShapeWithValidPadding(inputType, method, channels);
} else {
throw new IllegalStateException("border_mode is " + borderModeSetting + ". This should never happen.");
}
}
//output type function for upconvolution
protected static List<ArchTypeSymbol> computeUpConvOutputShape(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
String borderModeSetting = method.getStringValue(AllPredefinedLayers.PADDING_NAME).get();
if (borderModeSetting.equals(AllPredefinedLayers.PADDING_SAME)) {
return computeOutputUpConvShapeWithSamePadding(inputType, method, channels);
} else if (borderModeSetting.equals(AllPredefinedLayers.PADDING_VALID)) {
return computeOutputUpConvShapeWithValidPadding(inputType, method, channels);
} else if (borderModeSetting.equals(AllPredefinedLayers.PADDING_NO_LOSS)) {
return computeOutputUpConvShapeWithNoLossPadding(inputType, method, channels);
} else {
throw new IllegalStateException("border_mode is " + borderModeSetting + ". This should never happen.");
}
}
protected static void computeOneHotOutputSize(LayerSymbol layer) {
int outputChannels = 0;
......@@ -237,7 +222,7 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
}
//padding with border_mode=valid, no padding
private static List<ArchTypeSymbol> computeTransConvOutputShapeWithValidPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
private static List<ArchTypeSymbol> computeUpConvOutputShapeWithValidPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int kernelHeight = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(0);
......@@ -297,30 +282,8 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
.build());
}
private static List<ArchTypeSymbol> computeOutputUpConvShapeWithValidPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int kernelHeight = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(0);
int kernelWidth = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(1);
int inputHeight = inputType.getHeight();
int inputWidth = inputType.getWidth();
int outputWidth;
int outputHeight;
outputWidth = (inputWidth - 1) * strideWidth + kernelWidth;
outputHeight = (inputHeight - 1) * strideHeight + kernelHeight;
return Collections.singletonList(new ArchTypeSymbol.Builder()
.height(outputHeight)
.width(outputWidth)
.channels(channels)
.elementType("-oo", "oo")
.build());
}
//padding with border_mode='same'
private static List<ArchTypeSymbol> computeTransConvOutputShapeWithSamePadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
private static List<ArchTypeSymbol> computeUpConvOutputShapeWithSamePadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int inputHeight = inputType.getHeight();
......@@ -337,46 +300,6 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
.build());
}
private static List<ArchTypeSymbol> computeOutputUpConvShapeWithNoLossPadding (ArchTypeSymbol
inputType, LayerSymbol method,int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int kernelHeight = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(0);
int kernelWidth = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(1);
int inputHeight = inputType.getHeight();
int inputWidth = inputType.getWidth();
int outputWidth = Math.max(0, ((inputWidth - 1) * strideWidth - strideWidth + kernelWidth + 1));
int outputHeight = Math.max(0, ((inputHeight - 1) * strideHeight - strideHeight + kernelHeight + 1));
return Collections.singletonList(new ArchTypeSymbol.Builder()
.height(outputHeight)
.width(outputWidth)
.channels(channels)
.elementType("-oo", "oo")
.build());
}
private static List<ArchTypeSymbol> computeOutputUpConvShapeWithSamePadding (ArchTypeSymbol
inputType, LayerSymbol method,int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int inputHeight = inputType.getHeight();
int inputWidth = inputType.getWidth();
//no -strideWidth+1 at end as sugested by rearanging the corresponding formula for convolution. Tensorflow calculates it like this.
int outputWidth = inputWidth * strideWidth;
int outputHeight = inputHeight * strideHeight;
return Collections.singletonList(new ArchTypeSymbol.Builder()
.height(outputHeight)
.width(outputWidth)
.channels(channels)
.elementType("-oo", "oo")
.build());
}
protected List<String> computeStartAndEndValue
(List < ArchTypeSymbol > inputTypes, BinaryOperator < Rational > startValAccumulator, BinaryOperator < Rational > endValAccumulator)
{
......
......@@ -19,7 +19,6 @@ public class AllPredefinedLayers {
//predefined layer names
public static final String FULLY_CONNECTED_NAME = "FullyConnected";
public static final String CONVOLUTION_NAME = "Convolution";
public static final String TRANS_CONV_NAME = "TransConv";
public static final String UP_CONVOLUTION_NAME = "UpConvolution";
public static final String SOFTMAX_NAME = "Softmax";
public static final String SIGMOID_NAME = "Sigmoid";
......@@ -102,7 +101,6 @@ public class AllPredefinedLayers {
return Arrays.asList(
FullyConnected.create(),
Convolution.create(),
TransConv.create(),
UpConvolution.create(),
Softmax.create(),
Sigmoid.create(),
......
/**
*
* (c) https://github.com/MontiCore/monticore
*
* The license generally applicable for this project
* can be found under https://github.com/MontiCore/monticore.
*/
package de.monticore.lang.monticar.cnnarch.predefined;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TransConv extends PredefinedLayerDeclaration {
private TransConv() {
super(AllPredefinedLayers.TRANS_CONV_NAME);
}
@Override
public List<ArchTypeSymbol> computeOutputTypes(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
return computeTransConvOutputShape(layer.getInputTypes().get(0),
layer,
layer.getIntValue(AllPredefinedLayers.CHANNELS_NAME).get());
}
@Override
public void checkInput(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
errorIfInputSizeIsNotOne(inputTypes, layer);
}
public static TransConv create(){
TransConv declaration = new TransConv();
List<ParameterSymbol> parameters = new ArrayList<>(Arrays.asList(
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.KERNEL_NAME)
.constraints(Constraints.INTEGER_TUPLE, Constraints.POSITIVE)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.CHANNELS_NAME)
.constraints(Constraints.INTEGER, Constraints.POSITIVE)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.STRIDE_NAME)
.constraints(Constraints.INTEGER_TUPLE, Constraints.POSITIVE)
.defaultValue(Arrays.asList(1, 1))
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.NOBIAS_NAME)
.constraints(Constraints.BOOLEAN)
.defaultValue(false)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.TRANSPADDING_NAME)
.constraints(Constraints.TRANSPADDING_TYPE)
.defaultValue(AllPredefinedLayers.PADDING_SAME)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.GROUPS_NAME)
.constraints(Constraints.INTEGER, Constraints.POSITIVE)
.defaultValue(1)
.build()));
declaration.setParameters(parameters);
return declaration;
}
}
......@@ -53,9 +53,14 @@ public class UpConvolution extends PredefinedLayerDeclaration {
.defaultValue(false)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.PADDING_NAME)
.constraints(Constraints.PADDING_TYPE)
.name(AllPredefinedLayers.TRANSPADDING_NAME)
.constraints(Constraints.TRANSPADDING_TYPE)
.defaultValue(AllPredefinedLayers.PADDING_SAME)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.GROUPS_NAME)
.constraints(Constraints.INTEGER, Constraints.POSITIVE)
.defaultValue(1)
.build()));
declaration.setParameters(parameters);
return declaration;
......
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