diff --git a/community/cv/CPNet/eval.py b/community/cv/CPNet/eval.py index 2b0c400fc6312e12f69e095610e8de063450b3bb..689264790546df571fbd9f2ad17b5d403ba6f8d1 100644 --- a/community/cv/CPNet/eval.py +++ b/community/cv/CPNet/eval.py @@ -137,7 +137,8 @@ def net_process(model, image, mean, std=None, flip=True): _, _, h_i, w_i = input_.shape _, _, h_o, w_o = output.shape if (h_o != h_i) or (w_o != w_i): - output = ops.interpolate(output, size=(h_i, w_i), align_corners=True) + bi_linear = nn.ResizeBilinear() + output = bi_linear(output, size=(h_i, w_i), align_corners=True) softmax = nn.Softmax(axis=1) output = softmax(output) if flip: diff --git a/community/cv/CPNet/src/model/cpnet.py b/community/cv/CPNet/src/model/cpnet.py index b13c90303814f099a98cecf2a9fabc6bb4eec57a..06582d2d39d1c9c07ec3670934353fffb113b269 100644 --- a/community/cv/CPNet/src/model/cpnet.py +++ b/community/cv/CPNet/src/model/cpnet.py @@ -34,15 +34,16 @@ class ResNet(nn.Cell): self.layer3 = resnet.layer2 self.layer4 = resnet.layer3 self.layer5 = resnet.layer4 - self.resize_ops = ops.ResizeBilinearV2(True) - + self.resize_ops = ops.ResizeBilinear( + (60, 60), True + ) def construct(self, x): """ ResNet process """ x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x_aux = self.layer4(x) - x = self.resize_ops(self.layer5(x_aux), (60, 60)) + x = self.resize_ops(self.layer5(x_aux)) return x @@ -211,6 +212,7 @@ class CPNet(nn.Cell): 3, padding=1, ) + self.resize_bilinear = nn.ResizeBilinear() self.sigmoid = ops.Sigmoid() self.bmm = ops.BatchMatMul() self.cat = ops.Concat(axis=1) @@ -223,7 +225,7 @@ class CPNet(nn.Cell): H = inputs.shape[2] W = inputs.shape[3] if H != 480 or W != 480: - inputs = ops.interpolate(inputs, size=(480, 480), align_corners=True) + inputs = self.resize_bilinear(inputs, size=(480, 480), align_corners=True) conv4 = self.backbone(inputs) batch_size, _, height, width = conv4.shape value = self.aggregation(conv4) diff --git a/community/cv/CPNet/src/utils/metrics.py b/community/cv/CPNet/src/utils/metrics.py index 1d788639ba06631a96d87a02eb8018e1c2528a5d..d46fbb2c31db7e8109cfc0f26e4d4f6ec9dd999c 100644 --- a/community/cv/CPNet/src/utils/metrics.py +++ b/community/cv/CPNet/src/utils/metrics.py @@ -41,6 +41,7 @@ class SoftmaxCrossEntropyLoss(nn.Cell): self.sum = P.ReduceSum(False) self.div = P.RealDiv() self.transpose = P.Transpose() + self.resize = nn.ResizeBilinear() self.shape = P.Shape() #self.reshape = P.Reshape() diff --git a/community/cv/SRFlow/src/model/RRDBNet.py b/community/cv/SRFlow/src/model/RRDBNet.py index 3fe0798a27a12bf1c4c5409c5e088b9c9c8cfec0..cb9bbd8131549da22082bf321f046492f82e6705 100644 --- a/community/cv/SRFlow/src/model/RRDBNet.py +++ b/community/cv/SRFlow/src/model/RRDBNet.py @@ -136,7 +136,8 @@ class RRDBNet(nn.Cell): fea_up4 = resize_nearest_neighbor(feature) fea_up4 = self.upconv2(fea_up4) - fea_up0 = ops.interpolate(last_lr_fea, size=(shape(last_lr_fea)[2] // 2, shape(last_lr_fea)[3] // 2)) + resize_bilinear = nn.ResizeBilinear() + fea_up0 = resize_bilinear(x=last_lr_fea, size=(shape(last_lr_fea)[2] // 2, shape(last_lr_fea)[3] // 2)) keys = ['fea_up0', 'fea_up1', 'fea_up2', 'fea_up4'] diff --git a/community/cv/pointrend/maskrcnn/maskrcnn_mobilenetv1/fpn_neck.py b/community/cv/pointrend/maskrcnn/maskrcnn_mobilenetv1/fpn_neck.py index 761e6653c716ce5e61d6c7a7f83545b70e7e52dc..4d553fbc9e22024a56538f9808f7d484ef6f8a53 100644 --- a/community/cv/pointrend/maskrcnn/maskrcnn_mobilenetv1/fpn_neck.py +++ b/community/cv/pointrend/maskrcnn/maskrcnn_mobilenetv1/fpn_neck.py @@ -89,7 +89,9 @@ class FeatPyramidNeck(nn.Cell): 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.interpolate = P.ResizeBilinearV2() + self.interpolate1 = P.ResizeBilinear((48, 80)) + self.interpolate2 = P.ResizeBilinear((96, 160)) + self.interpolate3 = P.ResizeBilinear((192, 320)) self.cast = P.Cast() self.maxpool = P.MaxPool(kernel_size=1, strides=2, pad_mode="same") @@ -100,9 +102,9 @@ class FeatPyramidNeck(nn.Cell): x += (self.lateral_convs_list[i](inputs[i]),) y = (x[3],) - y = y + (x[2] + self.cast(self.interpolate(y[self.fpn_layer - 4], (48, 80)), self.platform_mstype),) - y = y + (x[1] + self.cast(self.interpolate(y[self.fpn_layer - 3], (96, 160)), self.platform_mstype),) - y = y + (x[0] + self.cast(self.interpolate(y[self.fpn_layer - 2], (192, 320)), self.platform_mstype),) + y = y + (x[2] + self.cast(self.interpolate1(y[self.fpn_layer - 4]), self.platform_mstype),) + y = y + (x[1] + self.cast(self.interpolate2(y[self.fpn_layer - 3]), self.platform_mstype),) + y = y + (x[0] + self.cast(self.interpolate3(y[self.fpn_layer - 2]), self.platform_mstype),) z = () for i in range(self.fpn_layer - 1, -1, -1): diff --git a/community/cv/pointrend/maskrcnn_pointrend/src/maskrcnnPointRend_r50.py b/community/cv/pointrend/maskrcnn_pointrend/src/maskrcnnPointRend_r50.py index 4f7a7fc0d4252392739fad0818fb425b342fb1ec..3f695f61533d3ae63889113b8f3bb01b139251e6 100644 --- a/community/cv/pointrend/maskrcnn_pointrend/src/maskrcnnPointRend_r50.py +++ b/community/cv/pointrend/maskrcnn_pointrend/src/maskrcnnPointRend_r50.py @@ -199,6 +199,7 @@ class maskrcnn_r50_pointrend(Mask_Rcnn_Mobilenetv1): '''_subdivision_inference''' mask_logits = None pred_classes = self.concat(pred_classes) + resize_bilinear = nn.ResizeBilinear() mask_features_list = [] features_scales = [] for k in self.mask_point_in_features_int: @@ -213,7 +214,7 @@ class maskrcnn_r50_pointrend(Mask_Rcnn_Mobilenetv1): self.mask_point_subdivision_init_resolution, ) else: - mask_logits = ops.interpolate(mask_logits, scale_factor=2, align_corners=False) + mask_logits = resize_bilinear(mask_logits, scale_factor=2, align_corners=False) gt_class_logits = mask_logits[ np.arange(mask_logits.shape[0]), pred_classes ] diff --git a/community/cv/wgan_gp/src/resgan_model.py b/community/cv/wgan_gp/src/resgan_model.py index 1f15ee2193e1018fca42b4e84a68c6b14cf40c3e..626f7463a74eb9081bec80ddcd07e6788c898208 100644 --- a/community/cv/wgan_gp/src/resgan_model.py +++ b/community/cv/wgan_gp/src/resgan_model.py @@ -28,9 +28,9 @@ class UpsampleConv(nn.Cell): super(UpsampleConv, self).__init__() self.conv = nn.Conv2d(input_dim, output_dim, filter_size, 1, 'pad', padding, has_bias=has_bias) - + self.up = nn.ResizeBilinear() def construct(self, x): - out = ops.interpolate(x, scale_factor=2) + out = self.up(x, scale_factor=2) out = self.conv(out) return out diff --git a/official/cv/CycleGAN/src/models/unet.py b/official/cv/CycleGAN/src/models/unet.py index f700396596f73b3af0028a8e2c7cbf8ffd3250fd..ffb317eb5129b2803d4721c5fce13d3a453b1a69 100644 --- a/official/cv/CycleGAN/src/models/unet.py +++ b/official/cv/CycleGAN/src/models/unet.py @@ -121,6 +121,6 @@ class UnetSkipConnectionBlock(nn.Cell): out = self.model(x) if self.skip_connections: if x.shape[-1] != out.shape[-1]: - out = ops.ResizeBilinearV2()(out, x.shape) + out = ops.ResizeBilinear(x.shape)(out) out = self.concat((out, x)) return out diff --git a/official/cv/DBNet/src/modules/asf.py b/official/cv/DBNet/src/modules/asf.py index 5675d6aee123fa2690d93f02028112ce79fffd51..90da664af4f8e665248f4e706ecf28f3b38ad809 100644 --- a/official/cv/DBNet/src/modules/asf.py +++ b/official/cv/DBNet/src/modules/asf.py @@ -42,6 +42,7 @@ class ScaleFeatureSelection(nn.Cell): out_features_num) else: exit(1) + self.interpolate = nn.ResizeBilinear() def weights_init(self, c): for m in c.cells(): @@ -59,7 +60,7 @@ class ScaleFeatureSelection(nn.Cell): exit(1) if self.type not in ['scale_channel_spatial', 'scale_spatial']: shape = features_list[0].shape[2:] - score = ops.interpolate(score, shape) + score = self.interpolate(score, shape) x = [] for i in range(self.out_features_num): diff --git a/official/cv/DeepLabV3P/src/deeplab_v3plus.py b/official/cv/DeepLabV3P/src/deeplab_v3plus.py index 17c35f660c11a639104593c0f4c1fd0dcf9db618..3f553bb4947f2c09ce46ca8aaca4b33351b1f3da 100644 --- a/official/cv/DeepLabV3P/src/deeplab_v3plus.py +++ b/official/cv/DeepLabV3P/src/deeplab_v3plus.py @@ -250,5 +250,5 @@ class DeepLabV3Plus(nn.Cell): low_level_features = self.relu(low_level_features) out = self.concat((out, low_level_features)) out = self.last_conv(out) - out = P.ResizeBilinearV2(True)(out, (size[2], size[3])) + out = P.ResizeBilinear((size[2], size[3]), True)(out) return out diff --git a/official/cv/DeepLabv3/src/nets/deeplab_v3/deeplab_v3.py b/official/cv/DeepLabv3/src/nets/deeplab_v3/deeplab_v3.py index c18963de8e129cc36e93fa21779ee65b2024a5ac..5e6c463ef9ce5df1771db8e377263343c86a6054 100644 --- a/official/cv/DeepLabv3/src/nets/deeplab_v3/deeplab_v3.py +++ b/official/cv/DeepLabv3/src/nets/deeplab_v3/deeplab_v3.py @@ -215,5 +215,5 @@ class DeepLabV3(nn.Cell): size = self.shape(x) out = self.resnet(x) out = self.aspp(out) - out = P.ResizeBilinearV2(True)(out, (size[2], size[3])) + out = P.ResizeBilinear((size[2], size[3]), True)(out) return out diff --git a/official/cv/MaskRCNN/maskrcnn_mobilenetv1/src/maskrcnn_mobilenetv1/fpn_neck.py b/official/cv/MaskRCNN/maskrcnn_mobilenetv1/src/maskrcnn_mobilenetv1/fpn_neck.py index 2ad832bbd6825ee8e843df141866ef68303b0de2..38ca57bb22996d9763dd6e02584b028674785f64 100644 --- a/official/cv/MaskRCNN/maskrcnn_mobilenetv1/src/maskrcnn_mobilenetv1/fpn_neck.py +++ b/official/cv/MaskRCNN/maskrcnn_mobilenetv1/src/maskrcnn_mobilenetv1/fpn_neck.py @@ -89,7 +89,9 @@ class FeatPyramidNeck(nn.Cell): 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.interpolate = P.ResizeBilinearV2() + self.interpolate1 = P.ResizeBilinear((48, 80)) + self.interpolate2 = P.ResizeBilinear((96, 160)) + self.interpolate3 = P.ResizeBilinear((192, 320)) self.cast = P.Cast() self.maxpool = P.MaxPool(kernel_size=1, strides=2, pad_mode="same") @@ -99,9 +101,9 @@ class FeatPyramidNeck(nn.Cell): x += (self.lateral_convs_list[i](inputs[i]),) y = (x[3],) - y = y + (x[2] + self.cast(self.interpolate(y[self.fpn_layer - 4], (48, 80)), self.platform_mstype),) - y = y + (x[1] + self.cast(self.interpolate(y[self.fpn_layer - 3], (96, 160)), self.platform_mstype),) - y = y + (x[0] + self.cast(self.interpolate(y[self.fpn_layer - 2], (192, 320)), self.platform_mstype),) + y = y + (x[2] + self.cast(self.interpolate1(y[self.fpn_layer - 4]), self.platform_mstype),) + y = y + (x[1] + self.cast(self.interpolate2(y[self.fpn_layer - 3]), self.platform_mstype),) + y = y + (x[0] + self.cast(self.interpolate3(y[self.fpn_layer - 2]), self.platform_mstype),) z = () for i in range(self.fpn_layer - 1, -1, -1): diff --git a/official/cv/MaskRCNN/maskrcnn_resnet50/src/maskrcnn/fpn_neck.py b/official/cv/MaskRCNN/maskrcnn_resnet50/src/maskrcnn/fpn_neck.py index 68590ec9db4630ce763821e8d1f685c8dc90f5a4..6a98de5d2c0b8b80587f7c9330979795e7d77ca8 100644 --- a/official/cv/MaskRCNN/maskrcnn_resnet50/src/maskrcnn/fpn_neck.py +++ b/official/cv/MaskRCNN/maskrcnn_resnet50/src/maskrcnn/fpn_neck.py @@ -92,8 +92,9 @@ class FeatPyramidNeck(nn.Cell): 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.interpolate = P.ResizeBilinearV2() - self.feature_shapes = feature_shapes + self.interpolate1 = P.ResizeBilinear(feature_shapes[2]) + self.interpolate2 = P.ResizeBilinear(feature_shapes[1]) + self.interpolate3 = P.ResizeBilinear(feature_shapes[0]) self.cast = P.Cast() self.maxpool = P.MaxPool(kernel_size=1, strides=2, pad_mode="same") @@ -103,9 +104,9 @@ class FeatPyramidNeck(nn.Cell): x += (self.lateral_convs_list[i](inputs[i]),) y = (x[3],) - y = y + (x[2] + self.cast(self.interpolate(y[self.fpn_layer - 4], self.feature_shapes[2]), self.cast_type),) - y = y + (x[1] + self.cast(self.interpolate(y[self.fpn_layer - 3], self.feature_shapes[1]), self.cast_type),) - y = y + (x[0] + self.cast(self.interpolate(y[self.fpn_layer - 2], self.feature_shapes[0]), self.cast_type),) + y = y + (x[2] + self.cast(self.interpolate1(y[self.fpn_layer - 4]), self.cast_type),) + y = y + (x[1] + self.cast(self.interpolate2(y[self.fpn_layer - 3]), self.cast_type),) + y = y + (x[0] + self.cast(self.interpolate3(y[self.fpn_layer - 2]), self.cast_type),) z = () for i in range(self.fpn_layer - 1, -1, -1): diff --git a/official/cv/OCRNet/eval.py b/official/cv/OCRNet/eval.py index 08d0f7617f4470cc08d5ccaeca1357120461285f..615664edb30eac7989197fea2ee994b872fc3584 100644 --- a/official/cv/OCRNet/eval.py +++ b/official/cv/OCRNet/eval.py @@ -94,7 +94,7 @@ def testval(dataset, helper, model, num_classes=19, ignore_label=255, scales=Non flip=flip) if pred.shape[-2] != shape[-2] or pred.shape[-1] != shape[-1]: - pred = P.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) # Tensor + pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred) # Tensor confusion_matrix += get_confusion_matrix(label, pred, shape, num_classes, ignore_label) count += 1 diff --git a/official/cv/OCRNet/src/basedataset.py b/official/cv/OCRNet/src/basedataset.py index 104ee830f131389cf35e7e5b033cbd1df131b8ec..692443736a2a41b478e4cc8e344940dd2e8e1918 100644 --- a/official/cv/OCRNet/src/basedataset.py +++ b/official/cv/OCRNet/src/basedataset.py @@ -139,12 +139,12 @@ class BaseDataset: shape = image.shape pred = model(image) pred = pred[-1] # image NCHW - pred = P.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) + pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred) if flip: flip_img = image.asnumpy()[:, :, :, ::-1] flip_output = model(Tensor(flip_img.copy())) flip_output = flip_output[-1] - flip_output = P.ResizeBilinearV2()(flip_output, (shape[-2], shape[-1])) + flip_output = P.ResizeBilinear((shape[-2], shape[-1]))(flip_output) flip_pred = flip_output.asnumpy() flip_pred = Tensor(flip_pred[:, :, :, ::-1]) pred = P.Add()(pred, flip_pred) @@ -206,7 +206,7 @@ class BaseDataset: count[:, :, h0:h1, w0:w1] += 1 preds = preds / count preds = preds[:, :, :height, :width] - preds = P.ResizeBilinearV2()(preds, (ori_height, ori_width)) + preds = P.ResizeBilinear((ori_height, ori_width))(preds) final_pred += preds final_pred = P.Add()(final_pred, preds) return final_pred diff --git a/official/cv/OCRNet/src/callback.py b/official/cv/OCRNet/src/callback.py index acf68b4b3d393dbc5272de984341f2aefaaa5c85..2cf6ff0be7aba64f5c2335dd86ed9b0670cc935d 100644 --- a/official/cv/OCRNet/src/callback.py +++ b/official/cv/OCRNet/src/callback.py @@ -52,7 +52,7 @@ def evaluate_model(net, data_helper, num_classes, ignore_label): shape = label.shape pred = net(image) pred = pred[-1] - pred = P.ResizeBilinearV2()(pred, (shape[-2], shape[-1])).asnumpy() + pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred).asnumpy() confusion_matrix += get_confusion_matrix(label, pred, shape, num_classes, ignore_label) pos = confusion_matrix.sum(1) res = confusion_matrix.sum(0) diff --git a/official/cv/OCRNet/src/cityscapes.py b/official/cv/OCRNet/src/cityscapes.py index 3dbe7a51944fad9b74a09a53a9a6f25a5ddb92a6..be290412f93d5ced99012a4f0213106624a2e5b1 100644 --- a/official/cv/OCRNet/src/cityscapes.py +++ b/official/cv/OCRNet/src/cityscapes.py @@ -163,6 +163,6 @@ class Cityscapes(BaseDataset): preds = preds / count preds = preds[:, :, :height, :width] preds = Tensor(preds) - preds = P.ResizeBilinearV2()(preds, (ori_height, ori_width)) + preds = P.ResizeBilinear((ori_height, ori_width))(preds) final_pred = P.Add()(final_pred, preds) return final_pred diff --git a/official/cv/OCRNet/src/loss.py b/official/cv/OCRNet/src/loss.py index 869cbecaffd202ffa209f793c37da2746fa6821d..5842464a4ba390391c12071b5253b3a85794bac6 100644 --- a/official/cv/OCRNet/src/loss.py +++ b/official/cv/OCRNet/src/loss.py @@ -38,8 +38,7 @@ class CrossEntropyWithLogits(LossBase): def __init__(self, num_classes=19, ignore_label=255, image_size=None): super(CrossEntropyWithLogits, self).__init__() - self.image_size = image_size - self.resize = F.ResizeBilinearV2() + self.resize = F.ResizeBilinear(image_size) self.one_hot = P.OneHot(axis=-1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) @@ -57,7 +56,7 @@ class CrossEntropyWithLogits(LossBase): def construct(self, logits, labels): """Loss construction.""" - logits = self.resize(logits, self.image_size) + logits = self.resize(logits) labels_int = self.cast(labels, mstype.int32) labels_int = self.reshape(labels_int, (-1,)) logits_ = self.transpose(logits, (0, 2, 3, 1)) @@ -130,13 +129,14 @@ class CrossEntropy(nn.Cell): self.weights = config.loss.balance_weights self.num_outputs = config.model.num_outputs self.align_corners = config.model.align_corners + self.resize_bilinear = nn.ResizeBilinear() self.concat = P.Concat() def _forward(self, score, target): ph, pw = score.shape[2], score.shape[3] h, w = target.shape[1], target.shape[2] if ph != h or pw != w: - score = F.interpolate(score, size=(h, w), align_corners=self.align_corners) + score = self.resize_bilinear(score, size=(h, w), align_corners=self.align_corners) loss = self.criterion(score, target) return loss diff --git a/official/cv/OCRNet/src/seg_hrnet.py b/official/cv/OCRNet/src/seg_hrnet.py index 49d3740abfc3d438b5cc02a9ca93224984b8decc..cf32f4027ba42285fd69c33d3c47e100fa90ec5e 100644 --- a/official/cv/OCRNet/src/seg_hrnet.py +++ b/official/cv/OCRNet/src/seg_hrnet.py @@ -147,6 +147,7 @@ class HighResolutionModule(nn.Cell): self.fuse_layers = self._make_fuse_layers() self.relu = nn.ReLU() self.add = ops.Add() + self.resize_bilinear = nn.ResizeBilinear() def _check_branches(self, num_branches, num_blocks, num_inchannels, num_channels): """Check branches.""" @@ -273,7 +274,7 @@ class HighResolutionModule(nn.Cell): height_output = x[i].shape[-2] t = self.flayer[j](x[j]) # t = ops.ResizeNearestNeighbor((height_output, width_output))(t) - y = self.add(y, ops.interpolate(t, size=(height_output, width_output))) + y = self.add(y, self.resize_bilinear(t, size=(height_output, width_output))) else: y = self.add(y, flayer[j](x[j])) x_fuse.append(self.relu(y)) @@ -342,6 +343,7 @@ class HighResolutionNet(nn.Cell): last_inp_channels = np.int(np.sum(pre_stage_channels)) + self.resize_bilinear = nn.ResizeBilinear() self.last_layer = nn.SequentialCell([ nn.Conv2d(in_channels=last_inp_channels, @@ -492,9 +494,9 @@ class HighResolutionNet(nn.Cell): out1, out2, out3, out4 = x h, w = ops.Shape()(out1)[2:] x1 = ops.Cast()(out1, mstype.float32) - x2 = ops.interpolate(out2, size=(h, w)) - x3 = ops.interpolate(out3, size=(h, w)) - x4 = ops.interpolate(out4, size=(h, w)) + x2 = self.resize_bilinear(out2, size=(h, w)) + x3 = self.resize_bilinear(out3, size=(h, w)) + x4 = self.resize_bilinear(out4, size=(h, w)) x = self.concat((x1, x2, x3, x4)) diff --git a/official/cv/OCRNet/src/seg_hrnet_ocr.py b/official/cv/OCRNet/src/seg_hrnet_ocr.py index 6cbd664a71daab6758c18d4cb5e2a5fd7c43859a..95d613c4a9eca3d88c65145411aedd2fd3f3b179 100644 --- a/official/cv/OCRNet/src/seg_hrnet_ocr.py +++ b/official/cv/OCRNet/src/seg_hrnet_ocr.py @@ -214,7 +214,7 @@ class _ObjectAttentionBlock(nn.Cell): context = context.view(batch_size, self.key_channels, *x.shape[2:]) context = self.f_up(context) if self.scale > 1: - context = P.ResizeBilinearV2(align_corners=True)(context, (h, w)) + context = P.ResizeBilinear(size=(h, w), align_corners=True)(context) return context @@ -369,6 +369,7 @@ class HighResolutionModule(nn.Cell): self.fuse_layers = self._make_fuse_layers() self.relu = nn.ReLU() self.add = ops.Add() + self.resize_bilinear = nn.ResizeBilinear() def _check_branches(self, num_branches, num_blocks, num_inchannels, num_channels): """Check branches.""" @@ -495,8 +496,8 @@ class HighResolutionModule(nn.Cell): height_output = x[i].shape[-2] t = self.fuse_layers[i][j](x[j]) # t = ops.ResizeNearestNeighbor((height_output, width_output))(t) - y = self.add(y, ops.interpolate(t, size=(height_output, width_output), - align_corners=ALIGN_CORNERS)) + y = self.add(y, self.resize_bilinear(t, size=(height_output, width_output), + align_corners=ALIGN_CORNERS)) else: y = self.add(y, self.fuse_layers[i][j](x[j])) x_fuse.append(self.relu(y)) @@ -569,6 +570,8 @@ class HighResolutionNet(nn.Cell): ocr_mid_channels = config.model.ocr.mid_channels ocr_key_channels = config.model.ocr.key_channels + self.resize_bilinear = nn.ResizeBilinear() + self.conv3x3_ocr = nn.SequentialCell([ nn.Conv2d(last_inp_channels, ocr_mid_channels, has_bias=False, kernel_size=3, stride=1, pad_mode='pad', padding=1), @@ -731,9 +734,9 @@ class HighResolutionNet(nn.Cell): out1, out2, out3, out4 = x h, w = ops.Shape()(out1)[2:] x1 = ops.Cast()(out1, mstype.float32) - x2 = ops.interpolate(out2, size=(h, w), align_corners=ALIGN_CORNERS) - x3 = ops.interpolate(out3, size=(h, w), align_corners=ALIGN_CORNERS) - x4 = ops.interpolate(out4, size=(h, w), align_corners=ALIGN_CORNERS) + x2 = self.resize_bilinear(out2, size=(h, w), align_corners=ALIGN_CORNERS) + x3 = self.resize_bilinear(out3, size=(h, w), align_corners=ALIGN_CORNERS) + x4 = self.resize_bilinear(out4, size=(h, w), align_corners=ALIGN_CORNERS) x2 = ops.Cast()(x2, mstype.float32) x3 = ops.Cast()(x3, mstype.float32) diff --git a/official/cv/PVNet/src/model_reposity.py b/official/cv/PVNet/src/model_reposity.py index d15330cd9cabec39b23097e0f4cb647573043533..fd969c6996fd6032bd3f2f7d75f9c44ed4109956 100644 --- a/official/cv/PVNet/src/model_reposity.py +++ b/official/cv/PVNet/src/model_reposity.py @@ -17,7 +17,6 @@ import math import mindspore import mindspore.nn as nn -import mindspore.ops as P import mindspore.ops.operations as ops from mindspore.common.initializer import HeUniform, Uniform from mindspore.nn.loss.loss import LossBase @@ -53,16 +52,19 @@ class Resnet18_8s(nn.Cell): nn.Conv2d(128 + fcdim, s8dim, 3, 1, 'pad', 1, has_bias=False, weight_init=self.conv_init), nn.BatchNorm2d(s8dim), nn.LeakyReLU(0.1)]) + self.up8sto4s = nn.ResizeBilinear() # x4s->64 self.conv4s = nn.SequentialCell([ nn.Conv2d(64 + s8dim, s4dim, 3, 1, 'pad', 1, has_bias=False, weight_init=self.conv_init), nn.BatchNorm2d(s4dim), nn.LeakyReLU(0.1)]) + self.up4sto2s = nn.ResizeBilinear() # x2s->64 self.conv2s = nn.SequentialCell([ nn.Conv2d(64 + s4dim, s2dim, 3, 1, 'pad', 1, has_bias=False, weight_init=self.conv_init), nn.BatchNorm2d(s2dim), nn.LeakyReLU(0.1)]) + self.up2storaw = nn.ResizeBilinear() self.convraw = nn.SequentialCell([ nn.Conv2d(3 + s2dim, raw_dim, 3, 1, 'pad', 1, has_bias=False, weight_init=self.conv_init), @@ -81,12 +83,12 @@ class Resnet18_8s(nn.Cell): """construct Network""" x2s, x4s, x8s, _, _, xfc = self.resnet18_8s(x) fm = self.conv8s(self.concat((xfc, x8s))) - fm = P.interpolate(fm, scale_factor=2, align_corners=True) # up8sto4s + fm = self.up8sto4s(fm, scale_factor=2, align_corners=True) fm = self.conv4s(self.concat((fm, x4s))) - fm = P.interpolate(fm, scale_factor=2, align_corners=True) # up4sto2s + fm = self.up4sto2s(fm, scale_factor=2, align_corners=True) fm = self.conv2s(self.concat((fm, x2s))) - fm = P.interpolate(fm, scale_factor=2, align_corners=True) # up2storaw + fm = self.up2storaw(fm, scale_factor=2, align_corners=True) x = self.convraw(self.concat((fm, x))) diff --git a/official/cv/ResNet/src/callback.py b/official/cv/ResNet/src/callback.py index c34fa6fd558430046b4698eda50747d889a0d669..249bf1aea757cd869e0889a789dbbe7c2bce41a5 100644 --- a/official/cv/ResNet/src/callback.py +++ b/official/cv/ResNet/src/callback.py @@ -156,7 +156,7 @@ class EvalCallBack(Callback): eval_cost = time.time() - eval_start self.logger.info("epoch: {}, {}: {}, eval_cost:{:.2f}".format(cur_epoch, self.metrics_name, res, eval_cost)) if res >= self.best_res: - if ms.context.get_context("enable_ge") and int(os.getenv('MS_DISABLE_REF_MODE')) == 1: + if ms.context.get_context("enable_ge") and int(os.getenv('MS_DISABLE_REF_MODE', default=0)) == 1: from mindspore.train.callback import _set_cur_net _set_cur_net(cb_params.train_network) cb_params.train_network.exec_checkpoint_graph() diff --git a/official/cv/SSD/src/fpn.py b/official/cv/SSD/src/fpn.py index f2befc5ba3a4aa4688d78261e8ccaa28ff1a3c0c..83beebd70d949035d24856524f6ce004a1a71eaa 100644 --- a/official/cv/SSD/src/fpn.py +++ b/official/cv/SSD/src/fpn.py @@ -47,7 +47,7 @@ class FpnTopDown(nn.Cell): top = len(inputs) - i - 1 down = top - 1 size = ops.shape(inputs[down]) - top_down = ops.ResizeBilinearV2()(features[-1], (size[2], size[3])) + top_down = ops.ResizeBilinear((size[2], size[3]))(features[-1]) top_down = top_down + image_features[down] features = features + (top_down,) diff --git a/official/cv/Unet/src/unet_nested/unet_parts.py b/official/cv/Unet/src/unet_nested/unet_parts.py index c7c002b4eb00ab2e49b9d28dee7de0eb28677f6a..31a4bb90d8f4fa1d1bb361df32397848c9170020 100644 --- a/official/cv/Unet/src/unet_nested/unet_parts.py +++ b/official/cv/Unet/src/unet_nested/unet_parts.py @@ -76,7 +76,7 @@ class UnetUp(nn.Cell): output = self.up_conv(high_feature) else: _, _, h, w = F.shape(high_feature) - output = P.ResizeBilinearV2()(high_feature, (h * 2, w * 2)) + output = P.ResizeBilinear((h * 2, w * 2))(high_feature) output = self.up_conv(output) for feature in low_feature: output = self.concat((output, feature)) diff --git a/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py b/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py index 72cdec353cfc8ad001908150d83d3b2654939b74..a34098bd93c5b3cdbe545427adf6960b4f6d49d3 100644 --- a/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py +++ b/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py @@ -14,7 +14,6 @@ # ============================================================================ """GPT training wrapper""" -import mindspore as ms import mindspore.nn as nn from mindspore.ops import operations as P from mindspore.ops import composite as C @@ -22,11 +21,6 @@ from mindspore.ops import functional as F from mindspore.common.tensor import Tensor import mindspore.common.dtype as mstype from mindspore.nn.wrap.loss_scale import TrainOneStepWithLossScaleCell -from mindspore import context, Parameter -from mindspore.context import ParallelMode -from mindspore.nn.wrap.grad_reducer import DistributedGradReducer -from mindspore.communication.management import get_group_size -from mindspore.ops.operations.math_ops import NPUGetFloatStatusV2, NPUClearFloatStatusV2 from mindspore.parallel._utils import _get_enable_parallel_optimizer from src.utils import GlobalNorm, ClipByGlobalNorm @@ -164,7 +158,7 @@ class PanguAlphaTrainOneStepWithLossScaleCell(TrainOneStepWithLossScaleCell): return loss, cond, scaling_sens.value() -class PanguAlphaTrainPipelineWithLossScaleCell(nn.Cell): +class PanguAlphaTrainPipelineWithLossScaleCell(TrainOneStepWithLossScaleCell): """ Encapsulation class of PanguAlpha network training. @@ -177,7 +171,7 @@ class PanguAlphaTrainPipelineWithLossScaleCell(nn.Cell): scale_update_cell (Cell): Cell to do the loss scale. Default: None. """ def __init__(self, network, optimizer, config, scale_update_cell=None, enable_global_norm=True): - super(PanguAlphaTrainPipelineWithLossScaleCell, self).__init__(auto_prefix=False) + super(PanguAlphaTrainPipelineWithLossScaleCell, self).__init__(network, optimizer, scale_update_cell) self.config = config self.network = network self.network.add_flags(defer_inline=True) @@ -185,32 +179,8 @@ class PanguAlphaTrainPipelineWithLossScaleCell(nn.Cell): self.accu_grads = self.weights.clone(prefix="accu_grads", init="zeros") self.optimizer = optimizer self.enable_global_norm = enable_global_norm - self.grad = C.GradOperation(get_by_list=True, - sens_param=True) - self.reducer_flag = False - self.allreduce = P.AllReduce() - self.parallel_mode = context.get_auto_parallel_context("parallel_mode") - if self.parallel_mode in [ParallelMode.DATA_PARALLEL, ParallelMode.HYBRID_PARALLEL]: - self.reducer_flag = True - self.grad_reducer = F.identity - self.degree = 1 - if self.reducer_flag: - self.degree = get_group_size() - self.grad_reducer = DistributedGradReducer(optimizer.parameters, False, self.degree) - self.is_distributed = (self.parallel_mode != ParallelMode.STAND_ALONE) self.cast = P.Cast() - self.get_status = NPUGetFloatStatusV2() - self.clear_before_grad = NPUClearFloatStatusV2() - self.reduce_sum = P.ReduceSum(keep_dims=False) - self.base = Tensor(1, mstype.float32) - self.less_equal = P.LessEqual() - self.hyper_map = C.HyperMap() - self.loss_scale = None - self.reshape = P.Reshape() - self.loss_scaling_manager = scale_update_cell - if scale_update_cell: - self.loss_scale = Parameter(Tensor(scale_update_cell.get_loss_scale(), dtype=mstype.float32), - name="loss_scale") + self.status = Tensor([0] * 8, mstype.int32) self.clip = ClipByGlobalNorm(self.weights, self.config) self.micro_size = config.parallel_config.micro_batch_num self.opt_shard = _get_enable_parallel_optimizer() @@ -231,38 +201,20 @@ class PanguAlphaTrainPipelineWithLossScaleCell(nn.Cell): """Defines the computation performed.""" weights = self.weights loss = self.network(input_ids, input_position, attention_mask) - if sens is None: - scaling_sens = self.loss_scale - scaling_sens = self.reshape(scaling_sens, (1,)) - else: - scaling_sens = sens - # alloc status and clear should be right before gradoperation - init = Tensor([0]*8, dtype=ms.int32) - status_clear = self.clear_before_grad(init) - scaling_sens = F.depend(scaling_sens, status_clear) + scaling_sens = self.scale_sense + scaling_sens_filled = C.ones_like(loss) * F.cast(scaling_sens, F.dtype(loss)) grads = self.grad(self.network, weights)(input_ids, input_position, attention_mask, - self.cast(scaling_sens / self.micro_size, + self.cast(scaling_sens_filled / self.micro_size, mstype.float32)) - init = F.depend(init, grads) - get_status = self.get_status(init) - flag_sum = self.reduce_sum(get_status, (0,)) - loss = F.depend(loss, status_clear) - if self.is_distributed: - # sum overflow flag over devices - flag_reduce = self.allreduce(flag_sum) - cond = self.less_equal(self.base, flag_reduce) - else: - cond = self.less_equal(self.base, flag_sum) - grads = F.depend(grads, cond) # apply grad reducer on grads if self.opt_shard: grads = self.grad_reducer(grads) - grads = self.hyper_map(F.partial(shard_grad_scale, scaling_sens * self.degree), grads, self.accu_grads) + grads = self.hyper_map(F.partial(shard_grad_scale, scaling_sens), grads, self.accu_grads) else: accu_grads = self.grad_reducer(self.accu_grads) - grads = self.hyper_map(F.partial(grad_scale, scaling_sens * self.degree), grads, accu_grads) + grads = self.hyper_map(F.partial(grad_scale, scaling_sens), grads, accu_grads) clip_value = self.clip_value if self.enable_global_norm: grads, _ = self.clip(grads) @@ -270,9 +222,9 @@ class PanguAlphaTrainPipelineWithLossScaleCell(nn.Cell): grads = self.hyper_map( F.partial(clip_grad, GRADIENT_CLIP_TYPE, GRADIENT_CLIP_VALUE), grads) - overflow = cond - if sens is None: - overflow = self.loss_scaling_manager(self.loss_scale, cond) + cond = self.get_overflow_status(self.status, grads) + cond = F.depend(cond, grads) + overflow = self.process_loss_scale(cond) if not overflow: if self.enable_offload: self.optimizer(grads, clip_value) diff --git a/research/cv/ArbitraryStyleTransfer/src/networks.py b/research/cv/ArbitraryStyleTransfer/src/networks.py index f790b1a1b8b84e8ce90392f574033aa90205f425..f893d1e2513fabe70c3e76a97b4ac1871a484dfd 100644 --- a/research/cv/ArbitraryStyleTransfer/src/networks.py +++ b/research/cv/ArbitraryStyleTransfer/src/networks.py @@ -264,7 +264,11 @@ class UpsampleConvInReluWithStyle(nn.Cell): activation=nn.ReLU): super(UpsampleConvInReluWithStyle, self).__init__() self.upsample = upsample - self.platform = args.platform + if self.upsample: + if args.platform == 'GPU': + self.upsample_layer = nn.ResizeBilinear() + elif args.platform == 'Ascend': + self.upsample_layer = nn.ResizeBilinear().to_float(mstype.float16) pad_mode = "CONSTANT" # The original paper uses reflect, but the performance of reflect in mindspore r1.3 is too poor padding = (kernel_size - 1) // 2 @@ -298,10 +302,7 @@ class UpsampleConvInReluWithStyle(nn.Cell): gamma = self.cast(gamma, mindspore.float32) beta = self.cast(beta, mindspore.float16) if self.upsample: - if self.platform == 'GPU': - x = ops.interpolate(x, scale_factor=self.upsample) - elif self.platform == 'Ascend': - x = ops.interpolate(x, scale_factor=self.upsample).to_float(mstype.float16) + x = self.upsample_layer(x, scale_factor=self.upsample) x = self.pad(x) x = self.conv(x) x = self.instancenorm(x) diff --git a/research/cv/Auto-DeepLab/src/core/aspp.py b/research/cv/Auto-DeepLab/src/core/aspp.py index 71bc1297e8b49ce83bf4016eca6ef2d91e2fda00..1dabd2870c7afa004d8a20f2120dbbbfd9405ab0 100644 --- a/research/cv/Auto-DeepLab/src/core/aspp.py +++ b/research/cv/Auto-DeepLab/src/core/aspp.py @@ -30,6 +30,7 @@ class ASPP(nn.Cell): super(ASPP, self).__init__() out_channels = 256 self.global_pooling = ops.ReduceMean(keep_dims=True) + self.upsample = nn.ResizeBilinear() self.cat = ops.Concat(axis=1) self.aspp1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, @@ -73,7 +74,7 @@ class ASPP(nn.Cell): x4_0 = self.global_pooling(x, (-2, -1)) x4_1 = self.aspp5(x4_0) x4_2 = self.aspp5_bn(x4_1) - x4 = ops.interpolate(x4_2, (x.shape[2], x.shape[3]), None, align_corners=True) + x4 = self.upsample(x4_2, (x.shape[2], x.shape[3]), None, True) x5 = self.cat((x0, x1, x2, x3, x4)) x6 = self.conv2(x5) diff --git a/research/cv/Auto-DeepLab/src/core/decoder.py b/research/cv/Auto-DeepLab/src/core/decoder.py index deea79c7373c24b8fb25bb925cb17ee211756714..3544d5e8609d36d8a8b45c887d5ef975d962e4e5 100644 --- a/research/cv/Auto-DeepLab/src/core/decoder.py +++ b/research/cv/Auto-DeepLab/src/core/decoder.py @@ -36,6 +36,7 @@ class Decoder(nn.Cell): self.conv1 = nn.Conv2d(low_level_inplanes, c_low, 1, has_bias=False) self.bn1 = NormReLU(c_low, momentum, eps, parallel=parallel) + self.interpolate = nn.ResizeBilinear() self.cat = ops.Concat(axis=1) self.last_conv = nn.SequentialCell( nn.Conv2d(c_cat, 256, 3, 1, pad_mode='same', has_bias=False, weight_init='HeNormal'), @@ -51,7 +52,7 @@ class Decoder(nn.Cell): low_level_feat_0 = self.conv1(low_level_feat) low_level_feat_1 = self.bn1(low_level_feat_0) - high_level_feat_0 = ops.interpolate(high_level_feat, (low_level_feat_1.shape[2:]), None, align_corners=True) + high_level_feat_0 = self.interpolate(high_level_feat, (low_level_feat_1.shape[2:]), None, True) concat_feat = self.cat((high_level_feat_0, low_level_feat_1)) output = self.last_conv(concat_feat) return output diff --git a/research/cv/Auto-DeepLab/src/core/encoder.py b/research/cv/Auto-DeepLab/src/core/encoder.py index 9f9260f268a0e9aabba46b5298c3390cd79ae51c..17340ea9cef885311b7a7e54def90c89703b1fa1 100644 --- a/research/cv/Auto-DeepLab/src/core/encoder.py +++ b/research/cv/Auto-DeepLab/src/core/encoder.py @@ -73,6 +73,7 @@ class NASBaseCell(nn.Cell): self.C_prev_prev = int(block_multiplier * prev_prev_fmultiplier) self.block_multiplier = self.scast(block_multiplier, mindspore.int32) + self.interpolate = nn.ResizeBilinear() self.pre_preprocess = ReLUConvBN(self.C_prev_prev, self.C_out, 1, 1, 0, 'pad', args.bn_momentum, args.bn_eps, args.affine, args.use_ABN, args.parallel) self.preprocess = ReLUConvBN(self.C_prev, self.C_out, 1, 1, 0, 'pad', args.bn_momentum, args.bn_eps, @@ -110,8 +111,8 @@ class NASBaseCell(nn.Cell): feature_size_h = scale_dimension(prev_input.shape[2], self.scale) feature_size_w = scale_dimension(prev_input.shape[3], self.scale) - s1 = ops.interpolate(prev_input, (feature_size_h, feature_size_w), None, align_corners=True) - s0 = ops.interpolate(prev_prev_input, (s1.shape[2], s1.shape[3]), None, align_corners=True) + s1 = self.interpolate(prev_input, (feature_size_h, feature_size_w), None, True) + s0 = self.interpolate(prev_prev_input, (s1.shape[2], s1.shape[3]), None, True) process_s0 = self.pre_preprocess(s0) process_s1 = self.preprocess(s1) diff --git a/research/cv/Auto-DeepLab/src/core/model.py b/research/cv/Auto-DeepLab/src/core/model.py index 6f7eff75fd3eb97f1fdf8acb78667fe0929e61ac..14d7e0fd5c55be190fe50321121bd93ec27a27ab 100644 --- a/research/cv/Auto-DeepLab/src/core/model.py +++ b/research/cv/Auto-DeepLab/src/core/model.py @@ -16,7 +16,7 @@ import numpy as np import mindspore.nn as nn -import mindspore.ops as ops + from .aspp import ASPP from .decoder import Decoder from .encoder import get_default_arch, Encoder @@ -51,10 +51,12 @@ class AutoDeepLab(nn.Cell): args.bn_eps, parallel=args.parallel) + self.resize_bilinear = nn.ResizeBilinear() + def construct(self, x): """construct""" encoder_output, low_level_feature = self.encoder(x) high_level_feature = self.aspp(encoder_output) decoder_output = self.decoder(high_level_feature, low_level_feature) - output = ops.interpolate(decoder_output, (x.shape[2], x.shape[3]), None, align_corners=True) + output = self.resize_bilinear(decoder_output, (x.shape[2], x.shape[3]), None, True) return output diff --git a/research/cv/CGAN/eval.py b/research/cv/CGAN/eval.py index e530fcf1384581536a627adfd75c3a88b9abd53c..38ee45be90d74ef8f80d69820a5f0bb1a43448bb 100644 --- a/research/cv/CGAN/eval.py +++ b/research/cv/CGAN/eval.py @@ -82,14 +82,14 @@ def main(): steps = np.ceil(generator_size / batch_size).astype(np.int32) sample_list = [] - resize = P.ResizeBilinearV2() + resize = P.ResizeBilinear((28, 28)) for step in range(steps): noise = Tensor(np.random.normal(size=(batch_size, input_dim)), dtype=mstype.float32) label = Tensor((np.arange(noise.shape[0]) % 10), dtype=mstype.int32) samples = netG(noise, label) # 10000 - samples = resize(samples, (28, 28)) + samples = resize(samples) samples = samples.asnumpy() sample_list.append(samples) reporter.visualizer_eval(samples, step) diff --git a/research/cv/CGAN/infer_onnx.py b/research/cv/CGAN/infer_onnx.py index bbeb4f775be06054e0e0c64c129d21a00f8a9983..558fef43250ed922736602faa30487464b24bc13 100644 --- a/research/cv/CGAN/infer_onnx.py +++ b/research/cv/CGAN/infer_onnx.py @@ -85,7 +85,7 @@ def run_eval(): steps = np.ceil(generator_size / batch_size).astype(np.int32) sample_list = [] - resize = P.ResizeBilinearV2() + resize = P.ResizeBilinear((28, 28)) for step in range(steps): noise = Tensor(np.random.normal(size=(batch_size, input_dim)), @@ -95,7 +95,7 @@ def run_eval(): samples = np.array(samples) samples = samples[0] samples = Tensor(samples, dtype=mstype.float32) - samples = resize(samples, (28, 28)) + samples = resize(samples) samples = samples.asnumpy() sample_list.append(samples) reporter.visualizer_eval(samples, step) diff --git a/research/cv/CSNL/src/Attention.py b/research/cv/CSNL/src/Attention.py index d457818e5f99c106378f84024e2f89778e6067fd..65e5b14338c62336bbb7cdd43ad08c11f2e2fc94 100644 --- a/research/cv/CSNL/src/Attention.py +++ b/research/cv/CSNL/src/Attention.py @@ -122,8 +122,8 @@ class CrossScaleAttention(nn.Cell): split = ops.Split(0, raw_w.shape[0]) raw_w_groups = split(raw_w) - reseze_bilinear = ops.ResizeBilinearV2() - ref = reseze_bilinear(inputs, (inputs.shape[2] // self.scale, inputs.shape[3] // self.scale)) + reseze_bilinear = ops.ResizeBilinear((inputs.shape[2] // self.scale, inputs.shape[3] // self.scale)) + ref = reseze_bilinear(inputs) ref = self.conv_match_2(ref) w = extract_image_patches(self.unfold_2, ref, ksizes=self.ksize, strides=self.stride, rates=1, padding='same') diff --git a/research/cv/CTSDG/src/generator/cfa.py b/research/cv/CTSDG/src/generator/cfa.py index 2ab8f375a5f393318e46ba164858f1d32f34de13..914bbc98d5e5b807a32c2eb446ed85f531ab4486 100644 --- a/research/cv/CTSDG/src/generator/cfa.py +++ b/research/cv/CTSDG/src/generator/cfa.py @@ -47,7 +47,7 @@ class RAL(nn.Cell): """construct""" # accelerated calculation bs, _, h, w = foreground.shape - foreground = ops.ResizeBilinearV2(align_corners=True)(foreground, (h // self.rate, w // self.rate)) + foreground = ops.ResizeBilinear(size=(h // self.rate, w // self.rate), align_corners=True)(foreground) foreground_size = foreground.shape background_size = background.shape diff --git a/research/cv/CTSDG/src/generator/generator.py b/research/cv/CTSDG/src/generator/generator.py index ebdbbc30c6ce44b983c49ec68e424e678c108eee..fc04a9e23299c4fba2d3e6a31d95f26d8005a027 100644 --- a/research/cv/CTSDG/src/generator/generator.py +++ b/research/cv/CTSDG/src/generator/generator.py @@ -115,7 +115,7 @@ class Generator(nn.Cell): ec_out_skip = ec_outputs[i] ec_out_mask_skip = ec_outputs_masks[i] _, _, h, w = dc_inp.shape - dc_inp = ops.ResizeBilinearV2(align_corners=True)(dc_inp, (2 * h, 2 * w)) # align_corners = False + dc_inp = ops.ResizeBilinear(size=(2 * h, 2 * w), align_corners=True)(dc_inp) # align_corners = False dc_inp_mask = ops.ResizeNearestNeighbor(size=(2 * h, 2 * w))(dc_inp_mask) dc_inp = ops.Concat(axis=1)([dc_inp, ec_out_skip]) @@ -158,7 +158,7 @@ class Generator(nn.Cell): output_atten = self.cfa(output, output) output = self.fusion_layer2(ops.Concat(axis=1)((output, output_atten))) _, _, h, w = output.shape - output = ops.ResizeBilinearV2(align_corners=True)(output, (2 * h, 2 * w)) # align_corners = False + output = ops.ResizeBilinear(size=(2 * h, 2 * w), align_corners=True)(output) # align_corners = False output = self.out_layer(ops.Concat(axis=1)((output, output_bigff))) return output, projected_image, projected_edge diff --git a/research/cv/DDM/eval.py b/research/cv/DDM/eval.py index d0b2b6e6f58be6e720f8eceacd717880e7fded56..64f94cae007d468787967db6e3b935732e547369 100644 --- a/research/cv/DDM/eval.py +++ b/research/cv/DDM/eval.py @@ -23,7 +23,7 @@ import os import argparse import numpy as np import mindspore.dataset as ds -from mindspore.ops import ResizeBilinearV2 +from mindspore.ops import ResizeBilinear from mindspore.train.serialization import load_checkpoint, load_param_into_net from mindspore import context, Tensor import mindspore.common.dtype as mstype @@ -68,13 +68,12 @@ def evaluate(model, testloader, num_class, fixed_test_size=True, verbose=True): nt = 0 for ti in test_iter: image, label = Tensor(ti["data"], mstype.float32), Tensor(ti["label"], mstype.float32) - interp = ResizeBilinearV2(align_corners=True) - pred_main = model(Tensor(image, mstype.float32))[1] if not fixed_test_size: - output = interp(pred_main, (label.shape[1], label.shape[2])).asnumpy()[0] + interp = ResizeBilinear(size=(label.shape[1], label.shape[2]), align_corners=True) else: - output = interp(pred_main, (cfg.TEST.OUTPUT_SIZE[1], cfg.TEST.OUTPUT_SIZE[0])).asnumpy()[0] - + interp = ResizeBilinear(size=(cfg.TEST.OUTPUT_SIZE[1], cfg.TEST.OUTPUT_SIZE[0]), align_corners=True) + pred_main = model(Tensor(image, mstype.float32))[1] + output = interp(pred_main).asnumpy()[0] output = output.transpose((1, 2, 0)) output = np.argmax(output, axis=2) label = label.asnumpy()[0] diff --git a/research/cv/DDRNet/src/models/ddrnet23.py b/research/cv/DDRNet/src/models/ddrnet23.py index e065421182dad119f51ab537da6f0ebc1a115aaf..cc79a629c7079feee14d9ea88dfadcb8aac63c3d 100644 --- a/research/cv/DDRNet/src/models/ddrnet23.py +++ b/research/cv/DDRNet/src/models/ddrnet23.py @@ -195,8 +195,8 @@ class DualResNet(nn.Cell): self.linear = nn.Dense(last_planes, num_classes) self.width_output = self.height_output = image_size // 8 - self.resize_1 = ops.ResizeBilinearV2() - self.resize_2 = ops.ResizeBilinearV2() + self.resize_1 = ops.ResizeBilinear(size=(self.width_output, self.height_output)) + self.resize_2 = ops.ResizeBilinear(size=(self.width_output, self.height_output)) self.weigit_init() def weigit_init(self): @@ -234,12 +234,12 @@ class DualResNet(nn.Cell): x_ = self.layer3_(self.relu(layer2_out)) x = layer3_out + self.down3(self.relu(x_)) - x_ = x_ + self.resize_1(self.compression3(self.relu(layer3_out)), (self.width_output, self.height_output)) + x_ = x_ + self.resize_1(self.compression3(self.relu(layer3_out))) layer4_out = self.layer4(self.relu(x)) x_ = self.layer4_(self.relu(x_)) x = layer4_out + self.down4(self.relu(x_)) - x_ = x_ + self.resize_2(self.compression4(self.relu(layer4_out)), (self.width_output, self.height_output)) + x_ = x_ + self.resize_2(self.compression4(self.relu(layer4_out))) x = self.layer5(self.relu(x)) + self.down5(self.relu(self.layer5_(self.relu(x_)))) x = self.last_layer(self.relu(x)) x = self.pool(x, [2, 3]) diff --git a/research/cv/DRNet/eval.py b/research/cv/DRNet/eval.py index ec2586f68868e24ffcadd5794fcab2ee43d60796..f9d046c142791b376c46472740f5de2faa010ea2 100644 --- a/research/cv/DRNet/eval.py +++ b/research/cv/DRNet/eval.py @@ -18,7 +18,6 @@ eval. import argparse import mindspore -import mindspore.ops as ops from mindspore import context from mindspore.train.serialization import load_checkpoint, load_param_into_net from src.dataset import create_dataset @@ -56,17 +55,18 @@ if __name__ == '__main__': gumbel_softmax = GumbleSoftmax() predictor_net.set_train(False) classifier_net.set_train(False) + interpolate = mindspore.nn.ResizeBilinear() resize_ratio = [224, 168, 112] img_tot = 0 top1_correct = 0 for batch_idx, imgs in enumerate(test_loader): images, target = mindspore.Tensor(imgs['image']), mindspore.Tensor(imgs['label']) - predictor_input = ops.interpolate(images, size=(128, 128)) + predictor_input = interpolate(images, size=(128, 128)) predictor_ratio_score = predictor_net(predictor_input) predictor_ratio_score_gumbel = gumbel_softmax(predictor_ratio_score) output = 0 for j, r in enumerate(resize_ratio): - new_images = ops.interpolate(images, size=(r, r)) + new_images = interpolate(images, size=(r, r)) new_output = classifier_net(new_images, int(j)) output += predictor_ratio_score_gumbel[:, j:j+1] * new_output output = output.asnumpy() diff --git a/research/cv/DecoMR/models/WithLossCellDP.py b/research/cv/DecoMR/models/WithLossCellDP.py index 65cba92849471a4d1b8f696afbc202cbbf463f6a..894a642f2bd74e65e3b2e07e38204af1d29a2e17 100644 --- a/research/cv/DecoMR/models/WithLossCellDP.py +++ b/research/cv/DecoMR/models/WithLossCellDP.py @@ -49,9 +49,9 @@ class WithLossCellDP(nn.Cell): pred_mask_shape = self.expand_dims(pred_dp_shape[:, 0], 1) pred_uv_shape = pred_dp_shape[:, 1:] - interpolate_bilinear = ops.ResizeBilinearV2() + interpolate_bilinear = ops.ResizeBilinear((gt_dp.shape[2], gt_dp.shape[3])) interpolate_nearest = ops.ResizeNearestNeighbor((gt_dp.shape[2], gt_dp.shape[3])) - pred_mask_shape = interpolate_bilinear(pred_mask_shape, (gt_dp.shape[2], gt_dp.shape[3])) + pred_mask_shape = interpolate_bilinear(pred_mask_shape) pred_uv_shape = interpolate_nearest(pred_uv_shape) if weight is not None: diff --git a/research/cv/DecoMR/models/WithLossCellEnd.py b/research/cv/DecoMR/models/WithLossCellEnd.py index f02111e847e064fe4a88e9a8fa66f8f97d11cf6e..3f157a9e938472e4d5115cb72dca123d1d237116 100644 --- a/research/cv/DecoMR/models/WithLossCellEnd.py +++ b/research/cv/DecoMR/models/WithLossCellEnd.py @@ -29,11 +29,9 @@ from models.uv_generator import Index_UV_Generator def generate_Tensor(temp): return Tensor(temp, dtype=mindspore.float32) - def generate_Tensor_Int(temp): return Tensor(temp, dtype=mindspore.int32) - class WithLossCellEnd(nn.Cell): def __init__(self, DMR, options, uv_weight, tv_factor, auto_prefix=False): super(WithLossCellEnd, self).__init__(auto_prefix=False) @@ -95,9 +93,9 @@ class WithLossCellEnd(nn.Cell): pred_mask_shape = self.expand_dims(pred_dp_shape[:, 0], 1) pred_uv_shape = pred_dp_shape[:, 1:] - interpolate_bilinear = ops.ResizeBilinearV2() + interpolate_bilinear = ops.ResizeBilinear((gt_dp.shape[2], gt_dp.shape[3])) interpolate_nearest = ops.ResizeNearestNeighbor((gt_dp.shape[2], gt_dp.shape[3])) - pred_mask_shape = interpolate_bilinear(pred_mask_shape, (gt_dp.shape[2], gt_dp.shape[3])) + pred_mask_shape = interpolate_bilinear(pred_mask_shape) pred_uv_shape = interpolate_nearest(pred_uv_shape) if weight is not None: diff --git a/research/cv/DecoMR/models/resnet.py b/research/cv/DecoMR/models/resnet.py index c84e9c39e00cb003fcf80f377f257fbc3ea0374e..21a929df935abf131748fb0c61aeca03a0e777a5 100644 --- a/research/cv/DecoMR/models/resnet.py +++ b/research/cv/DecoMR/models/resnet.py @@ -15,7 +15,6 @@ import mindspore as ms import mindspore.nn as nn -import mindspore.ops as ops from mindspore import load_checkpoint affine_par = True @@ -192,6 +191,7 @@ class ResNetLocate(nn.Cell): infos.append(nn.SequentialCell(nn.Conv2d(self.in_planes, ii, 3, 1, 1, bias=False), nn.ReLU())) self.infos = nn.CellList(infos) + self.resize_bilinear = nn.ResizeBilinear() self.cat = ms.ops.Concat(axis=1) def load_pretrained_model(self, model): @@ -206,15 +206,14 @@ class ResNetLocate(nn.Cell): xs_1 = self.ppms_pre(xs[-1]) xls = [xs_1] for k in range(len(self.ppms)): - xls.append(ops.interpolate(self.ppms[k](xs_1), xs_1.size()[2:], align_corners=True)) + xls.append(self.resize_bilinear(self.ppms[k](xs_1), xs_1.size()[2:], align_corners=True)) xls = self.ppm_cat(self.cat(xls)) top_score = None infos = [] for k in range(len(self.infos)): infos.append(self.infos[k]( - ops.interpolate(xls, xs[len(self.infos) - 1 - k].size()[2:], align_corners=True)) - ) + self.resize_bilinear(xls, xs[len(self.infos) - 1 - k].size()[2:], align_corners=True))) return xs, top_score, infos diff --git a/research/cv/DecoMR/models/upsample.py b/research/cv/DecoMR/models/upsample.py index eafbd3332df8f1565d262a25be317d41aaddc820..7f8a2731172f184d86de85c3d54a680bb4d34511 100644 --- a/research/cv/DecoMR/models/upsample.py +++ b/research/cv/DecoMR/models/upsample.py @@ -15,13 +15,12 @@ from __future__ import division import mindspore.nn as nn -import mindspore.ops as ops class Upsample(nn.Cell): def __init__(self): super(Upsample, self).__init__() - self.interpolate = ops.interpolate + self.upsample = nn.ResizeBilinear() def construct(self, x): - y = self.interpolate(x, scale_factor=2) + y = self.upsample(x, scale_factor=2) return y diff --git a/research/cv/E-NET/src/model.py b/research/cv/E-NET/src/model.py index 7629cfada6cb0fd36e78fddc38e6a231c4bd971a..f148083953a4d6dc4f5ca0903c496921f2ccdc4a 100644 --- a/research/cv/E-NET/src/model.py +++ b/research/cv/E-NET/src/model.py @@ -356,7 +356,7 @@ class UpsampleNeck(nn.Cell): """construct""" main = self.main_conv1(x) _, _, h, w = F.shape(main) - main = P.ResizeBilinearV2()(main, (h * 2, w * 2)) + main = P.ResizeBilinear((h * 2, w * 2))(main) # Extension branch ext = self.ext_conv1(x) ext = self.ext_tconv1(ext) diff --git a/research/cv/EGnet/src/egnet.py b/research/cv/EGnet/src/egnet.py index 22516a3b0b53d45d08abe47a0f2159ebd154c4b2..9679d68341e9bf0d3165420151a1999d218db353 100644 --- a/research/cv/EGnet/src/egnet.py +++ b/research/cv/EGnet/src/egnet.py @@ -16,7 +16,6 @@ """EGNet model define""" import mindspore -import mindspore.ops as ops import mindspore.common.initializer as init from mindspore import nn, load_checkpoint @@ -85,6 +84,7 @@ class MergeLayer1(nn.Cell): trans.append(nn.SequentialCell([nn.Conv2d(512, 128, 1, 1, has_bias=False), nn.ReLU()])) self.trans, self.up, self.score = nn.CellList(trans), nn.CellList(up), nn.CellList(score) self.relu = nn.ReLU() + self.resize_bilinear = nn.ResizeBilinear() def construct(self, list_x, x_size): """ @@ -99,29 +99,29 @@ class MergeLayer1(nn.Cell): u_tmp = tmp # layer6 -> layer0 - up_sal.append(ops.interpolate(self.score[num_f - 1](tmp), x_size, align_corners=True)) + up_sal.append(self.resize_bilinear(self.score[num_f - 1](tmp), x_size, align_corners=True)) # layer5 layer4 layer3 for j in range(2, num_f): i = num_f - j # different channel, use trans layer, or resize and add directly if list_x[i].shape[1] < u_tmp.shape[1]: - u_tmp = list_x[i] + ops.interpolate((self.trans[i](u_tmp)), list_x[i].shape[2:], - align_corners=True) + u_tmp = list_x[i] + self.resize_bilinear((self.trans[i](u_tmp)), list_x[i].shape[2:], + align_corners=True) else: - u_tmp = list_x[i] + ops.interpolate(u_tmp, list_x[i].shape[2:], align_corners=True) + u_tmp = list_x[i] + self.resize_bilinear(u_tmp, list_x[i].shape[2:], align_corners=True) # Conv tmp = self.up[i](u_tmp) u_tmp = tmp sal_feature.append(tmp) - up_sal.append(ops.interpolate(self.score[i](tmp), x_size, align_corners=True)) + up_sal.append(self.resize_bilinear(self.score[i](tmp), x_size, align_corners=True)) - u_tmp = list_x[0] + ops.interpolate(self.trans[-1](sal_feature[0]), list_x[0].shape[2:], - align_corners=True) + u_tmp = list_x[0] + self.resize_bilinear(self.trans[-1](sal_feature[0]), list_x[0].shape[2:], + align_corners=True) tmp = self.up[0](u_tmp) # layer 2 edge_feature.append(tmp) - up_edge.append(ops.interpolate(self.score[0](tmp), x_size, align_corners=True)) + up_edge.append(self.resize_bilinear(self.score[0](tmp), x_size, align_corners=True)) tuple_up_edge, tuple_edge_feature, tuple_up_sal, tuple_sal_feature = (), (), (), () for i in up_edge: tuple_up_edge += (i,) @@ -171,6 +171,7 @@ class MergeLayer2(nn.Cell): self.final_score = nn.SequentialCell([nn.Conv2d(list_k[0][0], list_k[0][0], 5, 1, has_bias=True), nn.ReLU(), nn.Conv2d(list_k[0][0], 1, 3, 1, has_bias=True)]) self.relu = nn.ReLU() + self.resize_bilinear = nn.ResizeBilinear() def construct(self, list_x, list_y, x_size): """ @@ -181,16 +182,16 @@ class MergeLayer2(nn.Cell): for i, i_x in enumerate(list_x): for j, j_x in enumerate(list_y): - tmp = ops.interpolate(self.trans[i][j](j_x), i_x.shape[2:], align_corners=True) + i_x + tmp = self.resize_bilinear(self.trans[i][j](j_x), i_x.shape[2:], align_corners=True) + i_x tmp_f = self.up[i][j](tmp) - up_score.append(ops.interpolate(self.score[i][j](tmp_f), x_size, align_corners=True)) + up_score.append(self.resize_bilinear(self.score[i][j](tmp_f), x_size, align_corners=True)) tmp_feature.append(tmp_f) tmp_fea = tmp_feature[0] for i_fea in range(len(tmp_feature) - 1): - tmp_fea = self.relu(tmp_fea + ops.interpolate(tmp_feature[i_fea + 1], tmp_feature[0].shape[2:], + tmp_fea = self.relu(tmp_fea + self.resize_bilinear(tmp_feature[i_fea + 1], tmp_feature[0].shape[2:], align_corners=True)) - up_score.append(ops.interpolate(self.final_score(tmp_fea), x_size, align_corners=True)) + up_score.append(self.resize_bilinear(self.final_score(tmp_fea), x_size, align_corners=True)) return up_score diff --git a/research/cv/EGnet/src/resnet.py b/research/cv/EGnet/src/resnet.py index 8aac018097c1eb09f4c43e13cffc735d08976902..2796e57d993dc6fa77adc208d75a08814804b590 100644 --- a/research/cv/EGnet/src/resnet.py +++ b/research/cv/EGnet/src/resnet.py @@ -17,7 +17,6 @@ import mindspore as ms import mindspore.nn as nn -import mindspore.ops as ops from mindspore import load_checkpoint affine_par = True @@ -194,6 +193,7 @@ class ResNetLocate(nn.Cell): infos.append(nn.SequentialCell(nn.Conv2d(self.in_planes, ii, 3, 1, 1, bias=False), nn.ReLU())) self.infos = nn.CellList(infos) + self.resize_bilinear = nn.ResizeBilinear() self.cat = ms.ops.Concat(axis=1) def load_pretrained_model(self, model): @@ -208,15 +208,14 @@ class ResNetLocate(nn.Cell): xs_1 = self.ppms_pre(xs[-1]) xls = [xs_1] for k in range(len(self.ppms)): - xls.append(ops.interpolate(self.ppms[k](xs_1), xs_1.size()[2:], align_corners=True)) + xls.append(self.resize_bilinear(self.ppms[k](xs_1), xs_1.size()[2:], align_corners=True)) xls = self.ppm_cat(self.cat(xls)) top_score = None infos = [] for k in range(len(self.infos)): infos.append(self.infos[k]( - ops.interpolate(xls, xs[len(self.infos) - 1 - k].size()[2:], align_corners=True)) - ) + self.resize_bilinear(xls, xs[len(self.infos) - 1 - k].size()[2:], align_corners=True))) return xs, top_score, infos diff --git a/research/cv/FCANet/src/model/fcanet.py b/research/cv/FCANet/src/model/fcanet.py index bef0136cab27823855f093acc6566e214a231e08..a5bb72ba44baabb0b4a93591eb7a2132b8258dd7 100644 --- a/research/cv/FCANet/src/model/fcanet.py +++ b/research/cv/FCANet/src/model/fcanet.py @@ -26,7 +26,7 @@ from mindspore.parallel._auto_parallel_context import auto_parallel_context from mindspore import Parameter from src.model.res2net import res2net101 -ResizeFunc = P.ResizeBilinearV2 +ResizeFunc = P.ResizeBilinear #######################################[ FCANet ]####################################### @@ -107,7 +107,7 @@ class ASPPPooling(nn.Cell): def construct(self, x): out = nn.AvgPool2d(x.shape[2:])(x) out = self.conv(out) - out = ResizeFunc(True)(out, x.shape[2:]) + out = ResizeFunc(x.shape[2:], True)(out) return out @@ -202,7 +202,7 @@ class Decoder(nn.Cell): side = self.side_conv(side) side = self.side_bn(side) side = self.relu(side) - x = ResizeFunc(True)(x, side.shape[2:]) + x = ResizeFunc(side.shape[2:], True)(x) x = self.concat((x, side)) x = self.merge_conv1(x) x = self.merge_bn1(x) @@ -326,19 +326,19 @@ class FCANet(nn.Cell): ) l1, _, _, l4 = self.resnet(img_with_anno) - first_map = ResizeFunc(True)( - get_mask_gauss(pos_mask_dist_first, 30), l1.shape[2:] + first_map = ResizeFunc(l1.shape[2:], True)( + get_mask_gauss(pos_mask_dist_first, 30) ) l1_first = self.concat((l1, first_map)) l1_first = self.first_conv(l1_first) result_first = self.first_pred_decoder(l1_first) - result_first = ResizeFunc(True)(result_first, img.shape[2:]) + result_first = ResizeFunc(img.shape[2:], True)(result_first) l4 = self.concat((l1_first, l4)) x = self.aspp(l4) x = self.decoder(x, l1) x = self.pred_decoder(x) - x = ResizeFunc(True)(x, img.shape[2:]) + x = ResizeFunc(img.shape[2:], True)(x) return [x, result_first] diff --git a/research/cv/HRNetW48_cls/src/cls_hrnet.py b/research/cv/HRNetW48_cls/src/cls_hrnet.py index d956643a11148ebcf0e0242fa8d34d3f3431cf8d..e8b81f42d56927a0aeeb52fd8e789b5df33c2fc2 100644 --- a/research/cv/HRNetW48_cls/src/cls_hrnet.py +++ b/research/cv/HRNetW48_cls/src/cls_hrnet.py @@ -144,6 +144,7 @@ class HighResolutionModule(nn.Cell): self.fuse_layers = self._make_fuse_layers() self.relu = nn.ReLU() self.add = ops.Add() + self.resize_bilinear = nn.ResizeBilinear() def _check_branches(self, num_branches, num_blocks, num_inchannels, num_channels): """Check branches.""" @@ -337,6 +338,8 @@ class HighResolutionNet(nn.Cell): self.stage4, pre_stage_channels = self._make_stage( self.stage4_cfg, num_channels, multi_scale_output=True) + self.resize_bilinear = nn.ResizeBilinear() + # Classification Head self.incre_modules, self.downsamp_modules, self.final_layer = self._make_head(pre_stage_channels) diff --git a/research/cv/HRNetW48_seg/eval.py b/research/cv/HRNetW48_seg/eval.py index cd21a3bc645678f301e58dba46431f080608371f..4e5ad652396c3c793dd96b91a4755e682fc7362f 100644 --- a/research/cv/HRNetW48_seg/eval.py +++ b/research/cv/HRNetW48_seg/eval.py @@ -73,7 +73,7 @@ def inference_cityscapes(net, helper, num_classes): image, label = batch shape = label.shape pred = net(image) - pred = ops.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) + pred = ops.ResizeBilinear((shape[-2], shape[-1]))(pred) pred = ops.Exp()(pred) confusion_matrix += get_confusion_matrix(label, pred, shape, num_classes, 255) @@ -90,11 +90,11 @@ def inference_lip(net, helper, num_classes): image, label = batch shape = image.shape pred = net(image) - pred = ops.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) + pred = ops.ResizeBilinear((shape[-2], shape[-1]))(pred) # flip flip_img = image.asnumpy()[:, :, :, ::-1] flip_output = net(Tensor(flip_img)) - flip_output = ops.ResizeBilinearV2()(flip_output, (shape[-2], shape[-1])).asnumpy() + flip_output = ops.ResizeBilinear((shape[-2], shape[-1]))(flip_output).asnumpy() flip_pred = flip_output flip_pred[:, 14, :, :] = flip_output[:, 15, :, :] flip_pred[:, 15, :, :] = flip_output[:, 14, :, :] diff --git a/research/cv/HRNetW48_seg/export.py b/research/cv/HRNetW48_seg/export.py index a38504a406f76f7913d106f88907f538be13b99e..da9efc12b75edce9073212e83a1f1b3cecf28937 100644 --- a/research/cv/HRNetW48_seg/export.py +++ b/research/cv/HRNetW48_seg/export.py @@ -30,13 +30,13 @@ class InferModel(nn.Cell): def __init__(self, num_classes): super(InferModel, self).__init__() self.model = get_seg_model(model_config, num_classes) - self.resize = ops.ResizeBilinearV2() + self.resize = ops.ResizeBilinear((1024, 2048)) self.exp = ops.Exp() def construct(self, x): """Model construction.""" out = self.model(x) - out = self.resize(out, (1024, 2048)) + out = self.resize(out) out = self.exp(out) return out diff --git a/research/cv/HRNetW48_seg/src/callback.py b/research/cv/HRNetW48_seg/src/callback.py index 6d4b72d0439c93c59b786376ed82d36bcee2593d..4a8597d4ed3c2b93a65b3a0f6a091b61f7ccc374 100644 --- a/research/cv/HRNetW48_seg/src/callback.py +++ b/research/cv/HRNetW48_seg/src/callback.py @@ -62,7 +62,7 @@ class SegEvalCallback(Callback): image, label = batch shape = label.shape pred = self.net(image) - pred = ops.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) + pred = ops.ResizeBilinear((shape[-2], shape[-1]))(pred) pred = ops.Exp()(pred) confusion_matrix += self.get_confusion_matrix(label, pred, shape, diff --git a/research/cv/HRNetW48_seg/src/dataset/basedataset.py b/research/cv/HRNetW48_seg/src/dataset/basedataset.py index 367b462f4c678b80d813498faf423ea3482e52c0..a8c7d2073716c1dee577c0b75a95cf6735da4016 100644 --- a/research/cv/HRNetW48_seg/src/dataset/basedataset.py +++ b/research/cv/HRNetW48_seg/src/dataset/basedataset.py @@ -136,11 +136,11 @@ class BaseDataset: """Inference using one feature.""" shape = image.shape pred = model(image) - pred = P.ResizeBilinearV2()(pred, (shape[-2], shape[-1])) + pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred) if flip: flip_img = image.asnumpy()[:, :, :, ::-1] flip_output = model(Tensor(flip_img.copy())) - flip_output = P.ResizeBilinearV2()(flip_output, (shape[-2], shape[-1])) + flip_output = P.ResizeBilinear((shape[-2], shape[-1]))(flip_output) flip_pred = flip_output.asnumpy() flip_pred = Tensor(flip_pred[:, :, :, ::-1]) pred = P.Add()(pred, flip_pred) @@ -202,6 +202,6 @@ class BaseDataset: count[:, :, h0:h1, w0:w1] += 1 preds = preds / count preds = preds[:, :, :height, :width] - preds = P.ResizeBilinearV2()(preds, (ori_height, ori_width)) + preds = P.ResizeBilinear((ori_height, ori_width))(preds) final_pred = P.Add()(final_pred, preds) return final_pred diff --git a/research/cv/HRNetW48_seg/src/dataset/cityscapes.py b/research/cv/HRNetW48_seg/src/dataset/cityscapes.py index cf30794a8f4e2f86a5b0f62a7963867ef9626a82..dddb9bf805247a19d1ce32475bba936534c5cc5c 100644 --- a/research/cv/HRNetW48_seg/src/dataset/cityscapes.py +++ b/research/cv/HRNetW48_seg/src/dataset/cityscapes.py @@ -161,6 +161,6 @@ class Cityscapes(BaseDataset): preds = preds / count preds = preds[:, :, :height, :width] preds = Tensor(preds) - preds = P.ResizeBilinearV2()(preds, (ori_height, ori_width)) + preds = P.ResizeBilinear((ori_height, ori_width))(preds) final_pred = P.Add()(final_pred, preds) return final_pred diff --git a/research/cv/HRNetW48_seg/src/loss.py b/research/cv/HRNetW48_seg/src/loss.py index 24f07decca58f28399f738b816af8136f2374fef..9f88cbde67d0ea99149942e603198e51c104568c 100644 --- a/research/cv/HRNetW48_seg/src/loss.py +++ b/research/cv/HRNetW48_seg/src/loss.py @@ -15,7 +15,7 @@ """Loss functions.""" import mindspore.nn as nn import mindspore.ops.operations as P -import mindspore.ops as ops +import mindspore.ops as F from mindspore.common.tensor import Tensor from mindspore import dtype as mstype from mindspore.nn.loss.loss import LossBase @@ -28,8 +28,7 @@ class CrossEntropyWithLogits(LossBase): """ def __init__(self, num_classes=19, ignore_label=255, image_size=None): super(CrossEntropyWithLogits, self).__init__() - self.image_size = image_size - self.resize = ops.ResizeBilinearV2() + self.resize = F.ResizeBilinear(image_size) self.one_hot = P.OneHot(axis=-1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) @@ -47,7 +46,7 @@ class CrossEntropyWithLogits(LossBase): def construct(self, logits, labels): """Loss construction.""" - logits = self.resize(logits, self.image_size) + logits = self.resize(logits) labels_int = self.cast(labels, mstype.int32) labels_int = self.reshape(labels_int, (-1,)) logits_ = self.transpose(logits, (0, 2, 3, 1)) @@ -69,17 +68,16 @@ class CrossEntropyWithWeights(LossBase): """ def __init__(self, num_classes=19, ignore_label=255, image_size=None, weights=None): super(CrossEntropyWithWeights, self).__init__() - self.image_size = image_size - self.resize = ops.ResizeBilinearV2() + self.resize = F.ResizeBilinear(image_size) self.one_hot = P.OneHot(axis=-1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.cast = P.Cast() self.ce = nn.SoftmaxCrossEntropyWithLogits() - self.zeros = ops.Zeros() - self.fill = ops.Fill() - self.equal = ops.Equal() - self.select = ops.Select() + self.zeros = F.Zeros() + self.fill = F.Fill() + self.equal = F.Equal() + self.select = F.Select() self.num_classes = num_classes self.ignore_label = ignore_label self.mul = P.Mul() @@ -95,7 +93,7 @@ class CrossEntropyWithWeights(LossBase): def construct(self, logits, labels): """Loss construction.""" - logits = self.resize(logits, self.image_size) + logits = self.resize(logits) labels_int = self.cast(labels, mstype.int32) labels_int = self.reshape(labels_int, (-1,)) logits_ = self.transpose(logits, (0, 2, 3, 1)) diff --git a/research/cv/HRNetW48_seg/src/seg_hrnet.py b/research/cv/HRNetW48_seg/src/seg_hrnet.py index a0131a603922f5588181c68af47fc738b87c8bbd..e0fa483e3a77fcc808dcdfbf0793917ce0e1cb09 100644 --- a/research/cv/HRNetW48_seg/src/seg_hrnet.py +++ b/research/cv/HRNetW48_seg/src/seg_hrnet.py @@ -145,6 +145,7 @@ class HighResolutionModule(nn.Cell): self.fuse_layers = self._make_fuse_layers() self.relu = nn.ReLU() self.add = ops.Add() + self.resize_bilinear = nn.ResizeBilinear() def _check_branches(self, num_branches, num_blocks, num_inchannels, num_channels): """Check branches.""" @@ -271,7 +272,7 @@ class HighResolutionModule(nn.Cell): height_output = x[i].shape[-2] t = self.fuse_layers[i][j](x[j]) # t = ops.ResizeNearestNeighbor((height_output, width_output))(t) - y = self.add(y, ops.interpolate(t, size=(height_output, width_output))) + y = self.add(y, self.resize_bilinear(t, size=(height_output, width_output))) else: y = self.add(y, self.fuse_layers[i][j](x[j])) x_fuse.append(self.relu(y)) @@ -339,6 +340,8 @@ class HighResolutionNet(nn.Cell): last_inp_channels = np.int(np.sum(pre_stage_channels)) + self.resize_bilinear = nn.ResizeBilinear() + self.last_layer = nn.SequentialCell([ nn.Conv2d(in_channels=last_inp_channels, out_channels=last_inp_channels, @@ -488,9 +491,9 @@ class HighResolutionNet(nn.Cell): out1, out2, out3, out4 = x h, w = ops.Shape()(out1)[2:] x1 = ops.Cast()(out1, mindspore.dtype.float16) - x2 = ops.interpolate(out2, size=(h, w)) - x3 = ops.interpolate(out3, size=(h, w)) - x4 = ops.interpolate(out4, size=(h, w)) + x2 = self.resize_bilinear(out2, size=(h, w)) + x3 = self.resize_bilinear(out3, size=(h, w)) + x4 = self.resize_bilinear(out4, size=(h, w)) x = self.concat((x1, x2, x3, x4)) diff --git a/research/cv/ICNet/src/loss.py b/research/cv/ICNet/src/loss.py index e540acad4eb99bd54f017cd075a0bf03bb35af56..9e171c2db2ce83e95935911d2bd31e1429302352 100644 --- a/research/cv/ICNet/src/loss.py +++ b/research/cv/ICNet/src/loss.py @@ -30,6 +30,7 @@ class ICNetLoss(nn.Cell): self.ignore_index = ignore_index self.sparse = True self.base_loss = SoftmaxCrossEntropyLoss(num_cls=19, ignore_label=-1) + self.resize_bilinear = nn.ResizeBilinear() # 输入必须为4D def construct(self, *inputs): """construct""" @@ -50,11 +51,11 @@ class ICNetLoss(nn.Cell): h, w = pred.shape[2:] - target_sub4 = ops.interpolate(target, size=(h / 4, w / 4)).squeeze(1) + target_sub4 = self.resize_bilinear(target, size=(h / 4, w / 4)).squeeze(1) - target_sub8 = ops.interpolate(target, size=(h / 8, w / 8)).squeeze(1) + target_sub8 = self.resize_bilinear(target, size=(h / 8, w / 8)).squeeze(1) - target_sub16 = ops.interpolate(target, size=(h / 16, w / 16)).squeeze(1) + target_sub16 = self.resize_bilinear(target, size=(h / 16, w / 16)).squeeze(1) loss1 = self.base_loss(pred_sub4, target_sub4) loss2 = self.base_loss(pred_sub8, target_sub8) diff --git a/research/cv/ICNet/src/models/icnet.py b/research/cv/ICNet/src/models/icnet.py index 9adf769159b0f9de41262ba6bb9bd12c65a07c98..5b589b1eb09944b562949fbe3a8be85327e5b8e9 100644 --- a/research/cv/ICNet/src/models/icnet.py +++ b/research/cv/ICNet/src/models/icnet.py @@ -40,6 +40,8 @@ class ICNet(nn.Cell): self.loss = ICNetLoss() + self.resize_bilinear = nn.ResizeBilinear() + self.__setattr__('exclusive', ['conv_sub1', 'head']) def construct(self, x): @@ -51,7 +53,7 @@ class ICNet(nn.Cell): h, w = x.shape[2:] # sub 2 - x_sub2 = ops.interpolate(x, size=(h / 2, w / 2)) + x_sub2 = self.resize_bilinear(x, size=(h / 2, w / 2)) _, x_sub2, _, _ = self.backbone(x_sub2) # sub 4 @@ -73,6 +75,7 @@ class PyramidPoolingModule(nn.Cell): self.pool2 = nn.AvgPool2d(kernel_size=15, stride=15) self.pool3 = nn.AvgPool2d(kernel_size=10, stride=10) self.pool6 = nn.AvgPool2d(kernel_size=5, stride=5) + self.resize_bilinear = nn.ResizeBilinear() def construct(self, x): """ppm_construct""" @@ -80,19 +83,19 @@ class PyramidPoolingModule(nn.Cell): height, width = x.shape[2:] x1 = self.avgpool(x, (2, 3)) - x1 = ops.interpolate(x1, size=(height, width), align_corners=True) + x1 = self.resize_bilinear(x1, size=(height, width), align_corners=True) feat = feat + x1 x2 = self.pool2(x) - x2 = ops.interpolate(x2, size=(height, width), align_corners=True) + x2 = self.resize_bilinear(x2, size=(height, width), align_corners=True) feat = feat + x2 x3 = self.pool3(x) - x3 = ops.interpolate(x3, size=(height, width), align_corners=True) + x3 = self.resize_bilinear(x3, size=(height, width), align_corners=True) feat = feat + x3 x6 = self.pool6(x) - x6 = ops.interpolate(x6, size=(height, width), align_corners=True) + x6 = self.resize_bilinear(x6, size=(height, width), align_corners=True) feat = feat + x6 return feat @@ -108,6 +111,7 @@ class _ICHead(nn.Cell): self.conv_cls = nn.Conv2d(128, nclass, 1, has_bias=False) self.outputs = list() + self.resize_bilinear = nn.ResizeBilinear() def construct(self, x_sub1, x_sub2, x_sub4): """Head_construct""" @@ -118,11 +122,13 @@ class _ICHead(nn.Cell): x_cff_12, x_12_cls = self.cff_12(x_cff_24, x_sub1) h1, w1 = x_cff_12.shape[2:] - up_x2 = ops.interpolate(x_cff_12, size=(h1 * 2, w1 * 2), align_corners=True) + up_x2 = self.resize_bilinear(x_cff_12, size=(h1 * 2, w1 * 2), + align_corners=True) up_x2 = self.conv_cls(up_x2) h2, w2 = up_x2.shape[2:] - up_x8 = ops.interpolate(up_x2, size=(h2 * 4, w2 * 4), align_corners=True) # scale_factor=4, + up_x8 = self.resize_bilinear(up_x2, size=(h2 * 4, w2 * 4), + align_corners=True) # scale_factor=4, outputs.append(up_x8) outputs.append(up_x2) outputs.append(x_12_cls) @@ -164,6 +170,7 @@ class CascadeFeatureFusion12(nn.Cell): norm_layer(out_channels, momentum=0.1) ) self.conv_low_cls = nn.Conv2d(in_channels=out_channels, out_channels=nclass, kernel_size=1, has_bias=False) + self.resize_bilinear = nn.ResizeBilinear() self.scalar_cast = ops.ScalarCast() @@ -172,7 +179,7 @@ class CascadeFeatureFusion12(nn.Cell): def construct(self, x_low, x_high): """cff_construct""" h, w = x_high.shape[2:] - x_low = ops.interpolate(x_low, size=(h, w), align_corners=True) + x_low = self.resize_bilinear(x_low, size=(h, w), align_corners=True) x_low = self.conv_low(x_low) x_high = self.conv_high(x_high) @@ -199,13 +206,14 @@ class CascadeFeatureFusion24(nn.Cell): ) self.conv_low_cls = nn.Conv2d(in_channels=out_channels, out_channels=nclass, kernel_size=1, has_bias=False) + self.resize_bilinear = nn.ResizeBilinear() self.relu = ms.nn.ReLU() def construct(self, x_low, x_high): """ccf_construct""" h, w = x_high.shape[2:] - x_low = ops.interpolate(x_low, size=(h, w), align_corners=True) + x_low = self.resize_bilinear(x_low, size=(h, w), align_corners=True) x_low = self.conv_low(x_low) x_high = self.conv_high(x_high) diff --git a/research/cv/ICNet/src/models/icnet_dc.py b/research/cv/ICNet/src/models/icnet_dc.py index 6d61a635401b4d11294b3002fa0f076c3f0bf0d3..57d7bcc2136e5d6f442907f63cdeb4ae3cf6c9f2 100644 --- a/research/cv/ICNet/src/models/icnet_dc.py +++ b/research/cv/ICNet/src/models/icnet_dc.py @@ -40,6 +40,8 @@ class ICNetdc(nn.Cell): self.loss = ICNetLoss() + self.resize_bilinear = nn.ResizeBilinear() + self.__setattr__('exclusive', ['conv_sub1', 'head']) def construct(self, x, y): @@ -51,7 +53,7 @@ class ICNetdc(nn.Cell): h, w = x.shape[2:] # sub 2 - x_sub2 = ops.interpolate(x, size=(h / 2, w / 2)) + x_sub2 = self.resize_bilinear(x, size=(h / 2, w / 2)) _, x_sub2, _, _ = self.backbone(x_sub2) # sub 4 @@ -76,6 +78,7 @@ class PyramidPoolingModule(nn.Cell): self.pool2 = nn.AvgPool2d(kernel_size=15, stride=15) self.pool3 = nn.AvgPool2d(kernel_size=10, stride=10) self.pool6 = nn.AvgPool2d(kernel_size=5, stride=5) + self.resize_bilinear = nn.ResizeBilinear() def construct(self, x): """ppm_construct""" @@ -83,19 +86,19 @@ class PyramidPoolingModule(nn.Cell): height, width = x.shape[2:] x1 = self.avgpool(x, (2, 3)) - x1 = ops.interpolate(x1, size=(height, width), align_corners=True) + x1 = self.resize_bilinear(x1, size=(height, width), align_corners=True) feat = feat + x1 x2 = self.pool2(x) - x2 = ops.interpolate(x2, size=(height, width), align_corners=True) + x2 = self.resize_bilinear(x2, size=(height, width), align_corners=True) feat = feat + x2 x3 = self.pool3(x) - x3 = ops.interpolate(x3, size=(height, width), align_corners=True) + x3 = self.resize_bilinear(x3, size=(height, width), align_corners=True) feat = feat + x3 x6 = self.pool6(x) - x6 = ops.interpolate(x6, size=(height, width), align_corners=True) + x6 = self.resize_bilinear(x6, size=(height, width), align_corners=True) feat = feat + x6 return feat @@ -111,6 +114,7 @@ class _ICHead(nn.Cell): self.conv_cls = nn.Conv2d(128, nclass, 1, has_bias=False) self.outputs = list() + self.resize_bilinear = nn.ResizeBilinear() def construct(self, x_sub1, x_sub2, x_sub4): """Head_construct""" @@ -120,11 +124,13 @@ class _ICHead(nn.Cell): x_cff_12, x_12_cls = self.cff_12(x_cff_24, x_sub1) h1, w1 = x_cff_12.shape[2:] - up_x2 = ops.interpolate(x_cff_12, size=(h1 * 2, w1 * 2), align_corners=True) + up_x2 = self.resize_bilinear(x_cff_12, size=(h1 * 2, w1 * 2), + align_corners=True) up_x2 = self.conv_cls(up_x2) h2, w2 = up_x2.shape[2:] - up_x8 = ops.interpolate(up_x2, size=(h2 * 4, w2 * 4), align_corners=True) # scale_factor=4, + up_x8 = self.resize_bilinear(up_x2, size=(h2 * 4, w2 * 4), + align_corners=True) # scale_factor=4, outputs.append(up_x8) outputs.append(up_x2) outputs.append(x_12_cls) @@ -166,6 +172,7 @@ class CascadeFeatureFusion12(nn.Cell): norm_layer(out_channels, momentum=0.1) ) self.conv_low_cls = nn.Conv2d(in_channels=out_channels, out_channels=nclass, kernel_size=1, has_bias=False) + self.resize_bilinear = nn.ResizeBilinear() self.scalar_cast = ops.ScalarCast() @@ -174,7 +181,7 @@ class CascadeFeatureFusion12(nn.Cell): def construct(self, x_low, x_high): """cff_construct""" h, w = x_high.shape[2:] - x_low = ops.interpolate(x_low, size=(h, w), align_corners=True) + x_low = self.resize_bilinear(x_low, size=(h, w), align_corners=True) x_low = self.conv_low(x_low) x_high = self.conv_high(x_high) @@ -201,13 +208,14 @@ class CascadeFeatureFusion24(nn.Cell): ) self.conv_low_cls = nn.Conv2d(in_channels=out_channels, out_channels=nclass, kernel_size=1, has_bias=False) + self.resize_bilinear = nn.ResizeBilinear() self.relu = ms.nn.ReLU() def construct(self, x_low, x_high): """ccf_construct""" h, w = x_high.shape[2:] - x_low = ops.interpolate(x_low, size=(h, w), align_corners=True) + x_low = self.resize_bilinear(x_low, size=(h, w), align_corners=True) x_low = self.conv_low(x_low) x_high = self.conv_high(x_high) diff --git a/research/cv/MIMO-UNet/src/loss.py b/research/cv/MIMO-UNet/src/loss.py index 5317f4307a0653ccacd9d35adfc213b8902ab2d6..417541763128ccc6e079d1309518cbe64a53a1d8 100644 --- a/research/cv/MIMO-UNet/src/loss.py +++ b/research/cv/MIMO-UNet/src/loss.py @@ -17,7 +17,6 @@ Loss function """ import mindspore.nn as nn -import mindspore.ops as ops class ContentLoss(nn.Cell): @@ -25,7 +24,7 @@ class ContentLoss(nn.Cell): def __init__(self): super().__init__() self.criterion = nn.L1Loss() - self.nn_interpolate = ops.interpolate + self.nn_interpolate = nn.ResizeBilinear() def interpolate_downscale(self, x, scale_factor): """downscale""" diff --git a/research/cv/MIMO-UNet/src/mimo_unet.py b/research/cv/MIMO-UNet/src/mimo_unet.py index cb15f69ef954de3f2d094b19a14c774dc9b23f1c..0909d4d956dd76fd3935085bf181f3bbe24eafdc 100644 --- a/research/cv/MIMO-UNet/src/mimo_unet.py +++ b/research/cv/MIMO-UNet/src/mimo_unet.py @@ -153,7 +153,7 @@ class MIMOUNet(nn.Cell): self.SCM2 = SCM(base_channel * 2) self.cat = ops.Stack(axis=1) - self.nn_interpolate = ops.interpolate + self.nn_interpolate = nn.ResizeBilinear() def interpolate(self, x, scale_factor): """interpolate""" @@ -277,7 +277,7 @@ class MIMOUNetPlus(nn.Cell): self.drop2 = nn.Dropout(p=0.9) self.cat = ops.Stack(axis=1) - self.interpolate = ops.interpolate + self.interpolate = nn.ResizeBilinear() def construct(self, x): """construct MIMOUNetPlus""" diff --git a/research/cv/MODNet/src/models/modnet.py b/research/cv/MODNet/src/models/modnet.py index cfec214a94cf62005478e4f7c1246e4d66efc8ec..3b3081bad2bc590a399a2bb28d58b95a5373813e 100644 --- a/research/cv/MODNet/src/models/modnet.py +++ b/research/cv/MODNet/src/models/modnet.py @@ -122,15 +122,17 @@ class LRBranch(nn.Cell): self.conv_lr8x = Conv2dIBNormRelu(enc_channels[3], enc_channels[2], 5, stride=1, padding=2) self.conv_lr = Conv2dIBNormRelu(enc_channels[2], 1, kernel_size=3, stride=2, padding=1, with_ibn=False, with_relu=False) + self.ResizeBilinear = nn.ResizeBilinear() + def construct(self, img, inference): enc_features = self.backbone(img) enc2x, enc4x, enc32x = enc_features[0], enc_features[1], enc_features[4] enc32x = self.se_block(enc32x) - lr16x = ops.interpolate(enc32x, scale_factor=2, align_corners=False) + lr16x = self.ResizeBilinear(enc32x, scale_factor=2, align_corners=False) lr16x = self.conv_lr16x(lr16x) - lr8x = ops.interpolate(lr16x, scale_factor=2, align_corners=False) + lr8x = self.ResizeBilinear(lr16x, scale_factor=2, align_corners=False) lr8x = self.conv_lr8x(lr8x) pred_semantic = None @@ -171,7 +173,7 @@ class HRBranch(nn.Cell): Conv2dIBNormRelu(hr_channels + 3, hr_channels, 3, stride=1, padding=1), Conv2dIBNormRelu(hr_channels, 1, kernel_size=1, stride=1, padding=0, with_ibn=False, with_relu=False), ) - self.resize_bilinear = ops.interpolate + self.resize_bilinear = nn.ResizeBilinear() def construct(self, img, enc2x, enc4x, lr8x, inference): size_img = img.shape[-2:] @@ -214,7 +216,7 @@ class FusionBranch(nn.Cell): Conv2dIBNormRelu(hr_channels + 3, int(hr_channels / 2), 3, stride=1, padding=1), Conv2dIBNormRelu(int(hr_channels / 2), 1, 1, stride=1, padding=0, with_ibn=False, with_relu=False), ) - self.resize_bilinear = ops.interpolate + self.resize_bilinear = nn.ResizeBilinear() def construct(self, img, lr8x, hr2x): lr4x = self.resize_bilinear(lr8x, scale_factor=2, align_corners=False) diff --git a/research/cv/PAGENet/src/pagenet.py b/research/cv/PAGENet/src/pagenet.py index 14bf6a19053bdfb7367b182573bc680b9b22ac4a..8c494972acb096fa7df10e22af971bbef6679442 100644 --- a/research/cv/PAGENet/src/pagenet.py +++ b/research/cv/PAGENet/src/pagenet.py @@ -22,10 +22,9 @@ class upSampleLike(nn.Cell): def __init__(self): super(upSampleLike, self).__init__() - self.interpolate = P.interpolate - + self.resize = nn.ResizeBilinear() def construct(self, fm, x1): - fm = self.interpolate(fm, (x1.shape[2], x1.shape[3])) + fm = self.resize(fm, (x1.shape[2], x1.shape[3])) return fm class MyMaxPool(nn.Cell): @@ -169,14 +168,14 @@ class MindsporeModel(nn.Cell): def mymax_16(self, target): target_shape = target.shape - resize = P.ResizeBilinearV2() - target = resize(target, (target_shape[-2]/16, target_shape[-1]/16)) + resize = P.ResizeBilinear((target_shape[-2]/16, target_shape[-1]/16)) + target = resize(target) return target def mymax_32(self, target): target_shape = target.shape - resize = P.ResizeBilinearV2() - target = resize(target, (target_shape[-2]/32, target_shape[-1]/32)) + resize = P.ResizeBilinear((target_shape[-2]/32, target_shape[-1]/32)) + target = resize(target) return target def attention5(self, sal_5): diff --git a/research/cv/PSPNet/eval.py b/research/cv/PSPNet/eval.py index 578d14d9a3bb26a5653bbbe7a94bb03d6379a557..e18ff69d4ccb0f60d4005935bf5a74beff54c851 100644 --- a/research/cv/PSPNet/eval.py +++ b/research/cv/PSPNet/eval.py @@ -151,7 +151,8 @@ def net_process(model, image, mean, std=None, flip=True): _, _, h_i, w_i = input_.shape _, _, h_o, w_o = output.shape if (h_o != h_i) or (w_o != w_i): - output = ops.interpolate(output, size=(h_i, w_i), align_corners=True) + bi_linear = nn.ResizeBilinear() + output = bi_linear(output, size=(h_i, w_i), align_corners=True) softmax = nn.Softmax(axis=1) output = softmax(output) if flip: diff --git a/research/cv/PSPNet/eval_onnx_cpu.py b/research/cv/PSPNet/eval_onnx_cpu.py index 1e892e633e9731826b0e75c7c917096da813c67a..102d485247b000e39fc783608ef0c70ecb8ad912 100644 --- a/research/cv/PSPNet/eval_onnx_cpu.py +++ b/research/cv/PSPNet/eval_onnx_cpu.py @@ -118,7 +118,8 @@ def net_process(model, image, mean, std=None, flip=False): _, _, h_i, w_i = input_.shape _, _, _, h_o, w_o = output.shape if (h_o != h_i) or (w_o != w_i): - output = ops.interpolate(output, size=(h_i, w_i), align_corners=True) + bi_linear = nn.ResizeBilinear() + output = bi_linear(output, size=(h_i, w_i), align_corners=True) softmax = nn.Softmax(axis=2) output = softmax(output) diff --git a/research/cv/PSPNet/postprocess.py b/research/cv/PSPNet/postprocess.py index 91392fd1c320e5811a81caf9e07ee019bb97826f..cea407082020c3d8811e80e08e698407b7d0ace0 100644 --- a/research/cv/PSPNet/postprocess.py +++ b/research/cv/PSPNet/postprocess.py @@ -22,7 +22,6 @@ from src.dataset import pt_dataset, pt_transform import src.utils.functions_args as fa from src.utils.p_util import AverageMeter, intersectionAndUnion, check_makedirs, colorize import mindspore -import mindspore.ops as ops from mindspore import Tensor from mindspore import context import mindspore.nn as nn @@ -102,7 +101,8 @@ def bin_process(image, image_idx, flip=True): image = image.astype(mindspore.float32) if (h_o != h_i) or (w_o != w_i): - image = ops.interpolate(image, size=(h_i, w_i), align_corners=True) + bi_linear = nn.ResizeBilinear() + image = bi_linear(image, size=(h_i, w_i), align_corners=True) softmax = nn.Softmax(axis=1) output = softmax(image) output = output.asnumpy() diff --git a/research/cv/PSPNet/src/model/pspnet.py b/research/cv/PSPNet/src/model/pspnet.py index ba0695f354c3e5c4e595fd18147cd0b08482cc73..dede35bb6b401123d60b573aa9c4b6b9f0a39535 100644 --- a/research/cv/PSPNet/src/model/pspnet.py +++ b/research/cv/PSPNet/src/model/pspnet.py @@ -128,16 +128,18 @@ class _PSPModule(nn.Cell): ) self.cat = ops.Concat(axis=1) self.feature_shape = feature_shape - self.resize_ops = ops.ResizeBilinearV2(True) + self.resize_ops = ops.ResizeBilinear( + (self.feature_shape[0], self.feature_shape[1]), True + ) self.cast = ops.Cast() def construct(self, x): """ PSP module process """ x = self.cast(x, mindspore.float32) - s1_out = self.resize_ops(self.stage1(x), (self.feature_shape[0], self.feature_shape[1])) - s2_out = self.resize_ops(self.stage2(x), (self.feature_shape[0], self.feature_shape[1])) - s3_out = self.resize_ops(self.stage3(x), (self.feature_shape[0], self.feature_shape[1])) - s4_out = self.resize_ops(self.stage4(x), (self.feature_shape[0], self.feature_shape[1])) + s1_out = self.resize_ops(self.stage1(x)) + s2_out = self.resize_ops(self.stage2(x)) + s3_out = self.resize_ops(self.stage3(x)) + s4_out = self.resize_ops(self.stage4(x)) out = (x, s1_out, s2_out, s3_out, s4_out) out = self.cat(out) @@ -197,7 +199,7 @@ class PSPNet(nn.Cell): nn.Dropout(p=0.1), nn.Conv2d(256, num_classes, kernel_size=1, has_bias=True) ) - self.resize = ops.interpolate + self.resize = nn.ResizeBilinear() self.shape = ops.Shape() self.init_weights(self.cls) diff --git a/research/cv/PWCNet/src/pwc_modules.py b/research/cv/PWCNet/src/pwc_modules.py index 18f34856aea632fac0c54b83cc692a153a9f4b16..34897b38e7561924fe7a24a028278b6d426cd502 100644 --- a/research/cv/PWCNet/src/pwc_modules.py +++ b/research/cv/PWCNet/src/pwc_modules.py @@ -50,7 +50,7 @@ def upsample2d_as(inputs, target_as): _, _, h1, w1 = P.Shape()(target_as) _, _, h2, _ = P.Shape()(inputs) resize = (h1 + 0.0) / (h2 + 0.0) - return P.ResizeBilinearV2()(inputs, (h1, w1)) * resize + return P.ResizeBilinear((h1, w1))(inputs) * resize class FeatureExtractor(nn.Cell): diff --git a/research/cv/PaDiM/eval.py b/research/cv/PaDiM/eval.py index 8c3a8a96188d7a135a617dc9b3d6b4a0607240cd..e00af8cc22c84ac976d91d4e83191611a792c6eb 100644 --- a/research/cv/PaDiM/eval.py +++ b/research/cv/PaDiM/eval.py @@ -29,6 +29,7 @@ import numpy as np from mindspore import context from mindspore import Tensor +from mindspore import nn from mindspore import ops from mindspore.ops import operations as P from mindspore.train.serialization import load_checkpoint, load_param_into_net @@ -115,7 +116,8 @@ if __name__ == '__main__': dist_list = Tensor(np.array(dist_list).transpose(1, 0).reshape((B, H, W))) # upsample expand_dims = ops.ExpandDims() - score_map = ops.interpolate(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() + resize_bilinear = nn.ResizeBilinear() + score_map = resize_bilinear(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() # apply gaussian smoothing on the score map for i in range(score_map.shape[0]): score_map[i] = gaussian_filter(score_map[i], sigma=4) diff --git a/research/cv/PaDiM/eval_onnx.py b/research/cv/PaDiM/eval_onnx.py index e6d67964d7187d2c7b7823ed77e3a61480b8bb8b..e1466549010a15113332df4ce5935f18121e0725 100644 --- a/research/cv/PaDiM/eval_onnx.py +++ b/research/cv/PaDiM/eval_onnx.py @@ -30,6 +30,7 @@ import numpy as np import onnxruntime from mindspore import context from mindspore import Tensor +from mindspore import nn from mindspore import ops from mindspore.ops import operations as P @@ -118,7 +119,8 @@ if __name__ == '__main__': dist_list = Tensor(np.array(dist_list).transpose(1, 0).reshape((B, H, W))) # upsample expand_dims = ops.ExpandDims() - score_map = ops.interpolate(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() + resize_bilinear = nn.ResizeBilinear() + score_map = resize_bilinear(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() # apply gaussian smoothing on the score map for i in range(score_map.shape[0]): score_map[i] = gaussian_filter(score_map[i], sigma=4) diff --git a/research/cv/PaDiM/postprocess.py b/research/cv/PaDiM/postprocess.py index 273e8f11990511a5e9438ba3cd0c5cb20a787130..71be6838797d6a2229d0381c06d6e47332c7f7d2 100644 --- a/research/cv/PaDiM/postprocess.py +++ b/research/cv/PaDiM/postprocess.py @@ -28,6 +28,7 @@ from sklearn.metrics import precision_recall_curve import numpy as np from mindspore import Tensor +from mindspore import nn from mindspore import ops from mindspore.ops import operations as P @@ -149,7 +150,8 @@ if __name__ == '__main__': dist_list = Tensor(np.array(dist_list).transpose(1, 0).reshape((B, H, W))) # upsample expand_dims = ops.ExpandDims() - score_map = ops.interpolate(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() + resize_bilinear = nn.ResizeBilinear() + score_map = resize_bilinear(expand_dims(dist_list, 1), size=(224, 224)).squeeze().asnumpy() # apply gaussian smoothing on the score map for i in range(score_map.shape[0]): score_map[i] = gaussian_filter(score_map[i], sigma=4) diff --git a/research/cv/Pix2PixHD/precompute_feature_maps.py b/research/cv/Pix2PixHD/precompute_feature_maps.py index 6f31b56387145e493f292f3bac84264842483175..c64fb9c1e44a1e01890cb589d179e892b832d4a1 100644 --- a/research/cv/Pix2PixHD/precompute_feature_maps.py +++ b/research/cv/Pix2PixHD/precompute_feature_maps.py @@ -15,7 +15,7 @@ import os import mindspore as ms -import mindspore.ops as ops +import mindspore.nn as nn from src.models.pix2pixHD import Pix2PixHD from src.dataset.pix2pixHD_dataset import Pix2PixHDDataset, create_train_dataset from src.utils.config import config @@ -38,6 +38,7 @@ if not os.path.exists(feat_img_path): os.makedirs(feat_img_path) netE = pix2pixHD.netE +resizeBilinear = nn.ResizeBilinear() "======Save precomputed feature maps for 1024p training======" for i, data in enumerate(data_loader): @@ -45,7 +46,7 @@ for i, data in enumerate(data_loader): image = ms.Tensor(data["image"]) inst = ms.Tensor(data["inst"]) feat_map = netE(image, inst) - feat_map = ops.interpolate(feat_map, scale_factor=2) + feat_map = resizeBilinear(feat_map, scale_factor=2) save_path = data["path"][0].replace("/train_label", "/train_feat") save_path = os.path.splitext(save_path)[0] save_image(feat_map, save_path, format_name=".png") diff --git a/research/cv/PyramidBox/src/pyramidbox.py b/research/cv/PyramidBox/src/pyramidbox.py index 43e56cb7b0af195975874f801d1d3563cb437f60..03a2b432be5ee7ea26879c3a9318318f2deb4ca7 100644 --- a/research/cv/PyramidBox/src/pyramidbox.py +++ b/research/cv/PyramidBox/src/pyramidbox.py @@ -138,7 +138,8 @@ class PyramidBox(nn.Cell): def _upsample_prod(self, x, y): _, _, H, W = y.shape - result = ops.interpolate(x, size=(H, W), align_corners=True) * y + resize_bilinear = nn.ResizeBilinear() + result = resize_bilinear(x, size=(H, W), align_corners=True) * y return result def construct(self, x): diff --git a/research/cv/RefineNet/src/refinenet.py b/research/cv/RefineNet/src/refinenet.py index 400528aba868a9070a0786f4c34ff4eb9a6db55f..824f68f0e61665ca448104c3029dbc478e97d38c 100644 --- a/research/cv/RefineNet/src/refinenet.py +++ b/research/cv/RefineNet/src/refinenet.py @@ -172,7 +172,7 @@ class RefineNet(nn.Cell): self.clf_conv = nn.Conv2d(256, num_classes, kernel_size=3, stride=1, pad_mode='pad', padding=1, has_bias=True) - self.resize = ops.interpolate + self.resize = nn.ResizeBilinear() def _make_crp(self, in_planes, out_planes, stages): """make_crp""" diff --git a/research/cv/ResidualAttentionNet/model/attention_module.py b/research/cv/ResidualAttentionNet/model/attention_module.py index 0209394464480706dadd71162a96f52b5ef19324..d38ae59433c4602a295eec229ece4a90078b5f66 100644 --- a/research/cv/ResidualAttentionNet/model/attention_module.py +++ b/research/cv/ResidualAttentionNet/model/attention_module.py @@ -177,9 +177,11 @@ class AttentionModule_stage1(nn.Cell): self.softmax3_blocks = nn.SequentialCell([ ResidualBlock(in_channels, out_channels), ResidualBlock(in_channels, out_channels)]) - self.interpolation = ops.ResizeBilinearV2(align_corners=True) + self.interpolation3 = ops.ResizeBilinear(size=size3, align_corners=True) self.softmax4_blocks = ResidualBlock(in_channels, out_channels) + self.interpolation2 = ops.ResizeBilinear(size=size2, align_corners=True) self.softmax5_blocks = ResidualBlock(in_channels, out_channels) + self.interpolation1 = ops.ResizeBilinear(size=size1, align_corners=True) self.softmax6_blocks = nn.SequentialCell([ nn.BatchNorm2d(num_features=out_channels, momentum=0.9), nn.ReLU(), @@ -204,19 +206,18 @@ class AttentionModule_stage1(nn.Cell): opt_pad2 = self.pad(out_softmax2) out_mpool3 = self.mpool3(opt_pad2) out_softmax3 = self.softmax3_blocks(out_mpool3) - out_interp3 = self.interpolation(out_softmax3, self.size3) + out_softmax2 + out_interp3 = self.interpolation3(out_softmax3) + out_softmax2 out = out_interp3 + out_skip2_connection out_softmax4 = self.softmax4_blocks(out) - out_interp2 = self.interpolation(out_softmax4, self.size2) + out_softmax1 + out_interp2 = self.interpolation2(out_softmax4) + out_softmax1 out = out_interp2 + out_skip1_connection out_softmax5 = self.softmax5_blocks(out) - out_interp1 = self.interpolation(out_softmax5, self.size1) + out_trunk + out_interp1 = self.interpolation1(out_softmax5) + out_trunk out_softmax6 = self.softmax6_blocks(out_interp1) out = (1 + out_softmax6) * out_trunk out_last = self.last_blocks(out) return out_last - class AttentionModule_stage2(nn.Cell): # input image size is 28*28 def __init__(self, in_channels, out_channels, size1=(28, 28), size2=(14, 14)): @@ -235,8 +236,9 @@ class AttentionModule_stage2(nn.Cell): self.softmax2_blocks = nn.SequentialCell([ ResidualBlock(in_channels, out_channels), ResidualBlock(in_channels, out_channels)]) - self.interpolation = ops.ResizeBilinearV2(align_corners=True) + self.interpolation2 = ops.ResizeBilinear(size=size2, align_corners=True) self.softmax3_blocks = ResidualBlock(in_channels, out_channels) + self.interpolation1 = ops.ResizeBilinear(size=size1, align_corners=True) self.softmax4_blocks = nn.SequentialCell([ nn.BatchNorm2d(num_features=out_channels, momentum=0.9), nn.ReLU(), @@ -257,16 +259,15 @@ class AttentionModule_stage2(nn.Cell): opt_pad2 = self.pad(out_softmax1) out_mpool2 = self.mpool2(opt_pad2) out_softmax2 = self.softmax2_blocks(out_mpool2) - out_interp2 = self.interpolation(out_softmax2, self.size2) + out_softmax1 + out_interp2 = self.interpolation2(out_softmax2) + out_softmax1 out = out_interp2 + out_skip1_connection out_softmax3 = self.softmax3_blocks(out) - out_interp1 = self.interpolation(out_softmax3, self.size1) + out_trunk + out_interp1 = self.interpolation1(out_softmax3) + out_trunk out_softmax4 = self.softmax4_blocks(out_interp1) out = (1 + out_softmax4) * out_trunk out_last = self.last_blocks(out) return out_last - class AttentionModule_stage3(nn.Cell): # input image size is 14*14 def __init__(self, in_channels, out_channels, size1=(14, 14)): @@ -281,7 +282,7 @@ class AttentionModule_stage3(nn.Cell): self.softmax1_blocks = nn.SequentialCell([ ResidualBlock(in_channels, out_channels), ResidualBlock(in_channels, out_channels)]) - self.interpolation = ops.ResizeBilinearV2(align_corners=True) + self.interpolation1 = ops.ResizeBilinear(size=size1, align_corners=True) self.softmax2_blocks = nn.SequentialCell([ nn.BatchNorm2d(num_features=out_channels, momentum=0.9), nn.ReLU(), @@ -298,13 +299,12 @@ class AttentionModule_stage3(nn.Cell): opt_pad = self.pad(x) out_mpool1 = self.mpool1(opt_pad) out_softmax1 = self.softmax1_blocks(out_mpool1) - out_interp1 = self.interpolation(out_softmax1, self.size1) + out_trunk + out_interp1 = self.interpolation1(out_softmax1) + out_trunk out_softmax2 = self.softmax2_blocks(out_interp1) out = (1 + out_softmax2) * out_trunk out_last = self.last_blocks(out) return out_last - class AttentionModule_stage1_cifar(nn.Cell): # input size is 16*16 def __init__(self, in_channels, out_channels, size1=(16, 16), size2=(8, 8)): @@ -323,8 +323,9 @@ class AttentionModule_stage1_cifar(nn.Cell): self.middle_2r_blocks = nn.SequentialCell([ ResidualBlock(in_channels, out_channels), ResidualBlock(in_channels, out_channels)]) - self.interpolation = ops.ResizeBilinearV2(align_corners=True) + self.interpolation1 = ops.ResizeBilinear(size=size2, align_corners=True) self.up_residual_blocks1 = ResidualBlock(in_channels, out_channels) + self.interpolation2 = ops.ResizeBilinear(size=size1, align_corners=True) self.conv1_1_blocks = nn.SequentialCell([ nn.BatchNorm2d(num_features=out_channels, momentum=0.9), nn.ReLU(), @@ -345,16 +346,15 @@ class AttentionModule_stage1_cifar(nn.Cell): opt_pad2 = self.pad(out_down_residual_blocks1) out_mpool2 = self.mpool2(opt_pad2) out_middle_2r_blocks = self.middle_2r_blocks(out_mpool2) - out_interp = self.interpolation(out_middle_2r_blocks, self.size2) + out_down_residual_blocks1 + out_interp = self.interpolation1(out_middle_2r_blocks) + out_down_residual_blocks1 out = out_interp + out_skip1_connection out_up_residual_blocks1 = self.up_residual_blocks1(out) - out_interp2 = self.interpolation(out_up_residual_blocks1, self.size1) + out_trunk + out_interp2 = self.interpolation2(out_up_residual_blocks1) + out_trunk out_conv1_1_blocks = self.conv1_1_blocks(out_interp2) out = (1 + out_conv1_1_blocks) * out_trunk out_last = self.last_blocks(out) return out_last - class AttentionModule_stage2_cifar(nn.Cell): # input size is 8*8 def __init__(self, in_channels, out_channels, size=(8, 8)): @@ -369,7 +369,7 @@ class AttentionModule_stage2_cifar(nn.Cell): self.middle_2r_blocks = nn.SequentialCell([ ResidualBlock(in_channels, out_channels), ResidualBlock(in_channels, out_channels)]) - self.interpolation = ops.ResizeBilinearV2(align_corners=True) + self.interpolation1 = ops.ResizeBilinear(size=size, align_corners=True) self.conv1_1_blocks = nn.SequentialCell([ nn.BatchNorm2d(num_features=out_channels, momentum=0.9), nn.ReLU(), @@ -386,13 +386,12 @@ class AttentionModule_stage2_cifar(nn.Cell): opt_pad1 = self.pad(x) out_mpool1 = self.mpool1(opt_pad1) out_middle_2r_blocks = self.middle_2r_blocks(out_mpool1) - out_interp = self.interpolation(out_middle_2r_blocks, self.size) + out_trunk + out_interp = self.interpolation1(out_middle_2r_blocks) + out_trunk out_conv1_1_blocks = self.conv1_1_blocks(out_interp) out = (1 + out_conv1_1_blocks) * out_trunk out_last = self.last_blocks(out) return out_last - class AttentionModule_stage3_cifar(nn.Cell): # input size is 4*4 def __init__(self, in_channels, out_channels, size=(8, 8)): diff --git a/research/cv/SPADE/src/models/inception.py b/research/cv/SPADE/src/models/inception.py index 78308c41d9d07500908bd6ba5301f98943b2a05e..42949606e11111998d80c39fafde5d3013290665 100644 --- a/research/cv/SPADE/src/models/inception.py +++ b/research/cv/SPADE/src/models/inception.py @@ -288,7 +288,7 @@ class InceptionV3(nn.Cell): self.include_top = include_top if self.include_top: self.logits = Logits(num_classes, dropout_keep_prob) - self.resize = ops.interpolate + self.resize = nn.ResizeBilinear() self.reduceMean = ops.ReduceMean(keep_dims=True) self.squeeze_2 = ops.Squeeze(2) self.squeeze_3 = ops.Squeeze(3) diff --git a/research/cv/SPC-Net/network/network.py b/research/cv/SPC-Net/network/network.py index 939cd8263c5388d410da6b254f16af74c58fdcc9..55f144ef14625e642ddd248cf1d13dcf0147c5b9 100644 --- a/research/cv/SPC-Net/network/network.py +++ b/research/cv/SPC-Net/network/network.py @@ -111,7 +111,7 @@ class _AtrousSpatialPyramidPoolingModule(nn.Cell): img_features = self.img_pooling(x) img_features = self.img_conv(img_features) - img_features = P.ResizeBilinearV2(True)(img_features, (x_size[2], x_size[3])) + img_features = P.ResizeBilinear((x_size[2], x_size[3]), True)(img_features) out = img_features for f in self.features: @@ -247,7 +247,7 @@ class DeepV3Plus(nn.Cell): dec0_up = self.bot_aspp(x) # [N, 256, 48, 48] dec0_fine = self.bot_fine(low_level) # [N, 48, 192, 192] low_level_size = low_level.shape - dec0_up = P.ResizeBilinearV2(True)(dec0_up, (low_level_size[2], low_level_size[3])) + dec0_up = P.ResizeBilinear((low_level_size[2], low_level_size[3]), True)(dec0_up) dec0 = self.concat((dec0_fine, dec0_up)) # [N, 304, 192, 192] dec1 = self.final1(dec0) # [N, 256, 192, 192] main_out = self.projection(dec1) @@ -272,7 +272,7 @@ class DeepV3Plus(nn.Cell): main_out = self.mask_norm(main_out) main_out = main_out.view(b, h, w, k) main_out = ops.transpose(main_out, (0, 3, 1, 2)) - main_out = P.ResizeBilinearV2(True)(main_out, (x_size[2], x_size[3])) + main_out = P.ResizeBilinear((x_size[2], x_size[3]), True)(main_out) return main_out diff --git a/research/cv/SSIM-AE/src/network.py b/research/cv/SSIM-AE/src/network.py index 8f8760704073f30cfdb982e3ae39d672af3cebb0..9b0f270e7c9f0c1828c170ade546fe6addfb1f26 100644 --- a/research/cv/SSIM-AE/src/network.py +++ b/research/cv/SSIM-AE/src/network.py @@ -220,12 +220,13 @@ class AutoEncoder(nn.Cell): ] ) self.decoded = nn.SequentialCell(decoded_layers) + self.resize = nn.ResizeBilinear() def construct(self, input_batch): temp = self.encoded(input_batch) output_batch = self.decoded(temp) if (input_batch.shape[2] != output_batch.shape[2]) or (input_batch.shape[3] != output_batch.shape[3]): - output_batch = ops.interpolate(output_batch, input_batch.shape[2:]) + output_batch = self.resize(output_batch, input_batch.shape[2:]) return output_batch diff --git a/research/cv/Segformer/src/loss.py b/research/cv/Segformer/src/loss.py index 9ecf2cc02977a67002e7acf6e094838096f10a6c..69d5a2f6b372d26b29c744c920fe65b7b2ab0980 100644 --- a/research/cv/Segformer/src/loss.py +++ b/research/cv/Segformer/src/loss.py @@ -35,8 +35,7 @@ class CrossEntropyWithLogits(LossBase): def __init__(self, num_classes=19, ignore_label=255, image_size=None): super(CrossEntropyWithLogits, self).__init__() - self.image_size = image_size - self.resize = F.ResizeBilinearV2() + self.resize = F.ResizeBilinear(image_size) self.one_hot = P.OneHot(axis=-1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) @@ -54,7 +53,7 @@ class CrossEntropyWithLogits(LossBase): def construct(self, logits, labels): """Loss construction.""" - logits = self.resize(logits, self.image_size) + logits = self.resize(logits) labels_int = self.cast(labels, mstype.int32) labels_int = self.reshape(labels_int, (-1,)) logits_ = self.transpose(logits, (0, 2, 3, 1)) @@ -132,7 +131,7 @@ class CrossEntropy(nn.Cell): self.weights = [0.4, 1] self.num_outputs = 1 self.align_corners = True - self.resize_bilinear = F.interpolate + self.resize_bilinear = nn.ResizeBilinear() self.concat = P.Concat() def get_loss(self, score, target): diff --git a/research/cv/StyTr-2/src/models/transformer.py b/research/cv/StyTr-2/src/models/transformer.py index 0af23f65fe9bc3c116c14221e5bb2256202e222d..442a7df3dcee585adf5d1e1f51dc04f4329f146d 100644 --- a/research/cv/StyTr-2/src/models/transformer.py +++ b/research/cv/StyTr-2/src/models/transformer.py @@ -34,7 +34,7 @@ class Transformer(nn.Cell): self.new_ps = nn.Conv2d(512, 512, (1, 1), has_bias=True, pad_mode='valid') self.averagepooling = ops.AdaptiveAvgPool2D(18) - self.interpolate = ops.interpolate + self.interpolate = nn.ResizeBilinear() self.transpose = ops.Transpose() def _reset_parameters(self): diff --git a/research/cv/UNet3+/src/models.py b/research/cv/UNet3+/src/models.py index b23583415bb965a62625ec93e16b6a8fb52ef58f..60708f9ad299c14d088f4165f09839417f5cb809 100644 --- a/research/cv/UNet3+/src/models.py +++ b/research/cv/UNet3+/src/models.py @@ -114,7 +114,7 @@ class UNet3Plus(nn.Cell): self.h4_Cat_hd4_relu = nn.ReLU() # hd5->20*20, hd4->40*40, Upsample 2 times - self.ResizeBilinear = ops.interpolate + self.ResizeBilinear = nn.ResizeBilinear() self.hd5_UT_hd4_conv = nn.Conv2d(filters[4], self.CatChannels, 3, \ pad_mode="pad", padding=1, weight_init="HeNormal") self.hd5_UT_hd4_bn = nn.BatchNorm2d(self.CatChannels, gamma_init="ones") diff --git a/research/cv/Yolact++/src/yolact/layers/fpn.py b/research/cv/Yolact++/src/yolact/layers/fpn.py index 87518cc4fdea9d5488042debe0b89e4f3a036978..e10111a077ef61a0201e5593a1a27910f25b4355 100644 --- a/research/cv/Yolact++/src/yolact/layers/fpn.py +++ b/research/cv/Yolact++/src/yolact/layers/fpn.py @@ -55,7 +55,7 @@ class FpnTopDown(nn.Cell): top = len(inputs) - i - 1 down = top - 1 size = F.shape(inputs[down]) - top_down = P.ResizeBilinearV2()(features[-1], (size[2], size[3])) + top_down = P.ResizeBilinear((size[2], size[3]))(features[-1]) top_down = top_down + image_features[down] features = features + (top_down,) diff --git a/research/cv/Yolact++/src/yolact/layers/modules/loss.py b/research/cv/Yolact++/src/yolact/layers/modules/loss.py index a80e27b46bdca6dfb5002651b14b1e55d5b8f206..45113c0fb9659e5cba24549885677a7b84801db9 100644 --- a/research/cv/Yolact++/src/yolact/layers/modules/loss.py +++ b/research/cv/Yolact++/src/yolact/layers/modules/loss.py @@ -74,7 +74,7 @@ class MultiBoxLoss(nn.Cell): self.cat = P.Concat(2) self.binary_cross_entropy = P.BinaryCrossEntropy(reduction='sum') self.sigmoid = P.Sigmoid() - self.resize_bilinear = P.interpolate + self.resize_bilinear = nn.ResizeBilinear() self.scalarCast = P.ScalarCast() self.smoothL1loss = nn.SmoothL1Loss() self.closs = nn.SoftmaxCrossEntropyWithLogits(sparse=True) diff --git a/research/cv/Yolact++/src/yolact/layers/protonet.py b/research/cv/Yolact++/src/yolact/layers/protonet.py index 6d5ba008e9febdaa4221c01a21bc8cc597674870..94fa36a90b27845ecdfecc9645c04a5ab62124ad 100644 --- a/research/cv/Yolact++/src/yolact/layers/protonet.py +++ b/research/cv/Yolact++/src/yolact/layers/protonet.py @@ -14,7 +14,6 @@ # ============================================================================ """Protonet""" import mindspore.nn as nn -import mindspore.ops as ops import mindspore.ops.functional @@ -28,7 +27,7 @@ class protonet(nn.Cell): self.proto_conv1_3 = nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), pad_mode='pad', padding=1) self.proto_conv1_4 = nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), pad_mode='pad', padding=1) self.relu = nn.ReLU() - self.proto_bi = ops.interpolate + self.proto_bi = nn.ResizeBilinear() self.transpose = mindspore.ops.operations.Transpose() def construct(self, proto_x): diff --git a/research/cv/Yolact++/src/yolact/utils/interpolate.py b/research/cv/Yolact++/src/yolact/utils/interpolate.py index 3acb859be615d5ae8b7ccc68f6a1531dc044ed8c..70fd61901ad4348806a7cc262584577960ef765b 100644 --- a/research/cv/Yolact++/src/yolact/utils/interpolate.py +++ b/research/cv/Yolact++/src/yolact/utils/interpolate.py @@ -14,9 +14,6 @@ # ============================================================================ """InterpolateModule""" import mindspore.nn as nn -import mindspore.ops as ops - - class InterpolateModule(nn.Cell): """ This is a module version of F.interpolate (rip nn.Upsampling). @@ -31,5 +28,5 @@ class InterpolateModule(nn.Cell): self.kwdargs = kwdargs def construct(self, x): - resize_bili = ops.interpolate + resize_bili = nn.ResizeBilinear() return resize_bili(x, *self.args, **self.kwdargs) diff --git a/research/cv/centernet_det/src/hourglass.py b/research/cv/centernet_det/src/hourglass.py index d22827cbf6d62e76ded97eb32f45643c256fc651..3a4af36fa98da7e954333c637588fe2455f37698 100644 --- a/research/cv/centernet_det/src/hourglass.py +++ b/research/cv/centernet_det/src/hourglass.py @@ -16,7 +16,6 @@ hourglass backbone """ import mindspore.nn as nn -import mindspore.ops as ops BN_MOMENTUM = 0.9 @@ -156,12 +155,14 @@ class Kp_module(nn.Cell): next_dim, curr_dim, 3, curr_mod, **kwargs ) + self.up2 = nn.ResizeBilinear() + def construct(self, x): """Defines the computation performed.""" up1 = self.up1(x) low1 = self.low1(up1) low2 = self.low2(low1) low3 = self.low3(low2) - up2 = ops.interpolate(low3, scale_factor=2) + up2 = self.up2(low3, scale_factor=2) outputs = up1 + up2 return outputs diff --git a/research/cv/csd/csd_train.py b/research/cv/csd/csd_train.py index 3d1cee0dbd7b3c15d6ad294bbfe135d92e30c33a..8b2f4c8bc021ccd2f757eaa272a0f5c2ed3d7ea6 100644 --- a/research/cv/csd/csd_train.py +++ b/research/cv/csd/csd_train.py @@ -71,7 +71,8 @@ class NetWithCSDLossCell(nn.Cell): loss = self.l1_loss(sr, hr) + self.l1_loss(tea_sr, hr) if self.contrast_w > 0: - bic = ops.interpolate(lr, size=(lr.shape[-2] * 4, lr.shape[-1] * 4)) + resize = nn.ResizeBilinear() + bic = resize(lr, size=(lr.shape[-2] * 4, lr.shape[-1] * 4)) neg = numpy.flip(bic, 0) neg = neg[:self.neg_num, :, :, :] loss += self.contrast_w * self.contrast_loss(tea_sr, sr, neg) diff --git a/research/cv/dgcnet_res101/eval.py b/research/cv/dgcnet_res101/eval.py index 0b23fd0160283020787546b7255b762305dfa0ab..9215ef335a94b1bf9e4802badf1f56a472d89a21 100644 --- a/research/cv/dgcnet_res101/eval.py +++ b/research/cv/dgcnet_res101/eval.py @@ -115,7 +115,7 @@ def pad_image(img, target_size): def predict_sliding(net, image, tile_size, classes): """predict_sliding""" - interp = mindspore.ops.ResizeBilinearV2(align_corners=True) + interp = mindspore.ops.ResizeBilinear(tile_size, align_corners=True) image_size = image.shape overlap = 1.0 / 3.0 @@ -144,7 +144,7 @@ def predict_sliding(net, image, tile_size, classes): padded_prediction = list(net(padded_img)) if isinstance(padded_prediction, list): padded_prediction = padded_prediction[0] - padded_prediction = interp(padded_prediction, tile_size)[0] + padded_prediction = interp(padded_prediction)[0] padded_prediction = transpose(padded_prediction, (1, 2, 0)).asnumpy() prediction = padded_prediction[0:img.shape[2], 0:img.shape[3], :] count_predictions[y1:y2, x1:x2] += 1 @@ -159,11 +159,11 @@ def predict_whole(net, image, tile_size): """predict_whole""" transpose = mindspore.ops.Transpose() image = Tensor(image) - interp = mindspore.ops.ResizeBilinearV2(align_corners=True) + interp = mindspore.ops.ResizeBilinear(tile_size, align_corners=True) prediction = net(image) if isinstance(prediction, list): prediction = prediction[0] - prediction = interp(prediction, tile_size).cpu().data[0].numpy() + prediction = interp(prediction).cpu().data[0].numpy() prediction = transpose(prediction, (1, 2, 0)) return prediction diff --git a/research/cv/dgcnet_res101/src/DualGCNNet.py b/research/cv/dgcnet_res101/src/DualGCNNet.py index c4a3dabca1d2a0df3935af57437795a9da4ce37f..41427142da5b97c435c4bda4130a5a014d649d87 100644 --- a/research/cv/dgcnet_res101/src/DualGCNNet.py +++ b/research/cv/dgcnet_res101/src/DualGCNNet.py @@ -154,8 +154,8 @@ class DualGCN(nn.Cell): x = feat local = self.local(feat) local = self.gcn_local_attention(local) - resize_bilinear = mindspore.ops.ResizeBilinearV2(align_corners=True) - local = resize_bilinear(local, x.shape[2:]) + resize_bilinear = mindspore.ops.ResizeBilinear(size=x.shape[2:], align_corners=True) + local = resize_bilinear(local) spatial_local_feat = x * local + x x_sqz, b = x, x diff --git a/research/cv/dgcnet_res101/src/loss.py b/research/cv/dgcnet_res101/src/loss.py index 7a53746b7a4f7e54854cb1b6f40b99fff2b077dc..5845281ef5b0e97308545559bb4612b5aebe9ee9 100644 --- a/research/cv/dgcnet_res101/src/loss.py +++ b/research/cv/dgcnet_res101/src/loss.py @@ -144,10 +144,10 @@ class CriterionOhemDSN(nn.Cell): """ preds = self.net(image) # [Tensor1, Tensor2] target = target.astype("int64") - resize1 = mindspore.ops.ResizeBilinearV2(align_corners=True) - scale_pred = resize1(preds[0], (self.h, self.w)) # (1, 19, 832, 832) + resize1 = mindspore.ops.ResizeBilinear((self.h, self.w), align_corners=True) + scale_pred = resize1(preds[0]) # (1, 19, 832, 832) loss1 = self.criterion1(scale_pred, target) - scale_pred = resize1(preds[1], (self.h, self.w)) # (1, 19, 832, 832) + scale_pred = resize1(preds[1]) # (1, 19, 832, 832) loss2 = self.criterion2(scale_pred, target) return loss1 + loss2 * 0.4 diff --git a/research/cv/east/src/east.py b/research/cv/east/src/east.py index f6d43e669e336177cef07f3c33f496abed9bc51a..37beb8be587eda2253830cd5e5c76fc4dc369c96 100644 --- a/research/cv/east/src/east.py +++ b/research/cv/east/src/east.py @@ -224,17 +224,17 @@ class Merge(nn.Cell): img_hight = P.Shape()(x)[2] img_width = P.Shape()(x)[3] - out = P.ResizeBilinearV2(True)(f4, (img_hight / 16, img_width / 16)) + out = P.ResizeBilinear((img_hight / 16, img_width / 16), True)(f4) out = self.concat((out, f3)) out = self.relu1(self.bn1(self.conv1(out))) out = self.relu2(self.bn2(self.conv2(out))) - out = P.ResizeBilinearV2(True)(out, (img_hight / 8, img_width / 8)) + out = P.ResizeBilinear((img_hight / 8, img_width / 8), True)(out) out = self.concat((out, f2)) out = self.relu3(self.bn3(self.conv3(out))) out = self.relu4(self.bn4(self.conv4(out))) - out = P.ResizeBilinearV2(True)(out, (img_hight / 4, img_width / 4)) + out = P.ResizeBilinear((img_hight / 4, img_width / 4), True)(out) out = self.concat((out, f1)) out = self.relu5(self.bn5(self.conv5(out))) out = self.relu6(self.bn6(self.conv6(out))) diff --git a/research/cv/eppmvsnet/src/eppmvsnet.py b/research/cv/eppmvsnet/src/eppmvsnet.py index c1f2cda3a0f9bb9bae2c6bffb72209b3a2f2c356..12f7c1104d39a25c8090048e440d9def440ef1ce 100644 --- a/research/cv/eppmvsnet/src/eppmvsnet.py +++ b/research/cv/eppmvsnet/src/eppmvsnet.py @@ -459,10 +459,10 @@ class EPPMVSNetP3(nn.Cell): for pair_result in pair_results_1: uncertainty_maps_1.append(pair_result[1][0]) for uncertainty_map in uncertainty_maps_1: - uncertainty_maps_2.append(P.ResizeBilinearV2(False)(uncertainty_map, (H // 4, W // 4))) + uncertainty_maps_2.append(P.ResizeBilinear((H // 4, W // 4), False)(uncertainty_map)) ref_feat_2, srcs_feat_2 = feat_pack_2[:, 0], feat_pack_2[:, 1:] - depth_start_2 = P.ResizeBilinearV2(False)(est_depth_1, (H // 4, W // 4)) + depth_start_2 = P.ResizeBilinear((H // 4, W // 4), False)(est_depth_1) if self.entropy_range: est_depth_2, _, _, conf_range_2 = self.stage2([ref_feat_2, srcs_feat_2, proj_mats[:, :, 1]], @@ -480,10 +480,10 @@ class EPPMVSNetP3(nn.Cell): stage3_conf_interval = None uncertainty_maps_3 = [] for uncertainty_map in uncertainty_maps_2: - uncertainty_maps_3.append(P.ResizeBilinearV2(False)(uncertainty_map, (H // 2, W // 2))) + uncertainty_maps_3.append(P.ResizeBilinear((H // 2, W // 2), False)(uncertainty_map)) ref_feat_3, srcs_feat_3 = feat_pack_3[:, 0], feat_pack_3[:, 1:] - depth_start_3 = P.ResizeBilinearV2(False)(est_depth_2, (H // 2, W // 2)) + depth_start_3 = P.ResizeBilinear((H // 2, W // 2), False)(est_depth_2) if self.entropy_range: est_depth_3, prob_map_3, _, _ = self.stage3([ref_feat_3, srcs_feat_3, proj_mats[:, :, 0]], diff --git a/research/cv/essay-recogination/src/cnv_model.py b/research/cv/essay-recogination/src/cnv_model.py index c3ca0ede3428251b2ae7fbd84ccf4145c7fba0a4..4a9a53354045e0a0872b2af5b316bedad9617d5c 100644 --- a/research/cv/essay-recogination/src/cnv_model.py +++ b/research/cv/essay-recogination/src/cnv_model.py @@ -27,7 +27,7 @@ ngates = 2 def ginM(n): return gin.query_parameter(f'%{n}') gin.external_configurable(nn.MaxPool2d, module='nn') -gin.external_configurable(ops.ResizeBilinearV2, module='nn') +gin.external_configurable(ops.ResizeBilinear, module='nn') class LN(nn.Cell): diff --git a/research/cv/fastscnn/src/fast_scnn.py b/research/cv/fastscnn/src/fast_scnn.py index 454629b7b4fdd30ace31919f7c9c9b2e74d5eea3..802f2bc65f7753bf253b5c7e412d6c185ea9a518 100644 --- a/research/cv/fastscnn/src/fast_scnn.py +++ b/research/cv/fastscnn/src/fast_scnn.py @@ -14,7 +14,6 @@ # ============================================================================ """Fast Segmentation Convolutional Neural Network""" import mindspore.nn as nn -import mindspore.ops as ops from mindspore.ops import operations as P from src.loss import MixSoftmaxCrossEntropyLoss @@ -45,7 +44,7 @@ class FastSCNN(nn.Cell): nn.Dropout(p=0.1), nn.Conv2d(32, num_classes, 1, has_bias=True)] ) - + self.ResizeBilinear = nn.ResizeBilinear() def construct(self, x): '''construct''' size = x.shape[2:] @@ -54,16 +53,15 @@ class FastSCNN(nn.Cell): x = self.feature_fusion(higher_res_features, lower_res_features) x = self.classifier(x) - x = ops.interpolate(x, size, align_corners=True) + x = self.ResizeBilinear(x, size, align_corners=True) if self.aux: auxout = self.auxlayer1(higher_res_features) - auxout = ops.interpolate(auxout, size, align_corners=True) + auxout = self.ResizeBilinear(auxout, size, align_corners=True) auxout2 = self.auxlayer2(lower_res_features) - auxout2 = ops.interpolate(auxout2, size, align_corners=True) + auxout2 = self.ResizeBilinear(auxout2, size, align_corners=True) return x, auxout, auxout2 return x - class _ConvBNReLU(nn.Cell): """Conv-BN-ReLU""" def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=0): @@ -145,6 +143,7 @@ class PyramidPooling(nn.Cell): self.pool12 = nn.AvgPool2d(kernel_size=12, stride=12) self.pool8 = nn.AvgPool2d(kernel_size=8, stride=8) self.pool6 = nn.AvgPool2d(kernel_size=4, stride=4) + self.resizeBilinear = nn.ResizeBilinear() def _AdaptiveAvgPool2d(self, x, output_size): #NCHW, for NCx24x24 and size in (1,2,3,6) only @@ -157,7 +156,7 @@ class PyramidPooling(nn.Cell): return self.pool6(x) def upsample(self, x, size): - return ops.interpolate(x, size, align_corners=True) + return self.resizeBilinear(x, size, align_corners=True) def construct(self, x): size = x.shape[2:] @@ -225,9 +224,10 @@ class FeatureFusionModule(nn.Cell): nn.Conv2d(highter_in_channels, out_channels, 1), nn.BatchNorm2d(out_channels)]) self.relu = nn.ReLU() - + self.ResizeBilinear = nn.ResizeBilinear() def construct(self, higher_res_feature, lower_res_feature): - lower_res_feature = ops.interpolate(lower_res_feature, scale_factor=4, align_corners=True) + lower_res_feature = self.ResizeBilinear(lower_res_feature, \ + scale_factor=4, align_corners=True) lower_res_feature = self.dwconv(lower_res_feature) lower_res_feature = self.conv_lower_res(lower_res_feature) higher_res_feature = self.conv_higher_res(higher_res_feature) diff --git a/research/cv/flownet2/src/submodels/submodules.py b/research/cv/flownet2/src/submodels/submodules.py index 93e43b61c6a5a005ecf0342c23182725e7fb1b01..c8adc05bcbe9c1fde372725fbd30a89ff8dcd521 100644 --- a/research/cv/flownet2/src/submodels/submodules.py +++ b/research/cv/flownet2/src/submodels/submodules.py @@ -57,10 +57,10 @@ class Upsample(nn.Cell): new_height = shape[2] * self.scale_factor new_width = shape[3] * self.scale_factor if self.mode == 'nearest': - upsample_op = ops.ResizeNearestNeighbor((new_height, new_width))(x) + upsample_op = ops.ResizeNearestNeighbor((new_height, new_width)) else: - upsample_op = ops.ResizeBilinearV2()(x, (new_height, new_width)) - return upsample_op + upsample_op = ops.ResizeBilinear((new_height, new_width)) + return upsample_op(x) def conv(batchnorm, in_planes, out_planes, kernel_size=3, stride=1): diff --git a/research/cv/lite-hrnet/src/model.py b/research/cv/lite-hrnet/src/model.py index 6ff7477e554e653dfe094ce979d75e8bf11cdd61..21b08260ea14a5870e02ea4ece632e6d0541cd5e 100644 --- a/research/cv/lite-hrnet/src/model.py +++ b/research/cv/lite-hrnet/src/model.py @@ -133,7 +133,7 @@ class DynamicUpsample(nn.Cell): if self.mode == 'nearest': operation = ops.ResizeNearestNeighbor((shape[0]*self.scale_factor, shape[1]*self.scale_factor))(x) else: - operation = ops.interpolate(x, size=(shape[0]*self.scale_factor, shape[1]*self.scale_factor)) + operation = nn.ResizeBilinear()(x, size=(shape[0]*self.scale_factor, shape[1]*self.scale_factor)) return operation @@ -146,6 +146,7 @@ class IdentityCell(nn.Cell): return x + class DepthwiseSeparableConvModule(nn.Cell): """Depthwise separable convolution module. See https://arxiv.org/pdf/1704.04861.pdf for details. @@ -562,7 +563,7 @@ class IterativeHead(nn.Cell): last_x = None for i, s in enumerate(x): if last_x is not None: - last_x = ops.ResizeBilinearV2(align_corners=True)(last_x, s.shape[-2:]) + last_x = ops.ResizeBilinear(s.shape[-2:], align_corners=True)(last_x) s = ops.Add()(s, last_x) s = self.projects[i](s) diff --git a/research/cv/m2det/src/model.py b/research/cv/m2det/src/model.py index 4f7f423499581aa6fd90171e54c957cbc4253570..db7610dba97b29ad9efafd2b262575598b63cb32 100644 --- a/research/cv/m2det/src/model.py +++ b/research/cv/m2det/src/model.py @@ -158,7 +158,7 @@ class DynamicUpscale(nn.Cell): if self.mode == 'nearest': operation = ops.ResizeNearestNeighbor((shape[0]*self.scale_factor, shape[1]*self.scale_factor))(x) else: - operation = ops.interpolate(x, size=(shape[0]*self.scale_factor, shape[1]*self.scale_factor)) + operation = nn.ResizeBilinear()(x, size=(shape[0]*self.scale_factor, shape[1]*self.scale_factor)) return operation diff --git a/research/cv/midas/midas_eval.py b/research/cv/midas/midas_eval.py index d329f082ef1e904a5337bd5fd6bb25822c8cc19b..6065f494c8422e1465715685eb27996c9bedbbdf 100644 --- a/research/cv/midas/midas_eval.py +++ b/research/cv/midas/midas_eval.py @@ -83,8 +83,8 @@ def eval_Kitti(data_path, net): expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -148,8 +148,8 @@ def eval_TUM(datapath, net): expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) print(prediction.shape, mask.shape) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -216,8 +216,8 @@ def eval_Sintel(datapath, net): expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -280,8 +280,8 @@ def eval_ETH3D(datapath, net): expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -332,8 +332,8 @@ def eval_DIW(datapath, net): shape_w, shape_h = [int(sample['depths'][-2]), int(sample['depths'][-1])] expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, (shape_h, shape_w)) + resize_bilinear = ops.ResizeBilinear((shape_h, shape_w)) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) pixtel_a = prediction[int(sample['depths'][0]) - 1][int(sample['depths'][1]) - 1] @@ -399,8 +399,8 @@ def eval_NYU(datamat, splitmat, net): expand_dims = ops.ExpandDims() prediction = expand_dims(prediction, 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) diff --git a/research/cv/midas/midas_eval_onnx.py b/research/cv/midas/midas_eval_onnx.py index d97b553a6597e9dd9737de72f089357bbd412eca..e9fd08f0199633e16af8f5914cc214e0c6ef0cd6 100644 --- a/research/cv/midas/midas_eval_onnx.py +++ b/research/cv/midas/midas_eval_onnx.py @@ -91,8 +91,8 @@ def eval_onnx_Kitti(data_path, session2, input_name2): expand_dims = ops.ExpandDims() prediction = expand_dims(Tensor(prediction), 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -158,8 +158,8 @@ def eval_onnx_TUM(datapath, session3, input_name3): expand_dims = ops.ExpandDims() prediction = expand_dims(Tensor(prediction), 0) print(prediction.shape, mask.shape) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) @@ -227,8 +227,8 @@ def eval_onnx_Sintel(datapath, session1, input_name1): expand_dims = ops.ExpandDims() prediction = expand_dims(Tensor(prediction), 0) - resize_bilinear = ops.ResizeBilinearV2() - prediction = resize_bilinear(prediction, mask.shape[1:]) + resize_bilinear = ops.ResizeBilinear(mask.shape[1:]) + prediction = resize_bilinear(prediction) prediction = np.squeeze(prediction.asnumpy()) loss = metric(prediction, depth, mask) diff --git a/research/cv/midas/midas_run.py b/research/cv/midas/midas_run.py index eaa3e604c73102e4d81d061f1e108c7624fb10fc..a3ac616cbe0ac9c8ac76dd664d8dca8a001a5fbb 100644 --- a/research/cv/midas/midas_run.py +++ b/research/cv/midas/midas_run.py @@ -51,7 +51,7 @@ def export(): print("start processing") expand_dims = ops.ExpandDims() - resize_bilinear = ops.ResizeBilinearV2 + resize_bilinear = ops.ResizeBilinear squeeze = ops.Squeeze() for ind, img_name in enumerate(img_names): print(" processing {} ({}/{})".format(img_name, ind + 1, num_images)) @@ -65,7 +65,7 @@ def export(): sample = expand_dims(sample, 0) prediction = net(sample) prediction = expand_dims(prediction, 1) - prediction = resize_bilinear()(prediction, (img.shape[:2])) + prediction = resize_bilinear((img.shape[:2]))(prediction) prediction = squeeze(prediction).asnumpy() # output filename = os.path.join( diff --git a/research/cv/midas/src/blocks_ms.py b/research/cv/midas/src/blocks_ms.py index 2122a2e50ed1a339517c9e8c6eaf2d45bba3eccf..0745be10a9dfaea21211c48378ec2c265eed8d42 100644 --- a/research/cv/midas/src/blocks_ms.py +++ b/research/cv/midas/src/blocks_ms.py @@ -23,7 +23,7 @@ class FeatureFusionBlock(nn.Cell): super(FeatureFusionBlock, self).__init__() self.resConfUnit1 = ResidualConvUnit(features) self.resConfUnit2 = ResidualConvUnit(features) - self.resize_bilinear = ops.ResizeBilinearV2 + self.resize_bilinear = ops.ResizeBilinear self.shape = ops.Shape() def construct(self, *xs): @@ -33,7 +33,7 @@ class FeatureFusionBlock(nn.Cell): output = self.resConfUnit2(output) size_x = self.shape(output)[2] * 2 size_y = self.shape(output)[3] * 2 - output = self.resize_bilinear()(output, (size_x, size_y)) + output = self.resize_bilinear((size_x, size_y))(output) return output @@ -64,12 +64,12 @@ class Interpolate(nn.Cell): """Interpolate.""" def __init__(self, scale_factor): super(Interpolate, self).__init__() - self.resize_bilinear = ops.ResizeBilinearV2 + self.resize_bilinear = ops.ResizeBilinear self.scale_factor = scale_factor self.shape = ops.Shape() def construct(self, x): size_x = self.shape(x)[2] * self.scale_factor size_y = self.shape(x)[3] * self.scale_factor - x = self.resize_bilinear()(x, (size_x, size_y)) + x = self.resize_bilinear((size_x, size_y))(x) return x diff --git a/research/cv/mifnet/src/MIFNet/MIF_net.py b/research/cv/mifnet/src/MIFNet/MIF_net.py index 4fa8c99bb2636091a827a65a27a56989637632e2..eae0936b37fec30e2db92a7824a84d3c82182669 100644 --- a/research/cv/mifnet/src/MIFNet/MIF_net.py +++ b/research/cv/mifnet/src/MIFNet/MIF_net.py @@ -141,17 +141,17 @@ class BiSeNet(mindspore.nn.Cell): # output of context path cx2 = ops.mul(down_32, tail) - cx1 = ops.interpolate(down_16, size=sx.shape[-2:]) - cx2 = ops.interpolate(cx2, size=sx.shape[-2:]) + cx1 = nn.ResizeBilinear()(down_16, size=sx.shape[-2:]) + cx2 = nn.ResizeBilinear()(cx2, size=sx.shape[-2:]) cx = ops.concat((cx1, cx2), axis=1) # output of feature fusion module result = self.feature_fusion_module(sx, cx) # upsampling - result = ops.interpolate(result, scale_factor=2) + result = nn.ResizeBilinear()(result, scale_factor=2) result = self.attention_adjust(result, down_4) - result = ops.interpolate(result, scale_factor=4) + result = nn.ResizeBilinear()(result, scale_factor=4) result = self.conv(result) return result diff --git a/research/cv/ntsnet/src/network.py b/research/cv/ntsnet/src/network.py index b8244bd1fd4f6d43eee5f5cceefe931343cf4436..5196e6d0777b0f887b12e4cbd3f16815cb8f1632 100644 --- a/research/cv/ntsnet/src/network.py +++ b/research/cv/ntsnet/src/network.py @@ -167,6 +167,7 @@ class NTS_NET(nn.Cell): self.nms = P.NMSWithMask(0.25) self.topK_op = ops.TopK(sorted=True) self.opReshape = ops.Reshape() + self.opResizeLinear = ops.ResizeBilinear((224, 224)) self.transpose = ops.Transpose() self.opsCropResize = ops.CropAndResize(method="bilinear_v2") self.min_float_num = -65536.0 @@ -233,6 +234,8 @@ class NTS_NET(nn.Cell): current_img_for_teacher = self.opReshape(current_img_for_teacher, (-1, 224, 224, 3)) current_img_for_teacher = self.transpose(current_img_for_teacher, self.perm2) + + current_img_for_teacher = self.opReshape(current_img_for_teacher, (-1, 3, 224, 224)) current_img_for_teachers.append(current_img_for_teacher) feature = self.opReshape(feature, (batch_size, 1, -1)) diff --git a/research/cv/psenet/src/PSENET/fpn.py b/research/cv/psenet/src/PSENET/fpn.py index 77f83fc368cfc9c3a8a963e572e8c6e3a9ef45db..eb4113efc7a8b639a28da9c11c0faa3a7708e0b4 100644 --- a/research/cv/psenet/src/PSENET/fpn.py +++ b/research/cv/psenet/src/PSENET/fpn.py @@ -55,7 +55,9 @@ class FPN(nn.Cell): self.smooth_bn_p2 = _bn(out_channel) self.smooth_relu_p2 = nn.ReLU() - self._upsample = P.ResizeBilinearV2(align_corners=True) + self._upsample_p4 = P.ResizeBilinear((long_size // 16, long_size // 16), align_corners=True) + self._upsample_p3 = P.ResizeBilinear((long_size // 8, long_size // 8), align_corners=True) + self._upsample_p2 = P.ResizeBilinear((long_size // 4, long_size // 4), align_corners=True) self.concat = P.Concat(axis=1) @@ -65,25 +67,25 @@ class FPN(nn.Cell): c4 = self.reduce_conv_c4(c4) c4 = self.reduce_relu_c4(self.reduce_bn_c4(c4)) - p4 = self._upsample(p5, (self.long_size // 16, self.long_size // 16)) + c4 + p4 = self._upsample_p4(p5) + c4 p4 = self.smooth_conv_p4(p4) p4 = self.smooth_relu_p4(self.smooth_bn_p4(p4)) c3 = self.reduce_conv_c3(c3) c3 = self.reduce_relu_c3(self.reduce_bn_c3(c3)) - p3 = self._upsample(p4, (self.long_size // 8, self.long_size // 8)) + c3 + p3 = self._upsample_p3(p4) + c3 p3 = self.smooth_conv_p3(p3) p3 = self.smooth_relu_p3(self.smooth_bn_p3(p3)) c2 = self.reduce_conv_c2(c2) c2 = self.reduce_relu_c2(self.reduce_bn_c2(c2)) - p2 = self._upsample(p3, (self.long_size // 4, self.long_size // 4)) + c2 + p2 = self._upsample_p2(p3) + c2 p2 = self.smooth_conv_p2(p2) p2 = self.smooth_relu_p2(self.smooth_bn_p2(p2)) - p3 = self._upsample(p3, (self.long_size // 4, self.long_size // 4)) - p4 = self._upsample(p4, (self.long_size // 4, self.long_size // 4)) - p5 = self._upsample(p5, (self.long_size // 4, self.long_size // 4)) + p3 = self._upsample_p2(p3) + p4 = self._upsample_p2(p4) + p5 = self._upsample_p2(p5) out = self.concat((p2, p3, p4, p5)) diff --git a/research/cv/psenet/src/PSENET/psenet.py b/research/cv/psenet/src/PSENET/psenet.py index e50e7b0e057b54d835d171e54d400ed9a0929495..adaf30339d80a7d1333c3ea5e43a59ba4f5bf36c 100644 --- a/research/cv/psenet/src/PSENET/psenet.py +++ b/research/cv/psenet/src/PSENET/psenet.py @@ -57,7 +57,7 @@ class PSENet(nn.Cell): config.KERNEL_NUM, kernel_size=1, has_bias=True) - self._upsample = P.ResizeBilinearV2(align_corners=True) + self._upsample = P.ResizeBilinear((self.long_size, self.long_size), align_corners=True) if self.inference: self.one_float32 = Tensor(1.0, mstype.float32) @@ -75,7 +75,7 @@ class PSENet(nn.Cell): output = self.conv1(feature) output = self.relu1(self.bn1(output)) output = self.conv2(output) - output = self._upsample(output, (self.long_size, self.long_size)) + output = self._upsample(output) if self.inference: text = output[::, 0:1:1, ::, ::] diff --git a/research/cv/ras/eval.py b/research/cv/ras/eval.py index 8f03cb1736fc329ebdafc608a3649bb050996a8a..ff83ca1b96ebb10e601fa322da7b30efa8e43078 100644 --- a/research/cv/ras/eval.py +++ b/research/cv/ras/eval.py @@ -151,8 +151,8 @@ if __name__ == "__main__": for data in testdataloader.dataset.create_dict_iterator(): data, data_org = data["data"], data["data_org"] img, _, _, _, _ = model(data) - upsample = ops.ResizeBilinearV2(align_corners=False) - img = upsample(img, (data_org.shape[1], data_org.shape[2])) + upsample = ops.ResizeBilinear((data_org.shape[1], data_org.shape[2]), align_corners=False) + img = upsample(img) img = sigmoid(img) img = img.asnumpy().squeeze() img = (img - img.min()) / (img.max() - img.min() + 1e-8) diff --git a/research/cv/ras/eval_onnx.py b/research/cv/ras/eval_onnx.py index 7c1ab495b2faaa460ff2be8456d80a0ed66a7b10..3fcd2bbd7fb60a36d999a5a09eb116f44f74b5a5 100644 --- a/research/cv/ras/eval_onnx.py +++ b/research/cv/ras/eval_onnx.py @@ -118,8 +118,8 @@ if __name__ == "__main__": data, data_org = data["data"], data["data_org"] img = sess.run(None, {input_sess: data})[0] img = Tensor(img) - upsample = ops.ResizeBilinearV2(align_corners=False) - img = upsample(img, (data_org.shape[1], data_org.shape[2])) + upsample = ops.ResizeBilinear((data_org.shape[1], data_org.shape[2]), align_corners=False) + img = upsample(img) img = sigmoid(img) img = img.asnumpy().squeeze() img = (img - img.min()) / (img.max() - img.min() + 1e-8) diff --git a/research/cv/ras/src/model.py b/research/cv/ras/src/model.py index d6f1283523b705ef49422703a29be208ac6990de..8883030e843cd3cccd6c85b48270debf6d9d5dd8 100644 --- a/research/cv/ras/src/model.py +++ b/research/cv/ras/src/model.py @@ -24,14 +24,17 @@ from mindspore import load_checkpoint, load_param_into_net from src.resnet50 import ResNet50 + def Upsample_resize(source, target, device_target): target_shape = target.shape + + resize = None if device_target == "GPU": - result = ops.ResizeNearestNeighbor((target_shape[-1], target_shape[-2]))(source) + resize = ops.ResizeNearestNeighbor((target_shape[-1], target_shape[-2])) else: - result = ops.ResizeBilinearV2()(source, (target_shape[-1], target_shape[-2])) + resize = ops.ResizeBilinear((target_shape[-1], target_shape[-2])) - return result + return resize(source) diff --git a/research/cv/res2net_deeplabv3/src/nets/deeplab_v3/deeplab_v3.py b/research/cv/res2net_deeplabv3/src/nets/deeplab_v3/deeplab_v3.py index 27281fcdeb8cf0c1d85b7ecb834331952fa9cbf0..3d237f4bb0362aa818d2c82f9de4f896394b7e5f 100644 --- a/research/cv/res2net_deeplabv3/src/nets/deeplab_v3/deeplab_v3.py +++ b/research/cv/res2net_deeplabv3/src/nets/deeplab_v3/deeplab_v3.py @@ -296,5 +296,5 @@ class DeepLabV3(nn.Cell): size = self.shape(x) out = self.res2net(x) out = self.aspp(out) - out = P.ResizeBilinearV2(True)(out, (size[2], size[3])) + out = P.ResizeBilinear((size[2], size[3]), True)(out) return out diff --git a/research/cv/ssd_mobilenetV2_FPNlite/src/mobilenet_v2_fpn.py b/research/cv/ssd_mobilenetV2_FPNlite/src/mobilenet_v2_fpn.py index 0a6741f29478727cd47fac5afc66438e770013ba..0c76ba6413a2efdac9c9ed392c6365c684295ed7 100644 --- a/research/cv/ssd_mobilenetV2_FPNlite/src/mobilenet_v2_fpn.py +++ b/research/cv/ssd_mobilenetV2_FPNlite/src/mobilenet_v2_fpn.py @@ -240,7 +240,7 @@ class FpnTopDown(nn.Cell): top = len(inputs) - i - 1 down = top - 1 size = F.shape(inputs[down]) - top_down = P.ResizeBilinearV2()(features[-1], (size[2], size[3])) + top_down = P.ResizeBilinear((size[2], size[3]))(features[-1]) top_down = top_down + image_features[down] features = features + (top_down,) diff --git a/research/cv/swenet/src/model/SWEnet.py b/research/cv/swenet/src/model/SWEnet.py index 08d4588f54406d07f70b4e1907d33eb6e8e66ee5..156e16df3c9c1e0b5068e2680b68806ef1b6cd9b 100644 --- a/research/cv/swenet/src/model/SWEnet.py +++ b/research/cv/swenet/src/model/SWEnet.py @@ -299,9 +299,9 @@ class ResNet(nn.Cell): x1 = self.op((x1_1, x1_3, x1_5)) x2 = self.op((x2_1, x2_3, x2_5)) - ops_resize = ops.ResizeBilinearV2() - x1 = ops_resize(x1, (16, 16)) - x2 = ops_resize(x2, (16, 16)) + ops_resize = ops.ResizeBilinear((16, 16)) + x1 = ops_resize(x1) + x2 = ops_resize(x2) d = ops.mul(x1, x1) - ops.mul(x2, x2) d = ops.mul(d, d) diff --git a/research/cv/textfusenet/src/textfusenet/fpn_neck.py b/research/cv/textfusenet/src/textfusenet/fpn_neck.py index bead3b6cfe6a040511f3fdf0b4a50b9bcbb99550..cc76e167de8ab4fe0bee3b9c3d0bf2b5eca1c755 100755 --- a/research/cv/textfusenet/src/textfusenet/fpn_neck.py +++ b/research/cv/textfusenet/src/textfusenet/fpn_neck.py @@ -84,7 +84,9 @@ class FeatPyramidNeck(nn.Cell): 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.interpolate = P.ResizeBilinearV2() + self.interpolate1 = P.ResizeBilinear((48, 80)) + self.interpolate2 = P.ResizeBilinear((96, 160)) + self.interpolate3 = P.ResizeBilinear((192, 320)) self.cast = P.Cast() self.maxpool = P.MaxPool(kernel_size=1, strides=2, pad_mode="same") @@ -95,9 +97,9 @@ class FeatPyramidNeck(nn.Cell): x += (self.lateral_convs_list[i](inputs[i]),) y = (x[3],) - y = y + (x[2] + self.cast(self.interpolate(y[self.fpn_layer - 4], (48, 80)), mstype.float16),) - y = y + (x[1] + self.cast(self.interpolate(y[self.fpn_layer - 3], (96, 160)), mstype.float16),) - y = y + (x[0] + self.cast(self.interpolate(y[self.fpn_layer - 2], (192, 320)), mstype.float16),) + y = y + (x[2] + self.cast(self.interpolate1(y[self.fpn_layer - 4]), mstype.float16),) + y = y + (x[1] + self.cast(self.interpolate2(y[self.fpn_layer - 3]), mstype.float16),) + y = y + (x[0] + self.cast(self.interpolate3(y[self.fpn_layer - 2]), mstype.float16),) z = () for i in range(self.fpn_layer - 1, -1, -1): diff --git a/research/cv/textfusenet/src/textfusenet/rcnn_seg.py b/research/cv/textfusenet/src/textfusenet/rcnn_seg.py index b8b78e954e03b146faa233a442f212a429cb8006..ab3d6d3eedc6363e5cc57a0edac67345febf247d 100755 --- a/research/cv/textfusenet/src/textfusenet/rcnn_seg.py +++ b/research/cv/textfusenet/src/textfusenet/rcnn_seg.py @@ -18,7 +18,7 @@ import mindspore.nn as nn from mindspore.ops import functional as F from mindspore.ops import operations as P from mindspore.common.tensor import Tensor -from mindspore.ops import ResizeBilinearV2 as Sample +from mindspore.ops import ResizeBilinear as Sample import mindspore.common.dtype as mstype from .roi_align import ROIAlign @@ -28,7 +28,6 @@ class FpnSeg(nn.Cell): def __init__(self, input_channels, output_channels, num_classes, image_shape): super(FpnSeg, self).__init__() - self.image_shape = image_shape self.conv1x1_list = nn.CellList() for _ in range(4): self.conv1x1_list.append(nn.Conv2d(input_channels, output_channels, kernel_size=1, stride=1, padding=0, @@ -48,15 +47,15 @@ class FpnSeg(nn.Cell): self.seg_logits = nn.Conv2d(output_channels, num_classes, kernel_size=1, stride=1, padding=0, has_bias=True).to_float(mstype.float16) - self.image_sampler = Sample(True) - self.feature_sampler = Sample(True) + self.image_sampler = Sample(image_shape, True) + self.feature_sampler = Sample((96, 160), True) def construct(self, x, feature_level, proposal_boxes): """rcnn seg submodule forward""" feature_fuse = self.conv1x1_list[feature_level](x[feature_level]) for i, feature in enumerate(x): if i != feature_level: - feature = self.feature_sampler(feature, (96, 160)) + feature = self.feature_sampler(feature) feature = self.conv1x1_list[i](feature) feature_fuse += feature @@ -66,7 +65,7 @@ class FpnSeg(nn.Cell): for i in range(len(self.conv3x3_roi_list)): global_context = self.conv3x3_roi_list[i](global_context) - feature_pred = self.image_sampler(feature_fuse, self.image_shape) + feature_pred = self.image_sampler(feature_fuse) feature_pred = self.conv1x1_seg_logits(feature_pred) feature_pred = self.seg_logits(feature_pred) diff --git a/research/cv/u2net/src/blocks.py b/research/cv/u2net/src/blocks.py index 4fae6823c20d4682d4384d69f13ca5e155f2c571..3f150073bd95f0e30cbe13b0fca0ca13ec01c73e 100644 --- a/research/cv/u2net/src/blocks.py +++ b/research/cv/u2net/src/blocks.py @@ -45,8 +45,8 @@ class REBNCONV(nn.Cell): def _upsample_like(src, tar): """generate upsample unit""" - resize_bilinear = mindspore.ops.operations.ResizeBilinearV2() - src = resize_bilinear(src, tar.shape[2:]) + resize_bilinear = mindspore.ops.operations.ResizeBilinear(tar.shape[2:]) + src = resize_bilinear(src) return src