def eval_lenet(): context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) network = LeNet5(config.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") # repeat_size = config.epoch_size net_opt = nn.Momentum(network.trainable_params(), config.lr, config.momentum) model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) print("============== Starting Testing ==============") param_dict = load_checkpoint(ckpt_path) load_param_into_net(network, param_dict) ds_eval = create_dataset(os.path.join(config.data_path, "test"), config.batch_size, 1) if ds_eval.get_dataset_size() == 0: raise ValueError("Please check dataset size > 0 and batch_size <= dataset size") acc = model.eval(ds_eval) print("============== {} ==============".format(acc))
def train_lenet(): context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) ds_train = create_dataset(os.path.join(config.data_path, "train"), config.batch_size) if ds_train.get_dataset_size() == 0: raise ValueError( "Please check dataset size > 0 and batch_size <= dataset size") network = LeNet5(config.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") net_opt = nn.Momentum(network.trainable_params(), config.lr, config.momentum) time_cb = TimeMonitor(data_size=ds_train.get_dataset_size()) config_ck = CheckpointConfig( save_checkpoint_steps=config.save_checkpoint_steps, keep_checkpoint_max=config.keep_checkpoint_max) ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", directory=config.checkpoint_path, config=config_ck) if config.device_target != "Ascend": model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) else: model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}, amp_level="O2") print("============== Starting Training ==============") model.train(config.epoch_size, ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()])
parser.add_argument('--ckpt_path', type=str, default="", help='if mode is test, must provide\ path where the trained ckpt file') parser.add_argument('--dataset_sink_mode', type=bool, default=False, help='dataset_sink_mode is False or True') args = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) network = LeNet5(cfg.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean") repeat_size = cfg.epoch_size net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) config_ck = CheckpointConfig( save_checkpoint_steps=cfg.save_checkpoint_steps, keep_checkpoint_max=cfg.keep_checkpoint_max) ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck) model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) print("============== Starting Testing ==============") param_dict = load_checkpoint(args.ckpt_path) load_param_into_net(network, param_dict) ds_eval = create_dataset(os.path.join(args.data_path, "test"),
""" import mindspore.nn as nn from mindspore import context, Model from mindspore.train.callback import LossMonitor from mindspore.nn.metrics import Accuracy from src.lenet import LeNet5 from src.datasets import create_dataset if __name__ == "__main__": context.set_context(mode=context.GRAPH_MODE, device_target="GPU") ds_train = create_dataset("./datasets/MNIST_Data/train", 32) ds_eval = create_dataset("./datasets/MNIST_Data/test", 32) # Initialize network network = LeNet5(10) # Define Loss and Optimizer net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9) # amp_leval=O2 in GPU, amp_leval=O3 in Ascend, O0 is without mixed precision model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}, amp_level="O2") # Run training model.train(epoch=1, callbacks=[LossMonitor()], train_dataset=ds_train) # Run training acc = model.eval(ds_eval, dataset_sink_mode=False) print("====Accuracy====:", acc)
def lenet(*args, **kwargs): return LeNet5(*args, **kwargs)
""" Copyright (R) @huawei.com, all rights reserved -*- coding:utf-8 -*- CREATED: 2021-01-20 20:12:13 MODIFIED: 2021-01-29 14:04:45 """ from mindspore.train.serialization import load_checkpoint, save_checkpoint, export from src.lenet import LeNet5 import numpy as np from mindspore import Tensor network = LeNet5() load_checkpoint("./checkpoint_lenet-1_1875.ckpt", network) input_data = np.random.uniform(0.0, 1.0, size = [1, 1, 32, 32]).astype(np.float32) export(network, Tensor(input_data), file_name = './mnist', file_format = 'AIR')
from mindspore import Tensor, context, load_checkpoint, load_param_into_net, export from src.lenet import LeNet5 if os.path.exists(config.data_path_local): ckpt_file = config.ckpt_path_local else: ckpt_file = os.path.join(config.data_path, 'checkpoint_lenet-10_1875.ckpt') context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) if config.device_target == "Ascend": context.set_context(device_id=get_device_id()) if __name__ == "__main__": # define fusion network network = LeNet5(config.num_classes) # load network checkpoint param_dict = load_checkpoint(ckpt_file) load_param_into_net(network, param_dict) # export network inputs = Tensor( np.ones( [config.batch_size, 1, config.image_height, config.image_width]), mindspore.float32) export(network, inputs, file_name=config.file_name, file_format=config.file_format)