tensorflow - Removing then Inserting a New Middle Layer in a Keras Model -
given predefined keras model, trying first load in pre-trained weights, remove 1 3 of models internal (non-last few) layers, , replace layer.
i can't seem find documentation on keras.io such thing or remove layers predefined model @ all.
the model using ole vgg-16 network instantiated in function shown below:
def model(self, output_shape): # prepare image input model img_input = input(shape=self._input_shape) # block 1 x = conv2d(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = conv2d(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = maxpooling2d((2, 2), strides=(2, 2), name='block1_pool')(x) # block 2 x = conv2d(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = conv2d(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = maxpooling2d((2, 2), strides=(2, 2), name='block2_pool')(x) # block 3 x = conv2d(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = conv2d(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = conv2d(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = maxpooling2d((2, 2), strides=(2, 2), name='block3_pool')(x) # block 4 x = conv2d(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = conv2d(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = conv2d(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = maxpooling2d((2, 2), strides=(2, 2), name='block4_pool')(x) # block 5 x = conv2d(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = conv2d(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = conv2d(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = maxpooling2d((2, 2), strides=(2, 2), name='block5_pool')(x) # classification block x = flatten(name='flatten')(x) x = dense(4096, activation='relu', name='fc1')(x) x = dropout(0.5)(x) x = dense(4096, activation='relu', name='fc2')(x) x = dropout(0.5)(x) x = dense(output_shape, activation='softmax', name='predictions')(x) inputs = img_input # create model. model = model(inputs, x, name=self._name) return model
so example, i'd take 2 conv layers in block 1 , replace them 1 conv layer, after loading original weights of other layers.
any ideas?
assuming have model vgg16_model
, initialized either function above or keras.applications.vgg16(weights='imagenet')
. now, need insert new layer in middle in such way weights of other layers saved.
the idea disassemble whole network separate layers, assemble back. here code task:
vgg_model = applications.vgg16(include_top=true, weights='imagenet') # disassemble layers layers = [l l in vgg_model.layers] # defining new convolutional layer. # important: number of filters should same! # note: receiptive field of 2 3x3 convolutions 5x5. new_conv = conv2d(filters=64, kernel_size=(5, 5), name='new_conv', padding='same')(layers[0].output) # stack # note: if going fine tune model, not forget # mark other layers un-trainable x = new_conv in range(3, len(layers)): layers[i].trainable = false x = layers[i](x) # final touch result_model = model(input=layer[0].input, output=x) result_model.summary()
and output of above code is:
_________________________________________________________________ layer (type) output shape param # ================================================================= input_50 (inputlayer) (none, 224, 224, 3) 0 _________________________________________________________________ new_conv (conv2d) (none, 224, 224, 64) 1792 _________________________________________________________________ block1_pool (maxpooling2d) (none, 112, 112, 64) 0 _________________________________________________________________ block2_conv1 (conv2d) (none, 112, 112, 128) 73856 _________________________________________________________________ block2_conv2 (conv2d) (none, 112, 112, 128) 147584 _________________________________________________________________ block2_pool (maxpooling2d) (none, 56, 56, 128) 0 _________________________________________________________________ block3_conv1 (conv2d) (none, 56, 56, 256) 295168 _________________________________________________________________ block3_conv2 (conv2d) (none, 56, 56, 256) 590080 _________________________________________________________________ block3_conv3 (conv2d) (none, 56, 56, 256) 590080 _________________________________________________________________ block3_pool (maxpooling2d) (none, 28, 28, 256) 0 _________________________________________________________________ block4_conv1 (conv2d) (none, 28, 28, 512) 1180160 _________________________________________________________________ block4_conv2 (conv2d) (none, 28, 28, 512) 2359808 _________________________________________________________________ block4_conv3 (conv2d) (none, 28, 28, 512) 2359808 _________________________________________________________________ block4_pool (maxpooling2d) (none, 14, 14, 512) 0 _________________________________________________________________ block5_conv1 (conv2d) (none, 14, 14, 512) 2359808 _________________________________________________________________ block5_conv2 (conv2d) (none, 14, 14, 512) 2359808 _________________________________________________________________ block5_conv3 (conv2d) (none, 14, 14, 512) 2359808 _________________________________________________________________ block5_pool (maxpooling2d) (none, 7, 7, 512) 0 _________________________________________________________________ flatten (flatten) (none, 25088) 0 _________________________________________________________________ fc1 (dense) (none, 4096) 102764544 _________________________________________________________________ fc2 (dense) (none, 4096) 16781312 _________________________________________________________________ predictions (dense) (none, 1000) 4097000 ================================================================= total params: 138,320,616 trainable params: 1,792 non-trainable params: 138,318,824 _________________________________________________________________
Comments
Post a Comment