def main(): args = parse_args() _, log_file_exist = initialize_logging( logging_dir_path=args.save_dir, logging_file_name=args.logging_file_name, script_args=args, log_packages=args.log_packages, log_pip_packages=args.log_pip_packages) ctx, batch_size = prepare_mx_context(num_gpus=args.num_gpus, batch_size=args.batch_size) net = prepare_model(model_name=args.model, use_pretrained=args.use_pretrained, pretrained_model_file_path=args.resume.strip(), dtype=args.dtype, classes=args.num_classes, in_channels=args.in_channels, do_hybridize=(not args.calc_flops), ctx=ctx) assert (hasattr(net, "in_size")) input_image_size = net.in_size ds_metainfo = get_dataset_metainfo(dataset_name=args.dataset) val_data = get_val_data_source(dataset_metainfo=ds_metainfo, dataset_dir=args.data_dir, batch_size=batch_size, num_workers=args.num_workers, input_image_size=input_image_size, resize_inv_factor=args.resize_inv_factor) batch_fn = get_batch_fn(use_imgrec=ds_metainfo.use_imgrec) assert (args.use_pretrained or args.resume.strip() or args.calc_flops_only) test( net=net, val_data=val_data, batch_fn=batch_fn, data_source_needs_reset=ds_metainfo.use_imgrec, val_metric=get_composite_metric(ds_metainfo.val_metric_names), dtype=args.dtype, ctx=ctx, input_image_size=input_image_size, in_channels=args.in_channels, # calc_weight_count=(not log_file_exist), calc_weight_count=True, calc_flops=args.calc_flops, calc_flops_only=args.calc_flops_only, extended_log=True)
def parse_args(): parser = argparse.ArgumentParser( description="Evaluate a model for image classification (Gluon/CIFAR)", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( "--dataset", type=str, default="CIFAR10", help="dataset name. options are CIFAR10, CIFAR100, and SVHN") args, _ = parser.parse_known_args() dataset_metainfo = get_dataset_metainfo(dataset_name=args.dataset) work_dir_path = os.path.join("..", "imgclsmob_data") dataset_metainfo.add_dataset_parser_arguments(parser, work_dir_path) add_eval_cls_parser_arguments(parser) args = parser.parse_args() return args
def main(): args = parse_args() args.seed = init_rand(seed=args.seed) _, log_file_exist = initialize_logging( logging_dir_path=args.save_dir, logging_file_name=args.logging_file_name, script_args=args, log_packages=args.log_packages, log_pip_packages=args.log_pip_packages) ctx, batch_size = prepare_mx_context(num_gpus=args.num_gpus, batch_size=args.batch_size) net = prepare_model(model_name=args.model, use_pretrained=args.use_pretrained, pretrained_model_file_path=args.resume.strip(), dtype=args.dtype, tune_layers=args.tune_layers, classes=args.num_classes, in_channels=args.in_channels, do_hybridize=(not args.not_hybridize), ctx=ctx) assert (hasattr(net, "classes")) num_classes = net.classes if hasattr(net, "classes") else 10 ds_metainfo = get_dataset_metainfo(dataset_name=args.dataset) train_data = get_train_data_source(dataset_metainfo=ds_metainfo, dataset_dir=args.data_dir, batch_size=batch_size, num_workers=args.num_workers) val_data = get_val_data_source(dataset_metainfo=ds_metainfo, dataset_dir=args.data_dir, batch_size=batch_size, num_workers=args.num_workers) batch_fn = get_batch_fn(use_imgrec=ds_metainfo.use_imgrec) num_training_samples = len( train_data._dataset ) if not ds_metainfo.use_imgrec else ds_metainfo.num_training_samples trainer, lr_scheduler = prepare_trainer( net=net, optimizer_name=args.optimizer_name, wd=args.wd, momentum=args.momentum, lr_mode=args.lr_mode, lr=args.lr, lr_decay_period=args.lr_decay_period, lr_decay_epoch=args.lr_decay_epoch, lr_decay=args.lr_decay, target_lr=args.target_lr, poly_power=args.poly_power, warmup_epochs=args.warmup_epochs, warmup_lr=args.warmup_lr, warmup_mode=args.warmup_mode, batch_size=batch_size, num_epochs=args.num_epochs, num_training_samples=num_training_samples, dtype=args.dtype, gamma_wd_mult=args.gamma_wd_mult, beta_wd_mult=args.beta_wd_mult, bias_wd_mult=args.bias_wd_mult, state_file_path=args.resume_state) if args.save_dir and args.save_interval: param_names = ds_metainfo.val_metric_capts + ds_metainfo.train_metric_capts + [ "Train.Loss", "LR" ] lp_saver = TrainLogParamSaver( checkpoint_file_name_prefix="{}_{}".format(args.dataset.lower(), args.model), last_checkpoint_file_name_suffix="last", best_checkpoint_file_name_suffix=None, last_checkpoint_dir_path=args.save_dir, best_checkpoint_dir_path=None, last_checkpoint_file_count=2, best_checkpoint_file_count=2, checkpoint_file_save_callback=save_params, checkpoint_file_exts=(".params", ".states"), save_interval=args.save_interval, num_epochs=args.num_epochs, param_names=param_names, acc_ind=ds_metainfo.saver_acc_ind, # bigger=[True], # mask=None, score_log_file_path=os.path.join(args.save_dir, "score.log"), score_log_attempt_value=args.attempt, best_map_log_file_path=os.path.join(args.save_dir, "best_map.log")) else: lp_saver = None train_net(batch_size=batch_size, num_epochs=args.num_epochs, start_epoch1=args.start_epoch, train_data=train_data, val_data=val_data, batch_fn=batch_fn, data_source_needs_reset=ds_metainfo.use_imgrec, dtype=args.dtype, net=net, trainer=trainer, lr_scheduler=lr_scheduler, lp_saver=lp_saver, log_interval=args.log_interval, mixup=args.mixup, mixup_epoch_tail=args.mixup_epoch_tail, label_smoothing=args.label_smoothing, num_classes=num_classes, grad_clip_value=args.grad_clip, batch_size_scale=args.batch_size_scale, val_metric=get_composite_metric(ds_metainfo.val_metric_names), train_metric=get_composite_metric( ds_metainfo.train_metric_names), opt_metric_name=ds_metainfo.val_metric_names[ ds_metainfo.saver_acc_ind], ctx=ctx)
def parse_args(): parser = argparse.ArgumentParser( description='Train a model for image classification (Gluon/CIFAR)', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--dataset', type=str, default="CIFAR10", help='dataset name. options are CIFAR10 and CIFAR100') args, _ = parser.parse_known_args() dataset_metainfo = get_dataset_metainfo(dataset_name=args.dataset) work_dir_path = os.path.join("..", "imgclsmob_data") dataset_metainfo.add_dataset_parser_arguments(parser, work_dir_path) parser.add_argument( '--model', type=str, required=True, help='type of model to use. see model_provider for options.') parser.add_argument('--use-pretrained', action='store_true', help='enable using pretrained model from gluon.') parser.add_argument('--dtype', type=str, default='float32', help='data type for training') parser.add_argument('--not-hybridize', action='store_true', help='do not hybridize model') parser.add_argument( '--resume', type=str, default='', help='resume from previously saved parameters if not None') parser.add_argument( '--resume-state', type=str, default='', help='resume from previously saved optimizer state if not None') parser.add_argument('--num-gpus', type=int, default=0, help='number of gpus to use.') parser.add_argument('-j', '--num-data-workers', dest='num_workers', default=4, type=int, help='number of preprocessing workers') parser.add_argument('--batch-size', type=int, default=128, help='training batch size per device (CPU/GPU).') parser.add_argument('--batch-size-scale', type=int, default=1, help='manual batch-size increasing factor.') parser.add_argument('--num-epochs', type=int, default=200, help='number of training epochs.') parser.add_argument( '--start-epoch', type=int, default=1, help='starting epoch for resuming, default is 1 for new training') parser.add_argument('--attempt', type=int, default=1, help='current number of training') parser.add_argument('--optimizer-name', type=str, default='nag', help='optimizer name') parser.add_argument('--lr', type=float, default=0.1, help='learning rate') parser.add_argument( '--lr-mode', type=str, default='cosine', help='learning rate scheduler mode. options are step, poly and cosine') parser.add_argument('--lr-decay', type=float, default=0.1, help='decay rate of learning rate') parser.add_argument( '--lr-decay-period', type=int, default=0, help= 'interval for periodic learning rate decays. default is 0 to disable.') parser.add_argument('--lr-decay-epoch', type=str, default='40,60', help='epoches at which learning rate decays') parser.add_argument('--target-lr', type=float, default=1e-8, help='ending learning rate') parser.add_argument('--poly-power', type=float, default=2, help='power value for poly LR scheduler') parser.add_argument('--warmup-epochs', type=int, default=0, help='number of warmup epochs.') parser.add_argument('--warmup-lr', type=float, default=1e-8, help='starting warmup learning rate') parser.add_argument( '--warmup-mode', type=str, default='linear', help= 'learning rate scheduler warmup mode. options are linear, poly and constant' ) parser.add_argument('--momentum', type=float, default=0.9, help='momentum value for optimizer') parser.add_argument('--wd', type=float, default=0.0001, help='weight decay rate') parser.add_argument('--gamma-wd-mult', type=float, default=1.0, help='weight decay multiplier for batchnorm gamma') parser.add_argument('--beta-wd-mult', type=float, default=1.0, help='weight decay multiplier for batchnorm beta') parser.add_argument('--bias-wd-mult', type=float, default=1.0, help='weight decay multiplier for bias') parser.add_argument('--grad-clip', type=float, default=None, help='max_norm for gradient clipping') parser.add_argument('--label-smoothing', action='store_true', help='use label smoothing') parser.add_argument('--mixup', action='store_true', help='use mixup strategy') parser.add_argument( '--mixup-epoch-tail', type=int, default=20, help='number of epochs without mixup at the end of training') parser.add_argument('--log-interval', type=int, default=200, help='number of batches to wait before logging.') parser.add_argument( '--save-interval', type=int, default=4, help='saving parameters epoch interval, best model will always be saved' ) parser.add_argument('--save-dir', type=str, default='', help='directory of saved models and log-files') parser.add_argument('--logging-file-name', type=str, default='train.log', help='filename of training log') parser.add_argument('--seed', type=int, default=-1, help='Random seed to be fixed') parser.add_argument('--log-packages', type=str, default='mxnet', help='list of python packages for logging') parser.add_argument('--log-pip-packages', type=str, default='mxnet-cu100', help='list of pip packages for logging') parser.add_argument('--tune-layers', type=str, default='', help='Regexp for selecting layers for fine tuning') args = parser.parse_args() return args
def parse_args(): parser = argparse.ArgumentParser( description="Train a model for image classification (Gluon/ImageNet1K)", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( "--dataset", type=str, default="ImageNet1K_rec", help="dataset name. options are ImageNet1K and ImageNet1K_rec") args, _ = parser.parse_known_args() dataset_metainfo = get_dataset_metainfo(dataset_name=args.dataset) work_dir_path = os.path.join("..", "imgclsmob_data") dataset_metainfo.add_dataset_parser_arguments(parser, work_dir_path) parser.add_argument( "--model", type=str, required=True, help="type of model to use. see model_provider for options") parser.add_argument( "--use-pretrained", action="store_true", help="enable using pretrained model from gluon") parser.add_argument( "--dtype", type=str, default="float32", help="data type for training") parser.add_argument( "--resume", type=str, default="", help="resume from previously saved parameters if not None") parser.add_argument( "--resume-state", type=str, default="", help="resume from previously saved optimizer state if not None") parser.add_argument( "--num-gpus", type=int, default=0, help="number of gpus to use") parser.add_argument( "-j", "--num-data-workers", dest="num_workers", default=4, type=int, help="number of preprocessing workers") parser.add_argument( "--batch-size", type=int, default=512, help="training batch size per device (CPU/GPU)") parser.add_argument( "--batch-size-scale", type=int, default=1, help="manual batch-size increasing factor") parser.add_argument( "--num-epochs", type=int, default=120, help="number of training epochs") parser.add_argument( "--start-epoch", type=int, default=1, help="starting epoch for resuming, default is 1 for new training") parser.add_argument( "--attempt", type=int, default=1, help="current attempt number for training") parser.add_argument( "--optimizer-name", type=str, default="nag", help="optimizer name") parser.add_argument( "--lr", type=float, default=0.1, help="learning rate") parser.add_argument( "--lr-mode", type=str, default="cosine", help="learning rate scheduler mode. options are step, poly and cosine") parser.add_argument( "--lr-decay", type=float, default=0.1, help="decay rate of learning rate") parser.add_argument( "--lr-decay-period", type=int, default=0, help="interval for periodic learning rate decays. default is 0 to disable") parser.add_argument( "--lr-decay-epoch", type=str, default="40,60", help="epoches at which learning rate decays") parser.add_argument( "--target-lr", type=float, default=1e-8, help="ending learning rate") parser.add_argument( "--poly-power", type=float, default=2, help="power value for poly LR scheduler") parser.add_argument( "--warmup-epochs", type=int, default=0, help="number of warmup epochs") parser.add_argument( "--warmup-lr", type=float, default=1e-8, help="starting warmup learning rate") parser.add_argument( "--warmup-mode", type=str, default="linear", help="learning rate scheduler warmup mode. options are linear, poly and constant") parser.add_argument( "--momentum", type=float, default=0.9, help="momentum value for optimizer") parser.add_argument( "--wd", type=float, default=0.0001, help="weight decay rate") parser.add_argument( "--gamma-wd-mult", type=float, default=1.0, help="weight decay multiplier for batchnorm gamma") parser.add_argument( "--beta-wd-mult", type=float, default=1.0, help="weight decay multiplier for batchnorm beta") parser.add_argument( "--bias-wd-mult", type=float, default=1.0, help="weight decay multiplier for bias") parser.add_argument( "--grad-clip", type=float, default=None, help="max_norm for gradient clipping") parser.add_argument( "--label-smoothing", action="store_true", help="use label smoothing") parser.add_argument( "--mixup", action="store_true", help="use mixup strategy") parser.add_argument( "--mixup-epoch-tail", type=int, default=20, help="number of epochs without mixup at the end of training") parser.add_argument( "--log-interval", type=int, default=50, help="number of batches to wait before logging") parser.add_argument( "--save-interval", type=int, default=4, help="saving parameters epoch interval, best model will always be saved") parser.add_argument( "--save-dir", type=str, default="", help="directory of saved models and log-files") parser.add_argument( "--logging-file-name", type=str, default="train.log", help="filename of training log") parser.add_argument( "--seed", type=int, default=-1, help="random seed to be fixed") parser.add_argument( "--log-packages", type=str, default="mxnet", help="list of python packages for logging") parser.add_argument( "--log-pip-packages", type=str, default="mxnet-cu100", help="list of pip packages for logging") parser.add_argument( "--tune-layers", type=str, default="", help="regexp for selecting layers for fine tuning") args = parser.parse_args() return args