Changing default padding of a keras.application model - python

I'm building a U-net using tensorflow on top of a keras.application model as encoder. However, I'd like to change the padding of some layers from 'valid' to 'same'. How can I achieve that without instancing the entire network?
Example:
encoder = tf.contrib.keras.applications.Xception(
include_top=False, weights='imagenet',
input_tensor=inputs, pooling=None)
encoder.layers[1].padding = 'same' # this does not work, does not update the tensor

The easier way I've found was to change the original Keras file and introduce padding= 'same' in the convolutions arguments during the model instantiation.

Related

Why is the fully connected layer + category outputs missing from tensorflow's pre-trained mobilenet v2?

I'm trying to use tensorflow's MobileNet v2.
I don't understand why, but it seems that the last fully connected layers, with the output categories (dimensionality 1000) layer is missing and I'm left with what seems to be just the embeddings after some convolutional layer.
Any idea on why this is happening? How can I add, or where can I find the pre-trained fully connected layers block?
Here is the code:
image = np.array(PIL.Image.open("amsterdam.jpg"))
image = np.expand_dims(image,0)
IMG_SIZE = image.shape[1:3]
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
tf.keras.utils.plot_model(base_model,to_file='model.png', show_shapes=True)
Here you can see the structure of the neural network as I plotted it with tf.keras.utils.plot_model:
Any idea on how to fix this?
include_top=False: return the model without dense layers for classification. You can add your own dense layers.
include_top=True: return the entire model.
If you want to get also dense layers for classification, use include_top=True as the default is. When you set include_top=False the model will not return the dense layers, in order to let you make your own dense layers and make your own classification to suit your needs.

How many features is VGG16 supposed to extract when used as a pre-trained feature extractor?

I'm using Keras with the TensorFlow backend to extract features from images with a pre-trained model (VGG16 on ImageNet). From what I can read online, I should get for each image a vector with 4096 features.
I'm using this line to import the model without the last fully connected layer (as I believe I'm supposed to):
applications.vgg16.VGG16(weights='imagenet', include_top=False, pooling='avg'
However, the vector I get in the end only has 512 features. Considering VGG16's architecture:
It looks like I'm actually getting the results from the last max pooling layer (which would be consistent with the Keras documentation).
So am I supposed to get 512 or 4096 features?
According to the Keras documentation when you set include_top = False it overlooks the last 3 Fully Connected(FC) layers so intuitively you should be getting a 512 feature vector which is correct. If you wish to consider the last 3 FC layers set include_top = True. Then you would get a 1000 feature prediction (considering the softmax layer at the end).
Try executing:
vggmodel = keras.applications.vgg16.VGG16(weights='imagenet', include_top=False, pooling='avg')
vggmodel.summary()
and
vggmodel = keras.applications.vgg16.VGG16(weights='imagenet', include_top=True, pooling='avg')
vggmodel.summary()
to get a more comprehensive understanding.

How to remove (pop) initial layers of Keras InceptionV3 pre-trained model?

I am trying to use pre-trained InceptionV3 model. However, I want to remove initial five layers and add my custom layers. How can I do that? I tried model.layers.pop(0), but that alone will not solve the problem.
Edit:
tf.keras does not help either as mentioned in the first answer:
model.layers.pop() doesn't work in the same way in tf.keras as it doesn in Keras. In tf.keras, model.layers is a view of the model. You can't remove the layers but what you can do is define the layer for which you want the output. For example,
base_model = InceptionV3(shape=shape, weights="imagenet", include_top=True)
# you don't want the last five layers:
base_model_output = base_model.layers[-6].output
# new layers
outputs = Dense(....)(base_model_output)
model = Model(base_model.input, outputs)
Since the first few layers starting from the input are changed, then the pretrained weights cannot be used. So, the architecture can be directly taken from here and modified accordingly instead of trying complex surgeries.
https://github.com/keras-team/keras-applications/blob/master/keras_applications/inception_v3.py

Where is the kernel weight initialization in my CNN model?

I am using a convolutional neural net (CNN) called make_unet from here. It works and code is able to run with this CNN. But I know that in deep learning you have to initialize weights for optimization of the neural network.
The documentation in Keras clearly indicates the use of a kernel_initializer for weight initialization. However, I do not see any kernel_initializer in the make_unet function I am using.
Anyone who can provide some insight would be appreciated.
In Keras initialisers are passed on a per-layer basis via arguments kernel_initializer and bias_initializer, e.g.
Dense(64, kernel_initializer='random_uniform', bias_initializer='zeros')
All built-in layers come with a sensible default initialiser. For example, all convolutional layers use kernel_initializer='glorot_uniform', bias_initializer='zeros'. Keras gives you many alternative options. You can also create your custom initialisers.

Keras: Is there any way to "pop()" the top layers?

In Keras there is a feature called pop() that lets you remove the bottom layer of a model. Is there any way to remove the top layer of a model?
I have a fully saved pre-trained Variational Autoencoder and am trying to only load the decoder (the bottom four layers).
I am using Keras with a Tensorflow backend.
Keras pop() removes the last (aka top) layer, not the bottom one.
I suggest you use model.summary() to print out the list of layers and than subsequently use pop() until only the necessary layers are left.
pop(0) works for me
from keras.applications import vgg16
vgg = vgg16.VGG16(include_top=False, input_shape=(604,604,3))
vgg.summary()
vgg.layers.pop(0)
vgg.summary()
vgg.layers.pop()
vgg.summary()

Categories

Resources