Caffe2 - Python API
A deep learning, cross platform ML framework
__init__.py
1 # Copyright (c) 2016-present, Facebook, Inc.
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 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 
21 from importlib import import_module
22 import pkgutil
23 import sys
24 from . import layers
25 
26 
27 def import_recursive(package):
28  """
29  Takes a package and imports all modules underneath it
30  """
31 
32  pkg_dir = package.__path__
33  module_location = package.__name__
34  for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
35  module_name = "{}.{}".format(module_location, name) # Module/package
36  module = import_module(module_name)
37  if ispkg:
38  import_recursive(module)
39 
40 
41 def find_subclasses_recursively(base_cls, sub_cls):
42  cur_sub_cls = base_cls.__subclasses__()
43  sub_cls.update(cur_sub_cls)
44  for cls in cur_sub_cls:
45  find_subclasses_recursively(cls, sub_cls)
46 
47 
48 import_recursive(sys.modules[__name__])
49 
50 model_layer_subcls = set()
51 find_subclasses_recursively(layers.ModelLayer, model_layer_subcls)
52 
53 for cls in list(model_layer_subcls):
54  layers.register_layer(cls.__name__, cls)