|
| 1 | +""" |
| 2 | +This script is used to run the verification experiment for a neural network model. |
| 3 | +""" |
| 4 | +import argparse |
| 5 | +import sys |
| 6 | + |
| 7 | +sys.path.append("../") |
| 8 | + |
| 9 | +import logging |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +from src import * |
| 13 | + |
| 14 | + |
| 15 | +def main(args): |
| 16 | + |
| 17 | + if args.bp == "deeppoly": |
| 18 | + bp_method = ActRelaxationMode.DEEPPOLY |
| 19 | + elif args.bp == "crown": |
| 20 | + bp_method = ActRelaxationMode.CROWN |
| 21 | + elif args.bp == "rover": |
| 22 | + bp_method = ActRelaxationMode.ROVER_SN |
| 23 | + else: |
| 24 | + raise ValueError(f"Invalid BP method: {args.bp}") |
| 25 | + |
| 26 | + if args.opt == "lp": |
| 27 | + opt_method = OptimizationMethod.LP |
| 28 | + elif args.opt == "mnlp": |
| 29 | + opt_method = OptimizationMethod.MNLP |
| 30 | + else: |
| 31 | + opt_method = None |
| 32 | + |
| 33 | + log_file = None |
| 34 | + if args.log_name is not None: |
| 35 | + current_time = datetime.now().strftime("%Y%m%d%H%M%S") |
| 36 | + log_file = args.log_name |
| 37 | + log_file = "./logs/" + log_file + f"_{current_time}.log" |
| 38 | + |
| 39 | + # Set arguments for the model. |
| 40 | + args = Arguments( |
| 41 | + net_file_path=args.net_file_path, |
| 42 | + dataset=args.dataset, |
| 43 | + perturbation_radius=args.perturbation_radius, |
| 44 | + log_level=logging.INFO, |
| 45 | + act_relax_mode=bp_method, |
| 46 | + log_file=log_file, |
| 47 | + optimization_method=opt_method, |
| 48 | + first_sample_index=0, |
| 49 | + num_samples=100, |
| 50 | + device="cuda:0", # noqa |
| 51 | + dtype="float64", |
| 52 | + ) |
| 53 | + |
| 54 | + # Create the model and verify it. |
| 55 | + model = ModelFactory(args) |
| 56 | + model.prepare() |
| 57 | + model.build() |
| 58 | + model.verify() |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + parser = argparse.ArgumentParser(description="Run the experiment.") |
| 63 | + parser.add_argument("--net_file_path", type=str, default=None) |
| 64 | + parser.add_argument("--dataset", type=str, default="mnist") |
| 65 | + parser.add_argument("--perturbation_radius", type=float, default=0.01) |
| 66 | + parser.add_argument("--bp", type=str, default=None) |
| 67 | + parser.add_argument("--opt", type=str, default=None) |
| 68 | + parser.add_argument("--log_name", type=str, default=None) |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + main(args) |
0 commit comments