Exemple #1
0
    def create_operator(self):
        assert len(self.inputs) == 1, "Conv accept single input only"
        rank = len(self.inputs[0].get_input_shape())
        W_weights = self.params[0]
        if (rank == 4):  # NCHW
            nb_filter = W_weights.shape[0]
            nb_row = int(self.onnx_attr['kernel_shape'][0])
            nb_col = int(self.onnx_attr['kernel_shape'][1])
            subSample = [int(i) for i in self.onnx_attr['strides']]
            dim_ordering = "th"
            assert self.onnx_attr['dilations'] == (1, 1), "we only support dilations == (1, 1)"
            assert self.onnx_attr['group'] == 1, "we only support group == 1"
            bias = True if (len(self.params) > 1) else False

            # TODO: activation?? init?? W_regularizer??
            border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)

            conv = zlayers.Convolution2D(nb_filter=nb_filter,
                                         nb_row=nb_row,
                                         nb_col=nb_col,
                                         subsample=subSample,
                                         dim_ordering=dim_ordering,
                                         bias=bias,
                                         border_mode=border_mode,
                                         pads=pads)
            return conv
        else:
            raise Exception("not supported.")
Exemple #2
0
    def _to_tensor(self):
        input = self.model_inputs[0]
        W_weights = self.model_trainable_values[0]
        rank = len(input.zvalue.get_input_shape())

        if (rank == 4):  # NCHW
            nb_filter = W_weights.shape[0]
            nb_row = int(self.onnx_attr['kernel_shape'][0])
            nb_col = int(self.onnx_attr['kernel_shape'][1])
            subSample = [int(i) for i in
                         self.onnx_attr['strides']] if "strides" in self.onnx_attr else (1, 1)
            dim_ordering = "th"
            assert 'dilations' not in self.onnx_attr or self.onnx_attr['dilations'] == (
                1, 1), "we only support dilations == (1, 1)"
            assert 'group' not in self.onnx_attr or self.onnx_attr[
                'group'] == 1, "we only support group == 1"
            bias = True if (len(self._input_list) > 2) else False

            border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)

            conv = zlayers.Convolution2D(nb_filter=nb_filter,
                                         nb_row=nb_row,
                                         nb_col=nb_col,
                                         subsample=subSample,
                                         dim_ordering=dim_ordering,
                                         bias=bias,
                                         border_mode=border_mode,
                                         pads=pads)
            return conv(input.zvalue)
        else:
            raise Exception("not supported.")
    def _to_tensor(self):
        assert len(self.model_inputs) == 1, "MaxPool accept single input only"
        rank = len(self.model_inputs[0].zvalue.shape)
        if "storage_order" in self.onnx_attr.keys():
            assert self.onnx_attr['storage_order'] == 0

        if (rank == 4):  # NCHW
            pool_size = [int(i) for i in self.onnx_attr['kernel_shape']]
            if "strides" in self.onnx_attr.keys():
                strides = [int(i) for i in self.onnx_attr['strides']]
            else:
                strides = [1 for i in self.onnx_attr['kernel_shape']]

            border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)

            maxpool = zlayers.MaxPooling2D(pool_size=pool_size,
                                           strides=strides,
                                           border_mode=border_mode,
                                           pads=pads)
            return maxpool(self.model_inputs[0].zvalue)
        elif (rank == 3):
            pool_length = int(self.onnx_attr['kernel_shape'][0])
            if "strides" in self.onnx_attr.keys():
                stride = int(self.onnx_attr['strides'][0])
            else:
                stride = 1
            border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)
            if border_mode is None and pads is None:
                border_mode = 'valid'
            if pads is None:
                pads = 0
            permute = zlayers.Permute(dims=(2, 1))(self.model_inputs[0].zvalue)
            maxpool = zlayers.MaxPooling1D(pool_length=pool_length,
                                           stride=stride,
                                           border_mode=border_mode,
                                           pad=pads)(permute)
            return zlayers.Permute(dims=(2, 1))(maxpool)
        else:
            raise Exception("not supported.")
Exemple #4
0
 def _to_tensor(self):
     assert len(self.model_inputs) == 1, "AveragePool accept single input only"
     rank = len(self.model_inputs[0].zvalue.shape)
     if (rank == 4):  # NCHW
         poolSize = [int(i) for i in self.onnx_attr['kernel_shape']]
         strides = [int(i) for i in self.onnx_attr['strides']]
         count_include_pad = bool(self.onnx_attr['count_include_pad'])\
             if "count_include_pad" in self.onnx_attr else False
         dim_ordering = "th"
         border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)
         averagepool2d = zlayers.AveragePooling2D(pool_size=poolSize,
                                                  strides=strides,
                                                  dim_ordering=dim_ordering,
                                                  pads=pads,
                                                  count_include_pad=count_include_pad)
         return averagepool2d(self.model_inputs[0].zvalue)
     else:
         raise Exception("not supported.")
Exemple #5
0
    def _to_tensor(self):
        assert len(self.model_inputs) == 1, "Conv accept single input only"
        rank = len(self.model_inputs[0].zvalue.get_input_shape())

        if (rank == 4):  # NCHW
            pool_size = [int(i) for i in self.onnx_attr['kernel_shape']]
            if "strides" in self.onnx_attr.keys():
                strides = [int(i) for i in self.onnx_attr['strides']]
            else:
                strides = [1 for i in self.onnx_attr['kernel_shape']]

            border_mode, pads = OnnxHelper.get_padds(self.onnx_attr)

            maxpool = zlayers.MaxPooling2D(pool_size=pool_size,
                                           strides=strides,
                                           border_mode=border_mode,
                                           pads=pads)
            return maxpool(self.model_inputs[0].zvalue)
        else:
            raise Exception("not supported.")