def _main(port): base_model = ShuffleNetV2(32) base_predictor = 'cortexA76cpu_tflite21' transf = [ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip() ] normalize = [ transforms.ToTensor(), transforms.Normalize([0.49139968, 0.48215827, 0.44653124], [0.24703233, 0.24348505, 0.26158768]) ] train_dataset = serialize(CIFAR10, 'data', train=True, download=True, transform=transforms.Compose(transf + normalize)) test_dataset = serialize(CIFAR10, 'data', train=False, transform=transforms.Compose(normalize)) trainer = pl.Classification(train_dataloader=pl.DataLoader(train_dataset, batch_size=64), val_dataloaders=pl.DataLoader(test_dataset, batch_size=64), max_epochs=2, gpus=1) simple_strategy = strategy.Random(model_filter=LatencyFilter(threshold=100, predictor=base_predictor)) exp = RetiariiExperiment(base_model, trainer, strategy=simple_strategy) exp_config = RetiariiExeConfig('local') exp_config.trial_concurrency = 2 exp_config.max_trial_number = 2 exp_config.trial_gpu_number = 1 exp_config.training_service.use_active_gpu = False exp_config.execution_engine = 'base' exp_config.dummy_input = [1, 3, 32, 32] exp.run(exp_config, port) print('Exported models:') for model in exp.export_top_models(formatter='dict'): print(model)
def _main(port): base_model = ShuffleNetV2OneShot(32) base_predictor = 'cortexA76cpu_tflite21' transf = [ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip() ] normalize = [ transforms.ToTensor(), transforms.Normalize([0.49139968, 0.48215827, 0.44653124], [0.24703233, 0.24348505, 0.26158768]) ] # FIXME # CIFAR10 is used here temporarily. # Actually we should load weight from supernet and evaluate on imagenet. train_dataset = serialize(CIFAR10, 'data', train=True, download=True, transform=transforms.Compose(transf + normalize)) test_dataset = serialize(CIFAR10, 'data', train=False, transform=transforms.Compose(normalize)) trainer = pl.Classification(train_dataloader=pl.DataLoader(train_dataset, batch_size=64), val_dataloaders=pl.DataLoader(test_dataset, batch_size=64), max_epochs=2, gpus=1) simple_strategy = strategy.RegularizedEvolution(model_filter=LatencyFilter( threshold=100, predictor=base_predictor), sample_size=1, population_size=2, cycles=2) exp = RetiariiExperiment(base_model, trainer, strategy=simple_strategy) exp_config = RetiariiExeConfig('local') exp_config.trial_concurrency = 2 # exp_config.max_trial_number = 2 exp_config.trial_gpu_number = 1 exp_config.training_service.use_active_gpu = False exp_config.execution_engine = 'base' exp_config.dummy_input = [1, 3, 32, 32] exp.run(exp_config, port) print('Exported models:') for i, model in enumerate(exp.export_top_models(formatter='dict')): print(model) with open(f'architecture_final_{i}.json', 'w') as f: json.dump(get_archchoice_by_model(model), f, indent=4)
def _main(): parser = argparse.ArgumentParser("SPOS Evolutional Search") parser.add_argument("--port", type=int, default=8084) parser.add_argument("--imagenet-dir", type=str, default="./data/imagenet") parser.add_argument("--checkpoint", type=str, default="./data/checkpoint-150000.pth.tar") parser.add_argument( "--spos-preprocessing", action="store_true", default=False, help="When true, image values will range from 0 to 255 and use BGR " "(as in original repo).") parser.add_argument("--seed", type=int, default=42) parser.add_argument("--workers", type=int, default=6) parser.add_argument("--train-batch-size", type=int, default=128) parser.add_argument("--train-iters", type=int, default=200) parser.add_argument("--test-batch-size", type=int, default=512) parser.add_argument("--log-frequency", type=int, default=10) parser.add_argument("--label-smoothing", type=float, default=0.1) parser.add_argument("--evolution-sample-size", type=int, default=10) parser.add_argument("--evolution-population-size", type=int, default=50) parser.add_argument("--evolution-cycles", type=int, default=10) parser.add_argument( "--latency-filter", type=str, default=None, help="Apply latency filter by calling the name of the applied hardware." ) parser.add_argument("--latency-threshold", type=float, default=100) args = parser.parse_args() # use a fixed set of image will improve the performance torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) random.seed(args.seed) torch.backends.cudnn.deterministic = True assert torch.cuda.is_available() base_model = ShuffleNetV2OneShot() criterion = CrossEntropyLabelSmooth(1000, args.label_smoothing) if args.latency_filter: latency_filter = LatencyFilter(threshold=args.latency_threshold, predictor=args.latency_filter) else: latency_filter = None evaluator = FunctionalEvaluator(evaluate_acc, criterion=criterion, args=args) evolution_strategy = strategy.RegularizedEvolution( model_filter=latency_filter, sample_size=args.evolution_sample_size, population_size=args.evolution_population_size, cycles=args.evolution_cycles) exp = RetiariiExperiment(base_model, evaluator, strategy=evolution_strategy) exp_config = RetiariiExeConfig('local') exp_config.trial_concurrency = 2 exp_config.trial_gpu_number = 1 exp_config.max_trial_number = args.evolution_cycles exp_config.training_service.use_active_gpu = False exp_config.execution_engine = 'base' exp_config.dummy_input = [1, 3, 224, 224] exp.run(exp_config, args.port) print('Exported models:') for i, model in enumerate(exp.export_top_models(formatter='dict')): print(model) with open(f'architecture_final_{i}.json', 'w') as f: json.dump(get_archchoice_by_model(model), f, indent=4)