Skip to content

Instantly share code, notes, and snippets.

@ResidentMario
Created March 30, 2019 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ResidentMario/52bb3b3fc03ff49e0521cae23475a899 to your computer and use it in GitHub Desktop.
Save ResidentMario/52bb3b3fc03ff49e0521cae23475a899 to your computer and use it in GitHub Desktop.
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(96, 96, 3), activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(96, 96, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# load the pretrained model
prior = load_model('resnet48_128/model-48.h5')
# add all but the first two layers of VGG16 to the new model
# strip the input layer out, this is now 96x96
# also strip out the first convolutional layer, this took the 48x48 input and convolved it but
# this is now the job of the three new layers.
for layer in prior.layers[0].layers[2:]:
model.add(layer)
# re-add the feedforward layers on top
for layer in prior.layers[1:]:
model.add(layer)
# the pretrained CNN layers are already marked non-trainable
# mark off the top layers as well
for layer in prior.layers[-4:]:
layer.trainable = False
# compile the model
model.compile(
optimizer=RMSprop(),
loss='categorical_crossentropy',
metrics=['accuracy']
)
@Tusharjoshi94
Copy link

for layer in prior.layers[0].layers[2:]:
    model.add(layer)

Can you please explain this piece of code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment