Transfer learning with Eulidean loss in the final

2019-08-21 23:14发布

Greatly appreciate it if someone could help me out here:

I'm trying to do some transfer learning on a regression task --- my inputs are 200X200 RGB images and my prediction output/label is a set of real values (let's say, within [0,10], though scaling is not a big deal here...?) --- on top of InceptionV3 architecture. Here are my functions that take a pretrained Inception model, remove the last layer and add a new layer for transfer learning...

"""
Transfer learning functions
"""
IM_WIDTH, IM_HEIGHT = 299, 299 #fixed size for InceptionV3
NB_EPOCHS = 3
BAT_SIZE = 32
FC_SIZE = 1024
NB_IV3_LAYERS_TO_FREEZE = 172

def eucl_dist(inputs):
    x, y = inputs
    return ((x - y)**2).sum(axis=-1)

def add_new_last_continuous_layer(base_model):
  """Add last layer to the convnet
  Args:
    base_model: keras model excluding top, for instance:
    base_model = InceptionV3(weights='imagenet',include_top=False)
  Returns:
    new keras model with last layer
  """
  x = base_model.output
  x = GlobalAveragePooling2D()(x)
  x = Dense(FC_SIZE, activation='relu')(x) 
  predictions = Lambda(eucl_dist, output_shape=(1,))(x)
  model = Model(input=base_model.input, output=predictions)
  return model

def setup_to_transfer_learn_continuous(model, base_model):
  """Freeze all layers and compile the model"""
  for layer in base_model.layers:
    layer.trainable = False
  model.compile(optimizer='rmsprop',    
            loss= 'eucl_dist',
            metrics=['accuracy'])

Here are my implementations:

base_model = InceptionV3(weights = "imagenet", 
include_top=False, input_shape=(3,200,200))
model0 = add_new_last_continuous_layer(base_model)

setup_to_transfer_learn_continuous(model0, base_model)

history=model0.fit(train_x, train_y, validation_data = (test_x, test_y), nb_epoch=epochs, batch_size=32)
scores = model0.evaluate(test_x, test_y, verbose = 0)
features = model0.predict(X_train)

where train_x is a (168435, 3, 200, 200) numpy array and train_y is a (168435,) numpy array. The same goes for test_x and test_y except the number of observations is 42509.

I got the TypeError: Tensor object is not iterable bug which occurred at predictions = Lambda(eucl_dist, output_shape=(1,))(x)'' when going through theadd_new_last_continuous_layer()`` function. Could you anyone kindly give me some guidance to get around that and what the problem is? Greatly appreciated and happy holidays!

0条回答
登录 后发表回答