Skip to content

Commit

Permalink
Yapf fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
XericZephyr committed Aug 21, 2019
1 parent f9a9bb6 commit 98d6799
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 171 deletions.
78 changes: 38 additions & 40 deletions eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ def get_input_evaluation_tensors(reader,
if not files:
raise IOError("Unable to find the evaluation files.")
logging.info("number of evaluation files: %d", len(files))
filename_queue = tf.train.string_input_producer(
files, shuffle=False, num_epochs=1)
filename_queue = tf.train.string_input_producer(files,
shuffle=False,
num_epochs=1)
eval_data = [
reader.prepare_reader(filename_queue) for _ in range(num_readers)
]
return tf.train.batch_join(
eval_data,
batch_size=batch_size,
capacity=3 * batch_size,
allow_smaller_final_batch=True,
enqueue_many=True)
return tf.train.batch_join(eval_data,
batch_size=batch_size,
capacity=3 * batch_size,
allow_smaller_final_batch=True,
enqueue_many=True)


def build_graph(reader,
Expand All @@ -125,8 +125,10 @@ def build_graph(reader,
"""

global_step = tf.Variable(0, trainable=False, name="global_step")
input_data_dict = get_input_evaluation_tensors(
reader, eval_data_pattern, batch_size=batch_size, num_readers=num_readers)
input_data_dict = get_input_evaluation_tensors(reader,
eval_data_pattern,
batch_size=batch_size,
num_readers=num_readers)
video_id_batch = input_data_dict["video_ids"]
model_input_raw = input_data_dict["video_matrix"]
labels_batch = input_data_dict["labels"]
Expand All @@ -139,12 +141,11 @@ def build_graph(reader,
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)

with tf.variable_scope("tower"):
result = model.create_model(
model_input,
num_frames=num_frames,
vocab_size=reader.num_classes,
labels=labels_batch,
is_training=False)
result = model.create_model(model_input,
num_frames=num_frames,
vocab_size=reader.num_classes,
labels=labels_batch,
is_training=False)
predictions = result["predictions"]
tf.summary.histogram("model_activations", predictions)
if "loss" in result.keys():
Expand Down Expand Up @@ -181,9 +182,8 @@ def evaluation_loop(fetches, saver, summary_writer, evl_metrics,
"""

global_step_val = -1
with tf.Session(
config=tf.ConfigProto(gpu_options=tf.GPUOptions(
allow_growth=True))) as sess:
with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(
allow_growth=True))) as sess:
latest_checkpoint = tf.train.latest_checkpoint(FLAGS.train_dir)
if latest_checkpoint:
logging.info("Loading checkpoint for eval: %s", latest_checkpoint)
Expand Down Expand Up @@ -239,11 +239,10 @@ def evaluation_loop(fetches, saver, summary_writer, evl_metrics,
output_data_dict["loss"])
iteration_info_dict["examples_per_second"] = example_per_second

iterinfo = utils.AddGlobalStepSummary(
summary_writer,
global_step_val,
iteration_info_dict,
summary_scope="Eval")
iterinfo = utils.AddGlobalStepSummary(summary_writer,
global_step_val,
iteration_info_dict,
summary_scope="Eval")
logging.info("examples_processed: %d | %s", examples_processed,
iterinfo)

Expand All @@ -256,11 +255,10 @@ def evaluation_loop(fetches, saver, summary_writer, evl_metrics,
epoch_info_dict["epoch_id"] = global_step_val

summary_writer.add_summary(summary_val, global_step_val)
epochinfo = utils.AddEpochSummary(
summary_writer,
global_step_val,
epoch_info_dict,
summary_scope="Eval")
epochinfo = utils.AddEpochSummary(summary_writer,
global_step_val,
epoch_info_dict,
summary_scope="Eval")
logging.info(epochinfo)
evl_metrics.clear()
except Exception as e: # pylint: disable=broad-except
Expand Down Expand Up @@ -296,8 +294,8 @@ def evaluate():
feature_sizes=feature_sizes,
segment_labels=FLAGS.segment_labels)
else:
reader = readers.YT8MAggregatedFeatureReader(
feature_names=feature_names, feature_sizes=feature_sizes)
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])()
Expand All @@ -307,13 +305,12 @@ def evaluate():
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)
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.
Expand All @@ -328,8 +325,9 @@ def evaluate():
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())
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)
Expand Down
9 changes: 5 additions & 4 deletions eval_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def calculate_gap(predictions, actuals, top_k=20):
gap_calculator = ap_calculator.AveragePrecisionCalculator()
sparse_predictions, sparse_labels, num_positives = top_k_by_class(
predictions, actuals, top_k)
gap_calculator.accumulate(
flatten(sparse_predictions), flatten(sparse_labels), sum(num_positives))
gap_calculator.accumulate(flatten(sparse_predictions), flatten(sparse_labels),
sum(num_positives))
return gap_calculator.peek_ap_at_n()


