def visit(self, op, *args): self.computation.set_op_rank(op) # op.args[0] : inputs # op.args[1] : filters # op.args[2] (optional): bias # op.conv_params # op.channel_axes # op.spatial_axes if len(args) == 2: inputs = args[0] filters = args[1] else: raise RuntimeError("Not Implemented: Convolution with bias") """ {'K': 16, 'T': 1, 'R': 5, 'S': 5, 'str_d': 1, 'pad_d': 0, 'dil_d': 1, 'str_h': 1, 'pad_h': 0, 'dil_h': 1, 'str_w': 1, 'pad_w': 0, 'dil_w': 1} """ """ print(inputs.axes) print(op.axes) print(filters.axes) """ # print(op_element_type.get_output_shape(0)) ngraph_conv = PyngConvolution( self.computation.lookup_cpp_op(inputs), self.computation.lookup_cpp_op(filters), Strides([op.conv_params['str_h'], op.conv_params['str_w']]), Strides([op.conv_params['dil_h'], op.conv_params['dil_w']]), CoordinateDiff([op.conv_params['pad_h'], op.conv_params['pad_w']]), CoordinateDiff([op.conv_params['pad_h'], op.conv_params['pad_w']]), Strides([1, 1])) ngraph_conv.name = op.name.replace('/', '_') + "_Convolution" self.computation.register_cpp_op(op, ngraph_conv, set_name=False)
def test_convolution_with_filter_dilation(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) strides = [1, 1] dilation = [2, 2] function = Function( [Convolution(A, B, Strides(strides), Strides(dilation))], parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) result = backend.create_tensor(element_type, Shape([1, 1, 6, 6])) result.write(util.numpy_to_c(result_arr), 6 * 6 * 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 6 * 6 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation).reshape(1, 1, 6, 6) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_filter_dilation(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) strides = [1, 1] dilation = [2, 2] function = Function( NodeVector([Convolution(A, B, Strides(strides), Strides(dilation))]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, image_shape) b = backend.make_primary_tensor_view(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) result = backend.make_primary_tensor_view(element_type, Shape([1, 1, 6, 6])) result.write(util.numpy_to_c(result_arr), 0, 6 * 6 * 4) cf.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 6 * 6 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation).reshape(1, 1, 6, 6) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_strides(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][1][1] = 1 strides = [2, 2] function = Function(NodeVector([Convolution(A, B, Strides(strides))]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) result_arr = np.zeros(16, dtype=np.float32).reshape(1, 1, 4, 4) result = backend.create_tensor(element_type, Shape([1, 1, 4, 4])) result.write(util.numpy_to_c(result_arr), 0, 4 * 4 * 4) backend.call(backend.compile(function), [result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 4 * 4 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) assert np.allclose(result_arr, result_arr_ref)
def test_convolution(): element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 result_arr = np.zeros(196, dtype=np.float32).reshape(1, 1, 14, 14) function = Function(NodeVector([Convolution(A, B)]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 16*16*4) b.write(util.numpy_to_c(filter_arr), 0, 3*3*4) result = backend.create_tensor(element_type, Shape([1, 1, 14, 14])) result.write(util.numpy_to_c(result_arr), 0, 14*14*4) backend.call(backend.compile(function), [result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 14*14*4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) assert np.allclose(result_arr, result_arr_ref)
def convolution( x, # type: Node weights, # type: Node strides=None, # type: List[int] dilation=None, # type: List[int] padding_above=None, # type: List[int] padding_below=None, # type: List[int] name=None, # type: str ): # type: (...) -> Node """Return convolution node.""" if strides is None: strides = [1] * ( len(x.shape) - 2 ) # Default to as many 1s as spatial dimensions of input. if dilation is None: dilation = [1] * (len(x.shape) - 2) if padding_above is None: padding_above = [0] * (len(x.shape) - 2) if padding_below is None: padding_below = [0] * (len(x.shape) - 2) return Convolution(x, weights, Strides(strides), Strides(dilation), CoordinateDiff(padding_above), CoordinateDiff(padding_below))
def convolution( data_batch, # type: Node filter_weights, # type: Node filter_strides=None, # type: List[int] filter_dilation_strides=None, # type: List[int] padding_below=None, # type: List[int] padding_above=None, # type: List[int] data_dilation_strides=None, # type: List[int] name=None, # type: str ): # type: (...) -> Node """Return node performing batched convolution operation. :param data_batch: The node providing data batch tensor. :param filter_weights: The node providing filters tensor. :param filter_strides: The kernel window movement strides. :param filter_dilation_strides: The filters dilation strides. :param padding_below: The number of zero padding elements to add on each axis below 0 coordinate. :param padding_above: The number of zero padding elements to add on each axis above max coordinate. :param data_dilation_strides: The data batch dilation strides. :param name: The optional new name for output node. :return: New node performing batched convolution operation. """ spatial_dim_count = len(data_batch.shape) - 2 if filter_strides is None: filter_strides = [1] * spatial_dim_count if filter_dilation_strides is None: filter_dilation_strides = [1] * spatial_dim_count if padding_above is None: padding_above = [0] * spatial_dim_count if padding_below is None: padding_below = [0] * spatial_dim_count if data_dilation_strides is None: data_dilation_strides = [1] * spatial_dim_count return Convolution(data_batch, filter_weights, Strides(filter_strides), Strides(filter_dilation_strides), CoordinateDiff(padding_below), CoordinateDiff(padding_above), Strides(data_dilation_strides))
def test_convolution_with_data_dilation(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) strides = [1, 1] dilation = [1, 1] padding_below = [0, 0] padding_above = [0, 0] data_dilation = [2, 2] function = Function( NodeVector([ Convolution(A, B, Strides(strides), Strides(dilation), CoordinateDiff(padding_below), CoordinateDiff(padding_above), Strides(data_dilation)) ]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) result_arr = np.zeros(17 * 17, dtype=np.float32).reshape(1, 1, 17, 17) result = backend.create_tensor(element_type, Shape([1, 1, 17, 17])) result.write(util.numpy_to_c(result_arr), 0, 17 * 17 * 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 17 * 17 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation, padding_below, padding_above, data_dilation).reshape(1, 1, 17, 17) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_padding(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][1][1] = 1 strides = [1, 1] dilation = [2, 2] padding_below = [0, 0] padding_above = [0, 0] function = Function([ Convolution(A, B, Strides(strides), Strides(dilation), CoordinateDiff(padding_below), CoordinateDiff(padding_above)) ], parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) result = backend.create_tensor(element_type, Shape([1, 1, 6, 6])) result.write(util.numpy_to_c(result_arr), 6 * 6 * 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 6 * 6 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation, padding_below, padding_above).reshape(1, 1, 6, 6) assert np.allclose(result_arr, result_arr_ref) # test with non-zero padding element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] dilation = [2, 2] padding_below = [2, 1] padding_above = [1, 2] function = Function([ Convolution(A, B, Strides(strides), Strides(dilation), CoordinateDiff(padding_below), CoordinateDiff(padding_above)) ], parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) result_arr = np.zeros(81, dtype=np.float32).reshape(1, 1, 9, 9) result = backend.create_tensor(element_type, Shape([1, 1, 9, 9])) result.write(util.numpy_to_c(result_arr), 9 * 9 * 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 9 * 9 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation, padding_below, padding_above).reshape(1, 1, 9, 9) assert np.allclose(result_arr, result_arr_ref)