def load(filepath, device=torch.device("cpu")): """Load image from filepath as tensor. Arguments: filepath (string): Path to image. device (torch device): Device on which to allocate the image tensor. Returns: Tensor of image content with shape (1, channels, height, width). """ if not os.path.isfile(filepath): logging_error("File {} does not exist.".format(filepath), should_exit=True) image = io.imread(filepath).transpose((2, 0, 1)) / (256.0 / 2.0) - 1.0 return torch.from_numpy(image).double().unsqueeze(0).to(device)
def parse(self): """Parse the arguments of the application. Returns: Namespace containing the parsed arguments. """ arguments = self.parser.parse_args() arguments_dict = vars(arguments) # validate optimizer if "optimizer" in arguments_dict: required_arguments = self.required_optimizer_arguments.get( arguments.optimizer, []) for argument in required_arguments: if argument not in arguments_dict or arguments_dict[ argument] is None: logging_error( "For the optimizer '{}' the following arguments are required: {}." .format(arguments.optimizer, required_arguments)) # validate number of steps and learning rate if "num_steps" in arguments_dict and "learning_rate" in arguments_dict: if len(arguments.num_steps) == 0 and len( arguments.learning_rate) != 1: logging_error( "When no step limit is given, exactly one learning rate must be given." ) elif len(arguments.num_steps) > 0: if len(arguments.num_steps) != len(arguments.learning_rate): logging_error( "There must be a learning rate (in total {}) for exactly one step block (in total {})." .format(len(arguments.learning_rate), len(arguments.num_steps))) return arguments
# create color palette colors = get_distinct_colors(dataset.num_classes) # get source capture_source = 0 if arguments.video_filename is not None: capture_source = arguments.video_filename is_webcam = capture_source == 0 # start a new Tensorflow session with tf.Session(graph=graph) as session: # capture the video capture = cv2.VideoCapture(capture_source) if not capture.isOpened(): logging_error("There was a problem opening the capture device.") capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) # hide the toolbar mpl.rcParams["toolbar"] = "None" # fit window to image plt.figure() plt.axis("off") image = plt.imshow(grab_single_frame(capture)) plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0,
argument_list.add_input_device_argument("Device for processing inputs.", default="/cpu:0") argument_list.add_inference_device_argument("Device for inference.", default="/gpu:0") argument_list.add_optimization_device_argument("Device for optimization.", default="/cpu:0") argument_list.add_tf_verbosity_argument("Tensorflow verbosity.", default="info") argument_list.add_tf_min_log_level_argument( "Tensorflow minimum log level.", default=3) arguments = argument_list.parse() # load run run = Run(run_id=arguments.run) if not run.open(): logging_error("There is no run '{}'.".format(arguments.run)) # print some information logging_info("Load run '{}'.".format(arguments.run)) logging_info("Model: {}".format( run.get_config_value("model", "name"))) logging_info("Dataset: {} {}".format( arguments.dataset, arguments.dataset_split)) logging_info("Preprocessing parallel calls: {}".format( arguments.num_parallel_calls)) logging_info("Prefetch buffer size: {}".format( arguments.prefetch_buffer_size)) logging_info("Batch size: {}".format( arguments.batch_size)) logging_info("Input device: {}".format( arguments.input_device))
epoch = tf.Variable(1, name="epoch", trainable=False, dtype=tf.int64) global_step = tf.Variable(1, name="global_step", trainable=False, dtype=tf.int64) # setup optimizer learning_rate = tf.placeholder(tf.float32, [], name="learning_rate") optimizer_kwargs = {} if arguments.optimizer == "momentum": optimizer_kwargs["momentum"] = arguments.momentum optimizer = tfu_get_optimizer(arguments.optimizer, learning_rate, **optimizer_kwargs) if optimizer is None: logging_error("The optimizer '{}' is not supported.".format( arguments.optimizer)) epoch_op = tf.assign(epoch, tf.add(epoch, 1)) train_op = optimizer.minimize(model.losses["total"], global_step=global_step) # initialize variables uninitialized_variables = tfu_get_uninitialized_variables(session) if len(uninitialized_variables) > 0: session.run(tf.variables_initializer(uninitialized_variables)) session.run(restore_ops) # initialize saver saver = tf.train.Saver(max_to_keep=100) # setup summaries