eval.py 2.5 KB
Newer Older
W
wandongdong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""
eval.
"""
import os
import argparse
from mindspore import context
C
chenzomi 已提交
21
from mindspore import nn
W
wandongdong 已提交
22 23
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint, load_param_into_net
24
from src.mobilenetV2_quant import mobilenet_v2_quant
C
chenzomi 已提交
25
from src.dataset import create_dataset
26
from src.config import config_ascend
W
wandongdong 已提交
27 28 29 30

parser = argparse.ArgumentParser(description='Image classification')
parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
C
chenzomi 已提交
31
parser.add_argument('--platform', type=str, default=None, help='run platform')
W
wandongdong 已提交
32 33 34
args_opt = parser.parse_args()

if __name__ == '__main__':
C
chenzomi 已提交
35
    config_platform = None
C
chenzomi 已提交
36
    net = None
C
chenzomi 已提交
37 38 39 40 41
    if args_opt.platform == "Ascend":
        config_platform = config_ascend
        device_id = int(os.getenv('DEVICE_ID'))
        context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
                            device_id=device_id, save_graphs=False)
42
        net = mobilenet_v2_quant(num_classes=config_platform.num_classes)
C
chenzomi 已提交
43 44 45 46 47 48 49 50 51 52 53
    else:
        raise ValueError("Unsupport platform.")

    loss = nn.SoftmaxCrossEntropyWithLogits(
        is_grad=False, sparse=True, reduction='mean')

    dataset = create_dataset(dataset_path=args_opt.dataset_path,
                             do_train=False,
                             config=config_platform,
                             platform=args_opt.platform,
                             batch_size=config_platform.batch_size)
W
wandongdong 已提交
54 55 56 57 58 59 60 61 62 63
    step_size = dataset.get_dataset_size()

    if args_opt.checkpoint_path:
        param_dict = load_checkpoint(args_opt.checkpoint_path)
        load_param_into_net(net, param_dict)
    net.set_train(False)

    model = Model(net, loss_fn=loss, metrics={'acc'})
    res = model.eval(dataset)
    print("result:", res, "ckpt=", args_opt.checkpoint_path)