Exemplo n.º 1
0
 def emit_Reshape(self, IR_node):
     raise NotImplementedError
     shape_str = IRGraph.shapeToStr(IR_node.IR_layer.attr["shape"].shape, True)
     self.add_body(1, "{:<15} = Reshape(name = \"{}\", target_shape = ({}))({})".format(
         IR_node.variable_name,
         IR_node.name,
         shape_str,
         self.IR_graph.get_node(IR_node.in_edges[0]).real_variable_name))
Exemplo n.º 2
0
 def emit_DataInput(self, IR_node):
     shape_str = IRGraph.shapeToStr(IR_node.IR_layer.attr["shape"].shape)
     dtype_str = ", dtype = '{}'".format(self.dtype_map[IR_node.layer.attr['dtype'].type]) if 'dtype' in IR_node.layer.attr else ""
     self.add_body(1, "{:<15} = layers.Input(name = '{}', shape = ({},) {})".format(
         IR_node.variable_name,
         IR_node.name,
         shape_str,
         dtype_str))
Exemplo n.º 3
0
 def emit_DataInput(self, IR_node):
     shape_str = IRGraph.shapeToStr(IR_node.IR_layer.attr["shape"].shape)
     dtype_str = ", dtype = '{}'".format(self.dtype_map[IR_node.layer.attr['dtype'].type]) if 'dtype' in IR_node.layer.attr else ""
     self.add_body(1, "{:<15} = layers.Input(name = '{}', shape = ({},) {})".format(
         IR_node.variable_name,
         IR_node.name,
         shape_str,
         dtype_str))
Exemplo n.º 4
0
    def emit_Reshape(self, IR_node):
        def ShapetrToTuple(string, batch_none=False):
            if batch_none == True:
                ls = [int(item) for item in string.split(', ')]
                ls.insert(0, None)
                return tuple(ls)
            else:
                ls = [int(item) for item in string.split(', ')]
                return tuple(ls)

        last_node = self.IR_graph.get_node(IR_node.in_edges[0]).layer
        input_shape_dims = last_node.attr["_output_shapes"].list.shape
        target_shape_dims = IR_node.IR_layer.attr["_output_shapes"].list.shape

        input_shape = ShapetrToTuple(IRGraph.shapeToStr(input_shape_dims[0]),
                                     True)
        target_shape = ShapetrToTuple(IRGraph.shapeToStr(target_shape_dims[0]))

        def get_coreml_target_shape(target_shape):
            if len(target_shape) == 1:  #(D,)
                coreml_shape = (1, target_shape[0], 1, 1)
            elif len(target_shape) == 2:  #(S,D)
                coreml_shape = target_shape + (1, 1)
            elif len(target_shape) == 3:  #(H,W,C)
                coreml_shape = (1, target_shape[2], target_shape[0],
                                target_shape[1])
            else:
                coreml_shape = None
            return coreml_shape

        def get_mode(input_shape, target_shape):
            in_shape = input_shape[1:]
            if len(in_shape) == 3 or len(target_shape) == 3:
                return 1
            else:
                return 0

        input_name = self.IR_graph.get_node(IR_node.in_edges[0]).real_name
        new_shape = get_coreml_target_shape(target_shape)
        mode = get_mode(input_shape, target_shape)

        self.builder.add_reshape(name=IR_node.real_name,
                                 input_name=input_name,
                                 output_name=IR_node.real_name,
                                 target_shape=new_shape,
                                 mode=mode)
Exemplo n.º 5
0
 def emit_Reshape(self, IR_node):
     assert False
     shape_str = IRGraph.shapeToStr(IR_node.IR_layer.attr["shape"].shape,
                                    True)
     code = "{:<15} = Reshape(name = \"{}\", target_shape = ({}))({})".format(
         IR_node.replace_scope(IR_node.name), IR_node.name, shape_str,
         IR_node.replace_scope(IR_node.in_edges[0]))
     return code
Exemplo n.º 6
0
    def emit_Reshape(self, IR_node):
        def ShapetrToTuple(string, batch_none = False):
            if batch_none == True:
                ls = [int(item) for item in string.split(', ')]
                ls.insert(0,None)
                return tuple(ls)
            else:
                ls = [int(item) for item in string.split(', ')]
                return tuple(ls)

        last_node = self.IR_graph.get_node(IR_node.in_edges[0]).layer
        input_shape_dims = last_node.attr["_output_shapes"].list.shape
        target_shape_dims = IR_node.IR_layer.attr["_output_shapes"].list.shape

        input_shape = ShapetrToTuple(IRGraph.shapeToStr(input_shape_dims[0]),True)
        target_shape = ShapetrToTuple(IRGraph.shapeToStr(target_shape_dims[0]))

        # print("input_shape, target_shape",input_shape,target_shape)

        def get_coreml_target_shape(target_shape):
            if len(target_shape) == 1: #(D,)
                coreml_shape = (1,target_shape[0],1,1)
            elif len(target_shape) == 2: #(S,D)
                coreml_shape = target_shape + (1,1)
            elif len(target_shape) == 3: #(H,W,C)
                coreml_shape = (1, target_shape[2], target_shape[0], target_shape[1])
            else:
                coreml_shape = None
            return coreml_shape

        def get_mode(input_shape, target_shape):
            in_shape = input_shape[1:]
            if len(in_shape) == 3 or len(target_shape) == 3:
                    return 1
            else:
                return 0

        new_shape = get_coreml_target_shape(target_shape)
        mode = get_mode(input_shape, target_shape)
        self.builder.add_reshape(
            name=IR_node.real_name,
            input_name=self.parent_variable_name(IR_node),
            output_name=IR_node.real_name,
            target_shape=new_shape,
            mode=mode)
Exemplo n.º 7
0
 def emit_Reshape(self, IR_node):
     shape_str = IRGraph.shapeToStr(IR_node.IR_layer.attr["shape"].shape,
                                    True)
     self.add_body(
         1,
         "{:<15} = Reshape(name = \"{}\", target_shape = ({}))({})".format(
             IR_node.variable_name, IR_node.name, shape_str,
             self.IR_graph.get_node(
                 IR_node.in_edges[0]).real_variable_name))
Exemplo n.º 8
0
 def _process_output_layers(self):
     for name in self.IR_graph.output_layers:
         IR_node = self.IR_graph.get_node(name)
         shape_str = IRGraph.shapeToStr(
             IR_node.layer.attr["_output_shapes"].list.shape[0])
         if IR_node.layer.attr['dtype'].type == graph_pb2.DT_UNDEFINED:
             IR_node.layer.attr['dtype'].type = graph_pb2.DT_FLOAT32
         dtype_str = self.dtype_map[IR_node.layer.attr['dtype'].type]
         self.add_body(
             1, "{:<15} = helper.make_tensor_value_info('{}', {}, ({},))".
             format(IR_node.variable_name + '_out', IR_node.variable_name,
                    dtype_str, shape_str))
         self.outputs.append(IR_node.variable_name + '_out')