From 2d72ad22e4e54faa048dac0a9e770501e88b924e Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Sun, 13 Sep 2020 14:30:32 +0000 Subject: [PATCH] update TSM use new APIS. test=develop --- tsm/infer.py | 12 +++++------- tsm/main.py | 22 +++++++--------------- tsm/modeling.py | 14 ++++++++++---- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/tsm/infer.py b/tsm/infer.py index 2351eb2..3ae3bb4 100644 --- a/tsm/infer.py +++ b/tsm/infer.py @@ -19,8 +19,8 @@ import os import argparse import numpy as np -from paddle.incubate.hapi.model import Input, set_device -from paddle.incubate.hapi.vision.transforms import Compose +import paddle +from paddle.vision.transforms import Compose from check import check_gpu, check_version from modeling import tsm_resnet50 @@ -33,8 +33,8 @@ logger = logging.getLogger(__name__) def main(): - device = set_device(FLAGS.device) - fluid.enable_dygraph(device) if FLAGS.dynamic else None + device = paddle.set_device(FLAGS.device) + paddle.disable_static(device) if FLAGS.dynamic else None transform = Compose([GroupScale(), GroupCenterCrop(), NormalizeImage()]) dataset = KineticsDataset( @@ -47,9 +47,7 @@ def main(): model = tsm_resnet50( num_classes=len(labels), pretrained=FLAGS.weights is None) - inputs = [Input([None, 8, 3, 224, 224], 'float32', name='image')] - - model.prepare(inputs=inputs, device=FLAGS.device) + model.prepare() if FLAGS.weights is not None: model.load(FLAGS.weights, reset_optimizer=True) diff --git a/tsm/main.py b/tsm/main.py index d292d7d..74e3875 100644 --- a/tsm/main.py +++ b/tsm/main.py @@ -19,13 +19,11 @@ import os import argparse import numpy as np +import paddle from paddle import fluid from paddle.fluid.dygraph.parallel import ParallelEnv -from paddle.incubate.hapi.model import Model, Input, set_device -from paddle.incubate.hapi.loss import CrossEntropy -from paddle.incubate.hapi.metrics import Accuracy -from paddle.incubate.hapi.vision.transforms import Compose +from paddle.vision.transforms import Compose from modeling import tsm_resnet50 from check import check_gpu, check_version @@ -50,8 +48,8 @@ def make_optimizer(step_per_epoch, parameter_list=None): def main(): - device = set_device(FLAGS.device) - fluid.enable_dygraph(device) if FLAGS.dynamic else None + device = paddle.set_device(FLAGS.device) + paddle.disable_static(device) if FLAGS.dynamic else None train_transform = Compose([ GroupScale(), GroupMultiScaleCrop(), GroupRandomCrop(), @@ -79,16 +77,10 @@ def main(): / ParallelEnv().nranks) optim = make_optimizer(step_per_epoch, model.parameters()) - inputs = [Input([None, 8, 3, 224, 224], 'float32', name='image')] - labels = [Input([None, 1], 'int64', name='label')] - model.prepare( - optim, - CrossEntropy(), - metrics=Accuracy(topk=(1, 5)), - inputs=inputs, - labels=labels, - device=FLAGS.device) + optimizer=optim, + loss=paddle.nn.CrossEntropyLoss(), + metrics=paddle.metric.Accuracy(topk=(1, 5))) if FLAGS.eval_only: if FLAGS.weights is not None: diff --git a/tsm/modeling.py b/tsm/modeling.py index a42f426..0b39bfb 100644 --- a/tsm/modeling.py +++ b/tsm/modeling.py @@ -13,12 +13,13 @@ #limitations under the License. import math +import paddle import paddle.fluid as fluid from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear -from paddle.incubate.hapi.model import Model -from paddle.incubate.hapi.download import get_weights_path_from_url +from paddle.static import InputSpec +from paddle.utils.download import get_weights_path_from_url __all__ = ["TSM_ResNet", "tsm_resnet50"] @@ -112,7 +113,7 @@ class BottleneckBlock(fluid.dygraph.Layer): return y -class TSM_ResNet(Model): +class TSM_ResNet(fluid.dygraph.Layer): """ TSM network with ResNet as backbone @@ -193,7 +194,10 @@ class TSM_ResNet(Model): def _tsm_resnet(num_layers, seg_num=8, num_classes=400, pretrained=True): - model = TSM_ResNet(num_layers, seg_num, num_classes) + inputs = [InputSpec([None, 8, 3, 224, 224], 'float32', name='image')] + labels = [InputSpec([None, 1], 'int64', name='label')] + net = TSM_ResNet(num_layers, seg_num, num_classes) + model = paddle.Model(net, inputs, labels) if pretrained: assert num_layers in pretrain_infos.keys(), \ "TSM-ResNet{} do not have pretrained weights now, " \ @@ -201,6 +205,8 @@ def _tsm_resnet(num_layers, seg_num=8, num_classes=400, pretrained=True): weight_path = get_weights_path_from_url(*(pretrain_infos[num_layers])) assert weight_path.endswith('.pdparams'), \ "suffix of weight must be .pdparams" + # weight_dict, _ = fluid.load_dygraph(weight_path) + # model.set_dict(weight_dict) model.load(weight_path) return model -- GitLab