def create_encoder(input_shape, levels = 4): import ngraph as ng # input input_node = ng.parameter(input_shape, np.float32, name="data") padding_begin = padding_end = [0, 0] strides = [1, 1] dilations = [1, 1] input_channels = [input_shape[1]] last_output = input_node # convolution layers for i in range(levels): input_c = input_channels[-1] output_c = input_c * 2 conv_w = np.random.uniform(0, 1, [output_c, input_c, 5, 5]).astype(np.float32) conv_node = ng.convolution(last_output, conv_w, strides, padding_begin, padding_end, dilations) input_channels.append(output_c) last_output = conv_node # deconvolution layers for i in range(levels): input_c = input_channels[-2] output_c = input_channels.pop(-1) deconv_w = np.random.uniform(0, 1, [output_c, input_c, 5, 5]).astype(np.float32) deconv_node = ng.convolution_backprop_data(last_output, deconv_w, strides) last_output = deconv_node # result last_output.set_friendly_name("out") result_node = ng.result(last_output) return ng.Function(result_node, [input_node], "Encoder")
def test_convolution_backprop_data(): runtime = get_runtime() output_spatial_shape = [9, 9] filter_shape = [1, 1, 3, 3] data_shape = [1, 1, 7, 7] strides = [1, 1] data_node = ng.parameter(shape=data_shape) filter_node = ng.parameter(shape=filter_shape) output_shape_node = ng.constant(np.array(output_spatial_shape, dtype=np.int64)) deconvolution = ng.convolution_backprop_data(data_node, filter_node, strides, output_shape_node) input_data = np.array( [ [ [ [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], ] ] ], dtype=np.float32, ) filter_data = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( 1, 1, 3, 3 ) model = runtime.computation(deconvolution, data_node, filter_node) result = model(input_data, filter_data) assert np.allclose( result, np.array( [ [ [ [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], ] ] ], dtype=np.float32, ), )
def ConvTranspose( onnx_node, ng_inputs): # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode """Calculate convolution transpose.""" if len(ng_inputs) == 3: data, weights, bias = ng_inputs elif len(ng_inputs) == 2: data, weights = ng_inputs bias = ng.constant(0, dtype=get_dtype(data.get_element_type())) strides = get_strides(onnx_node) dilation = get_dilations(onnx_node) padding_below, padding_above = get_pads(onnx_node) output_padding = onnx_node.get_attribute_value('output_padding') if output_padding is None: raise ValueError( 'ConvTranspose node (s%): output_padding attribute is required.', onnx_node.name) data_shape = list(data.shape) weights_shape = list(weights.shape) num_spatial_dims = len(data.shape) - 2 data_dilation_strides = [1, 1] data_batch_shape = [1] * (num_spatial_dims + 2) data_batch_shape[0] = data_shape[0] data_batch_shape[1] = weights_shape[1] for i in range(num_spatial_dims): # Calculating spatial dims of data output shape for ngraph conv backprop op # | pb + s(ds-1) + op - d(ws-1)+1 | # | ----------------------------- | + 1 # |_ dds _| # # d - dilation # ds - data shape # dds - data dilation strides # op - putput padding # pb - padding below # s - strides # ws - weights shape data_batch_shape[i + 2] = ( (padding_below[i] + ((data_shape[i + 2] - 1) * strides[i] + 1) + output_padding[i]) - ((weights_shape[i + 2] - 1) * dilation[i] + 1) + 1) // data_dilation_strides[i] + 1 transconv = ng.convolution_backprop_data(data_batch_shape, weights, data, strides, dilation, padding_below, padding_above, data_dilation_strides) if len(bias.shape) > 0: return transconv + ng.broadcast_to(bias, transconv.shape, 1) else: return transconv
def test_convolution_backprop_data(): runtime = get_runtime() data_batch_shape = [1, 1, 9, 9] filter_shape = [1, 1, 3, 3] output_delta_shape = [1, 1, 7, 7] filter_param = ng.parameter(shape=filter_shape) output_delta_param = ng.parameter(shape=output_delta_shape) deconvolution = ng.convolution_backprop_data(data_batch_shape, filter_param, output_delta_param) data_batch_data = np.array([[[[-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0]]]], dtype=np.float32) filter_data = np.array([ [1., 0., -1.], [2., 0., -2.], [1., 0., -1.]], dtype=np.float32).reshape(1, 1, 3, 3) model = runtime.computation(deconvolution, filter_param, output_delta_param) result = model(filter_data, data_batch_data) assert np.allclose(result, np.array([[[[-20., -20., 40., 40., -20., -20., 0., 0., 0.], [-60., -60., 120., 120., -60., -60., 0., 0., 0.], [-80., -80., 160., 160., -80., -80., 0., 0., 0.], [-80., -80., 160., 160., -80., -80., 0., 0., 0.], [-80., -80., 160., 160., -80., -80., 0., 0., 0.], [-80., -80., 160., 160., -80., -80., 0., 0., 0.], [-80., -80., 160., 160., -80., -80., 0., 0., 0.], [-60., -60., 120., 120., -60., -60., 0., 0., 0.], [-20., -20., 40., 40., -20., -20., 0., 0., 0.]]]], dtype=np.float32))