def run(main=None, argv=None): """Runs the program with an optional main function and argv list. The program will run with eager execution enabled. Example: ```python import tensorflow as tf # Import subject to future changes: from tensorflow.contrib.eager.python import tfe def main(_): u = tf.constant(6.0) v = tf.constant(7.0) print(u * v) if __name__ == "__main__": tfe.run() ``` Args: main: the main function to run. argv: the arguments to pass to it. """ enable_eager_execution() app.run(main, argv)
def run(main=None, argv=None): """Runs the program with an optional 'main' function and 'argv' list. The program will run with eager execution enabled. Args: main: the main function to run argv: the arguments to pass to it """ enable_eager_execution() app.run(main, argv)
def benchmarks_main(true_main): """Run benchmarks as declared in args. Args: true_main: True main function to run if benchmarks are not requested. """ argv = sys.argv found_arg = [arg for arg in argv if arg.startswith("--benchmarks=") or arg.startswith("-benchmarks=")] if found_arg: # Remove --benchmarks arg from sys.argv argv.remove(found_arg[0]) regex = found_arg[0].split("=")[1] app.run(lambda _: _run_benchmarks(regex)) else: true_main()
def main(): global FLAGS # Parses flags. parser = argparse.ArgumentParser( description="Invoke SavedModel to frozen model converter.") parser.add_argument( "saved_model_directory", type=str, help="Full path to directory containing the SavedModel.") parser.add_argument( "output_file_model", type=str, help="Full file path to save frozen graph.") parser.add_argument( "output_file_flags", type=str, help="Full file path to save ModelFlags.") parser.add_argument( "--input_arrays", type=str, help="Name of the input arrays, comma-separated.") parser.add_argument( "--input_shapes", type=str, help="Shapes corresponding to --input_arrays, colon-separated.") parser.add_argument( "--output_arrays", type=str, help="Name of the output arrays, comma-separated.") parser.add_argument( "--tag_set", type=str, help="Name of output arrays, comma-separated.") parser.add_argument( "--signature_key", type=str, help="Key identifying SignatureDef containing inputs and outputs.") parser.add_argument( "--batch_size", type=int, help="Batch size for the model. Replaces the first dimension of an " "input size array if undefined.") FLAGS, unparsed = parser.parse_known_args() app.run(main=execute, argv=[sys.argv[0]] + unparsed)
def benchmarks_main(true_main, argv=None): """Run benchmarks as declared in argv. Args: true_main: True main function to run if benchmarks are not requested. argv: the command line arguments (if None, uses sys.argv). """ if argv is None: argv = sys.argv found_arg = [arg for arg in argv if arg.startswith("--benchmarks=") or arg.startswith("-benchmarks=")] if found_arg: # Remove --benchmarks arg from sys.argv argv.remove(found_arg[0]) regex = found_arg[0].split("=")[1] app.run(lambda _: _run_benchmarks(regex), argv=argv) else: true_main()
def main(): global FLAGS parser = argparse.ArgumentParser( description="Invoke toco using protos as input.") parser.add_argument( "model_proto_file", type=str, help="File containing serialized proto that describes the model.") parser.add_argument( "toco_proto_file", type=str, help="File containing serialized proto describing how TOCO should run.") parser.add_argument( "model_input_file", type=str, help="Input model is read from this file.") parser.add_argument( "model_output_file", type=str, help="Result of applying TOCO conversion is written here.") FLAGS, unparsed = parser.parse_known_args() app.run(main=execute, argv=[sys.argv[0]] + unparsed)
path_to_run = ParseEventFilesFlag(FLAGS.logdir) multiplexer = event_multiplexer.AutoloadingMultiplexer( path_to_run=path_to_run, interval_secs=60, size_guidance=TENSORBOARD_SIZE_GUIDANCE) multiplexer.AutoUpdate(interval=30) factory = functools.partial(tensorboard_handler.TensorboardHandler, multiplexer) try: server = ThreadedHTTPServer((FLAGS.host, FLAGS.port), factory) except socket.error: logging.error('Tried to connect to port %d, but that address is in use.', FLAGS.port) return -2 try: tag = resource_loader.load_resource('tensorboard/TAG').strip() logging.info('TensorBoard is tag: %s', tag) except IOError: logging.warning('Unable to read TensorBoard tag') tag = '' status_bar.SetupStatusBarInsideGoogle('TensorBoard %s' % tag, FLAGS.port) print('Starting TensorBoard %s on port %d' % (tag, FLAGS.port)) print('(You can navigate to http://localhost:%d)' % FLAGS.port) server.serve_forever() if __name__ == '__main__': app.run()
def main_wrapper(): args = argv if args is None: args = sys.argv return app.run(main=g_main, argv=args)
for tensor in subgraph.tensors: tensor.name = '' builder = flatbuffers.Builder(1024) # Initial size of the buffer, which # will grow automatically if needed model_offset = model.Pack(builder) builder.Finish(model_offset) model_data = builder.Output() with open(output_tflite_file, 'wb') as out_file: out_file.write(model_data) def main(_): """Application run loop.""" parser = argparse.ArgumentParser( description='Strips all nonessential strings from a tflite file.') parser.add_argument('input_tflite_file', type=str, help='Full path name to the input tflite file.') parser.add_argument( 'output_tflite_file', type=str, help='Full path name to the stripped output tflite file.') args = parser.parse_args() # Invoke the strip tflite file function StripTfliteFile(args.input_tflite_file, args.output_tflite_file) if __name__ == '__main__': app.run(main=main, argv=sys.argv[:1])
def main(): app.run(main=run_main, argv=sys.argv[:1])
def run_main(): parser = argparse.ArgumentParser() parser.register("type", "bool", lambda v: v.lower() == "true") parser.add_argument("--input_graph", type=str, default="", help="TensorFlow \'GraphDef\' file to load.") parser.add_argument("--input_saver", type=str, default="", help="TensorFlow saver file to load.") parser.add_argument("--input_checkpoint", type=str, default="", help="TensorFlow variables file to load.") parser.add_argument("--checkpoint_version", type=int, default=2, help="Tensorflow variable file format") parser.add_argument("--output_graph", type=str, default="", help="Output \'GraphDef\' file name.") parser.add_argument("--input_binary", nargs="?", const=True, type="bool", default=False, help="Whether the input files are in binary format.") parser.add_argument("--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.") parser.add_argument("--restore_op_name", type=str, default="save/restore_all", help="""\ The name of the master restore operator. Deprecated, unused by updated \ loading code. """) parser.add_argument("--filename_tensor_name", type=str, default="save/Const:0", help="""\ The name of the tensor holding the save path. Deprecated, unused by \ updated loading code. """) parser.add_argument("--clear_devices", nargs="?", const=True, type="bool", default=True, help="Whether to remove device specifications.") parser.add_argument( "--initializer_nodes", type=str, default="", help="Comma separated list of initializer nodes to run before freezing." ) parser.add_argument("--variable_names_whitelist", type=str, default="", help="""\ Comma separated list of variables to convert to constants. If specified, \ only those variables will be converted to constants.\ """) parser.add_argument("--variable_names_blacklist", type=str, default="", help="""\ Comma separated list of variables to skip converting to constants.\ """) parser.add_argument("--input_meta_graph", type=str, default="", help="TensorFlow \'MetaGraphDef\' file to load.") parser.add_argument( "--input_saved_model_dir", type=str, default="", help= "Path to the dir with TensorFlow \'SavedModel\' file and variables.") parser.add_argument("--saved_model_tags", type=str, default="serve", help="""\ Group of tag(s) of the MetaGraphDef to load, in string format,\ separated by \',\'. For tag-set contains multiple tags, all tags \ must be passed in.\ """) flags, unparsed = parser.parse_known_args() my_main = lambda unused_args: main(unused_args, flags) app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
def run_main(): parser = argparse.ArgumentParser( description="Xilinx's Quantization Tools" + version_string(), usage=usage_string(), formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--version", action="version", version=version_string()) parser.register("type", "bool", lambda v: v.lower() == "true") parser.add_argument("command", type=str, default="", help="Specify a command for vai_q_tensorflow", choices=['quantize', 'inspect', 'dump', 'deploy']) #################### # Main Arguments # #################### parser.add_argument("--mode", type=str, default="frozen", help="Mode for quantization.", choices=['frozen', 'train', 'eval']) parser.add_argument("--input_frozen_graph", type=str, default="", help="Path to input frozen graph(.pb)") parser.add_argument("--input_meta_graph", type=str, default="", help="Path to input meta graph(.meta)") parser.add_argument("--input_checkpoint", type=str, default="", help="Path to checkpoint files") parser.add_argument("--output_dir", type=str, default="./quantize_results", help="The directory to save the quantization results") ###################################### # Quantize Configuration Arguments # ###################################### parser.add_argument("--weight_bit", type=int, default=8, help="The target bit width for weights/biases") parser.add_argument("--activation_bit", type=int, default=8, help="The target bit width for activation") parser.add_argument( "--nodes_bit", type=str, default="", help="Specify bit width of nodes, nodes name and bit width \ form a pair of parameter joined by a colon, and parameters \ are comma separated. When specify conv op name only vai_q_tensorflow \ will quantize weights of conv op using specified bit width . \ e.g 'conv1/Relu:16,conv1/weights:8,conv1:16'") parser.add_argument("--nodes_method", type=str, default="", help="Specify method of nodes, nodes name and method \ form a pair of parameter joined by a colon, and parameter pairs \ are comma separated. When specify conv op name only vai_q_tensorflow \ will quantize weights of conv op using specified method. \ e.g 'conv1/Relu:1,depthwise_conv1/weights:2,conv1:1'") parser.add_argument("--method", type=int, default=1, choices=[0, 1, 2], help=" The method for quantization, options are: \ 0: non-overflow method, make sure no values are saturated during quantization, \ may get bad results incase of outliers. \ 1: min-diffs method, allow saturation for large values during quantization to get smaller quantization errors. \ This method is slower than method 0 but has higher endurance to outliers. \ 2: min-diffs method with strategy for depthwise, allow saturation for large values during quantization to get smaller quantization errors. \ And apply special strategy for depthwise weights, but do implement method 1 to normal weights and activation. \ This method is slower than method 0 but has higher endurance to outliers." ) parser.add_argument( "--calib_iter", type=int, default=100, help= "The iterations of calibration, total number of images for calibration = calib_iter * batch_size" ) parser.add_argument( "--input_nodes", type=str, default="", help= "The name list of input nodes of the subgraph to be quantized, comma separated. \ Used together with output_nodes. When generating the model for deploy, only the subgraph between input_nodes and \ output_nodes will be included. Please set it to the begining of the main body fo the model to quantize, \ such as the nodes after data preprocessing and augmentation.") parser.add_argument( "--input_shapes", type=str, default="", help= "The shape list of input_nodes, The shape must be a 4-dimension shape for each node, comma separated, e.g. 1,224,224,3;\ Unknown size for batchsize is supported, e.g. ?,224,224,3; \ In case of multiple input_nodes, please assign the shape list of each node,\ separated by `:`. e.g. ?,224,224,3:?,300,300,1") parser.add_argument( "--output_nodes", type=str, default="", help= "The name list of output nodes of the subgraph to be quantized, comma separated. \ Used together with input_nodes. When generating the model for deploy, only the subgraph between input_nodes and \ output_nodes will be included. Please set it to the end of the main body of the model to quantize, \ such as the nodes before postprocessing.") parser.add_argument( "--ignore_nodes", type=str, default="", help= "The name list of nodes to be ignored during quantization, comma separated. The ignored nodes \ will be left unquantized during quantization even if it is quantizable. This argument has no effect for non-quantizable nodes." ) parser.add_argument("--skip_check", type=int, default=0, choices=[0, 1], help="Set to 1 to skip the check for float model.") parser.add_argument( "--align_concat", type=int, default=0, choices=[0, 1, 2], help= "The strategy for alignment of the input quantize positions for concat nodes. Set to 0 to align \ all concat nodes, 1 to align the output concat nodes, 2 to disable alignment" ) parser.add_argument( "--adjust_shift_bias", type=int, default=1, choices=[0, 1, 2], help= "The strategy for shift bias check and adjustment for DPU compiler. Set to 0 to disable shift bias\ check and adjustment, 1 to enable with static constraints, 2 to enable with dynamic constraints." ) parser.add_argument( "--adjust_shift_cut", type=int, default=1, choices=[0, 1], help= "The strategy for shift cut check and adjustment for DPU compiler. Set to 0 to disable shift cut\ check and adjustment, 1 to enable with static constraints.") parser.add_argument( "--simulate_dpu", type=int, default=1, choices=[0, 1], help= "Set to 1 to enable simulation of DPU. The behavior of DPU for some operations are different from tensorflow. \ For example, the dividing in LeakyRelu and AvgPooling are replaced by bit-shifting, so there maybe slight difference \ between DPU outputs and CPU/GPU outputs. This quantizer will simulate the behavior for these operations if this flag is set to 1" ) ############################################ # Input Function Configuration Arguments # ############################################ parser.add_argument( "--input_fn", type=str, default="", help= "The python importable function that provides the input data. The format is \ `module_name.input_fn_name`, e.g. 'my_input_fn.input_fn'. The input_fn should take a `int` object as input \ indicating the calibration step, and should return a dict`(placeholder_node_name : numpy.Array)` object \ for each call, which will be fed into the model's placeholder nodes.") ################################## # Dump Configuration Arguments # ################################## parser.add_argument("--max_dump_batches", type=int, default=1, help="The maximum batches to be dumped") parser.add_argument( "--dump_float", type=int, default=0, choices=[0, 1], help= "Set to 1 to dump the float weights/biases and activation tensors together with the quantized tensors." ) parser.add_argument( "--dump_input_tensors", type=str, default="", help= "Specify input tensor name of Graph when graph entrance is not a placeholder. " "We will add a placeholder according to the dump_input_tensor, so that input_fn can feed data." ) parser.add_argument( "--dump_as_xir", default=False, action="store_true", help= "Specify whether dump quantized tensorflow frozen model as xir model.", ) parser.add_argument( "--arch_type", type=str, default="DEFAULT", choices=["DEFAULT", 'DPUCADF8H'], help= "Specify the arch type for fix neuron. 'DEFAULT' means quantization range of both wquant and aquant \ is [-128, 127]. 'DPUCADF8H' means wquant quantization range is [-128, 127] while aquant is [-127, 127]", ) ##################################### # Session Configuration Arguments # ##################################### parser.add_argument( "--gpu", type=str, default="0", help="The gpu id used for quantization, comma separated.") parser.add_argument( "--gpu_memory_fraction", type=float, default=0.5, help="The gpu memory fraction used for quantization, between 0-1.") FLAGS, unparsed = parser.parse_known_args() if unparsed: raise ValueError("Unknown arguments: ", unparsed) my_main = lambda unused_args: main(unused_args, FLAGS) app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
type=str, default="", help="Input node names, comma separated.") parser.add_argument( "--output_names", type=str, default="", help="Output node names, comma separated.") parser.add_argument( "--frozen_graph", nargs="?", const=True, type="bool", default=True, help="""\ If true, the input graph is a binary frozen GraphDef file; if false, it is a text GraphDef proto file.\ """) parser.add_argument( "--placeholder_type_enum", type=int, default=dtypes.float32.as_datatype_enum, help="The AttrValue enum to use for placeholders.") return parser.parse_known_args() if __name__ == "__main__": FLAGS, UNPARSED = parse_args() app.run(main=main, argv=[sys.argv[0]] + UNPARSED)
def main_wrapper(): return app.run(main=g_main, argv=sys.argv)
def run_main(): parser = argparse.ArgumentParser() parser.register("type", "bool", lambda v: v.lower() == "true") parser.add_argument( "--input_graph", type=str, default="", help="TensorFlow \'GraphDef\' file to load.") parser.add_argument( "--input_saver", type=str, default="", help="TensorFlow saver file to load.") parser.add_argument( "--input_checkpoint", type=str, default="", help="TensorFlow variables file to load.") parser.add_argument( "--checkpoint_version", type=int, default=2, help="Tensorflow variable file format") parser.add_argument( "--output_graph", type=str, default="", help="Output \'GraphDef\' file name.") parser.add_argument( "--input_binary", nargs="?", const=True, type="bool", default=False, help="Whether the input files are in binary format.") parser.add_argument( "--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.") parser.add_argument( "--restore_op_name", type=str, default="save/restore_all", help="""\ The name of the master restore operator. Deprecated, unused by updated \ loading code. """) parser.add_argument( "--filename_tensor_name", type=str, default="save/Const:0", help="""\ The name of the tensor holding the save path. Deprecated, unused by \ updated loading code. """) parser.add_argument( "--clear_devices", nargs="?", const=True, type="bool", default=True, help="Whether to remove device specifications.") parser.add_argument( "--initializer_nodes", type=str, default="", help="Comma separated list of initializer nodes to run before freezing.") parser.add_argument( "--variable_names_whitelist", type=str, default="", help="""\ Comma separated list of variables to convert to constants. If specified, \ only those variables will be converted to constants.\ """) parser.add_argument( "--variable_names_blacklist", type=str, default="", help="""\ Comma separated list of variables to skip converting to constants.\ """) parser.add_argument( "--input_meta_graph", type=str, default="", help="TensorFlow \'MetaGraphDef\' file to load.") parser.add_argument( "--input_saved_model_dir", type=str, default="", help="Path to the dir with TensorFlow \'SavedModel\' file and variables.") parser.add_argument( "--saved_model_tags", type=str, default="serve", help="""\ Group of tag(s) of the MetaGraphDef to load, in string format,\ separated by \',\'. For tag-set contains multiple tags, all tags \ must be passed in.\ """) flags, unparsed = parser.parse_known_args() my_main = lambda unused_args: main(unused_args, flags) app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
AffineTransform, min_samples=3, residual_threshold=20, max_trials=1000) if inliers is None: raise Exception('match_images.py: inliers is None') else: print('number of inliers -> %d' % len(inliers)) # -- # Plot fig, ax = plt.subplots() inlier_idxs = np.nonzero(inliers)[0] plot_matches(ax, mpimg.imread(args.image_1_path), mpimg.imread(args.image_2_path), locations_1_to_use, locations_2_to_use, np.column_stack((inlier_idxs, inlier_idxs)), matches_color='b') ax.axis('off') ax.set_title('DELF correspondences') plt.savefig(args.output_image) if __name__ == '__main__': app.run(main=main)
:nb_samples], Y_test[:nb_samples], args=eval_par) t2 = time.time() print('Test accuracy on adversarial examples %0.4f\n' % acc) print("Took", t2 - t1, "seconds") if __name__ == '__main__': if "CIFAR10_CHALLENGE_DIR" in os.environ: cifar10_root = os.environ['CIFAR10_CHALLENGE_DIR'] default_ckpt_dir = os.path.join(cifar10_root, 'models/adv_trained') default_data_dir = os.path.join(cifar10_root, 'cifar10_data') flags.DEFINE_integer('batch_size', 100, "Batch size") flags.DEFINE_integer('nb_samples', 1000, "Number of samples to test") flags.DEFINE_string('attack_type', 'fgsm', ("Attack type: 'fgsm'->'fast " "gradient sign method', " "'pgd'->'projected " "gradient descent', 'cwl2'->" "'Carlini & Wagner L2'")) flags.DEFINE_string('checkpoint_dir', default_ckpt_dir, 'Checkpoint directory to load') flags.DEFINE_string('dataset_dir', default_data_dir, 'Dataset directory') flags.DEFINE_bool('sweep', False, 'Sweep epsilon or single epsilon?') app.run(main)
predictions_2_adv, X_test, Y_test, args=eval_params) print('Test accuracy on adversarial examples: ' + str(accuracy_adv)) # Perform adversarial training model_train(sess, x, y, predictions_2, X_train, Y_train, predictions_adv=predictions_2_adv, evaluate=evaluate_2, args=train_params) # Evaluate the accuracy of the CIFAR10 model on adversarial examples accuracy = model_eval(sess, x, y, predictions_2_adv, X_test, Y_test, args=eval_params) print('Test accuracy on adversarial examples: ' + str(accuracy)) if __name__ == '__main__': app.run()
else: rf_writer = None # Compute RF parameters for each network architecture. _alexnet_v2_rf(rf_writer) _vgg_rf(rf_writer) _inception_v2_rf(rf_writer) _inception_v3_rf(rf_writer) _inception_v4_rf(rf_writer) _inception_resnet_v2_rf(rf_writer) _mobilenet_v1_rf(rf_writer) _resnet_rf(rf_writer) # Close CSV file, if it was opened. if cmd_args.csv_path: csv_file.close() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.register('type', 'bool', lambda v: v.lower() == 'true') parser.add_argument('--csv_path', type=str, default='', help="""\ Path to CSV file that will be written with RF parameters.If empty, no file will be written.\ """) cmd_args, unparsed = parser.parse_known_args() app.run(main=main, argv=[sys.argv[0]] + unparsed)
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.register('type', 'bool', lambda v: v.lower() == 'true') parser.add_argument( '--config_path', type=str, default='delf_config_example.pbtxt', help=""" Path to DelfConfig proto text file with configuration to be used for DELF extraction. """) parser.add_argument( '--list_images_path', type=str, default='list_images.txt', help=""" Path to list of images whose DELF features will be extracted. """) parser.add_argument( '--output_dir', type=str, default='test_features', help=""" Directory where DELF features will be written to. Each image's features will be written to a file with same name, and extension replaced by .delf. """) cmd_args, unparsed = parser.parse_known_args() app.run(main=main, argv=[sys.argv[0]] + unparsed)