def evaluate(): tf.set_random_seed(0) # for reproducibility with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) if FLAGS.distill_data_pattern is not None: distill_reader = readers.YT8MAggregatedFeatureReader(feature_names=["predictions"], feature_sizes=[4716]) else: distill_reader = None model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(FLAGS.label_loss, [losses])() transformer_class = find_class_by_name(FLAGS.feature_transformer, [feature_transform]) if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, transformer_class=transformer_class, distill_reader=distill_reader, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] loss = tf.get_collection("loss")[0] summary_op = tf.get_collection("summary_op")[0] saver = tf.train.Saver(tf.global_variables()) summary_writer = tf.summary.FileWriter( FLAGS.train_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k) last_global_step_val = -1 while True: last_global_step_val = evaluation_loop(video_id_batch, prediction_batch, label_batch, loss, summary_op, saver, summary_writer, evl_metrics, last_global_step_val) if FLAGS.run_once: break
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) if FLAGS.input_model_tgz: if FLAGS.train_dir: raise ValueError("You cannot supply --train_dir if supplying " "--input_model_tgz") # Untar. if not os.path.exists(FLAGS.untar_model_dir): os.makedirs(FLAGS.untar_model_dir) tarfile.open(FLAGS.input_model_tgz).extractall(FLAGS.untar_model_dir) FLAGS.train_dir = FLAGS.untar_model_dir flags_dict_file = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(flags_dict_file): raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file) flags_dict = json.loads(file_io.FileIO(flags_dict_file, "r").read()) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) """ if not FLAGS.output_file: raise ValueError("'output_file' was not specified. " "Unable to continue with inference.") """ if not FLAGS.input_data_pattern: raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") ################################################################################# if FLAGS.distill_data_pattern is not None: distill_reader = readers.YT8MAggregatedFeatureReader( feature_names=["predictions"], feature_sizes=[4716]) else: distill_reader = None model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() transformer_fn = find_class_by_name(FLAGS.feature_transformer, [feature_transform]) build_graph(reader, model, input_data_pattern=FLAGS.input_data_pattern, batch_size=FLAGS.batch_size, distill_reader=distill_reader) ## saver = tf.train.Saver(max_to_keep=3, keep_checkpoint_every_n_hours=10000000000) ################################################################################# inference(saver, FLAGS.train_dir, FLAGS.input_data_pattern, FLAGS.output_dir, FLAGS.batch_size, FLAGS.top_k)
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) distill_names, distill_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.distill_names, FLAGS.distill_sizes) if FLAGS.frame_features: reader1 = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader1 = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) reader2 = readers.YT8MAggregatedFeatureReader( feature_names=distill_names, feature_sizes=distill_sizes) if FLAGS.output_dir is "": raise ValueError("'output_dir' was not specified. " "Unable to continue with inference.") if FLAGS.input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(FLAGS.label_loss, [losses])() if FLAGS.input_data_pattern is "": raise IOError("'input_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader1=reader1, reader2=reader2, model=model, eval_data_pattern=FLAGS.input_data_pattern, distill_data_pattern=FLAGS.distill_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] saver = tf.train.Saver(tf.global_variables()) inference(video_id_batch, prediction_batch, label_batch, saver, FLAGS.output_dir)
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) if FLAGS.output_dir is "": raise ValueError("'output_dir' was not specified. " "Unable to continue with inference.") if FLAGS.input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") if FLAGS.distill_data_pattern is not None: predictions_patterns = FLAGS.distill_data_pattern.strip().split(",") predictions_readers = [] for pattern in predictions_patterns: predictions_readers.append( readers.YT8MAggregatedFeatureReader( feature_names=["predictions"], feature_sizes=[4716])) else: predictions_patterns = None predictions_readers = None model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() transformer_fn = find_class_by_name(FLAGS.feature_transformer, [feature_transform]) build_graph(reader=reader, input_data_pattern=FLAGS.input_data_pattern, model=model, distill_readers=predictions_readers, distill_data_patterns=predictions_patterns, batch_size=FLAGS.batch_size, transformer_class=transformer_fn) saver = tf.train.Saver(max_to_keep=3, keep_checkpoint_every_n_hours=10000000000) inference(saver, FLAGS.model_checkpoint_path, FLAGS.output_dir, FLAGS.batch_size, FLAGS.top_k)
def evaluate(): tf.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not os.path.exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(open(model_flags_path).read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(flags_dict["model"], [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph(reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] loss = tf.get_collection("loss")[0] summary_op = tf.get_collection("summary_op")[0] saver = tf.train.Saver(tf.global_variables()) summary_writer = tf.summary.FileWriter(FLAGS.train_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k) last_global_step_val = -1 with tf.device("/gpu:0"): while True: last_global_step_val = evaluation_loop( video_id_batch, prediction_batch, label_batch, loss, summary_op, saver, summary_writer, evl_metrics, last_global_step_val) if FLAGS.run_once: break
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) print("tensorflow version: %s" % tf.__version__) is_chief = (FLAGS.task == 0) # Recover session saver = None latest_checkpoint = tf.train.latest_checkpoint(FLAGS.train_dir) if FLAGS.start_new_model: logging.info( "'start_new_model' flag is set. Removing existing train dir.") try: gfile.DeleteRecursively(FLAGS.train_dir) except: logging.error( "Failed to delete directory " + FLAGS.train_dir + " when starting a new model. Please delete it manually and" + " try again.") elif not latest_checkpoint: logging.info("No checkpoint file found. Building a new model.") else: meta_filename = latest_checkpoint + ".meta" if not gfile.Exists(meta_filename): logging.info("No meta graph file found. Building a new model.") else: logging.info("Restoring from meta graph file %s", meta_filename) saver = tf.train.import_meta_graph(meta_filename) if not saver: # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(FLAGS.label_loss, [losses])() optimizer_class = find_class_by_name(FLAGS.optimizer, [tf.train]) build_graph(reader=reader, model=model, optimizer_class=optimizer_class, train_data_pattern=FLAGS.train_data_pattern, label_loss_fn=label_loss_fn, base_learning_rate=FLAGS.base_learning_rate, regularization_penalty=FLAGS.regularization_penalty, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built graph") saver = tf.train.Saver() train_loop(is_chief=is_chief, train_dir=FLAGS.train_dir, saver=saver, master=FLAGS.master)
def evaluate(): tf.set_random_seed(0) # for reproducibility with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) random_selection = 0 if FLAGS.select_randomly: random_selection = 1 if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes, random_selection = random_selection) model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(FLAGS.label_loss, [losses])() if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] loss = tf.get_collection("loss")[0] summary_op = tf.get_collection("summary_op")[0] if (FLAGS.model == "DidacModel") | (FLAGS.model == "EmbeddingModel"): hidden_layer_batch = tf.get_collection("hidden_layer_activations")[0] saver = tf.train.Saver(tf.global_variables()) if FLAGS.board_dir is "": board_dir = FLAGS.train_dir else: board_dir = FLAGS.board_dir summary_writer = tf.summary.FileWriter(board_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k) last_global_step_val = -1 while True: last_global_step_val, video_id_batch_val\ = evaluation_loop(video_id_batch, prediction_batch, label_batch, loss, summary_op, saver, summary_writer, evl_metrics, last_global_step_val, hidden_layer_batch) video_id_batch_array = np.asarray(video_id_batch_val) logging.info("Size video_id: " + str(video_id_batch_array.shape)) logging.info(video_id_batch_val[0]) if FLAGS.run_once: break
def infer(train_dir, input_data_pattern, output_file, top_k=20, batch_size=8192, num_readers=1): logging.set_verbosity(tf.logging.INFO) flags_dict_file = os.path.join(train_dir, "model_flags.json") if not os.path.exists(flags_dict_file): raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file) flags_dict = json.loads(open(flags_dict_file).read()) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) if output_file is "": raise ValueError("'output_file' was not specified. " "Unable to continue with inference.") if input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") inference(reader, train_dir, input_data_pattern, output_file, batch_size, top_k)
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) if FLAGS.output_file is "": raise ValueError("'output_file' was not specified. " "Unable to continue with inference.") if FLAGS.input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") inference(reader, FLAGS.checkpoint_file, FLAGS.train_dir, FLAGS.input_data_pattern, FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k)
def build_model(self): """Find the model and build the graph.""" # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) distill_names, distill_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.distill_names, FLAGS.distill_sizes) if FLAGS.frame_features: reader1 = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) else: reader1 = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) reader2 = readers.YT8MAggregatedFeatureReader( feature_names=distill_names, feature_sizes=distill_sizes) # Find the model. model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(FLAGS.label_loss, [losses])() optimizer_class = find_class_by_name(FLAGS.optimizer, [tf.train]) build_graph( reader1=reader1, reader2=reader2, model=model, optimizer_class=optimizer_class, clip_gradient_norm=FLAGS.clip_gradient_norm, train_data_pattern=FLAGS.train_data_pattern, distill_data_pattern=FLAGS.distill_data_pattern, label_loss_fn=label_loss_fn, base_learning_rate=FLAGS.base_learning_rate, learning_rate_decay=FLAGS.learning_rate_decay, learning_rate_decay_examples=FLAGS.learning_rate_decay_examples, regularization_penalty=FLAGS.regularization_penalty, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size, num_epochs=FLAGS.num_epochs) logging.info("%s: Built graph.", task_as_string(self.task)) return tf.train.Saver(max_to_keep=2, keep_checkpoint_every_n_hours=1)
def get_reader(): # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) return reader
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) if FLAGS.input_model_tgz: if FLAGS.train_dir: raise ValueError( "You cannot supply --train_dir if supplying " "--input_model_tgz" ) # Untar. if not os.path.exists(FLAGS.untar_model_dir): os.makedirs(FLAGS.untar_model_dir) tarfile.open(FLAGS.input_model_tgz).extractall(FLAGS.untar_model_dir) FLAGS.train_dir = FLAGS.untar_model_dir flags_dict_file = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(flags_dict_file): raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file) flags_dict = json.loads(file_io.FileIO(flags_dict_file, "r").read()) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"] ) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes ) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes ) if FLAGS.output_file is "": raise ValueError( "'output_file' was not specified. " "Unable to continue with inference." ) if FLAGS.input_data_pattern is "": raise ValueError( "'input_data_pattern' was not specified. " "Unable to continue with inference." ) inference( reader, FLAGS.train_dir, FLAGS.input_data_pattern, FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k, )
def get_reader(): # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) random_selection = 0 if FLAGS.select_randomly: random_selection = 2 if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, random_selection=random_selection, negative_sampling=FLAGS.negative_sampling, percentage_negative=FLAGS.percentage_negative) return reader
def evaluate(): """Starts main evaluation loop.""" tf.compat.v1.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(file_io.FileIO(model_flags_path, mode="r").read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, segment_labels=FLAGS.segment_labels) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(flags_dict["model"], [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if not FLAGS.input_data_pattern: raise IOError("'input_data_pattern' was not specified. Nothing to " "evaluate.") build_graph(reader=reader, model=model, input_data_pattern=FLAGS.input_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built inference graph") # A dict of tensors to be run in Session. saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables()) inference(saver, reader, FLAGS.train_dir, FLAGS.output_dir, FLAGS.batch_size, FLAGS.top_k)
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) if FLAGS.input_model_tgz: if FLAGS.train_dir: raise ValueError("You cannot supply --train_dir if supplying " "--input_model_tgz") # Untar. if not os.path.exists(FLAGS.untar_model_dir): os.makedirs(FLAGS.untar_model_dir) tarfile.open(FLAGS.input_model_tgz).extractall(FLAGS.untar_model_dir) FLAGS.train_dir = FLAGS.untar_model_dir flags_dict_file = os.path.join(FLAGS.train_dir, "model_flags.json") if not os.path.exists(flags_dict_file): raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file) flags_dict = json.loads(open(flags_dict_file).read()) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) all_data_patterns = [] with open(FLAGS.data_config) as f: all_data_patterns = f.read().splitlines() all_readers = [] for i in xrange(len(all_data_patterns)): reader = readers.EnsembleReader(feature_names=["predictions"], feature_sizes=["3862"]) all_readers.append(reader) # if FLAGS.output_file is "": # raise ValueError("'output_file' was not specified. " # "Unable to continue with inference.") # if FLAGS.input_data_pattern is "": # raise ValueError("'input_data_pattern' was not specified. " # "Unable to continue with inference.") inference(all_readers, FLAGS.train_dir, all_data_patterns, FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k)
def get_reader(): # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, is_training=True, data_augmentation=FLAGS.data_augmentation) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) return reader
def get_params(flags_dict): feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(flags_dict["model"], [frame_level_models, video_level_models])() # a model instance is created label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() return model, label_loss_fn, reader
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) print("START FLAGS===========================") print("train_dir: " + str(FLAGS.train_dir)) print("output_file: " + str(FLAGS.output_file)) print("input_data_pattern: " + str(FLAGS.input_data_pattern)) print("frame_features: " + str(FLAGS.frame_features)) print("batch_size: " + str(FLAGS.batch_size)) print("feature_names: " + str(FLAGS.feature_names)) print("feature_sizes: " + str(FLAGS.feature_sizes)) print("c_vars: " + str(FLAGS.c_vars)) print("num_readers: " + str(FLAGS.num_readers)) print("top_k: " + str(FLAGS.top_k)) print("layers_keep_probs: " + str(FLAGS.layers_keep_probs)) print("gpu_only: " + str(FLAGS.gpu_only)) print("END FLAGS ============================") # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, num_classes=FLAGS.truncated_num_classes, decode_zlib=FLAGS.decode_zlib) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, feature_calcs=FLAGS.c_vars, feature_remove=FLAGS.r_vars, num_classes=FLAGS.truncated_num_classes, decode_zlib=FLAGS.decode_zlib) if FLAGS.output_file is "": raise ValueError("'output_file' was not specified. " "Unable to continue with inference.") if FLAGS.input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") inference(reader, FLAGS.train_dir, FLAGS.input_data_pattern, FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k)
def get_reader(): # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( num_classes=FLAGS.truncated_num_classes, decode_zlib=FLAGS.decode_zlib, feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( num_classes=FLAGS.truncated_num_classes, decode_zlib=FLAGS.decode_zlib, feature_names=feature_names, feature_sizes=feature_sizes) return reader
def evaluate(): tf.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(file_io.FileIO(model_flags_path, mode="r").read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(flags_dict["model"], [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") summary_writer = tf.summary.FileWriter( FLAGS.train_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k)
def get_reader(): # Convert feature_names and feature_sizes to lists of values. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) #print('FEATURE NAMES',feature_names,'\n\n\n\n\n') #print('FEATURES SIZE',feature_sizes,'\n\n\n\n\n') if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, segment_labels=FLAGS.segment_labels) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) print(reader) return reader
def get_reader(): """ Convert feature_names and feature_sizes to lists of values. :return: A reader to read data from tfrecords. """ # Convert csv format into a list of strings or list of integers. Added by Sophie. # For example, feature_names = 'mean_rgb, mean_audio' => ['mean_rgb', 'mean_audio']. # feature_sizes = '1024, 128' => [1024, 128]. feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: # using frame-level features. Added by Sophie. reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: # using video-level features. Added by Sophie. reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) return reader
def predict_init(self): # logging.set_verbosity(tf.logging.INFO) self.log.info("Initing pre_model and reader") if self.pred_flags.input_model_tgz: if self.pred_flags.train_dir: raise ValueError("You cannot supply --train_dir if supplying " "--input_model_tgz") # Untar. if not os.path.exists(self.pred_flags.untar_model_dir): os.makedirs(self.pred_flags.untar_model_dir) tarfile.open(self.pred_flags.input_model_tgz).extractall(self.pred_flags.untar_model_dir) self.pred_flags.train_dir = self.pred_flags.untar_model_dir flags_dict_file = os.path.join(self.pred_flags.train_dir, "model_flags.json") if not os.path.exists(flags_dict_file): raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file) flags_dict = json.loads(open(flags_dict_file).read()) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes, prepare_distill=True) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) # if pred_flags.output_file is "": # raise ValueError("'output_file' was not specified. " # "Unable to continue with inference.") # if self.pred_flags.input_data_pattern is "": # raise ValueError("'input_data_pattern' was not specified. " # "Unable to continue with inference.") self.log.info("Successful init pre_model and reader") return reader
def main(unused_argv): logging.set_verbosity(tf.logging.INFO) # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) if FLAGS.frame_features: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) if FLAGS.output_file is "": raise ValueError("'output_file' was not specified. " "Unable to continue with inference.") if FLAGS.input_data_pattern is "": raise ValueError("'input_data_pattern' was not specified. " "Unable to continue with inference.") model = find_class_by_name(FLAGS.model, [frame_level_models, video_level_models])() transformer_fn = find_class_by_name(FLAGS.feature_transformer, [feature_transform]) build_graph(reader, model, input_data_pattern=FLAGS.input_data_pattern, batch_size=FLAGS.batch_size, transformer_class=transformer_fn) saver = tf.train.Saver(max_to_keep=3, keep_checkpoint_every_n_hours=10000000000) inference(saver, FLAGS.train_dir, FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k)
def evaluate(): ema_tensors = None if FLAGS.use_EMA: latest_checkpoint = get_latest_checkpoint() assert latest_checkpoint, "No checkpoint found" with tf.device("/cpu:0"): saver = tf.train.import_meta_graph(latest_checkpoint + ".meta", clear_devices=True) # saver.restore(sess, "../trained_models/attention_frames_v0_EMA/model.ckpt-15512") xvars = tf.get_collection("ema_vars") assert len(xvars) > 0, "No EMA shadow variables found. Did you train with EMA?" ema_tensors = list(set([x.name for x in xvars])) tf.reset_default_graph() tf.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not os.path.exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(open(model_flags_path).read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name(flags_dict["model"], [frame_level_models, video_level_models])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] loss = tf.get_collection("loss")[0] summary_op = tf.get_collection("summary_op")[0] saver = tf.train.Saver(tf.global_variables()) summary_writer = tf.summary.FileWriter( FLAGS.train_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k) last_global_step_val = -1 while True: last_global_step_val = evaluation_loop(video_id_batch, prediction_batch, label_batch, loss, summary_op, saver, summary_writer, evl_metrics, last_global_step_val, ema_tensors) if FLAGS.run_once: break
# convert to tfrecord to hd5 # import numpy as np import tensorflow as tf import readers # parameters tfrecords_filename = 'data/traineA.tfrecord' feature_names = ["mean_audio"] feature_sizes = [128] num_epochs = 10 batch_size = 10 # reader instance reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names, feature_sizes=feature_sizes) # prepare filename_queue = tf.train.string_input_producer([tfrecords_filename], num_epochs=num_epochs) # video tuple: # video_id, features, labels, ones video_id, feat, labels, batch_ones = reader.prepare_reader( filename_queue, batch_size=batch_size) # init init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) with tf.Session() as sess:
def evaluate(): """Starts main evaluation loop.""" tf.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(file_io.FileIO(model_flags_path, mode="r").read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, segment_labels=FLAGS.segment_labels) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name( flags_dict["model"], [frame_level_models, video_level_models, nextvlad])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if not FLAGS.eval_data_pattern: raise IOError("'eval_data_pattern' was not specified. Nothing to " "evaluate.") build_graph(reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") # A dict of tensors to be run in Session. fetches = { "video_id": tf.get_collection("video_id_batch")[0], "predictions": tf.get_collection("predictions")[0], "labels": tf.get_collection("labels")[0], "loss": tf.get_collection("loss")[0], "summary": tf.get_collection("summary_op")[0] } if FLAGS.segment_labels: fetches["label_weights"] = tf.get_collection("label_weights")[0] saver = tf.train.Saver(tf.global_variables()) summary_writer = tf.summary.FileWriter(os.path.join( FLAGS.train_dir, "eval"), graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k, None) last_global_step_val = -1 cur_checkpoint_file = FLAGS.checkpoint_file while True: checkpoint_file = 'model.ckpt-{}'.format(cur_checkpoint_file) checkpoint = get_checkpoint(checkpoint_file) try: last_global_step_val = evaluation_loop(fetches, saver, summary_writer, evl_metrics, checkpoint, last_global_step_val) cur_checkpoint_file += 200 except: logging.info( 'Find invalid checkpoint, try again using checkpoint: {}'. format(cur_checkpoint_file)) break if FLAGS.run_once: break
def evaluate(): tf.compat.v1.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not file_io.file_exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(open(model_flags_path).read()) with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes, segment_labels=FLAGS.segment_labels, ) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) model = find_class_by_name( flags_dict["model"], [frame_level_models, video_level_models, nextvlad])() label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if FLAGS.eval_data_pattern: raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") build_graph( reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size, ) logging.info("built evaluation graph") # xxx 2018 # video_id_batch = tf.compat.v1.get_collection("video_id_batch")[0] # prediction_batch = tf.compat.v1.get_collection("predictions")[0] # label_batch = tf.compat.v1.get_collection("labels")[0] # loss = tf.compat.v1.get_collection("loss")[0] # summary_op = tf.compat.v1.get_collection("summary_op")[0] # A dict of tensors to be run in Session. fetches = { "video_id": tf.compat.v1.get_collection("video_id_batch")[0], "predictions": tf.compat.v1.get_collection("predictions")[0], "labels": tf.compat.v1.get_collection("labels")[0], "loss": tf.compat.v1.get_collection("loss")[0], "summary": tf.compat.v1.get_collection("summary_op")[0], } if FLAGS.segment_labels: fetches["label_weights"] = tf.compat.v1.get_collection( "label_weights")[0] saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables()) summary_writer = tf.compat.v1.summary.FileWriter( os.path.join(FLAGS.train_dir, "eval"), graph=tf.compat.v1.get_default_graph(), ) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k, None) last_global_step_val = -1 while True: last_global_step_val = evaluation_loop(fetches, saver, summary_writer, evl_metrics, last_global_step_val) if FLAGS.run_once: break
def evaluate(model, checkpoint_name, k_frame): tf.set_random_seed(0) # for reproducibility # Write json of flags model_flags_path = os.path.join(FLAGS.train_dir, "model_flags.json") if not os.path.exists(model_flags_path): raise IOError(("Cannot find file %s. Did you run train.py on the same " "--train_dir?") % model_flags_path) flags_dict = json.loads(open(model_flags_path).read()) with tf.Graph().as_default() as graph: # scope_name # if FLAGS.model_type=='KD': # scope_name='student' # with tf.variable_scope(, reuse=False): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( flags_dict["feature_names"], flags_dict["feature_sizes"]) if flags_dict["frame_features"]: reader = readers.YT8MFrameFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) else: reader = readers.YT8MAggregatedFeatureReader( feature_names=feature_names, feature_sizes=feature_sizes) label_loss_fn = find_class_by_name(flags_dict["label_loss"], [losses])() if FLAGS.eval_data_pattern is "": raise IOError("'eval_data_pattern' was not specified. " + "Nothing to evaluate.") scope_name = 'teacher' if k_frame != 300: scope_name = 'student' with tf.variable_scope(scope_name, reuse=None): build_graph(reader=reader, model=model, eval_data_pattern=FLAGS.eval_data_pattern, label_loss_fn=label_loss_fn, num_readers=FLAGS.num_readers, batch_size=FLAGS.batch_size, k_frame=k_frame) # print("See variables in student in EVAL") # for i in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope='student'): # print(i) # i.name if you want just a name logging.info("built evaluation graph") video_id_batch = tf.get_collection("video_id_batch")[0] prediction_batch = tf.get_collection("predictions")[0] label_batch = tf.get_collection("labels")[0] loss = tf.get_collection("loss")[0] summary_op = tf.get_collection("summary_op")[0] saver = tf.train.Saver(tf.global_variables()) if k_frame != 300: student_variables = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope='student') saver = tf.train.Saver(var_list=student_variables) # student_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='student') # print("See variables in student") # for i in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='student'): # print(i) # i.name if you want just a name # student_saver = tf.train.Saver(var_list=student_variables) summary_writer = tf.summary.FileWriter(FLAGS.train_dir, graph=tf.get_default_graph()) evl_metrics = eval_util.EvaluationMetrics(reader.num_classes, FLAGS.top_k) last_global_step_val = -1 while True: last_global_step_val, EvalGAP = evaluation_loop( video_id_batch, prediction_batch, label_batch, loss, summary_op, saver, summary_writer, evl_metrics, last_global_step_val, checkpoint_name) if FLAGS.run_once: break return EvalGAP