Exemplo n.º 1
0
def eval_alexnet():
    print("============== Starting Testing ==============")

    device_num = get_device_num()
    if device_num > 1:
        # context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
        context.set_context(mode=context.GRAPH_MODE,
                            device_target='Davinci',
                            save_graphs=False)
        if config.device_target == "Ascend":
            context.set_context(device_id=get_device_id())
            init()
        elif config.device_target == "GPU":
            init()

    if config.dataset_name == 'cifar10':
        network = AlexNet(config.num_classes, phase='test')
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
        opt = nn.Momentum(network.trainable_params(), config.learning_rate,
                          config.momentum)
        ds_eval = create_dataset_cifar10(config.data_path, config.batch_size, status="test", \
            target=config.device_target)
        param_dict = load_checkpoint(load_path)
        print("load checkpoint from [{}].".format(load_path))
        load_param_into_net(network, param_dict)
        network.set_train(False)
        model = Model(network, loss, opt, metrics={"Accuracy": Accuracy()})

    elif config.dataset_name == 'imagenet':
        network = AlexNet(config.num_classes, phase='test')
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
        ds_eval = create_dataset_imagenet(config.data_path,
                                          config.batch_size,
                                          training=False)
        param_dict = load_checkpoint(load_path)
        print("load checkpoint from [{}].".format(load_path))
        load_param_into_net(network, param_dict)
        network.set_train(False)
        model = Model(network,
                      loss_fn=loss,
                      metrics={'top_1_accuracy', 'top_5_accuracy'})

    else:
        raise ValueError("Unsupported dataset.")

    if ds_eval.get_dataset_size() == 0:
        raise ValueError(
            "Please check dataset size > 0 and batch_size <= dataset size")

    result = model.eval(ds_eval, dataset_sink_mode=config.dataset_sink_mode)
    print("result : {}".format(result))
Exemplo n.º 2
0
    context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)

    print("============== Starting Testing ==============")

    if args.dataset_name == 'cifar10':
        cfg = alexnet_cifar10_cfg
        network = AlexNet(cfg.num_classes)
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
        opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum)
        ds_eval = create_dataset_cifar10(args.data_path, cfg.batch_size, status="test", target=args.device_target)

        param_dict = load_checkpoint(args.ckpt_path)
        print("load checkpoint from [{}].".format(args.ckpt_path))
        load_param_into_net(network, param_dict)
        network.set_train(False)

        model = Model(network, loss, opt, metrics={"Accuracy": Accuracy()})

    elif args.dataset_name == 'imagenet':
        cfg = alexnet_imagenet_cfg
        network = AlexNet(cfg.num_classes)
        loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
        ds_eval = create_dataset_imagenet(args.data_path, cfg.batch_size, training=False)

        param_dict = load_checkpoint(args.ckpt_path)
        print("load checkpoint from [{}].".format(args.ckpt_path))
        load_param_into_net(network, param_dict)
        network.set_train(False)

        model = Model(network, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})