module onnxrt.ops_cpu.op_conv_helper#

Short summary#

module mlprodict.onnxrt.ops_cpu.op_conv_helper

Helpers for operators Conv, ConvTranspose.

source on GitHub

Functions#

function

truncated documentation

_get_indices

_is_out

col2im_indices

Source im2col.py.

col2im_nchw

C implementation of a partial col2im.

get_im2col_indices

Source im2col.py.

im2col

Returns the result of im2col on a image NHCW where N is 1. The function is equivalent to torch.nn.Unfold()

im2col_indices

Source im2col.py.

im2col_infer_output_shape

Computes the ouput shape of im2col.

im2col_naive_implementation

Naive implementation for im2col or torch.nn.Unfold() (but with padding=1).

im2col_nchw

C implementation of a partial im2col.

im2col_nn

Functions nn_im2col_2d() and im2col() returns the same results but with different shapes. This function …

im2col_recursive

Recursive implementation, falls back to im2col_naive_implementation() for dimension <= fall_back_dim. The …

new_array

Creates a new empty array.

nn_col2im_2d

C++ implementation for col2im or torch.nn.Fold().

nn_im2col_2d

C++ implementation for im2col or torch.nn.Unfold().

Documentation#

Helpers for operators Conv, ConvTranspose.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper._get_indices(i, shape)#
mlprodict.onnxrt.ops_cpu.op_conv_helper._is_out(ind, shape)#
mlprodict.onnxrt.ops_cpu.op_conv_helper.col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=0, stride=1)#

Source im2col.py.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.col2im_nchw(data_col, image_shape, kernel_shape, padding, dilations)#

C implementation of a partial col2im.

Parameters:
  • data_col – image (float)

  • image_shape – expected image shape

  • kernel_shape – kernel shape

  • padding – padding

  • dilations – dilations

Returns:

result

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1)#

Source im2col.py.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col(data, kernel_shape=None, fill_value=0)#

Returns the result of im2col on a image NHCW where N is 1. The function is equivalent to torch.nn.Unfold() (but with padding=1 on all dimensions).

Parameters:
  • image – image (float)

  • kernel_shape – kernel shape

  • fill_value – fill value

Returns:

result

This function is equivalent to function torch.nn.Unfold() with padding=kernel_shape / 2 followed by a reshape and a transpose.

import numpy
from numpy.testing import assert_almost_equal
import torch

data = (numpy.arange(20).astype(numpy.float64) + 10).reshape((4, 5))
expected = im2col_recursive(data, (3, 3), fill_value=0)
unfold = torch.nn.Unfold(kernel_size=(3, 3), padding=1)
input = torch.from_numpy(data.reshape((1, 1) + data.shape))
output = unfold(input)
mat = output.numpy()
tr = numpy.transpose(mat, [0, 2, 1])
resh = tr.reshape(expected.shape)
assert_almost_equal(expected, resh)

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_indices(x, field_height, field_width, padding=0, stride=1)#

Source im2col.py.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_infer_output_shape(input_shape, kernel_shape, strides, dilations, padding, auto_padding='NOTSET')#

Computes the ouput shape of im2col.

Parameters:
  • input_shape – input _shape

  • kernel_shape – kernel shape

  • strides – strides

  • dilations – dilations

  • padding – padding

  • auto_padding – among NOTSET, VALID, SAME_UPPER, SAME_LOWER

:return output_shape, modified padding

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_naive_implementation(data, kernel_shape, fill_value=0)#

Naive implementation for im2col or torch.nn.Unfold() (but with padding=1).

Parameters:
  • image – image (float)

  • kernel_shape – kernel shape

  • fill_value – fill value

Returns:

result

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_nchw(image_id, group_id, group, image, kernel_shape, padding, dilations)#

C implementation of a partial im2col.

Parameters:
  • image – image (float)

  • kernel_shape – kernel shape

  • padding – padding

  • dilations – dilations

Returns:

result

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_nn(res)#

Functions nn_im2col_2d and im2col() returns the same results but with different shapes. This function converts a result from nn_im2col_2d into the same shape as a return from nn_im2col_2d.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.im2col_recursive(data, kernel_shape, fill_value=0, fall_back_dim=2)#

Recursive implementation, falls back to im2col_naive_implementation for dimension <= fall_back_dim. The function is equivalent to torch.nn.Unfold() (but with padding=1 on all dimensions).

Parameters:
  • image – image (float)

  • kernel_shape – kernel shape

  • fill_value – fill value

  • fall_back_dim – below that threshold, switches to im2col_naive_implementation.

Returns:

result

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.new_array(shape, dtype=<class 'numpy.float32'>)#

Creates a new empty array.

Parameters:
  • shape – shape

  • dtype – dtype

Returns:

new array

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.nn_col2im_2d(data, output_shape, kernel_shape, dilations, padding)#

C++ implementation for col2im or torch.nn.Fold().

Parameters:
  • data – image (float), 2 dimensions.

  • output_shape – output size

  • kernel_shape – kernel shape

  • dilations – dilations

  • padding – padding

Returns:

result

source on GitHub

mlprodict.onnxrt.ops_cpu.op_conv_helper.nn_im2col_2d(data, kernel_shape, dilations, padding, fill_value=0)#

C++ implementation for im2col or torch.nn.Unfold().

Parameters:
  • data – image (float), 2 dimensions.

  • kernel_shape – kernel shape

  • dilations – dilations

  • padding – padding

  • fill_value – fill value

Returns:

result

source on GitHub