def construct(self, x): # input_shape of x is (batch_size, 3, h, w) # feature_map1 is (batch_size, backbone_shape[2], h/8, w/8) # feature_map2 is (batch_size, backbone_shape[3], h/16, w/16) # feature_map3 is (batch_size, backbone_shape[4], h/32, w/32) img_hight = P.Shape()(x)[2] img_width = P.Shape()(x)[3] feature_map1, feature_map2, feature_map3 = self.backbone(x) con1, big_object_output = self.backblock0(feature_map3) con1 = self.conv1(con1) img_hight_16 = int(img_hight / 16) img_width_16 = int(img_width / 16) ups1 = P.ResizeNearestNeighbor((img_hight_16, img_width_16))(con1) con1 = self.concat((ups1, feature_map2)) con2, medium_object_output = self.backblock1(con1) con2 = self.conv2(con2) img_hight_8 = int(img_hight / 8) img_width_8 = int(img_width / 8) ups2 = P.ResizeNearestNeighbor((img_hight_8, img_width_8))(con2) con3 = self.concat((ups2, feature_map1)) _, small_object_output = self.backblock2(con3) return big_object_output, medium_object_output, small_object_output
def __init__(self, feature_shape, backbone_shape, backbone, out_channel): super(YOLOv3, self).__init__() self.out_channel = out_channel self.net = backbone self.backblock0 = YoloBlock(backbone_shape[-1], out_chls=backbone_shape[-2], out_channels=out_channel) self.conv1 = _conv_bn_relu(in_channel=backbone_shape[-2], out_channel=backbone_shape[-2] // 2, ksize=1) self.upsample1 = P.ResizeNearestNeighbor( (feature_shape[2] // 16, feature_shape[3] // 16)) self.backblock1 = YoloBlock(in_channels=backbone_shape[-2] + backbone_shape[-3], out_chls=backbone_shape[-3], out_channels=out_channel) self.conv2 = _conv_bn_relu(in_channel=backbone_shape[-3], out_channel=backbone_shape[-3] // 2, ksize=1) self.upsample2 = P.ResizeNearestNeighbor( (feature_shape[2] // 8, feature_shape[3] // 8)) self.backblock2 = YoloBlock(in_channels=backbone_shape[-3] + backbone_shape[-4], out_chls=backbone_shape[-4], out_channels=out_channel) self.concat = P.Concat(axis=1)
def __init__(self, in_channels, out_channels, num_outs): super(FeatPyramidNeck, self).__init__() self.num_outs = num_outs self.in_channels = in_channels self.fpn_layer = len(self.in_channels) assert not self.num_outs < len(in_channels) self.lateral_convs_list_ = [] self.fpn_convs_ = [] for _, channel in enumerate(in_channels): l_conv = _conv(channel, out_channels, kernel_size=1, stride=1, padding=0, pad_mode='valid') fpn_conv = _conv(out_channels, out_channels, kernel_size=3, stride=1, padding=0, pad_mode='same') self.lateral_convs_list_.append(l_conv) self.fpn_convs_.append(fpn_conv) self.lateral_convs_list = nn.layer.CellList(self.lateral_convs_list_) self.fpn_convs_list = nn.layer.CellList(self.fpn_convs_) self.interpolate1 = P.ResizeNearestNeighbor((48, 80)) self.interpolate2 = P.ResizeNearestNeighbor((96, 160)) self.interpolate3 = P.ResizeNearestNeighbor((192, 320)) self.maxpool = P.MaxPool(ksize=1, strides=2, padding="same")
def __init__(self, backbone, config, is_training=True): super(retinanet50, self).__init__() self.backbone = backbone feature_size = config.feature_size self.P5_1 = nn.Conv2d(2048, 256, kernel_size=1, stride=1, pad_mode='same') self.P_upsample1 = P.ResizeNearestNeighbor( (feature_size[1], feature_size[1])) self.P5_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, pad_mode='same') self.P4_1 = nn.Conv2d(1024, 256, kernel_size=1, stride=1, pad_mode='same') self.P_upsample2 = P.ResizeNearestNeighbor( (feature_size[0], feature_size[0])) self.P4_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, pad_mode='same') self.P3_1 = nn.Conv2d(512, 256, kernel_size=1, stride=1, pad_mode='same') self.P3_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, pad_mode='same') self.P6_0 = nn.Conv2d(2048, 256, kernel_size=3, stride=2, pad_mode='same') self.P7_1 = nn.ReLU() self.P7_2 = nn.Conv2d(256, 256, kernel_size=3, stride=2, pad_mode='same') self.multi_box = MultiBox(config) self.is_training = is_training if not is_training: self.activation = P.Sigmoid()
def construct(self, x): """ input_shape of x is (batch_size, 3, h, w) feature_map1 is (batch_size, backbone_shape[2], h/8, w/8) feature_map2 is (batch_size, backbone_shape[3], h/16, w/16) feature_map3 is (batch_size, backbone_shape[4], h/32, w/32) """ img_hight = P.Shape()(x)[2] img_width = P.Shape()(x)[3] # input=(1,3,608,608) # feature_map1=(1,256,76,76) # feature_map2=(1,512,38,38) # feature_map3=(1,1024,19,19) feature_map1, feature_map2, feature_map3 = self.backbone(x) con1 = self.conv1(feature_map3) con2 = self.conv2(con1) con3 = self.conv3(con2) m1 = self.maxpool1(con3) m2 = self.maxpool2(con3) m3 = self.maxpool3(con3) spp = self.concat((m3, m2, m1, con3)) con4 = self.conv4(spp) con5 = self.conv5(con4) con6 = self.conv6(con5) con7 = self.conv7(con6) ups1 = P.ResizeNearestNeighbor((img_hight / 16, img_width / 16))(con7) con8 = self.conv8(feature_map2) con9 = self.concat((ups1, con8)) con10, _ = self.backblock0(con9) con11 = self.conv9(con10) ups2 = P.ResizeNearestNeighbor((img_hight / 8, img_width / 8))(con11) con12 = self.conv10(feature_map1) con13 = self.concat((ups2, con12)) con14, small_object_output = self.backblock1(con13) con15 = self.conv11(con14) con16 = self.concat((con15, con10)) con17, medium_object_output = self.backblock2(con16) con18 = self.conv12(con17) con19 = self.concat((con18, con6)) _, big_object_output = self.backblock3(con19) return big_object_output, medium_object_output, small_object_output
def construct(self, x): size = self.shape(x) out = self.encoder(x) out = self.conv3x3(out) out = P.ResizeNearestNeighbor((size[2], size[3]), True)(out) out = self.decoder(out) return out
def construct(self, x): size = self.shape(x) out = nn.AvgPool2d(size[2])(x) out = self.conv(out) out = P.ResizeNearestNeighbor((size[2], size[3]), True)(out) # out = P.ResizeBilinear((size[2], size[3]), True)(out) # do not support GPU yet return out
def test_resize_nn_rgb_multiple(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]], [[111, 112, 113, 114, 115], [116, 117, 118, 119, 120]]], [[[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]], [[111, 112, 113, 114, 115], [116, 117, 118, 119, 120]], [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]], [[[111, 112, 113, 114, 115], [116, 117, 118, 119, 120]], [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]]).astype(np.int32)) resize_nn = P.ResizeNearestNeighbor((5, 2)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 3], [1, 3], [1, 3], [6, 8], [6, 8]], [[11, 13], [11, 13], [11, 13], [16, 18], [16, 18]], [[111, 113], [111, 113], [111, 113], [116, 118], [116, 118]]], [[[11, 13], [11, 13], [11, 13], [16, 18], [16, 18]], [[111, 113], [111, 113], [111, 113], [116, 118], [116, 118]], [[1, 3], [1, 3], [1, 3], [6, 8], [6, 8]]], [[[111, 113], [111, 113], [111, 113], [116, 118], [116, 118]], [[1, 3], [1, 3], [1, 3], [6, 8], [6, 8]], [[11, 13], [11, 13], [11, 13], [16, 18], [16, 18]]]]).astype(np.int32)) np.testing.assert_array_equal(output.asnumpy(), expected_output.asnumpy())
def construct(self, input1, input2, input3): output1 = self.output1(input1) output2 = self.output2(input2) output3 = self.output3(input3) up3 = P.ResizeNearestNeighbor( [P.Shape()(output2)[2], P.Shape()(output2)[3]])(output3) output2 = up3 + output2 output2 = self.merge2(output2) up2 = P.ResizeNearestNeighbor( [P.Shape()(output1)[2], P.Shape()(output1)[3]])(output2) output1 = up2 + output1 output1 = self.merge1(output1) return output1, output2, output3
def construct(self, x): size = self.shape(x) out = self.stem(x) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.stern_conv1(out) if self.stern_drop and self.phase == 'train': out = self.stern_drop(out) out = self.stern_conv2(out) out = P.ResizeNearestNeighbor((size[2], size[3]), True)(out) return out
def resize_nn_grayscale_align_corners(datatype): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8]]]]).astype(datatype)) resize_nn_corners_aligned = P.ResizeNearestNeighbor((3, 7), align_corners=True) output_corners_aligned = resize_nn_corners_aligned(input_tensor) resize_nn = P.ResizeNearestNeighbor((3, 7)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4], [0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8], [0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8]]]]).astype(datatype)) np.testing.assert_array_equal(output_corners_aligned.asnumpy(), expected_output.asnumpy()) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, output.asnumpy(), expected_output.asnumpy())
def test_resize_nn_rgb_align_corners(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[1, 2, 3, 4], [5, 6, 7, 8]], [[11, 12, 13, 14], [15, 16, 17, 18]], [[21, 22, 23, 24], [25, 26, 27, 28]]]]).astype(np.int32)) resize_nn_corners_aligned = P.ResizeNearestNeighbor((5, 2), align_corners=True) output_corners_aligned = resize_nn_corners_aligned(input_tensor) resize_nn = P.ResizeNearestNeighbor((5, 2)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 4], [1, 4], [5, 8], [5, 8], [5, 8]], [[11, 14], [11, 14], [15, 18], [15, 18], [15, 18]], [[21, 24], [21, 24], [25, 28], [25, 28], [25, 28]]]]).astype(np.int32)) np.testing.assert_array_equal(output_corners_aligned.asnumpy(), expected_output.asnumpy()) np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, output.asnumpy(), expected_output.asnumpy())
def resize_nn_grayscale_multiple_images(datatype): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]], [[[0.4, 0.5, 0.6], [0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]], [[[0.7, 0.8, 0.9], [0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]]]).astype(datatype)) resize_nn = P.ResizeNearestNeighbor((2, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3], [0.4, 0.4, 0.5, 0.5, 0.6, 0.6]]], [[[0.4, 0.4, 0.5, 0.5, 0.6, 0.6], [0.7, 0.7, 0.8, 0.8, 0.9, 0.9]]], [[[0.7, 0.7, 0.8, 0.8, 0.9, 0.9], [0.1, 0.1, 0.2, 0.2, 0.3, 0.3]]]]).astype(datatype)) np.testing.assert_array_equal(output.asnumpy(), expected_output.asnumpy())
def resize(inputs: _Tensor, size: Tuple[int, int], mode: str) -> _Tensor: """ Resize the intermediate layer _attribution to the same size as inputs. Args: inputs (ms.Tensor): the input tensor to be resized size (tupleint]): the targeted size resize to mode (str): the resize mode. Options: 'nearest_neighbor', 'bilinear' Returns: outputs (ms.Tensor): the resized tensor. Raises: ValueError: the resize mode is not in ['nearest_neighbor', 'bilinear']. """ h, w = size if mode == 'nearest_neighbor': resize_nn = op.ResizeNearestNeighbor((h, w)) outputs = resize_nn(inputs) elif mode == 'bilinear': inputs_np = inputs.asnumpy() inputs_np = np.transpose(inputs_np, [0, 2, 3, 1]) array_lst = [] for inp in inputs_np: array = (np.repeat(inp, 3, axis=2) * 255).astype(np.uint8) image = Image.fromarray(array) image = image.resize(size, resample=Image.BILINEAR) array = np.asarray(image).astype(np.float32) / 255 array_lst.append(array[:, :, 0:1]) resized_np = np.transpose(array_lst, [0, 3, 1, 2]) outputs = ms.Tensor(resized_np, inputs.dtype) else: raise ValueError('Unsupported resize mode {}'.format(mode)) return outputs
def __init__(self, size): super(ResizeNearestNeighborAlignCornerF, self).__init__() self.ResizeNearestNeighborAlignCornerF = P.ResizeNearestNeighbor(size, align_corners=False)
def test_resize_nn_rgb_integer_ratio(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[11, 12, 13], [14, 15, 16], [17, 18, 19]], [[111, 112, 113], [114, 115, 116], [117, 118, 119]]]]).astype(np.int32)) # larger h and w resize_nn = P.ResizeNearestNeighbor((9, 9)) output = resize_nn(input_tensor) expected_output_array = np.array( [[[[1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6], [4, 4, 4, 5, 5, 5, 6, 6, 6], [4, 4, 4, 5, 5, 5, 6, 6, 6], [7, 7, 7, 8, 8, 8, 9, 9, 9], [7, 7, 7, 8, 8, 8, 9, 9, 9], [7, 7, 7, 8, 8, 8, 9, 9, 9]], [[11, 11, 11, 12, 12, 12, 13, 13, 13], [11, 11, 11, 12, 12, 12, 13, 13, 13], [11, 11, 11, 12, 12, 12, 13, 13, 13], [14, 14, 14, 15, 15, 15, 16, 16, 16], [14, 14, 14, 15, 15, 15, 16, 16, 16], [14, 14, 14, 15, 15, 15, 16, 16, 16], [17, 17, 17, 18, 18, 18, 19, 19, 19], [17, 17, 17, 18, 18, 18, 19, 19, 19], [17, 17, 17, 18, 18, 18, 19, 19, 19]], [[111, 111, 111, 112, 112, 112, 113, 113, 113], [111, 111, 111, 112, 112, 112, 113, 113, 113], [111, 111, 111, 112, 112, 112, 113, 113, 113], [114, 114, 114, 115, 115, 115, 116, 116, 116], [114, 114, 114, 115, 115, 115, 116, 116, 116], [114, 114, 114, 115, 115, 115, 116, 116, 116], [117, 117, 117, 118, 118, 118, 119, 119, 119], [117, 117, 117, 118, 118, 118, 119, 119, 119], [117, 117, 117, 118, 118, 118, 119, 119, 119]]]]) expected_output = Tensor(np.array(expected_output_array).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h and w resize_nn = P.ResizeNearestNeighbor((1, 1)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1]], [[11]], [[111]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, larger w resize_nn = P.ResizeNearestNeighbor((1, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 1, 2, 2, 3, 3]], [[11, 11, 12, 12, 13, 13]], [[111, 111, 112, 112, 113, 113]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, smaller w resize_nn = P.ResizeNearestNeighbor((6, 1)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1], [1], [4], [4], [7], [7]], [[11], [11], [14], [14], [17], [17]], [[111], [111], [114], [114], [117], [117]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, same w resize_nn = P.ResizeNearestNeighbor((1, 3)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3]], [[11, 12, 13]], [[111, 112, 113]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, same w resize_nn = P.ResizeNearestNeighbor((6, 3)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9], [7, 8, 9]], [[11, 12, 13], [11, 12, 13], [14, 15, 16], [14, 15, 16], [17, 18, 19], [17, 18, 19]], [[111, 112, 113], [111, 112, 113], [114, 115, 116], [114, 115, 116], [117, 118, 119], [117, 118, 119]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, smaller w resize_nn = P.ResizeNearestNeighbor((3, 1)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1], [4], [7]], [[11], [14], [17]], [[111], [114], [117]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, larger w resize_nn = P.ResizeNearestNeighbor((3, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6], [7, 7, 8, 8, 9, 9]], [[11, 11, 12, 12, 13, 13], [14, 14, 15, 15, 16, 16], [17, 17, 18, 18, 19, 19]], [[111, 111, 112, 112, 113, 113], [114, 114, 115, 115, 116, 116], [117, 117, 118, 118, 119, 119]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same w, same h (identity) resize_nn = P.ResizeNearestNeighbor((3, 3)) output = resize_nn(input_tensor) np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
def construct(self, x): _, _, h, w = x.shape interpolate_op = P.ResizeNearestNeighbor( (self.y_scale * h, self.x_scale * w)) return interpolate_op(x)
def construct(self, x): size = self.shape(x) out = nn.AvgPool2d(size[2])(x) out = self.conv(out) out = P.ResizeNearestNeighbor((size[2], size[3]), True)(out) return out
def resize_nn_grayscale_integer_ratio(datatype): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]]]).astype(datatype)) # larger h and w resize_nn = P.ResizeNearestNeighbor((9, 9)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3], [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3], [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3], [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6], [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6], [0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.6, 0.6, 0.6], [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9], [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9], [0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h and w resize_nn = P.ResizeNearestNeighbor((1, 1)) output = resize_nn(input_tensor) expected_output = Tensor(np.array([[[[0.1]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, larger w resize_nn = P.ResizeNearestNeighbor((1, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, smaller w resize_nn = P.ResizeNearestNeighbor((6, 1)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1], [0.1], [0.4], [0.4], [0.7], [0.7]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, same w resize_nn = P.ResizeNearestNeighbor((1, 3)) output = resize_nn(input_tensor) expected_output = Tensor(np.array([[[[0.1, 0.2, 0.3]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, same w resize_nn = P.ResizeNearestNeighbor((6, 3)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9], [0.7, 0.8, 0.9]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, smaller w resize_nn = P.ResizeNearestNeighbor((3, 1)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1], [0.4], [0.7]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, larger w resize_nn = P.ResizeNearestNeighbor((3, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[0.1, 0.1, 0.2, 0.2, 0.3, 0.3], [0.4, 0.4, 0.5, 0.5, 0.6, 0.6], [0.7, 0.7, 0.8, 0.8, 0.9, 0.9]]]]).astype(datatype)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same w, same h (identity) resize_nn = P.ResizeNearestNeighbor((3, 3)) output = resize_nn(input_tensor) np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
def __init__(self, size=None, align_corners=False): super(NetResizeNearestNeighbor, self).__init__() self.op = P.ResizeNearestNeighbor(size=size, align_corners=align_corners)
def __init__(self, size): super(ResizeNearestNeighborAlignCornerT, self).__init__() self.ResizeNearestNeighborAlignCornerT = P.ResizeNearestNeighbor(size, align_corners=True)
def test_resize_nn_rgb_not_integer_ratio(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") input_tensor = Tensor( np.array([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2]], [[11, 12, 13, 14], [15, 16, 17, 18], [19, 10, 11, 12]], [[111, 112, 113, 114], [115, 116, 117, 118], [119, 110, 111, 112]]]]).astype(np.int32)) # larger h and w resize_nn = P.ResizeNearestNeighbor((7, 7)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 1, 2, 2, 3, 3, 4], [1, 1, 2, 2, 3, 3, 4], [1, 1, 2, 2, 3, 3, 4], [5, 5, 6, 6, 7, 7, 8], [5, 5, 6, 6, 7, 7, 8], [9, 9, 0, 0, 1, 1, 2], [9, 9, 0, 0, 1, 1, 2]], [[11, 11, 12, 12, 13, 13, 14], [11, 11, 12, 12, 13, 13, 14], [11, 11, 12, 12, 13, 13, 14], [15, 15, 16, 16, 17, 17, 18], [15, 15, 16, 16, 17, 17, 18], [19, 19, 10, 10, 11, 11, 12], [19, 19, 10, 10, 11, 11, 12]], [[111, 111, 112, 112, 113, 113, 114], [111, 111, 112, 112, 113, 113, 114], [111, 111, 112, 112, 113, 113, 114], [115, 115, 116, 116, 117, 117, 118], [115, 115, 116, 116, 117, 117, 118], [119, 119, 110, 110, 111, 111, 112], [119, 119, 110, 110, 111, 111, 112]]]]).astype(np.int32)) # smaller h and w resize_nn = P.ResizeNearestNeighbor((2, 3)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3], [5, 6, 7]], [[11, 12, 13], [15, 16, 17]], [[111, 112, 113], [115, 116, 117]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, larger w resize_nn = P.ResizeNearestNeighbor((2, 7)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 1, 2, 2, 3, 3, 4], [5, 5, 6, 6, 7, 7, 8]], [[11, 11, 12, 12, 13, 13, 14], [15, 15, 16, 16, 17, 17, 18]], [[111, 111, 112, 112, 113, 113, 114], [115, 115, 116, 116, 117, 117, 118]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, smaller w resize_nn = P.ResizeNearestNeighbor((5, 3)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3], [1, 2, 3], [5, 6, 7], [5, 6, 7], [9, 0, 1]], [[11, 12, 13], [11, 12, 13], [15, 16, 17], [15, 16, 17], [19, 10, 11]], [[111, 112, 113], [111, 112, 113], [115, 116, 117], [115, 116, 117], [119, 110, 111]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # smaller h, same w resize_nn = P.ResizeNearestNeighbor((2, 4)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3, 4], [5, 6, 7, 8]], [[11, 12, 13, 14], [15, 16, 17, 18]], [[111, 112, 113, 114], [115, 116, 117, 118]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # larger h, same w resize_nn = P.ResizeNearestNeighbor((8, 4)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [5, 6, 7, 8], [5, 6, 7, 8], [5, 6, 7, 8], [9, 0, 1, 2], [9, 0, 1, 2]], [[11, 12, 13, 14], [11, 12, 13, 14], [11, 12, 13, 14], [15, 16, 17, 18], [15, 16, 17, 18], [15, 16, 17, 18], [19, 10, 11, 12], [19, 10, 11, 12]], [[111, 112, 113, 114], [111, 112, 113, 114], [111, 112, 113, 114], [115, 116, 117, 118], [115, 116, 117, 118], [115, 116, 117, 118], [119, 110, 111, 112], [119, 110, 111, 112]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, smaller w resize_nn = P.ResizeNearestNeighbor((3, 2)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 3], [5, 7], [9, 1]], [[11, 13], [15, 17], [19, 11]], [[111, 113], [115, 117], [119, 111]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same h, larger w resize_nn = P.ResizeNearestNeighbor((3, 6)) output = resize_nn(input_tensor) expected_output = Tensor( np.array([[[[1, 1, 2, 3, 3, 4], [5, 5, 6, 7, 7, 8], [9, 9, 0, 1, 1, 2]], [[11, 11, 12, 13, 13, 14], [15, 15, 16, 17, 17, 18], [19, 19, 10, 11, 11, 12]], [[111, 111, 112, 113, 113, 114], [115, 115, 116, 117, 117, 118], [119, 119, 110, 111, 111, 112]]]]).astype(np.int32)) np.testing.assert_array_equal(expected_output.asnumpy(), output.asnumpy()) # same w, same h (identity) resize_nn = P.ResizeNearestNeighbor((3, 4)) output = resize_nn(input_tensor) np.testing.assert_array_equal(output.asnumpy(), input_tensor.asnumpy())
def __init__(self): super(Net, self).__init__() self.upsample = P.ResizeNearestNeighbor((2, 2))
def construct(self, x): size = self.shape(x) _, _, c3, c4 = self.backbone(x) out = self.head(c3, c4) out = P.ResizeNearestNeighbor((size[2], size[3]), True)(out) return out