Expand Down Expand Up @@ -194,8 +194,9 @@ def accumulate(self, predictions, labels, loss):
predictions, labels, self.top_k)
self.map_calculator.accumulate(sparse_predictions, sparse_labels,
num_positives)
self.global_ap_calculator.accumulate(
flatten(sparse_predictions), flatten(sparse_labels), sum(num_positives))
self.global_ap_calculator.accumulate(flatten(sparse_predictions),
flatten(sparse_labels),
sum(num_positives))

self.num_examples += batch_size
self.sum_hit_at_one += mean_hit_at_one * batch_size
Expand Down
16 changes: 7 additions & 9 deletions export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ def build_inputs_and_outputs(self):
serialized_examples = tf.placeholder(tf.string, shape=(None,))

fn = lambda x: self.build_prediction_graph(x)
video_id_output, top_indices_output, top_predictions_output = (
tf.map_fn(
fn, serialized_examples, dtype=(tf.string, tf.int32, tf.float32)))
video_id_output, top_indices_output, top_predictions_output = (tf.map_fn(
fn, serialized_examples, dtype=(tf.string, tf.int32, tf.float32)))

else:
serialized_examples = tf.placeholder(tf.string, shape=(None,))
Expand Down Expand Up @@ -105,12 +104,11 @@ def build_prediction_graph(self, serialized_examples):
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)

with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False)
result = self.model.create_model(model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False)

for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
Expand Down
62 changes: 31 additions & 31 deletions frame_level_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ def create_model(self, model_input, vocab_size, num_frames, **unused_params):
num_frames = tf.cast(tf.expand_dims(num_frames, 1), tf.float32)
feature_size = model_input.get_shape().as_list()[2]

denominators = tf.reshape(
tf.tile(num_frames, [1, feature_size]), [-1, feature_size])
denominators = tf.reshape(tf.tile(num_frames, [1, feature_size]),
[-1, feature_size])
avg_pooled = tf.reduce_sum(model_input, axis=[1]) / denominators

output = slim.fully_connected(
avg_pooled,
vocab_size,
activation_fn=tf.nn.sigmoid,
weights_regularizer=slim.l2_regularizer(1e-8))
output = slim.fully_connected(avg_pooled,
vocab_size,
activation_fn=tf.nn.sigmoid,
weights_regularizer=slim.l2_regularizer(1e-8))
return {"predictions": output}


