def calc_pad_dim(cls, node, expected_len): if "auto_pad" not in node.attrs or node.attrs["auto_pad"] == "NOTSET": pad_dim = PadDim(*cls.mix_pads(node.attrs.get("pads", []))) elif node.attrs["auto_pad"] == "VALID": pad_dim = PadDim.valid() elif node.attrs["auto_pad"] == "SAME_UPPER": pad_dim = PadDim.same(same_type="balanced_left") elif node.attrs["auto_pad"] == "SAME_LOWER": pad_dim = PadDim.same(same_type="balanced_right") else: raise ValueError("bad pad type") return pad_dim
def calc_pad_dim(cls, node, spatial_size): pads = cls.pad_start_with(node.attrs.get("pads", [0, 0] * spatial_size), [0, 0], 2) if "auto_pad" not in node.attrs or node.attrs["auto_pad"] == "NOTSET": pad_dim = PadDim(*pads) elif node.attrs["auto_pad"] == "VALID": pad_dim = PadDim.valid() elif node.attrs["auto_pad"] == "SAME_UPPER": pad_dim = PadDim.same(same_type="balanced_left") elif node.attrs["auto_pad"] == "SAME_LOWER": pad_dim = PadDim.same(same_type="balanced_right") else: raise ValueError("bad pad type") return pad_dim
def test_paddim(): dim1 = PadDim(1) assert not dim1.is_same assert dim1.h == 2 and dim1.w == 2 assert dim1.l == 1 and dim1.r == 1 and dim1.t == 1 and dim1.b == 1 assert dim1.numpy_pad_shape(Dim.named_ordered(w=10, h=10)) == [(1, 1), (1, 1)] stride_dim = StrideDim(1) filt_dim = Conv2DFilterDim(5, 5, 1, 1) in_dim = Dim.named_ordered(c=1, h=20, w=20) dim1 = PadDim.same() dim1.calculate_same(in_dim, filt_dim, stride_dim) assert dim1.shape == [2, 2, 2, 2]
def test_conf2d_pad_dilate(): weights = np.arange(9).reshape([1, 1, 3, 3]) filt = Conv2DFilterDim(3, 3, 1, 1) stride = StrideDim(1) pad = PadDim.same() dilation = DilationDim(2) params = Conv2DParameters("test", filt=filt, stride=stride, padding=pad, dilation=dilation, in_dims_hint=[['c', 'h', 'w']], out_dims_hint=[['c', 'h', 'w']]) input_ = np.arange(16).reshape([1, 4, 4]) in_dims = Dim.named(c=1, h=4, w=4).impose_order(['c', 'h', 'w']) out_dims = params.get_output_size([in_dims]) output_ = conv2d(params, in_dims, out_dims[0], input_, weights, None, None) assert np.array_equal(output_, [[[266., 206.], [98., 66.]]])
def test_conf2d_pad(): weights = np.arange(9).reshape([1, 1, 3, 3]) filt = Conv2DFilterDim(3, 3, 1, 1) stride = StrideDim(1) pad = PadDim.same() dilation = DilationDim(1) params = Conv2DParameters("test", filt=filt, stride=stride, padding=pad, dilation=dilation, in_dims_hint=[['c', 'h', 'w']], out_dims_hint=[['c', 'h', 'w']]) input_ = np.arange(16).reshape([1, 4, 4]) in_dims = Dim.named(c=1, h=4, w=4).impose_order(['c', 'h', 'w']) out_dims = params.get_output_size([in_dims]) output_ = conv2d(params, in_dims, out_dims[0], input_, weights, None) assert np.array_equal(output_, [[[73, 121, 154, 103], [171, 258, 294, 186],\ [279, 402, 438, 270], [139, 187, 202, 113]]])
def get_tf_padding(padding): if padding == Padding.Padding.SAME: return PadDim.same() if padding == Padding.Padding.VALID: return PadDim.valid() raise ValueError("Strange padding type")