from keras.layers import Input, Lambda, Dense, Dropout
from keras.regularizers import l2
from keras.models import Model

def add_regularizer(model, kernel_regularizer = l2(), bias_regularizer = l2()):
  for layer in model.layers:
    if hasattr(layer, "kernel_regularizer"):
      layer.kernel_regularizer = kernel_regularizer
    if hasattr(layer, "bias_regularizer"):
      layer.bias_regularizer = bias_regularizer

def create_model(FLAGS):
  input_a = Input(shape = (224, 224, 3))
  input_b = Input(shape = (224, 224, 3))

  if FLAGS.model == "vgg16":
    from keras.applications.vgg16 import VGG16
    feature_extractor = VGG16()
  elif FLAGS.model == "vgg19":
    from keras.applications.vgg19 import VGG19
    feature_extractor = VGG19()
  elif FLAGS.model == "resnet50":
    from keras.applications.resnet50 import ResNet50
    feature_extractor = ResNet50()
  else:
    raise NotImplementedError

  feature_extractor.layers.pop()
  add_regularizer(feature_extractor)

  feature_a = feature_extractor(input_a)
  feature_b = feature_extractor(input_b)
  delta = Lambda(lambda x: x[0] - x[1], output_shape = lambda s: s[0])([feature_a, feature_b])

  hidden1 = Dropout(0.5)(Dense(128, activation = "tanh")(delta))
  hidden2 = Dense(128, activation = "tanh")(hidden1)
  output = Dense(2, activation = "softmax")(hidden2)

  model = Model([input_a, input_b], output)
  add_regularizer(model)
  return model
@@ -114,16 +114,268 @@
  Model%0A%0A
+def remove_last_layer(model):%0A  model.layers.pop()%0A  if not model.layers:%0A    model.outputs = %5B%5D%0A    model.inbound_nodes = %5B%5D%0A    model.outbound_nodes = %5B%5D%0A  else:%0A    model.layers%5B-1%5D.outbound_nodes = %5B%5D%0A    model.outputs = %5Bmodel.layers%5B-1%5D.output%5D%0A%0A
 def add_
@@ -1149,24 +1149,42 @@
 tedError%0A%0A  
+remove_last_layer(
 feature_extr
@@ -1188,28 +1188,16 @@
 xtractor
-.layers.pop(
 )%0A  add_

fix bug: remove last layer