Expand Down Expand Up @@ -156,12 +155,11 @@ def create_model(self,
tf.compat.v1.summary.histogram("input_hist", reshaped_input)

if add_batch_norm:
reshaped_input = slim.batch_norm(
reshaped_input,
center=True,
scale=True,
is_training=is_training,
scope="input_bn")
reshaped_input = slim.batch_norm(reshaped_input,
center=True,
scale=True,
is_training=is_training,
scope="input_bn")

cluster_weights = tf.compat.v1.get_variable(
"cluster_weights", [feature_size, cluster_size],
Expand All @@ -170,12 +168,11 @@ def create_model(self,
tf.compat.v1.summary.histogram("cluster_weights", cluster_weights)
activation = tf.matmul(reshaped_input, cluster_weights)
if add_batch_norm:
activation = slim.batch_norm(
activation,
center=True,
scale=True,
is_training=is_training,
scope="cluster_bn")
activation = slim.batch_norm(activation,
center=True,
scale=True,
is_training=is_training,
scope="cluster_bn")
else:
cluster_biases = tf.compat.v1.get_variable(
"cluster_biases", [cluster_size],
Expand All @@ -196,12 +193,11 @@ def create_model(self,
tf.compat.v1.summary.histogram("hidden1_weights", hidden1_weights)
activation = tf.matmul(activation, hidden1_weights)
if add_batch_norm:
activation = slim.batch_norm(
activation,
center=True,
scale=True,
is_training=is_training,
scope="hidden1_bn")
activation = slim.batch_norm(activation,
center=True,
scale=True,
is_training=is_training,
scope="hidden1_bn")
else:
hidden1_biases = tf.compat.v1.get_variable(
"hidden1_biases", [hidden1_size],
Expand All @@ -213,8 +209,9 @@ def create_model(self,

aggregated_model = getattr(video_level_models,
FLAGS.video_level_classifier_model)
return aggregated_model().create_model(
model_input=activation, vocab_size=vocab_size, **unused_params)
return aggregated_model().create_model(model_input=activation,
vocab_size=vocab_size,
**unused_params)


class LstmModel(models.BaseModel):
Expand Down Expand Up @@ -243,11 +240,14 @@ def create_model(self, model_input, vocab_size, num_frames, **unused_params):
for _ in range(number_of_layers)
])

_, state = tf.nn.dynamic_rnn(
stacked_lstm, model_input, sequence_length=num_frames, dtype=tf.float32)
_, state = tf.nn.dynamic_rnn(stacked_lstm,
model_input,
sequence_length=num_frames,
dtype=tf.float32)

aggregated_model = getattr(video_level_models,
FLAGS.video_level_classifier_model)

return aggregated_model().create_model(
model_input=state[-1].h, vocab_size=vocab_size, **unused_params)
return aggregated_model().create_model(model_input=state[-1].h,
vocab_size=vocab_size,
**unused_params)
32 changes: 15 additions & 17 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,17 @@ def get_input_data_tensors(reader, data_pattern, batch_size, num_readers=1):
raise IOError("Unable to find input files. data_pattern='" +
data_pattern + "'")
logging.info("number of input files: " + str(len(files)))
filename_queue = tf.train.string_input_producer(
files, num_epochs=1, shuffle=False)
filename_queue = tf.train.string_input_producer(files,
num_epochs=1,
shuffle=False)
examples_and_labels = [
reader.prepare_reader(filename_queue) for _ in range(num_readers)
]

input_data_dict = (
tf.train.batch_join(
examples_and_labels,
batch_size=batch_size,
allow_smaller_final_batch=True,
enqueue_many=True))
input_data_dict = (tf.train.batch_join(examples_and_labels,
batch_size=batch_size,
allow_smaller_final_batch=True,
enqueue_many=True))
video_id_batch = input_data_dict["video_ids"]
video_batch = input_data_dict["video_matrix"]
num_frames_batch = input_data_dict["num_frames"]
Expand Down Expand Up @@ -203,13 +202,12 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
with tarfile.open(FLAGS.output_model_tgz, "w:gz") as tar:
for model_file in glob.glob(checkpoint_file + ".*"):
tar.add(model_file, arcname=os.path.basename(model_file))
tar.add(
os.path.join(train_dir, "model_flags.json"),
arcname="model_flags.json")
tar.add(os.path.join(train_dir, "model_flags.json"),
arcname="model_flags.json")
print("Tarred model onto " + FLAGS.output_model_tgz)
with tf.device("/cpu:0"):
saver = tf.train.import_meta_graph(
meta_graph_location, clear_devices=True)
saver = tf.train.import_meta_graph(meta_graph_location,
clear_devices=True)
logging.info("restoring variables from " + checkpoint_file)
saver.restore(sess, checkpoint_file)
input_tensor = tf.get_collection("input_batch_raw")[0]
Expand Down Expand Up @@ -358,11 +356,11 @@ def main(unused_argv):
flags_dict["feature_names"], flags_dict["feature_sizes"])

if flags_dict["frame_features"]:
reader = readers.YT8MFrameFeatureReader(
feature_names=feature_names, feature_sizes=feature_sizes)
reader = readers.YT8MFrameFeatureReader(feature_names=feature_names,
feature_sizes=feature_sizes)
else:
reader = readers.YT8MAggregatedFeatureReader(
feature_names=feature_names, feature_sizes=feature_sizes)
reader = readers.YT8MAggregatedFeatureReader(feature_names=feature_names,
feature_sizes=feature_sizes)

if not FLAGS.output_file:
raise ValueError("'output_file' was not specified. "
Expand Down
Loading

0 comments on commit 98d6799

Please sign in to comment.