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):
def image_preprocess(image, bgr=True):
if image.dtype.base_dtype != tf.float32:
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]
if bgr:
mean = mean[::-1]
std = std[::-1]
image_mean = tf.constant(mean, dtype=tf.float32)
image_std = tf.constant(std, dtype=tf.float32)
image = (image - image_mean) / image_std
return image
with tf.name_scope('image_preprocess'):
if image.dtype.base_dtype != tf.float32:
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]
if bgr:
mean = mean[::-1]
std = std[::-1]
image_mean = tf.constant(mean, dtype=tf.float32)
image_std = tf.constant(std, dtype=tf.float32)
image = (image - image_mean) / image_std
return image
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')
def prediction_incorrect(logits, label, topk=1, name='incorrect_vector'):
return tf.cast(tf.logical_not(tf.nn.in_top_k(logits, label, topk)),
tf.float32, name=name)
with tf.name_scope('prediction_incorrect'):
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')
add_moving_summary(tf.reduce_mean(wrong, name='train-error-top1'))
......
......@@ -46,14 +46,15 @@ def regularize_cost(regex, func, name='regularize_cost'):
# If vars are replicated, only regularize those in the current tower
params = ctx.filter_vars_by_vs_name(params)
costs = []
for p in params:
para_name = p.name
if re.search(regex, para_name):
costs.append(func(p))
_log_regularizer(para_name)
if not costs:
return tf.constant(0, dtype=tf.float32, name='empty_' + name)
with tf.name_scope('regularize_cost'):
costs = []
for p in params:
para_name = p.name
if re.search(regex, para_name):
costs.append(func(p))
_log_regularizer(para_name)
if not costs:
return tf.constant(0, dtype=tf.float32, name='empty_' + 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