Beispiel #1
0
 def __init__(self, desc=None, weight_file=None, pb_file=None):
     super(GraphGetter, self).__init__()
     if isinstance(desc, dict):
         src_model = ModelZoo().get_model(desc)
     else:
         src_model = desc
     weights = OrderedDict()
     if is_tf_backend():
         import tensorflow.compat.v1 as tf
         from tensorflow.python.framework import tensor_util
         tf.reset_default_graph()
         data_shape = (1, 224, 224, 3)
         x = tf.ones(data_shape)
         if pb_file:
             with tf.io.gfile.GFile(pb_file, 'rb') as f:
                 graph_def = tf.GraphDef()
             graph_def.ParseFromString(f.read())
             graph = tf.Graph()
             with graph.as_default():
                 tf.import_graph_def(graph_def, name='')
             weight_file = None
             wts = [n for n in graph_def.node if n.op == 'Const']
             for n in wts:
                 weights[n.name] = tensor_util.MakeNdarray(
                     n.attr['value'].tensor)
         else:
             src_model(x, self.training)
             graph = tf.get_default_graph()
         desc = graph2desc(graph)
         tf.reset_default_graph()
     self.model = ModelZoo().get_model(desc, weight_file)
     if weights:
         self.model.load_checkpoint_from_numpy(weights)
Beispiel #2
0
 def __init__(self, desc, weight_file=None):
     super(Deformation, self).__init__()
     self.search_space = OrderedDict()
     self.conditions = deque()
     self.desc = deepcopy(desc.get('desc')) or desc
     self.model = ModelZoo().get_model(desc, weight_file)
     self.get_search_space(self.model)
     self.decode()
     self.deform()
     self.props.clear()
Beispiel #3
0
 def __init__(self, desc, from_graph=None, weight_file=None):
     super(Deformation, self).__init__()
     self._desc = {"props": deepcopy(desc.get('props')) or {}}
     if from_graph:
         self.model = GraphGetter(desc, weight_file).model
     else:
         self.model = ModelZoo().get_model(desc, weight_file)
     self._apply_names()
     self.get_search_space()
     self.decode()
     self.deform()
     self.props.clear()
Beispiel #4
0
 def get_space(self, desc):
     """Get model and input."""
     model_desc = PipeStepConfig.model.model_desc
     model = ModelZoo().get_model(
         dict(type='PruneDeformation', desc=model_desc))
     search_space = model.search_space
     params = []
     for key, value in search_space.items():
         hparam_name = 'network.props.{}'.format(key)
         params.append(
             dict(key=hparam_name, type="BINARY_CODE", range=[value]))
     params.append(
         dict(key='network.deformation',
              type="CATEGORY",
              range=['PruneDeformation']))
     logging.info("Prune Search Space: {}".format(params))
     return {"hyperparameters": params}
Beispiel #5
0
 def __init__(self,
              model_name=None,
              pretrained_model_file=None,
              downsamples=[4, 14, 27, 46],
              residauls=[7, 10, 17, 20, 23, 30, 33, 36, 39, 42, 49, 52],
              model=None,
              in_channels=None,
              out_channels=None,
              num_classes=None,
              **kwargs):
     super(PruneGetter, self).__init__()
     self.model = model
     if model_name is not None:
         model_desc = dict(type=model_name)
         model_desc.update(kwargs)
         self.model = ModelZoo().get_model(model_desc,
                                           pretrained_model_file)
     self.num_classes = num_classes
     convs = [
         module for name, module in self.model.named_modules()
         if isinstance(module, torch.nn.Conv2d)
     ]
     self.in_channels_size = sum([conv.in_channels for conv in convs])
     self.out_channels_size = sum([conv.out_channels for conv in convs])
     if in_channels and len(in_channels) < self.in_channels_size:
         raise ValueError(
             "in channels mask length should be getter than {}".format(
                 self.in_channels_size))
     if out_channels and len(out_channels) < self.out_channels_size:
         raise ValueError(
             "out channels mask length should be getter than {}".format(
                 self.out_channels_size))
     in_channels_code = self.define_props('in_channels', in_channels)
     out_channels_code = self.define_props('out_channels', out_channels)
     if not in_channels_code and not out_channels_code:
         logging.info(
             "channels_code is null. use 1 as default, this model will not pruned."
         )
         in_channels_code = [1] * self.in_channels_size
         out_channels_code = [1] * self.out_channels_size
     # TODO check downsample auto
     self.downsamples = downsamples
     self.residauls = residauls
     self.model = self._prune(self.model, in_channels_code,
                              out_channels_code)
Beispiel #6
0
 def get_space(self, desc):
     """Get model and input."""
     model_desc = PipeStepConfig.model.model_desc
     model = ModelZoo().get_model(
         dict(type='BackboneDeformation', desc=model_desc))
     search_space = model.search_space
     times = random.randint(3, 5)
     params = [
         dict(key="network.props.doublechannel",
              type="BINARY_CODE",
              range=[len(search_space), times]),
         dict(key="network.props.downsample",
              type="BINARY_CODE",
              range=[len(search_space), times])
     ]
     params.append(
         dict(key='network.deformation',
              type="CATEGORY",
              range=['BackboneDeformation']))
     logging.info("Backbone Search Space: {}".format(params))
     return {"hyperparameters": params}