Updated convolution and pooling layer templates

parent c995f497
......@@ -165,31 +165,22 @@ public class ArchitectureElementData {
}
@Nullable
public List<Integer> getPadding(){
public Integer getPadding(){
return getPadding((LayerSymbol) getElement());
}
@Nullable
public List<Integer> getPadding(LayerSymbol layer){
List<Integer> kernel = layer.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get();
List<Integer> stride = layer.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get();
ArchTypeSymbol inputType = layer.getInputTypes().get(0);
ArchTypeSymbol outputType = layer.getOutputTypes().get(0);
int heightWithPad = kernel.get(0) + stride.get(0)*(outputType.getHeight() - 1);
int widthWithPad = kernel.get(1) + stride.get(1)*(outputType.getWidth() - 1);
int heightPad = Math.max(0, heightWithPad - inputType.getHeight());
int widthPad = Math.max(0, widthWithPad - inputType.getWidth());
int topPad = (int)Math.ceil(heightPad / 2.0);
int bottomPad = (int)Math.floor(heightPad / 2.0);
int leftPad = (int)Math.ceil(widthPad / 2.0);
int rightPad = (int)Math.floor(widthPad / 2.0);
if (topPad == 0 && bottomPad == 0 && leftPad == 0 && rightPad == 0){
return null;
public Integer getPadding(LayerSymbol layer){
String padding_type = ((LayerSymbol) getElement()).getStringValue(AllPredefinedLayers.PADDING_NAME).get();
Integer pad=0;
if (padding_type.equals(AllPredefinedLayers.PADDING_VALID)){
pad = 0;
}
else if (padding_type.equals(AllPredefinedLayers.PADDING_SAME)){
pad = 1;
}
return Arrays.asList(0,0,0,0,topPad,bottomPad,leftPad,rightPad);
return pad;
}
}
......@@ -3,8 +3,14 @@
<#assign strideWidth = element.stride[1]>
<#assign kernelHeight = element.kernel[0]>
<#assign kernelWidth = element.kernel[1]>
<#if element.padding??> <#-- Check wheather padding null is. -->
<#-- TODO: check how to adapt CNNArchLang argument pad_width=${element.padding[0]} -->
<#if element.padding??>
<#if element.padding == 0>
<#assign padParameter = ""><#--Don't add anything since "valid" is the default padding of Caffe2-->
<#elseif element.padding == 1>
<#assign padParameter = ", pad=1">
</#if>
<#else>
<#assign padParameter = ", pad=1">
</#if>
<#if strideHeight == strideWidth>
<#assign strideParameter = "stride=${strideHeight}">
......@@ -16,6 +22,5 @@
<#else>
<#assign kernelParameter = "kernel=[${kernelHeight},${kernelWidth}]">
</#if>
${element.name} = brew.conv(model, ${input}, '${element.name}', dim_in=${element.element.inputTypes[0].channels?c}, dim_out=${element.channels?c}, ${kernelParameter}, ${strideParameter})
<#-- TODO: check how to adapt CNNArchLang argument no_bias=${element.noBias?string("True","False")} -->
${element.name} = brew.conv(model, ${input}, '${element.name}', dim_in=${element.element.inputTypes[0].channels?c}, dim_out=${element.channels?c}, ${kernelParameter}, ${strideParameter}${padParameter})
<#include "OutputShape.ftl">
\ No newline at end of file
......@@ -4,7 +4,13 @@
<#assign kernelHeight = element.kernel[0]>
<#assign kernelWidth = element.kernel[1]>
<#if element.padding??>
<#-- TODO: check how to adapt CNNArchLang argument pad_width=${element.padding[0]} -->
<#if element.padding == 0>
<#assign padParameter = ""><#--Don't add anything since "valid" is the default padding of Caffe2-->
<#elseif element.padding == 1>
<#assign padParameter = ", pad=1">
</#if>
<#else>
<#assign padParameter = ", pad=1">
</#if>
<#if strideHeight == strideWidth>
<#assign strideParameter = "stride=${strideHeight}">
......@@ -19,6 +25,6 @@
<#if element.poolType == "max">
${element.name} = brew.max_pool(model, ${input}, '${element.name}', ${kernelParameter}, ${strideParameter})
<#elseif element.poolType == "avg">
${element.name} = brew.average_pool(model, ${input}, '${element.name}', ${kernelParameter}, ${strideParameter})
${element.name} = brew.average_pool(model, ${input}, '${element.name}', ${kernelParameter}, ${strideParameter}${padParameter})
</#if>
<#include "OutputShape.ftl">
\ No newline at end of file
......@@ -64,51 +64,51 @@ class CNNCreator_VGG16:
data = data
# data, output shape: {[3,224,224]}
conv1_ = brew.conv(model, data, 'conv1_', dim_in=3, dim_out=64, kernel=3, stride=1)
conv1_ = brew.conv(model, data, 'conv1_', dim_in=3, dim_out=64, kernel=3, stride=1, pad=1)
# conv1_, output shape: {[64,224,224]}
relu1_ = brew.relu(model, conv1_, conv1_)
conv2_ = brew.conv(model, relu1_, 'conv2_', dim_in=64, dim_out=64, kernel=3, stride=1)
conv2_ = brew.conv(model, relu1_, 'conv2_', dim_in=64, dim_out=64, kernel=3, stride=1, pad=1)
# conv2_, output shape: {[64,224,224]}
relu2_ = brew.relu(model, conv2_, conv2_)
pool2_ = brew.max_pool(model, relu2_, 'pool2_', kernel=2, stride=2)
# pool2_, output shape: {[64,112,112]}
conv3_ = brew.conv(model, pool2_, 'conv3_', dim_in=64, dim_out=128, kernel=3, stride=1)
conv3_ = brew.conv(model, pool2_, 'conv3_', dim_in=64, dim_out=128, kernel=3, stride=1, pad=1)
# conv3_, output shape: {[128,112,112]}
relu3_ = brew.relu(model, conv3_, conv3_)
conv4_ = brew.conv(model, relu3_, 'conv4_', dim_in=128, dim_out=128, kernel=3, stride=1)
conv4_ = brew.conv(model, relu3_, 'conv4_', dim_in=128, dim_out=128, kernel=3, stride=1, pad=1)
# conv4_, output shape: {[128,112,112]}
relu4_ = brew.relu(model, conv4_, conv4_)
pool4_ = brew.max_pool(model, relu4_, 'pool4_', kernel=2, stride=2)
# pool4_, output shape: {[128,56,56]}
conv5_ = brew.conv(model, pool4_, 'conv5_', dim_in=128, dim_out=256, kernel=3, stride=1)
conv5_ = brew.conv(model, pool4_, 'conv5_', dim_in=128, dim_out=256, kernel=3, stride=1, pad=1)
# conv5_, output shape: {[256,56,56]}
relu5_ = brew.relu(model, conv5_, conv5_)
conv6_ = brew.conv(model, relu5_, 'conv6_', dim_in=256, dim_out=256, kernel=3, stride=1)
conv6_ = brew.conv(model, relu5_, 'conv6_', dim_in=256, dim_out=256, kernel=3, stride=1, pad=1)
# conv6_, output shape: {[256,56,56]}
relu6_ = brew.relu(model, conv6_, conv6_)
conv7_ = brew.conv(model, relu6_, 'conv7_', dim_in=256, dim_out=256, kernel=3, stride=1)
conv7_ = brew.conv(model, relu6_, 'conv7_', dim_in=256, dim_out=256, kernel=3, stride=1, pad=1)
# conv7_, output shape: {[256,56,56]}
relu7_ = brew.relu(model, conv7_, conv7_)
pool7_ = brew.max_pool(model, relu7_, 'pool7_', kernel=2, stride=2)
# pool7_, output shape: {[256,28,28]}
conv8_ = brew.conv(model, pool7_, 'conv8_', dim_in=256, dim_out=512, kernel=3, stride=1)
conv8_ = brew.conv(model, pool7_, 'conv8_', dim_in=256, dim_out=512, kernel=3, stride=1, pad=1)
# conv8_, output shape: {[512,28,28]}
relu8_ = brew.relu(model, conv8_, conv8_)
conv9_ = brew.conv(model, relu8_, 'conv9_', dim_in=512, dim_out=512, kernel=3, stride=1)
conv9_ = brew.conv(model, relu8_, 'conv9_', dim_in=512, dim_out=512, kernel=3, stride=1, pad=1)
# conv9_, output shape: {[512,28,28]}
relu9_ = brew.relu(model, conv9_, conv9_)
conv10_ = brew.conv(model, relu9_, 'conv10_', dim_in=512, dim_out=512, kernel=3, stride=1)
conv10_ = brew.conv(model, relu9_, 'conv10_', dim_in=512, dim_out=512, kernel=3, stride=1, pad=1)
# conv10_, output shape: {[512,28,28]}
relu10_ = brew.relu(model, conv10_, conv10_)
pool10_ = brew.max_pool(model, relu10_, 'pool10_', kernel=2, stride=2)
# pool10_, output shape: {[512,14,14]}
conv11_ = brew.conv(model, pool10_, 'conv11_', dim_in=512, dim_out=512, kernel=3, stride=1)
conv11_ = brew.conv(model, pool10_, 'conv11_', dim_in=512, dim_out=512, kernel=3, stride=1, pad=1)
# conv11_, output shape: {[512,14,14]}
relu11_ = brew.relu(model, conv11_, conv11_)
conv12_ = brew.conv(model, relu11_, 'conv12_', dim_in=512, dim_out=512, kernel=3, stride=1)
conv12_ = brew.conv(model, relu11_, 'conv12_', dim_in=512, dim_out=512, kernel=3, stride=1, pad=1)
# conv12_, output shape: {[512,14,14]}
relu12_ = brew.relu(model, conv12_, conv12_)
conv13_ = brew.conv(model, relu12_, 'conv13_', dim_in=512, dim_out=512, kernel=3, stride=1)
conv13_ = brew.conv(model, relu12_, 'conv13_', dim_in=512, dim_out=512, kernel=3, stride=1, pad=1)
# conv13_, output shape: {[512,14,14]}
relu13_ = brew.relu(model, conv13_, conv13_)
pool13_ = brew.max_pool(model, relu13_, 'pool13_', kernel=2, stride=2)
......
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