You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.3 KiB

  1. # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Utility functions for manipulating Keras models."""
  16. import tensorflow as tf
  17. def extract_submodel(model, inputs, outputs, name=None):
  18. """Extracts a section of a Keras model into a new model.
  19. This method walks an existing model from the specified outputs back to the
  20. specified inputs in order to construct a new model containing only a portion
  21. of the old model, while sharing the layers and weights with the original
  22. model.
  23. WARNING: This method does not work for submodels containing layers that have
  24. been used multiple times in the original model, or in other models beyond
  25. the original model. (E.g. does not work for submodels that contain layers that
  26. use shared weights). This also means that multiple overlapping submodels
  27. cannot be extracted from the same model.
  28. It also relies on recursion and will hit python's recursion limit for large
  29. submodels.
  30. Args:
  31. model: The existing Keras model this method extracts a submodel from.
  32. inputs: The layer inputs in the existing model that start the submodel
  33. outputs: The layer outputs in the existing model that should be output by
  34. the submodel
  35. name: The name for the extracted model
  36. Returns:
  37. The extracted submodel specified by the given inputs and outputs
  38. """
  39. output_to_layer = {}
  40. output_to_layer_input = {}
  41. for layer in model.layers:
  42. layer_output = layer.output
  43. layer_inputs = layer.input
  44. output_to_layer[layer_output] = layer
  45. output_to_layer_input[layer_output] = layer_inputs
  46. model_inputs_dict = {}
  47. memoized_results = {}
  48. # Relies on recursion, very low limit in python
  49. def _recurse_in_model(tensor):
  50. """Walk the existing model recursively to copy a submodel."""
  51. if tensor in memoized_results:
  52. return memoized_results[tensor]
  53. if (tensor == inputs) or (isinstance(inputs, list) and tensor in inputs):
  54. if tensor not in model_inputs_dict:
  55. model_inputs_dict[tensor] = tf.keras.layers.Input(tensor=tensor)
  56. out = model_inputs_dict[tensor]
  57. else:
  58. cur_inputs = output_to_layer_input[tensor]
  59. cur_layer = output_to_layer[tensor]
  60. if isinstance(cur_inputs, list):
  61. out = cur_layer([_recurse_in_model(inp) for inp in cur_inputs])
  62. else:
  63. out = cur_layer(_recurse_in_model(cur_inputs))
  64. memoized_results[tensor] = out
  65. return out
  66. if isinstance(outputs, list):
  67. model_outputs = [_recurse_in_model(tensor) for tensor in outputs]
  68. else:
  69. model_outputs = _recurse_in_model(outputs)
  70. if isinstance(inputs, list):
  71. model_inputs = [model_inputs_dict[tensor] for tensor in inputs]
  72. else:
  73. model_inputs = model_inputs_dict[inputs]
  74. return tf.keras.Model(inputs=model_inputs, outputs=model_outputs, name=name)