Skip to content

Instantly share code, notes, and snippets.

@ResidentMario
Created March 30, 2019 00:39
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/9fd06cb6683754b2afa1151dca9e0c39 to your computer and use it in GitHub Desktop.
Save ResidentMario/9fd06cb6683754b2afa1151dca9e0c39 to your computer and use it in GitHub Desktop.
# define the model
# init a new model with 192x192 CNN layers
# padding='same' will downsample to 96x96
# # which is the expected input size for pretrain
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(192, 192, 3), activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# load the pretrained model
prior = load_model('resnet96_128/model-96.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 96x96 input and convolved it but
# this is now the job of the three new layers.
for layer in prior.layers[1:]:
layer.name += '_prior' # set layer names to avoid name collisions
model.add(layer)
# the pretrained CNN layers are already marked non-trainable
# mark off the top layers as well
for layer in model.layers[-4:]:
layer.trainable = False
# set layer names (otherwise names may collide)
for i, layer in enumerate(model.layers):
layer.name = 'layer_' + str(i)
# compile the model
model.compile(
optimizer=RMSprop(),
loss='categorical_crossentropy',
metrics=['accuracy']
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment