From 5a2369b7f358b7c4b20a4140b154b8242504681f Mon Sep 17 00:00:00 2001 From: daiyuxin0511 <455472400@qq.com> Date: Wed, 3 Jan 2024 14:06:50 +0800 Subject: [PATCH 1/7] Revert "ResizeBilinear -> ResizeBilinearV2" This reverts commit 70f5bd2bad008d45585162190059fad4109a75ae. --- community/cv/CPNet/eval.py | 3 +- community/cv/CPNet/src/model/cpnet.py | 10 +++-- community/cv/CPNet/src/utils/metrics.py | 1 + community/cv/SRFlow/src/model/RRDBNet.py | 3 +- .../maskrcnn/maskrcnn_mobilenetv1/fpn_neck.py | 10 +++-- .../src/maskrcnnPointRend_r50.py | 3 +- community/cv/wgan_gp/src/resgan_model.py | 4 +- official/cv/CycleGAN/src/models/unet.py | 2 +- official/cv/DBNet/src/modules/asf.py | 3 +- official/cv/DeepLabV3P/src/deeplab_v3plus.py | 2 +- .../src/nets/deeplab_v3/deeplab_v3.py | 2 +- official/cv/OCRNet/eval.py | 2 +- official/cv/OCRNet/src/basedataset.py | 6 +-- official/cv/OCRNet/src/callback.py | 2 +- official/cv/OCRNet/src/cityscapes.py | 2 +- official/cv/OCRNet/src/loss.py | 8 ++-- official/cv/OCRNet/src/seg_hrnet.py | 10 +++-- official/cv/OCRNet/src/seg_hrnet_ocr.py | 15 +++++--- official/cv/PVNet/src/model_reposity.py | 10 +++-- official/cv/SSD/src/fpn.py | 2 +- .../cv/Unet/src/unet_nested/unet_parts.py | 2 +- .../cv/ArbitraryStyleTransfer/src/networks.py | 11 +++--- research/cv/Auto-DeepLab/src/core/aspp.py | 3 +- research/cv/Auto-DeepLab/src/core/decoder.py | 3 +- research/cv/Auto-DeepLab/src/core/encoder.py | 5 ++- research/cv/Auto-DeepLab/src/core/model.py | 6 ++- research/cv/CGAN/eval.py | 4 +- research/cv/CGAN/infer_onnx.py | 4 +- research/cv/CSNL/src/Attention.py | 4 +- research/cv/CTSDG/src/generator/cfa.py | 2 +- research/cv/CTSDG/src/generator/generator.py | 4 +- research/cv/DDM/eval.py | 11 +++--- research/cv/DDRNet/src/models/ddrnet23.py | 8 ++-- research/cv/DRNet/eval.py | 6 +-- research/cv/DecoMR/models/WithLossCellDP.py | 4 +- research/cv/DecoMR/models/WithLossCellEnd.py | 6 +-- research/cv/DecoMR/models/resnet.py | 7 ++-- research/cv/DecoMR/models/upsample.py | 5 +-- research/cv/E-NET/src/model.py | 2 +- research/cv/EGnet/src/egnet.py | 27 +++++++------- research/cv/EGnet/src/resnet.py | 7 ++-- research/cv/FCANet/src/model/fcanet.py | 14 +++---- research/cv/HRNetW48_cls/src/cls_hrnet.py | 3 ++ research/cv/HRNetW48_seg/eval.py | 6 +-- research/cv/HRNetW48_seg/export.py | 4 +- research/cv/HRNetW48_seg/src/callback.py | 2 +- .../HRNetW48_seg/src/dataset/basedataset.py | 6 +-- .../cv/HRNetW48_seg/src/dataset/cityscapes.py | 2 +- research/cv/HRNetW48_seg/src/loss.py | 20 +++++----- research/cv/HRNetW48_seg/src/seg_hrnet.py | 11 ++++-- research/cv/ICNet/src/loss.py | 7 ++-- research/cv/ICNet/src/models/icnet.py | 26 ++++++++----- research/cv/ICNet/src/models/icnet_dc.py | 26 ++++++++----- research/cv/MIMO-UNet/src/loss.py | 3 +- research/cv/MIMO-UNet/src/mimo_unet.py | 4 +- research/cv/MODNet/src/models/modnet.py | 10 +++-- research/cv/PAGENet/src/pagenet.py | 13 +++---- research/cv/PSPNet/eval.py | 3 +- research/cv/PSPNet/eval_onnx_cpu.py | 3 +- research/cv/PSPNet/postprocess.py | 4 +- research/cv/PSPNet/src/model/pspnet.py | 14 ++++--- research/cv/PWCNet/src/pwc_modules.py | 2 +- research/cv/PaDiM/eval.py | 4 +- research/cv/PaDiM/eval_onnx.py | 4 +- research/cv/PaDiM/postprocess.py | 4 +- .../cv/Pix2PixHD/precompute_feature_maps.py | 5 ++- research/cv/PyramidBox/src/pyramidbox.py | 3 +- research/cv/RefineNet/src/refinenet.py | 2 +- .../model/attention_module.py | 37 +++++++++---------- research/cv/SPADE/src/models/inception.py | 2 +- research/cv/SPC-Net/network/network.py | 6 +-- research/cv/SSIM-AE/src/network.py | 3 +- research/cv/Segformer/src/loss.py | 7 ++-- research/cv/StyTr-2/src/models/transformer.py | 2 +- research/cv/UNet3+/src/models.py | 2 +- research/cv/Yolact++/src/yolact/layers/fpn.py | 2 +- .../src/yolact/layers/modules/loss.py | 2 +- .../cv/Yolact++/src/yolact/layers/protonet.py | 3 +- .../Yolact++/src/yolact/utils/interpolate.py | 5 +-- research/cv/centernet_det/src/hourglass.py | 5 ++- research/cv/csd/csd_train.py | 3 +- research/cv/dgcnet_res101/eval.py | 8 ++-- research/cv/dgcnet_res101/src/DualGCNNet.py | 4 +- research/cv/dgcnet_res101/src/loss.py | 6 +-- research/cv/east/src/east.py | 6 +-- research/cv/eppmvsnet/src/eppmvsnet.py | 8 ++-- .../cv/essay-recogination/src/cnv_model.py | 2 +- research/cv/fastscnn/src/fast_scnn.py | 18 ++++----- .../cv/flownet2/src/submodels/submodules.py | 6 +-- research/cv/lite-hrnet/src/model.py | 5 ++- research/cv/m2det/src/model.py | 2 +- research/cv/midas/midas_eval.py | 24 ++++++------ research/cv/midas/midas_eval_onnx.py | 12 +++--- research/cv/midas/midas_run.py | 4 +- research/cv/midas/src/blocks_ms.py | 8 ++-- research/cv/mifnet/src/MIFNet/MIF_net.py | 8 ++-- research/cv/ntsnet/src/network.py | 3 ++ research/cv/psenet/src/PSENET/fpn.py | 16 ++++---- research/cv/psenet/src/PSENET/psenet.py | 4 +- research/cv/ras/eval.py | 4 +- research/cv/ras/eval_onnx.py | 4 +- research/cv/ras/src/model.py | 9 +++-- .../src/nets/deeplab_v3/deeplab_v3.py | 2 +- .../src/mobilenet_v2_fpn.py | 2 +- research/cv/swenet/src/model/SWEnet.py | 6 +-- .../textfusenet/src/textfusenet/fpn_neck.py | 10 +++-- .../textfusenet/src/textfusenet/rcnn_seg.py | 11 +++--- research/cv/u2net/src/blocks.py | 4 +- 108 files changed, 384 insertions(+), 327 deletions(-) diff --git a/community/cv/CPNet/eval.py b/community/cv/CPNet/eval.py index 2b0c400fc..689264790 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 b13c90303..06582d2d3 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 1d788639b..d46fbb2c3 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 3fe0798a2..cb9bbd813 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 761e6653c..4d553fbc9 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 4f7a7fc0d..3f695f615 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 1f15ee219..626f7463a 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 f70039659..ffb317eb5 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 5675d6aee..90da664af 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 17c35f660..3f553bb49 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 c18963de8..5e6c463ef 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/OCRNet/eval.py b/official/cv/OCRNet/eval.py index 08d0f7617..615664edb 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 104ee830f..692443736 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 acf68b4b3..2cf6ff0be 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 3dbe7a519..be290412f 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 869cbecaf..5842464a4 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 49d3740ab..cf32f4027 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 6cbd664a7..95d613c4a 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 d15330cd9..fd969c699 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/SSD/src/fpn.py b/official/cv/SSD/src/fpn.py index f2befc5ba..83beebd70 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 c7c002b4e..31a4bb90d 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/research/cv/ArbitraryStyleTransfer/src/networks.py b/research/cv/ArbitraryStyleTransfer/src/networks.py index f790b1a1b..f893d1e25 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 71bc1297e..1dabd2870 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 deea79c73..3544d5e86 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 9f9260f26..17340ea9c 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 6f7eff75f..14d7e0fd5 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 e530fcf13..38ee45be9 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 bbeb4f775..558fef432 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 d457818e5..65e5b1433 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 2ab8f375a..914bbc98d 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 ebdbbc30c..fc04a9e23 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 d0b2b6e6f..64f94cae0 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 e06542118..cc79a629c 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 ec2586f68..f9d046c14 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 65cba9284..894a642f2 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 f02111e84..3f157a9e9 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 c84e9c39e..21a929df9 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 eafbd3332..7f8a27311 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 7629cfada..f14808395 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 22516a3b0..9679d6834 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 8aac01809..2796e57d9 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 bef0136ca..a5bb72ba4 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 d956643a1..e8b81f42d 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 cd21a3bc6..4e5ad6523 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 a38504a40..da9efc12b 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 6d4b72d04..4a8597d4e 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 367b462f4..a8c7d2073 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 cf30794a8..dddb9bf80 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 24f07decc..9f88cbde6 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 a0131a603..e0fa483e3 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 e540acad4..9e171c2db 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 9adf76915..5b589b1eb 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 6d61a6354..57d7bcc21 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 5317f4307..417541763 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 cb15f69ef..0909d4d95 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 cfec214a9..3b3081bad 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 14bf6a190..8c494972a 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 578d14d9a..e18ff69d4 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 1e892e633..102d48524 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 91392fd1c..cea407082 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 ba0695f35..dede35bb6 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 18f34856a..34897b38e 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 8c3a8a961..e00af8cc2 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 e6d67964d..e14665490 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 273e8f119..71be68387 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 6f31b5638..c64fb9c1e 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 43e56cb7b..03a2b432b 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 400528aba..824f68f0e 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 020939446..d38ae5943 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 78308c41d..42949606e 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 939cd8263..55f144ef1 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 8f8760704..9b0f270e7 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 9ecf2cc02..69d5a2f6b 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 0af23f65f..442a7df3d 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 b23583415..60708f9ad 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 87518cc4f..e10111a07 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 a80e27b46..45113c0fb 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 6d5ba008e..94fa36a90 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 3acb859be..70fd61901 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 d22827cbf..3a4af36fa 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 3d1cee0db..8b2f4c8bc 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 0b23fd016..9215ef335 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 c4a3dabca..41427142d 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 7a53746b7..5845281ef 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 f6d43e669..37beb8be5 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 c1f2cda3a..12f7c1104 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 c3ca0ede3..4a9a53354 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 454629b7b..802f2bc65 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 93e43b61c..c8adc05bc 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 6ff7477e5..21b08260e 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 4f7f42349..db7610dba 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 d329f082e..6065f494c 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 d97b553a6..e9fd08f01 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 eaa3e604c..a3ac616cb 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 2122a2e50..0745be10a 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 4fa8c99bb..eae0936b3 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 b8244bd1f..5196e6d07 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 77f83fc36..eb4113efc 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 e50e7b0e0..adaf30339 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 8f03cb173..ff83ca1b9 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 7c1ab495b..3fcd2bbd7 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 d6f128352..8883030e8 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 27281fcde..3d237f4bb 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 0a6741f29..0c76ba641 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 08d4588f5..156e16df3 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 bead3b6cf..cc76e167d 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 b8b78e954..ab3d6d3ee 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 4fae6823c..3f150073b 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 -- Gitee From 41a21b0b67d564b87fe2fc618d9ea02c95fce5af Mon Sep 17 00:00:00 2001 From: daiyuxin0511 <455472400@qq.com> Date: Thu, 4 Jan 2024 10:13:07 +0800 Subject: [PATCH 2/7] Revert "modify the input of concat from ms.bool_ to ms.int32" This reverts commit da505fa86daecff3255a862e6d523b5efcfe202c. --- .../src/FasterRcnn/bbox_assign_sample.py | 8 +------- .../src/FasterRcnn/bbox_assign_sample_stage2.py | 10 ++-------- .../src/FasterRcnn/proposal_generator.py | 14 +++----------- .../src/maskrcnn_mobilenetv1/fpn_neck.py | 10 ++++++---- .../maskrcnn_resnet50/src/maskrcnn/fpn_neck.py | 11 ++++++----- 5 files changed, 18 insertions(+), 35 deletions(-) diff --git a/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample.py b/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample.py index a49572c6a..57c758a51 100644 --- a/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample.py +++ b/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample.py @@ -144,13 +144,7 @@ class BboxAssignSample(nn.Cell): num_pos = self.cast(self.logicalnot(valid_pos_index), self.ms_type) num_pos = self.sum_inds(num_pos, -1) unvalid_pos_index = self.less(self.range_pos_size, num_pos) - valid_neg_index = self.logicaland( - self.cast(self.concat(( - self.cast(self.check_neg_mask, ms.int32), - self.cast(unvalid_pos_index, ms.int32) - )), ms.bool_), - self.cast(valid_neg_index, ms.bool_) - ) + valid_neg_index = self.logicaland(self.concat((self.check_neg_mask, unvalid_pos_index)), valid_neg_index) pos_bboxes_ = self.gatherND(bboxes, pos_index) pos_gt_bboxes_ = self.gatherND(gt_bboxes_i, pos_assigned_gt_index) diff --git a/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample_stage2.py b/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample_stage2.py index 7602adcc5..5cbc16ee5 100644 --- a/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample_stage2.py +++ b/official/cv/FasterRCNN/src/FasterRcnn/bbox_assign_sample_stage2.py @@ -114,7 +114,7 @@ class BboxAssignSampleForRcnn(nn.Cell): gt_bboxes_i, self.check_gt_one) bboxes = self.select(self.cast(self.tile(self.reshape(self.cast(valid_mask, ms.int32), \ (self.num_bboxes, 1)), (1, 4)), ms.bool_), \ - self.cast(bboxes, ms.float16), self.check_anchor_two) + bboxes, self.check_anchor_two) overlaps = self.iou(bboxes, gt_bboxes_i) @@ -171,13 +171,7 @@ class BboxAssignSampleForRcnn(nn.Cell): neg_index, valid_neg_index = self.random_choice_with_mask_neg(self.equal(assigned_gt_inds5, 0)) unvalid_pos_index = self.less(self.range_pos_size, num_pos) - valid_neg_index = self.logicaland( - self.cast(self.concat(( - self.cast(self.check_neg_mask, ms.int32), - self.cast(unvalid_pos_index, ms.int32) - )), ms.bool_), - self.cast(valid_neg_index, ms.bool_) - ) + valid_neg_index = self.logicaland(self.concat((self.check_neg_mask, unvalid_pos_index)), valid_neg_index) neg_index = self.reshape(neg_index, self.reshape_shape_neg) valid_neg_index = self.cast(valid_neg_index, ms.int32) diff --git a/official/cv/FasterRCNN/src/FasterRcnn/proposal_generator.py b/official/cv/FasterRCNN/src/FasterRcnn/proposal_generator.py index 5317ca51c..16e2b4265 100644 --- a/official/cv/FasterRCNN/src/FasterRcnn/proposal_generator.py +++ b/official/cv/FasterRCNN/src/FasterRcnn/proposal_generator.py @@ -183,21 +183,13 @@ class Proposal(nn.Cell): mlvl_proposals = mlvl_proposals + (proposals,) mlvl_mask = mlvl_mask + (mask_valid,) - proposals = self.concat_axis0( - tuple(self.cast(proposal, ms.int64) for proposal in mlvl_proposals) - ) - masks = self.concat_axis0( - tuple(self.cast(mask, ms.int64) for mask in mlvl_mask) - ) + proposals = self.concat_axis0(mlvl_proposals) + masks = self.concat_axis0(mlvl_mask) _, _, _, _, scores = self.split(proposals) scores = self.squeeze(scores) topk_mask = self.cast(self.topK_mask, self.ms_type) - scores_using = self.cast(self.select( - self.cast(masks, ms.bool_), - self.cast(scores, ms.bool_), - self.cast(topk_mask, ms.bool_) - ), ms.int32) + scores_using = self.select(masks, scores, topk_mask) _, topk_inds = self.topKv2(scores_using, self.max_num) 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 2ad832bbd..38ca57bb2 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 68590ec9d..6a98de5d2 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): -- Gitee From 917cb1c10259713650cb509ee00b5c3df4482e97 Mon Sep 17 00:00:00 2001 From: huangxinjing Date: Sat, 13 Jan 2024 18:10:19 +0800 Subject: [PATCH 3/7] fix_pangu_overflow_check --- .../Pangu_alpha/src/pangu_alpha_wrapcell.py | 70 +++---------------- 1 file changed, 11 insertions(+), 59 deletions(-) diff --git a/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py b/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py index 72cdec353..8e53a619c 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_overflow(cond) if not overflow: if self.enable_offload: self.optimizer(grads, clip_value) -- Gitee From aaac369ef660e3d29994cd3ce24694964b0c9837 Mon Sep 17 00:00:00 2001 From: lichen Date: Tue, 23 Jan 2024 16:45:16 +0800 Subject: [PATCH 4/7] r2.2 --- official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py b/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py index 8e53a619c..a34098bd9 100644 --- a/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py +++ b/official/nlp/Pangu_alpha/src/pangu_alpha_wrapcell.py @@ -224,7 +224,7 @@ class PanguAlphaTrainPipelineWithLossScaleCell(TrainOneStepWithLossScaleCell): grads) cond = self.get_overflow_status(self.status, grads) cond = F.depend(cond, grads) - overflow = self.process_overflow(cond) + overflow = self.process_loss_scale(cond) if not overflow: if self.enable_offload: self.optimizer(grads, clip_value) -- Gitee From 75e7b1f51b40b6b86c5a7dfd4746ca379a87721b Mon Sep 17 00:00:00 2001 From: PingqiLi Date: Mon, 29 Jan 2024 17:59:45 +0800 Subject: [PATCH 5/7] modify the learning rate of the example in SSD training scripts --- official/cv/SSD/scripts/run_distribute_train.sh | 2 +- official/cv/SSD/scripts/run_distribute_train_gpu.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/official/cv/SSD/scripts/run_distribute_train.sh b/official/cv/SSD/scripts/run_distribute_train.sh index 893f4a76d..37fe46d37 100644 --- a/official/cv/SSD/scripts/run_distribute_train.sh +++ b/official/cv/SSD/scripts/run_distribute_train.sh @@ -17,7 +17,7 @@ echo "==============================================================================================================" echo "Please run the script as: " echo "bash run_distribute_train.sh DEVICE_NUM EPOCH_SIZE LR DATASET RANK_TABLE_FILE CONFIG_PATH PRE_TRAINED PRE_TRAINED_EPOCH_SIZE" -echo "for example: bash run_distribute_train.sh 8 500 0.2 coco /data/hccl.json /config_path /opt/ssd-300.ckpt(optional) 200(optional)" +echo "for example: bash run_distribute_train.sh 8 500 0.05 coco /data/hccl.json /config_path /opt/ssd-300.ckpt(optional) 200(optional)" echo "It is better to use absolute path." echo "=================================================================================================================" diff --git a/official/cv/SSD/scripts/run_distribute_train_gpu.sh b/official/cv/SSD/scripts/run_distribute_train_gpu.sh index 0778ad70f..0ff4b1818 100644 --- a/official/cv/SSD/scripts/run_distribute_train_gpu.sh +++ b/official/cv/SSD/scripts/run_distribute_train_gpu.sh @@ -17,7 +17,7 @@ echo "==============================================================================================================" echo "Please run the script as: " echo "bash run_distribute_train_gpu.sh DEVICE_NUM EPOCH_SIZE LR DATASET CONFIG_PATH PRE_TRAINED PRE_TRAINED_EPOCH_SIZE" -echo "for example: bash run_distribute_train_gpu.sh 8 500 0.2 coco /config_path /opt/ssd-300.ckpt(optional) 200(optional)" +echo "for example: bash run_distribute_train_gpu.sh 8 500 0.05 coco /config_path /opt/ssd-300.ckpt(optional) 200(optional)" echo "It is better to use absolute path." echo "=================================================================================================================" -- Gitee From 7eb4f356d02d83006083ae9c1f4d95f4b6a76bef Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 4 Feb 2024 10:40:17 +0800 Subject: [PATCH 6/7] fix MELGAN readme file name --- official/audio/MELGAN/README.md | 2 +- official/audio/MELGAN/README_CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/official/audio/MELGAN/README.md b/official/audio/MELGAN/README.md index 448fa1bb5..4185a79a5 100644 --- a/official/audio/MELGAN/README.md +++ b/official/audio/MELGAN/README.md @@ -46,7 +46,7 @@ Dataset used: [LJ Speech]() - Dataset size:2.6GB,13,100 short audio clips of a single speaker reading passages from 7 non-fiction books. - Data format:Each audio file is a single-channel 16-bit PCM WAV with a sample rate of 22050 Hz - - The audio data needs to be processed to a mel-spectrum, and you can refer to the script in [mel-spectrogram data creation](https://github.com/seungwonpark/melgan/blob/master/preprocess.py). Non CUDA environment needs to delete `. cuda()` in `utils/stfy.py`. To save data in the `npy` format, `preprocess.py` also needs to be modified. As follows: + - The audio data needs to be processed to a mel-spectrum, and you can refer to the script in [mel-spectrogram data creation](https://github.com/seungwonpark/melgan/blob/master/preprocess.py). Non CUDA environment needs to delete `. cuda()` in `utils/stft.py`. To save data in the `npy` format, `preprocess.py` also needs to be modified. As follows: ``` # 37 - 38 lines diff --git a/official/audio/MELGAN/README_CN.md b/official/audio/MELGAN/README_CN.md index 03a3b7bad..5d80d749a 100644 --- a/official/audio/MELGAN/README_CN.md +++ b/official/audio/MELGAN/README_CN.md @@ -46,7 +46,7 @@ MelGAN模型是非自回归全卷积模型。它的参数比同类模型少得 - Dataset size:2.6GB,包含13,100条只有一个说话人的短语音。语音的内容来自7本纪实书籍。 - 数据格式:每条语音文件都是单声道、16-bit以及采样率为22050。 - - 语音需要被处理为Mel谱, 可以参考脚本[Mel谱处理脚本](https://github.com/seungwonpark/melgan/blob/master/preprocess.py)。非CUDA环境需删除`utils/stfy.py`中的`.cuda()`,因为要保存`npy`格式的数据,所以`preproccess.py`也需要修改以下,参考代码如下: + - 语音需要被处理为Mel谱, 可以参考脚本[Mel谱处理脚本](https://github.com/seungwonpark/melgan/blob/master/preprocess.py)。非CUDA环境需删除`utils/stft.py`中的`.cuda()`,因为要保存`npy`格式的数据,所以`preproccess.py`也需要修改以下,参考代码如下: ``` # 37 - 38 行 -- Gitee From 4e630949ec9fde2eac61ff275ab325e0352fd22a Mon Sep 17 00:00:00 2001 From: tomzwang11 Date: Wed, 22 May 2024 16:59:29 +0800 Subject: [PATCH 7/7] add default ref mode --- official/cv/ResNet/src/callback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/official/cv/ResNet/src/callback.py b/official/cv/ResNet/src/callback.py index c34fa6fd5..249bf1aea 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() -- Gitee