0

The keras API of tensorflow is convenient to build a CNN model.

the tf.keras.layers.Conv2D only provide 2 padding method, one is 'same' that pad zeros to the border of tensor to make sure the resolutions of input and output of convolution layer may be the same. Another is 'valid' that has no padding so the resolution of tensor may be decrease after convolution.

But I got a problem when I load a pre-train VGG19 model and want to change the padding method of convolution layer from padding zeros ('same' method of tf.keras.layers.Conv2D ) to reflecting.

The only way I thought out is to use tf.pad() method before input the VGG conv-layer, and crop the output tensor. This method need to customize the data flow in each layer and will be a trifling work.

For example, I create an VGG19 net, and I can simply call it to process my tensor:

import tensorflow as tf
from tensorflow.keras.applications import VGG19
import numpy as np

# create vgg net.
vggnet=VGG19(include_top=False, weights='imagenet')
# create input tensor.
inputdata=np.random.normal(size=[1,512,512,3])
# simply call it to process.
outputdata=vggnet(inputdata)

But if we want to add custom padding layer before convolution, we need to re-define the call() function in vggnet, that will be extract every layers in VGG and define the data flow for every layer.

It's there any better method can achieve that? Thanks.

ZhiYang
  • 26
  • 2
  • Does this answer your question? [Reflection padding Conv2D](https://stackoverflow.com/questions/50677544/reflection-padding-conv2d) – Vivek Mehta Feb 12 '20 at 09:00
  • @VivekMehta the problem is I still need to re-define the data flow just like the method I thought out. But still thanks for mention me! – ZhiYang Feb 12 '20 at 09:45

0 Answers0