Commit 83c3a098 authored by Yuxin Wu's avatar Yuxin Wu

name scope clean-ups in regularization and resnet (#340)

parent 979d18ca
...@@ -157,19 +157,20 @@ def eval_on_ILSVRC12(model, model_file, dataflow): ...@@ -157,19 +157,20 @@ def eval_on_ILSVRC12(model, model_file, dataflow):
def image_preprocess(image, bgr=True): def image_preprocess(image, bgr=True):
if image.dtype.base_dtype != tf.float32: with tf.name_scope('image_preprocess'):
image = tf.cast(image, tf.float32) if image.dtype.base_dtype != tf.float32:
image = image * (1.0 / 255) image = tf.cast(image, tf.float32)
image = image * (1.0 / 255)
mean = [0.485, 0.456, 0.406] # rgb
std = [0.229, 0.224, 0.225] mean = [0.485, 0.456, 0.406] # rgb
if bgr: std = [0.229, 0.224, 0.225]
mean = mean[::-1] if bgr:
std = std[::-1] mean = mean[::-1]
image_mean = tf.constant(mean, dtype=tf.float32) std = std[::-1]
image_std = tf.constant(std, dtype=tf.float32) image_mean = tf.constant(mean, dtype=tf.float32)
image = (image - image_mean) / image_std image_std = tf.constant(std, dtype=tf.float32)
return image image = (image - image_mean) / image_std
return image
def compute_loss_and_error(logits, label): def compute_loss_and_error(logits, label):
...@@ -177,8 +178,9 @@ def compute_loss_and_error(logits, label): ...@@ -177,8 +178,9 @@ def compute_loss_and_error(logits, label):
loss = tf.reduce_mean(loss, name='xentropy-loss') loss = tf.reduce_mean(loss, name='xentropy-loss')
def prediction_incorrect(logits, label, topk=1, name='incorrect_vector'): def prediction_incorrect(logits, label, topk=1, name='incorrect_vector'):
return tf.cast(tf.logical_not(tf.nn.in_top_k(logits, label, topk)), with tf.name_scope('prediction_incorrect'):
tf.float32, name=name) x = tf.logical_not(tf.nn.in_top_k(logits, label, topk))
return tf.cast(x, tf.float32, name=name)
wrong = prediction_incorrect(logits, label, 1, name='wrong-top1') wrong = prediction_incorrect(logits, label, 1, name='wrong-top1')
add_moving_summary(tf.reduce_mean(wrong, name='train-error-top1')) add_moving_summary(tf.reduce_mean(wrong, name='train-error-top1'))
......
...@@ -46,14 +46,15 @@ def regularize_cost(regex, func, name='regularize_cost'): ...@@ -46,14 +46,15 @@ def regularize_cost(regex, func, name='regularize_cost'):
# If vars are replicated, only regularize those in the current tower # If vars are replicated, only regularize those in the current tower
params = ctx.filter_vars_by_vs_name(params) params = ctx.filter_vars_by_vs_name(params)
costs = [] with tf.name_scope('regularize_cost'):
for p in params: costs = []
para_name = p.name for p in params:
if re.search(regex, para_name): para_name = p.name
costs.append(func(p)) if re.search(regex, para_name):
_log_regularizer(para_name) costs.append(func(p))
if not costs: _log_regularizer(para_name)
return tf.constant(0, dtype=tf.float32, name='empty_' + name) if not costs:
return tf.constant(0, dtype=tf.float32, name='empty_' + name)
return tf.add_n(costs, name=name) return tf.add_n(costs, name=name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment