Browse Source

change name from interpolate to resizeBilinear

tags/v1.1.0
l00591931 5 years ago
parent
commit
130c61cfc0
2 changed files with 24 additions and 25 deletions
  1. +12
    -13
      mindspore/nn/layer/basic.py
  2. +12
    -12
      tests/ut/python/nn/test_resizebilinear.py

+ 12
- 13
mindspore/nn/layer/basic.py View File

@@ -34,7 +34,7 @@ from ..cell import Cell
from .activation import get_activation from .activation import get_activation


__all__ = ['Dropout', 'Flatten', 'Dense', 'ClipByNorm', 'Norm', 'OneHot', 'Pad', 'Unfold', __all__ = ['Dropout', 'Flatten', 'Dense', 'ClipByNorm', 'Norm', 'OneHot', 'Pad', 'Unfold',
'Tril', 'Triu', 'Interpolate', 'MatrixDiag', 'MatrixDiagPart', 'MatrixSetDiag', 'L1Regularizer']
'Tril', 'Triu', 'ResizeBilinear', 'MatrixDiag', 'MatrixDiagPart', 'MatrixSetDiag', 'L1Regularizer']




class L1Regularizer(Cell): class L1Regularizer(Cell):
@@ -621,7 +621,7 @@ class Pad(Cell):




@constexpr @constexpr
def interpolate(shape, size, scale, align_corners):
def bilinear(shape, size, scale, align_corners):
"""Check input and calculate shape""" """Check input and calculate shape"""
if not isinstance(align_corners, bool): if not isinstance(align_corners, bool):
raise TypeError("align_corners should be type boolean") raise TypeError("align_corners should be type boolean")
@@ -632,19 +632,18 @@ def interpolate(shape, size, scale, align_corners):
if size is not None: if size is not None:
if not isinstance(size, (tuple, list)): if not isinstance(size, (tuple, list)):
raise ValueError("size must be tuple or list") raise ValueError("size must be tuple or list")
Validator.check_int(len(size), 2, Rel.EQ, "size", "interpolate")
Validator.check_int(size[0], 1, Rel.GE, "size[0]", "interpolate")
Validator.check_int(size[1], 1, Rel.GE, "size[1]", "interpolate")
Validator.check_int(len(size), 2, Rel.EQ, "size", "bilinear")
Validator.check_int(size[0], 1, Rel.GE, "size[0]", "bilinear")
Validator.check_int(size[1], 1, Rel.GE, "size[1]", "bilinear")
return size return size
Validator.check_int(scale, 1, Rel.GE, "scale factor", "interpolate")
Validator.check_int(scale, 1, Rel.GE, "scale factor", "bilinear")
ret = (scale * shape[2], scale * shape[3]) ret = (scale * shape[2], scale * shape[3])
return ret return ret




class Interpolate(Cell):
class ResizeBilinear(Cell):
r""" r"""
Samples the input tensor to the given size or scale_factor. Now, only support
bilinear interpolation.
Samples the input tensor to the given size or scale_factor by using bilinear interpolate.


Inputs: Inputs:
- **x** (Tensor) - Tensor to be resized. Input tensor must be a 4-D tensor with shape: - **x** (Tensor) - Tensor to be resized. Input tensor must be a 4-D tensor with shape:
@@ -668,17 +667,17 @@ class Interpolate(Cell):


Examples: Examples:
>>> tensor = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mindspore.float32) >>> tensor = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mindspore.float32)
>>> interpolate = nn.Interpolate()
>>> result = interpolate(tensor, size=(5,5))
>>> resize_bilinear = nn.ResizeBilinear()
>>> result = resize_bilinear(tensor, size=(5,5))
>>> print(result.shape) >>> print(result.shape)
(1, 1, 5, 5) (1, 1, 5, 5)
""" """


def __init__(self): def __init__(self):
super(Interpolate, self).__init__()
super(ResizeBilinear, self).__init__()


def construct(self, x, size=None, scale_factor=None, align_corners=False): def construct(self, x, size=None, scale_factor=None, align_corners=False):
shape = interpolate(x.shape, size, scale_factor, align_corners)
shape = bilinear(x.shape, size, scale_factor, align_corners)
resize_bilinear = P.ResizeBilinear(shape, align_corners) resize_bilinear = P.ResizeBilinear(shape, align_corners)
return resize_bilinear(x) return resize_bilinear(x)




tests/ut/python/nn/test_interpolate.py → tests/ut/python/nn/test_resizebilinear.py View File

@@ -23,68 +23,68 @@ from mindspore import context
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)




def test_interpolate():
def test_resizebilinear():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32) self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)


def construct(self): def construct(self):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(self.value, size=(5, 5)) return interpolate(self.value, size=(5, 5))


net = Net() net = Net()
net() net()




def test_interpolate_1():
def test_resizebilinear_1():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32) self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)


def construct(self): def construct(self):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(self.value, scale_factor=2) return interpolate(self.value, scale_factor=2)


net = Net() net = Net()
net() net()




def test_interpolate_parameter():
def test_resizebilinear_parameter():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()


def construct(self, x): def construct(self, x):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(x, size=(5, 5)) return interpolate(x, size=(5, 5))


net = Net() net = Net()
net(Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)) net(Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32))




def test_interpolate_parameter_1():
def test_resizebilinear_parameter_1():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()


def construct(self, x): def construct(self, x):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(x, scale_factor=2) return interpolate(x, scale_factor=2)


net = Net() net = Net()
net(Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)) net(Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32))




def test_interpolate_error():
def test_resizebilinear_error():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32) self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)


def construct(self): def construct(self):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(self.value) return interpolate(self.value)


net = Net() net = Net()
@@ -93,14 +93,14 @@ def test_interpolate_error():
assert "size and scale both none" in str(ex.value) assert "size and scale both none" in str(ex.value)




def test_interpolate_error_1():
def test_resizebilinear_error_1():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32) self.value = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mstype.float32)


def construct(self): def construct(self):
interpolate = nn.Interpolate()
interpolate = nn.ResizeBilinear()
return interpolate(self.value, size=(5, 5), scale_factor=2) return interpolate(self.value, size=(5, 5), scale_factor=2)


net = Net() net = Net()

Loading…
Cancel
Save