infer.py 3.0 KB
Newer Older
D
dengkaipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import division
from __future__ import print_function

import os
import argparse
import numpy as np

L
LielinJiang 已提交
22 23
from paddle.incubate.hapi.model import Input, set_device
from paddle.incubate.hapi.vision.transforms import Compose
D
dengkaipeng 已提交
24 25

from check import check_gpu, check_version
D
dengkaipeng 已提交
26
from modeling import tsm_resnet50
D
dengkaipeng 已提交
27 28
from kinetics_dataset import KineticsDataset
from transforms import *
D
dengkaipeng 已提交
29
from utils import print_arguments
D
dengkaipeng 已提交
30 31 32 33 34 35 36 37 38

import logging
logger = logging.getLogger(__name__)


def main():
    device = set_device(FLAGS.device)
    fluid.enable_dygraph(device) if FLAGS.dynamic else None

L
LielinJiang 已提交
39
    transform = Compose([GroupScale(), GroupCenterCrop(), NormalizeImage()])
D
dengkaipeng 已提交
40
    dataset = KineticsDataset(
L
LielinJiang 已提交
41 42 43 44
        pickle_file=FLAGS.infer_file,
        label_list=FLAGS.label_list,
        mode='test',
        transform=transform)
D
dengkaipeng 已提交
45 46
    labels = dataset.label_list

L
LielinJiang 已提交
47 48
    model = tsm_resnet50(
        num_classes=len(labels), pretrained=FLAGS.weights is None)
D
dengkaipeng 已提交
49 50 51 52 53 54 55 56 57

    inputs = [Input([None, 8, 3, 224, 224], 'float32', name='image')]

    model.prepare(inputs=inputs, device=FLAGS.device)

    if FLAGS.weights is not None:
        model.load(FLAGS.weights, reset_optimizer=True)

    imgs, label = dataset[0]
D
dengkaipeng 已提交
58
    pred = model.test_batch([imgs[np.newaxis, :]])
D
dengkaipeng 已提交
59 60 61 62 63 64 65 66
    pred = labels[np.argmax(pred)]
    logger.info("Sample {} predict label: {}, ground truth label: {}" \
                .format(FLAGS.infer_file, pred, labels[int(label)]))


if __name__ == '__main__':
    parser = argparse.ArgumentParser("CNN training on TSM")
    parser.add_argument(
L
LielinJiang 已提交
67 68 69
        "--data",
        type=str,
        default='dataset/kinetics',
D
dengkaipeng 已提交
70 71
        help="path to dataset root directory")
    parser.add_argument(
L
LielinJiang 已提交
72
        "--device", type=str, default='gpu', help="device to use, gpu or cpu")
D
dengkaipeng 已提交
73
    parser.add_argument(
L
LielinJiang 已提交
74
        "-d", "--dynamic", action='store_true', help="enable dygraph mode")
D
dengkaipeng 已提交
75
    parser.add_argument(
L
LielinJiang 已提交
76 77 78
        "--label_list",
        type=str,
        default=None,
D
dengkaipeng 已提交
79 80
        help="path to category index label list file")
    parser.add_argument(
L
LielinJiang 已提交
81 82 83
        "--infer_file",
        type=str,
        default=None,
D
dengkaipeng 已提交
84 85 86 87 88 89 90 91
        help="path to pickle file for inference")
    parser.add_argument(
        "-w",
        "--weights",
        default=None,
        type=str,
        help="weights path for evaluation")
    FLAGS = parser.parse_args()
D
dengkaipeng 已提交
92
    print_arguments(FLAGS)
D
dengkaipeng 已提交
93 94 95 96

    check_gpu(str.lower(FLAGS.device) == 'gpu')
    check_version()
    main()