Commit 79dbd183 authored by Yuxin Wu's avatar Yuxin Wu

name change

parent 1908fbe7
...@@ -12,8 +12,8 @@ You can actually train them and reproduce the performance... not just to see how ...@@ -12,8 +12,8 @@ You can actually train them and reproduce the performance... not just to see how
+ [Fully-convolutional Network for Holistically-Nested Edge Detection](examples/HED) + [Fully-convolutional Network for Holistically-Nested Edge Detection](examples/HED)
+ [Spatial Transformer Networks on MNIST addition](examples/SpatialTransformer) + [Spatial Transformer Networks on MNIST addition](examples/SpatialTransformer)
+ [Generative Adversarial Networks](examples/GAN) + [Generative Adversarial Networks](examples/GAN)
+ [Double DQN plays Atari games](examples/Atari2600) + [DQN variants on Atari games](examples/Atari2600)
+ [Asynchronous Advantage Actor-Critic(A3C) with demos on OpenAI Gym Atari games](examples/OpenAIGym) + [Asynchronous Advantage Actor-Critic(A3C) with demos on OpenAI Gym](examples/OpenAIGym)
+ [char-rnn language model](examples/char-rnn) + [char-rnn language model](examples/char-rnn)
## Features: ## Features:
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File: celebA.py # File: DCGAN-CelebA.py
# Author: Yuxin Wu <ppwwyyxxc@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import numpy as np import numpy as np
...@@ -68,8 +68,9 @@ class Model(ModelDesc): ...@@ -68,8 +68,9 @@ class Model(ModelDesc):
def _build_graph(self, input_vars): def _build_graph(self, input_vars):
image_pos = input_vars[0] image_pos = input_vars[0]
image_pos = image_pos / 128.0 - 1 image_pos = image_pos / 128.0 - 1
z = tf.random_uniform(tf.pack([tf.shape(image_pos)[0], 100]), -1, 1, name='z')
z.set_shape([None, 100]) # issue#5680 z = tf.random_uniform([BATCH, 100], -1, 1, name='z_train')
z = tf.placeholder_with_default(z, [None, 100], name='z')
with argscope([Conv2D, Deconv2D, FullyConnected], with argscope([Conv2D, Deconv2D, FullyConnected],
W_init=tf.truncated_normal_initializer(stddev=0.02)): W_init=tf.truncated_normal_initializer(stddev=0.02)):
...@@ -110,7 +111,7 @@ def get_config(): ...@@ -110,7 +111,7 @@ def get_config():
session_config=get_default_sess_config(0.5), session_config=get_default_sess_config(0.5),
model=Model(), model=Model(),
step_per_epoch=300, step_per_epoch=300,
max_epoch=500, max_epoch=300,
) )
def sample(model_path): def sample(model_path):
...@@ -119,7 +120,7 @@ def sample(model_path): ...@@ -119,7 +120,7 @@ def sample(model_path):
model=Model(), model=Model(),
input_names=['z'], input_names=['z'],
output_names=['gen/gen']) output_names=['gen/gen'])
pred = SimpleDatasetPredictor(pred, RandomZData((128, 100))) pred = SimpleDatasetPredictor(pred, RandomZData((100, 100)))
for o in pred.get_result(): for o in pred.get_result():
o = o[0] + 1 o = o[0] + 1
o = o * 128.0 o = o * 128.0
...@@ -141,6 +142,7 @@ if __name__ == '__main__': ...@@ -141,6 +142,7 @@ if __name__ == '__main__':
if args.sample: if args.sample:
sample(args.load) sample(args.load)
else: else:
assert args.data
config = get_config() config = get_config()
if args.load: if args.load:
config.session_init = SaverRestore(args.load) config.session_init = SaverRestore(args.load)
......
...@@ -70,7 +70,7 @@ class Model(ModelDesc): ...@@ -70,7 +70,7 @@ class Model(ModelDesc):
costs = [] costs = []
for idx, b in enumerate([b1, b2, b3, b4, b5, final_map]): for idx, b in enumerate([b1, b2, b3, b4, b5, final_map]):
output = tf.nn.sigmoid(b, name='output{}'.format(idx+1)) output = tf.nn.sigmoid(b, name='output{}'.format(idx+1))
xentropy = class_balanced_sigmoid_binary_class_cross_entropy( xentropy = class_balanced_sigmoid_cross_entropy(
b, edgemap, b, edgemap,
name='xentropy{}'.format(idx+1)) name='xentropy{}'.format(idx+1))
costs.append(xentropy) costs.append(xentropy)
......
...@@ -124,6 +124,7 @@ def get_data(train_or_test): ...@@ -124,6 +124,7 @@ def get_data(train_or_test):
pp_mean = meta.get_per_pixel_mean() pp_mean = meta.get_per_pixel_mean()
if isTrain: if isTrain:
# TODO use the augmentor in GoogleNet
augmentors = [ augmentors = [
imgaug.Resize((256, 256)), imgaug.Resize((256, 256)),
imgaug.Brightness(30, False), imgaug.Brightness(30, False),
......
...@@ -40,8 +40,8 @@ def regularize_cost(regex, func, name=None): ...@@ -40,8 +40,8 @@ def regularize_cost(regex, func, name=None):
@layer_register(log_shape=False) @layer_register(log_shape=False)
def Dropout(x, prob=0.5): def Dropout(x, keep_prob=0.5):
is_training = get_current_tower_context().is_training is_training = get_current_tower_context().is_training
keep_prob = tf.constant(prob if is_training else 1.0) keep_prob = tf.constant(keep_prob if is_training else 1.0)
return tf.nn.dropout(x, keep_prob) return tf.nn.dropout(x, keep_prob)
...@@ -27,18 +27,18 @@ def batch_flatten(x): ...@@ -27,18 +27,18 @@ def batch_flatten(x):
""" """
shape = x.get_shape().as_list()[1:] shape = x.get_shape().as_list()[1:]
if None not in shape: if None not in shape:
return tf.reshape(x, [-1, np.prod(shape)]) return tf.reshape(x, [-1, int(np.prod(shape))])
return tf.reshape(x, tf.pack([tf.shape(x)[0], -1])) return tf.reshape(x, tf.pack([tf.shape(x)[0], -1]))
def class_balanced_binary_class_cross_entropy(pred, label, name='cross_entropy_loss'): def class_balanced_cross_entropy(pred, label, name='cross_entropy_loss'):
""" """
The class-balanced cross entropy loss for binary classification, The class-balanced cross entropy loss,
as in `Holistically-Nested Edge Detection as in `Holistically-Nested Edge Detection
<http://arxiv.org/abs/1504.06375>`_. <http://arxiv.org/abs/1504.06375>`_.
:param pred: size: b x ANYTHING. the predictions in [0,1]. :param pred: size: b x ANYTHING. the predictions in [0,1].
:param label: size: b x ANYTHING. the ground truth in {0,1}. :param label: size: b x ANYTHING. the ground truth in {0,1}.
:returns: class-balanced binary classification cross entropy loss :returns: class-balanced cross entropy loss
""" """
z = batch_flatten(pred) z = batch_flatten(pred)
y = tf.cast(batch_flatten(label), tf.float32) y = tf.cast(batch_flatten(label), tf.float32)
...@@ -53,30 +53,34 @@ def class_balanced_binary_class_cross_entropy(pred, label, name='cross_entropy_l ...@@ -53,30 +53,34 @@ def class_balanced_binary_class_cross_entropy(pred, label, name='cross_entropy_l
cost = tf.sub(loss_pos, loss_neg, name=name) cost = tf.sub(loss_pos, loss_neg, name=name)
return cost return cost
def class_balanced_sigmoid_binary_class_cross_entropy(pred, label, name='cross_entropy_loss'): def class_balanced_sigmoid_cross_entropy(logits, label, name='cross_entropy_loss'):
""" """
The class-balanced cross entropy loss for binary classification, The class-balanced cross entropy loss,
as in `Holistically-Nested Edge Detection as in `Holistically-Nested Edge Detection
<http://arxiv.org/abs/1504.06375>`_. <http://arxiv.org/abs/1504.06375>`_.
This is more numerically stable than class_balanced_cross_entropy
:param pred: size: b x ANYTHING. the logits. :param logits: size: the logits.
:param label: size: b x ANYTHING. the ground truth in {0,1}. :param label: size: the ground truth in {0,1}, of the same shape as logits.
:returns: class-balanced binary classification cross entropy loss :returns: a scalar. class-balanced cross entropy loss
""" """
z = batch_flatten(pred) z = batch_flatten(logits)
y = tf.cast(batch_flatten(label), tf.float32) y = tf.cast(batch_flatten(label), tf.float32)
count_neg = tf.reduce_sum(1. - y) count_neg = tf.reduce_sum(1. - y)
count_pos = tf.reduce_sum(y) count_pos = tf.reduce_sum(y)
beta = count_neg / (count_neg + count_pos) beta = count_neg / (count_neg + count_pos)
#eps = 1e-12 pos_weight = beta / (1 - beta)
logstable = tf.log(1 + tf.exp(-tf.abs(z))) cost = tf.nn.weighted_cross_entropy_with_logits(z, y, pos_weight)
loss_pos = -beta * tf.reduce_mean(-y * cost = tf.reduce_mean(cost * (1 - beta), name=name)
(logstable - tf.minimum(0.0, z)))
loss_neg = (1. - beta) * tf.reduce_mean((y - 1.) * #logstable = tf.log(1 + tf.exp(-tf.abs(z)))
(logstable + tf.maximum(z, 0.0))) #loss_pos = -beta * tf.reduce_mean(-y *
cost = tf.sub(loss_pos, loss_neg, name=name) #(logstable - tf.minimum(0.0, z)))
#loss_neg = (1. - beta) * tf.reduce_mean((y - 1.) *
#(logstable + tf.maximum(z, 0.0)))
#cost = tf.sub(loss_pos, loss_neg, name=name)
return cost return cost
def print_stat(x, message=None): def print_stat(x, message=None):
......
...@@ -98,7 +98,20 @@ def build_patch_list(patch_list, ...@@ -98,7 +98,20 @@ def build_patch_list(patch_list,
def dump_dataflow_images(df, index=0, batched=True, def dump_dataflow_images(df, index=0, batched=True,
number=300, output_dir=None, number=300, output_dir=None,
scale=1, resize=None, viz=None, flipRGB=False, exit_after=True): scale=1, resize=None, viz=None,
flipRGB=False, exit_after=True):
"""
:param df: a DataFlow
:param index: the index of the image component
:param batched: whether the component contains batched images or not
:param number: how many datapoint to take from the DataFlow
:param output_dir: output directory to save images, default to not save.
:param scale: scale the value, usually either 1 or 255
:param resize: (h, w) or Nne, resize the images
:param viz: (h, w) or None, visualize the images in grid with imshow
:param flipRGB: apply a RGB<->BGR conversion or not
:param exit_after: exit the process after this function
"""
if output_dir: if output_dir:
mkdir_p(output_dir) mkdir_p(output_dir)
if viz is not None: if viz is not None:
......
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