Change specific layer fprop behavior
Detect abstract layers and set their last output node as default.
@@ -124,6 +124,19 @@ class KerasModelWrapper(Model):
raise Exception("No softmax layers found")
+ def _get_abstract_layer_name(self):
+ """
+ Looks for the name of abstracted layer.
+ Usually these layers appears when model is stacked.
+ :return: List of abstracted layers
+ """
+ abstract_layers = []
+ for layer in self.model.layers:
+ if 'layers' in layer.get_config():
+ abstract_layers.append(layer.name)
+
+ return abstract_layers
+
def _get_logits_name(self):
"""
Looks for the name of the layer producing the logits.
@@ -192,13 +205,20 @@ class KerasModelWrapper(Model):
new_input = self.model.get_input_at(0)
# Make a new model that returns each of the layers as output
- outnodes_last = [x_layer.get_output_at(-1) for x_layer in self.model.layers]
- outnodes_first = [x_layer.get_output_at(0) for x_layer in self.model.layers]
- if outnodes_last != outnodes_first:
- warnings.warn("Mutiple output nodes detected, picking last ones as default."
+ abstract_layers = self._get_abstract_layer_name()
+ if abstract_layers:
+ warnings.warn("Abstract layer detected, picking last ouput node as default."
"This could happen due to using of stacked model.")
- self.keras_model = KerasModel(new_input, outnodes_last)
+ layer_outputs = []
+ # For those abstract model layers, return their last output node as default.
+ for x_layer in self.model.layers:
+ if x_layer.name not in abstract_layers:
+ layer_outputs.append(x_layer.output)
+ else:
+ layer_outputs.append(x_layer.get_output_at(-1))
+
+ self.keras_model = KerasModel(new_input, layer_outputs)
# and get the outputs for that model on the input x
outputs = self.keras_model(x)
