0

I am trying to take a particular convolution layer of VGG 19 and trying to replace it with 3 different convolutional layers such that the first conv layer of the 3 layers will satisfy the input conditions of previous layer and the last conv layer produce exactly the same output as that of original convolution layer.

from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg19 import preprocess_input
from tensorflow.keras.models import Model
import numpy as np

model_old = VGG19(weights='imagenet')

I have named the VGG model as 'model_old'.

Here is the function for a 3 Conv layer:


import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten

def multiple_conv_layer(layer_id):
    
    model = Sequential()

    model.add(Conv2D(3, kernel_size=1,input_shape=(28,28,3), strides = (1,1), \
                 padding = 'same',dilation_rate = (1,1), activation = 'relu', use_bias = False))
    model.add(Conv2D(8, kernel_size=3, activation = 'relu', \
                                        use_bias = False))
    model.add(Conv2D(64, kernel_size=1, strides = (1,1), \
                                        padding = 'same', dilation_rate = a.dilation_rate, use_bias = False))
    return(model)

When I tried doing like model_old.layers[1] = multiple_conv_layer( 1 ) the layer was not getting replaced by the new sequential model. Any leads on how to do this?

  • you are doing transfer learning and also wanted to put layers inside a model already trained. not a good idea! but if you're sure, you can do it by TF functional API. – SoheilStar May 09 '21 at 03:01
  • Did you tried solutions that discussed [Keras replacing input layer](https://stackoverflow.com/questions/49546922/keras-replacing-input-layer) may resolve your issue? –  May 20 '21 at 16:33

0 Answers0