Skip to content

Commit

Permalink
- named the layers to allow weights loading by name
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmic committed Aug 29, 2019
1 parent 2b5ca3a commit caa5663
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions few_shot_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
parser.add_argument('--shots', dest='shots', type=int, default=5)
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--set_model_img_to_weights', dest='set_model_img_to_weights', action='store_true')
parser.add_argument('--load_weights_name', dest='load_weights_name', type=str, default=None)

args = parser.parse_args()

Expand Down Expand Up @@ -261,6 +262,7 @@ def get_FindModel(model):
class BiasLayer(Layer):

def __init__(self, proto_num, do_bias, bias_num, **kwargs):
#bias num allows to identify the correct bias layer but allows to change the name for weights loading
self.proto_num = proto_num
self.do_bias = do_bias
self.bias_num = bias_num
Expand Down Expand Up @@ -306,16 +308,16 @@ def get_config(self):

inputs = Input(shape=(None,84,84,3))
printdeb('the shape', inputs.shape)
conv1 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu'))(inputs)
pool1 = TimeDistributed(MaxPooling2D(pool_size = 2))(conv1)
conv2 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu'))(pool1)
pool2 = TimeDistributed(MaxPooling2D(pool_size = 2))(conv2)
conv3 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu'))(pool2)
pool3 = TimeDistributed(MaxPooling2D(pool_size = 2))(conv3)
conv4 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu'))(pool3)
pool4 = TimeDistributed(MaxPooling2D(pool_size = 2))(conv4)
conv5 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu'))(pool4)
pool5 = TimeDistributed(MaxPooling2D(pool_size = 2))(conv5)
conv1 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu', name = 'conv_1'))(inputs)
pool1 = TimeDistributed(MaxPooling2D(pool_size = 2, name = 'pool_1'))(conv1)
conv2 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu', name = 'conv_2'))(pool1)
pool2 = TimeDistributed(MaxPooling2D(pool_size = 2, name = 'pool_2'))(conv2)
conv3 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu', name = 'conv_3'))(pool2)
pool3 = TimeDistributed(MaxPooling2D(pool_size = 2, name = 'pool_3'))(conv3)
conv4 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu', name = 'conv_4'))(pool3)
pool4 = TimeDistributed(MaxPooling2D(pool_size = 2, name = 'pool_4'))(conv4)
conv5 = TimeDistributed(Conv2D(args.hidden_size, 3, padding='same', activation = 'relu', name = 'conv_5'))(pool4)
pool5 = TimeDistributed(MaxPooling2D(pool_size = 2, name = 'pool_5'))(conv5)

flat = TimeDistributed(Flatten())(pool5)
#x = TimeDistributed(Dense(100, activation = 'relu'))(flat)
Expand All @@ -330,17 +332,17 @@ def get_config(self):
input1 = Input(shape=(None,84,84,3))
input2 = Input(shape=(None,84,84,3)) #, tensor = K.variable(episode_train_img[0:0]))

input2b = BiasLayer(shots * cathegories, args.biaslayer1, 1)(input2)
input2b = BiasLayer(shots * cathegories, args.biaslayer1, bias_num = 1, name = 'bias1')(input2)
encoded_l = model_img(input1)
encoded_r = model_img(input2b)

encoded_rb = BiasLayer(shots * cathegories, args.biaslayer2, 2)(encoded_r)
encoded_rb = BiasLayer(shots * cathegories, args.biaslayer2, bias_num = 2, name = 'bias2')(encoded_r)
# Add a customized layer to compute the absolute difference between the encodings
L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))
L1_distance = L1_layer([encoded_l, encoded_rb])

# Add a dense layer with a sigmoid unit to generate the similarity score
prediction = Dense(1)(L1_distance)
prediction = Dense(1, name = 'dense_siamese')(L1_distance)

# Connect the inputs with the outputs
siamese_net = Model(inputs=[input1,input2],outputs=prediction)
Expand Down Expand Up @@ -372,6 +374,10 @@ def call(x):
lambda_model = load_model(args.pretrained_name, custom_objects = { "keras": tensorflow.keras , "args":args, "BiasLayer": BiasLayer, "FindModel": FindModel})
print("loaded model",lambda_model)

if args.load_weights_name:
lambda_model.load_weights(args.load_weights_name, by_name=True)
print('weights loaded')

# models in models forget the layer name, therefore one must use the automatically given layer name and iterate throught the models by hand
# here we can try setting the layer not trainable
def all_layers(model):
Expand Down Expand Up @@ -503,7 +509,9 @@ def print_FindModels(model):

for l in range(len(lambda_model_layers)):
lambda_model_layers[l].trainable = True

lambda_model.save(args.final_name+'.hdf5')
lambda_model.save_weights(args.final_name+'-weights.hdf5')

# tools for debugging
def get_weight_grad(model, inputs, outputs):
Expand Down

0 comments on commit caa5663

Please sign in to comment.