Ejemplo n.º 1
0
    def _resolve_hierarchical_resolution_conflicts(self, descriptors):
        """
        :type descriptors: list(LayerDescriptor)
        :rtype: list(LayerDescriptor)
        """
        input_ops = set(
            [o for d in self._input_descriptors for o in d.child_ops])
        op_to_descriptor = OrderedDict()
        for d in descriptors:
            for o in d.child_ops:
                if o in input_ops and len(d.child_ops) == 1:
                    continue

                current_descriptor = op_to_descriptor.get(o, None)
                if current_descriptor:
                    if (len(d.child_ops) > len(current_descriptor.child_ops)) or \
                            (len(d.child_ops) == len(current_descriptor.child_ops) and
                             isinstance(current_descriptor, IgnoredLayersResolver.Descriptor)):
                        op_to_descriptor[o] = d
                        for op, descriptor in list(op_to_descriptor.items()):
                            if descriptor == current_descriptor:
                                op_to_descriptor[op].child_ops.remove(o)
                                op_to_descriptor[op].set_ignored(True)
                                op_to_descriptor[op].layer_name += '_ignored'
                    else:
                        break
                else:
                    op_to_descriptor[o] = d
        return uniques(list(op_to_descriptor.values()))
Ejemplo n.º 2
0
    def _get_input_layers_output_tensors_for(self, operation):
        """
        :type operation: tensorflow.Operation
        :rtype: list[tensorflow.Tensor]
        """
        descriptor = self._topology_resolver.descriptor_ops_map.get(
            operation, None)
        if descriptor is None:
            raise ConverterError(
                'Unable to find input layer for operation not in layer.')

        output_tensors = []

        input_descriptors = self._topology_resolver.get_input_layers_for(
            descriptor)
        input_descriptors_outputs = [
            o for d in input_descriptors for o in d.child_ops
            if d.is_output_op(o)
        ]

        visited = set()
        op_queue = [operation]
        while len(op_queue) > 0:
            next_op = op_queue.pop(0)
            visited.add(next_op)
            for input_tensor in next_op.inputs:
                input_op = input_tensor.op
                if input_op in input_descriptors_outputs:
                    output_tensors.append(input_tensor)
                elif input_op not in visited:
                    op_queue.insert(0, input_op)

        return uniques(output_tensors)
Ejemplo n.º 3
0
    def _get_input_layers_for(self, descriptor):
        """
        :type descriptor: LayerDescriptor
        :rtype: list[LayerDescriptor]
        """
        predecessors = []
        descriptor_input_ops = [
            op for op in descriptor.child_ops if descriptor.is_input_op(op)
        ]
        for o in descriptor_input_ops:
            q = [t.op for t in o.inputs]
            visited = set()
            while len(q) > 0:
                next_op = q.pop(0)
                if next_op in visited:
                    continue
                visited.add(next_op)

                d = self._descriptor_ops_map.get(next_op, None)
                if d is None or d == descriptor:
                    continue

                if d.is_ignored:
                    q.extend([t.op for t in next_op.inputs])
                else:
                    predecessors.append(d)
        return uniques(predecessors)
Ejemplo n.º 4
0
 def get_output_tensors_between(self, descriptor_from, descriptor_to):
     """
     :type descriptor_from: LayerDescriptor
     :type descriptor_to: LayerDescriptor
     :rtype: list[tensorflow.Tensor]
     """
     tensors = []
     for o in descriptor_to.child_ops:
         ts = self._get_input_layers_output_tensors_for(o)
         for t in ts:
             d = self._topology_resolver.descriptor_ops_map.get(t.op, None)
             if d == descriptor_from:
                 tensors.append(t)
     return uniques(tensors)
Ejemplo n.º 5
0
 def _get_input_layer_output_op_for(self, operation):
     """
     :type operation: tensorflow.Operation
     :rtype: tensorflow.Operation
     """
     input_tensors = self._get_input_layers_output_tensors_for(operation)
     ops = uniques([t.op for t in input_tensors])
     if len(ops) == 0:
         raise ConverterError(
             code_to_message.get_message(
                 'ERROR_TF_INPUT_OPERATION_NOT_FOUND')(operation.name))
     if len(ops) != 1:
         raise ConverterError(
             code_to_message.get_message(
                 'ERROR_TF_EXPECTED_SINGLE_OUTPUT_FROM_PREVIOUS_LAYER'))
     return ops[0]
Ejemplo n.º 6
0
 def _resolve_hierarchical_resolution_conflicts(cls, descriptors):
     """
     :type descriptors: list(LayerDescriptor)
     :rtype: list(LayerDescriptor)
     """
     op_to_descriptor = OrderedDict()
     for d in descriptors:
         for o in d.child_ops:
             current_descriptor = op_to_descriptor.get(o, None)
             if current_descriptor:
                 if len(d.child_ops) > len(current_descriptor.child_ops):
                     op_to_descriptor[o] = d
                 elif len(d.child_ops) == len(
                         current_descriptor.child_ops) and isinstance(
                             current_descriptor,
                             IgnoredLayersResolver.Descriptor):
                     op_to_descriptor[o] = d
             else:
                 op_to_descriptor[o] = d
     return uniques(op_to_descriptor.values())