return cifar_ds if __name__ == '__main__': args_opt = parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target) # download cifar10 dataset if not args_opt.dataset_path: args_opt.dataset_path = download_dataset('cifar10') # build the network if args_opt.do_eval and args_opt.load_pretrained == 'hub': from tinyms import hub net = hub.load(args_opt.hub_uid, class_num=args_opt.num_classes) else: net = vgg16(class_num=args_opt.num_classes) net.update_parameters_name(prefix='huawei') model = Model(net) # define the loss function net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") # define the optimizer net_opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9) model.compile(loss_fn=net_loss, optimizer=net_opt, metrics={"Accuracy": Accuracy()}) epoch_size = args_opt.epoch_size batch_size = args_opt.batch_size
return mnist_ds if __name__ == "__main__": args_opt = parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target) # download mnist dataset if not args_opt.dataset_path: args_opt.dataset_path = download_dataset('mnist') # build the network if args_opt.do_eval and args_opt.load_pretrained == 'hub': from tinyms import hub net = hub.load(args_opt.hub_uid) else: net = lenet5() model = Model(net) # define the loss function net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') # define the optimizer net_opt = Momentum(net.trainable_params(), 0.01, 0.9) model.compile(net_loss, net_opt, metrics={"Accuracy": Accuracy()}) epoch_size = args_opt.epoch_size batch_size = args_opt.batch_size mnist_path = args_opt.dataset_path dataset_sink_mode = not args_opt.device_target == "CPU" if args_opt.do_eval: # as for evaluation, users could use model.eval
model.train(epoch_size, ds_train, callbacks=[ ckpoint_cb, LossMonitor(), TimeMonitor(data_size=dataset_size) ], dataset_sink_mode=dataset_sink_mode) else: # as for evaluation, users could use model.eval ds_eval = create_dataset(voc_path, batch_size=1, is_training=False) total = ds_eval.get_dataset_size() # define the infer wrapper if args_opt.load_pretrained == 'hub': from tinyms import hub eval_net = hub.load(args_opt.hub_uid, class_num=args_opt.num_classes, is_training=False) else: eval_net = ssd300_mobilenetv2(class_num=args_opt.num_classes, is_training=False) model = Model(eval_net) if args_opt.load_pretrained == 'local': if args_opt.checkpoint_path: model.load_checkpoint(args_opt.checkpoint_path) # perform the model predict operation print("\n========================================\n") print("total images num: ", total) print("Processing, please wait a moment...") start = time.time() pred_data = [] id_iter = 0
cifar10_path = args_opt.dataset_path # set runtime environment context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target) dataset_sink_mode = not args_opt.device_target == "CPU" # create cifar10 dataset for training ds_train = create_dataset(cifar10_path, batch_size=batch_size) step_size = ds_train.get_dataset_size() # build the network if args_opt.do_eval and args_opt.load_pretrained == 'hub': from tinyms import hub net = hub.load(args_opt.hub_uid, class_num=args_opt.num_classes, is_training=not args_opt.do_eval) else: net = mobilenetv2(class_num=args_opt.num_classes, is_training=not args_opt.do_eval) model = Model(net) # define the loss function loss = CrossEntropyWithLabelSmooth(smooth_factor=args_opt.label_smooth, num_classes=args_opt.num_classes) # get learning rate lr_max = 0.001 lr_init_scale = 0.01 lr_end_scale = 0.01 lr = mobilenetv2_lr(global_step=0, lr_init=lr_max * lr_init_scale, lr_end=lr_max * lr_end_scale,