Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate images to cheat mlp network for mnist #6523

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion paddle/operators/cast_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ REGISTER_OP_WITH_KERNEL(cast, ops::CastOpGradMaker, ops::CastOpInferShape,
REGISTER_OP_CPU_KERNEL(cast, ops::CastOpKernel<CPU, float>,
ops::CastOpKernel<CPU, double>,
ops::CastOpKernel<CPU, int>,
ops::CastOpKernel<CPU, int64_t>);
ops::CastOpKernel<CPU, int64_t>,
ops::CastOpKernel<CPU, bool>);
3 changes: 2 additions & 1 deletion paddle/operators/cast_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ using CastOpKernel =
paddle::operators::CastOpKernel<paddle::platform::CUDADeviceContext, T>;

REGISTER_OP_CUDA_KERNEL(cast, CastOpKernel<float>, CastOpKernel<double>,
CastOpKernel<int>, CastOpKernel<int64_t>);
CastOpKernel<int>, CastOpKernel<int64_t>,
CastOpKernel<bool>);
4 changes: 4 additions & 0 deletions python/paddle/v2/fluid/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ def func(**kwargs):
_create_op_func_('mean')
_create_op_func_('mul')
_create_op_func_('elementwise_add')
_create_op_func_('elementwise_sub')
_create_op_func_('clip')
_create_op_func_('abs')
_create_op_func_('elementwise_div')
_create_op_func_('dropout')
_create_op_func_('reshape')
Expand Down Expand Up @@ -702,6 +705,7 @@ def accuracy(input, label, k=1, correct=None, total=None, **kwargs):
"Correct": [correct],
"Total": [total],
})
acc_out.stop_gradient = True
return acc_out


Expand Down
13 changes: 7 additions & 6 deletions python/paddle/v2/fluid/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def _append_optimize_op(self, block, param_and_grad):
def _create_param_lr(self, param_and_grad):
# create learning rate variable for every parameter
param = param_and_grad[0]
param_lr = param.optimize_attr['learning_rate']
param_lr = getattr(param, 'optimize_attr',
{'learning_rate': 1.0})['learning_rate']
param_lr_shape = [1]
param_lr_var = self.helper.create_global_variable(
name=unique_name("learning_rate"),
Expand Down Expand Up @@ -167,11 +168,11 @@ def create_optimization_pass(self,
[p[0] for p in parameters_and_grads])

optimize_ops = []
for param_and_grad in parameters_and_grads:
if param_and_grad[0].trainable is True and param_and_grad[
1] is not None:
optimize_op = self._append_optimize_op(loss.block,
param_and_grad)
for p, g in parameters_and_grads:
is_trainable = getattr(p, 'trainable', True)

if is_trainable is True and g is not None:
optimize_op = self._append_optimize_op(loss.block, (p, g))
optimize_ops.append(optimize_op)

# Returned list of ops can include more ops in addition
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/v2/fluid/param_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def set_default_bias_initializer(self):
def to_attr(arg):
if arg is None:
return ParamAttr()
elif isinstance(arg, list) or isinstance(arg, tuple):
return [ParamAttr.to_attr(a) for a in arg]
elif isinstance(arg, ParamAttr):
return arg
elif isinstance(arg, str) or isinstance(arg, unicode):
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/v2/fluid/regularizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def append_regularization_ops(parameters_and_grads, regularization=None):
params_and_grads = []
for param, grad in parameters_and_grads:
regularization_term = None
if param.regularizer is not None:
if hasattr(param, 'regularizer') and param.regularizer is not None:
# Add variable for regularization term in grad block
regularization_term = param.regularizer(param, grad.block)
elif regularization is not None:
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/v2/fluid/tests/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mnist
*.png
143 changes: 143 additions & 0 deletions python/paddle/v2/fluid/tests/demo/cheat_mnist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import paddle.v2 as paddle
import paddle.v2.fluid as fluid
import random
import numpy

import math
import matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


def plot(gen_data):
gen_data.resize(gen_data.shape[0], 28, 28)
n = int(math.ceil(math.sqrt(gen_data.shape[0])))
fig = plt.figure(figsize=(n, n))
gs = gridspec.GridSpec(n, n)
gs.update(wspace=0.05, hspace=0.05)

for i, sample in enumerate(gen_data):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
plt.imshow(sample.reshape(28, 28), cmap='Greys_r')

return fig


def train_net(x, label):
hidden = fluid.layers.fc(input=x,
size=200,
act='tanh',
param_attr='classification.fc1.w',
bias_attr='classification.fc1.b')
prediction = fluid.layers.fc(input=hidden,
size=10,
act='softmax',
param_attr='classification.fc2.w',
bias_attr='classification.fc2.b')
return fluid.layers.mean(x=fluid.layers.cross_entropy(
input=prediction, label=label)), prediction


def train_classification():
train_program = fluid.Program()
train_startup_program = fluid.Program()
with fluid.program_guard(train_program, train_startup_program):
x = fluid.layers.data(name='img', shape=[784], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
loss, prediction = train_net(x, label)
adam = fluid.optimizer.Adam()
adam.minimize(loss=loss)
acc = fluid.evaluator.Accuracy(input=prediction, label=label)

train_reader = paddle.batch(
paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192),
batch_size=1024)

place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(train_startup_program)
feeder = fluid.DataFeeder(feed_list=[x, label], place=place)

for pass_id in range(100):
acc.reset(exe)
for data in train_reader():
exe.run(train_program, feed=feeder.feed(data))
pass_acc = acc.eval(exe)
if pass_acc[0] > 0.95:
break

fluid.io.save_params(exe, dirname='./mnist', main_program=train_program)
print 'train mnist done'


def train_cheat_net():
cheat_init_program = fluid.Program()
data_program = fluid.Program()
with fluid.program_guard(data_program):
x = fluid.layers.data(
name='img',
shape=[1, 784],
dtype='float32',
append_batch_size=False)
x.stop_gradient = False
x.persistable = True
label = fluid.layers.data(
name='label', shape=[1, 1], dtype='int64', append_batch_size=False)
label.persistable = True

cheat_program = data_program.clone()

with fluid.program_guard(cheat_program, cheat_init_program):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to write cheat_net's x and train_net in two with statements? Seems that they are both in the cheat_program.

loss, prediction = train_net(
fluid.layers.clip(
x=x, min=-1.0, max=1.0), label)
adam = fluid.optimizer.Adam()
adam.minimize(loss=loss, parameter_list=[x.name]) # only optimize x
acc = fluid.layers.accuracy(input=prediction, label=label)

place = fluid.CPUPlace()
exe = fluid.Executor(place)

train_reader = paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192)
counter = 0
for data, label in train_reader():
new_lbl = random.randint(0, 9)
while new_lbl == label:
new_lbl = random.randint(0, 9)
fluid.io.load_params(
executor=exe, dirname='./mnist', main_program=cheat_program)
exe.run(program=data_program,
feed={
'img': numpy.array(
data, dtype='float32').reshape(1, 784),
'label': numpy.array(
[new_lbl], dtype='int64').reshape(1, 1)
}) # feed image
exe.run(cheat_init_program) # reset train parameters
acc_np = [0.]
while acc_np[0] < 0.5: # acc should be 0 or 1 since batch_size == 1.
loss_np, acc_np = exe.run(program=cheat_program,
fetch_list=[loss, acc])

generated_img = exe.run(program=data_program, fetch_list=[x])[0]
fig = plot(generated_img)
fig.savefig(
'{0}_{1}.png'.format(str(counter).zfill(6), str(new_lbl)),
bbox_inches='tight')
plt.close(fig)
counter += 1
print 'generate a fake image for label %d' % new_lbl


if __name__ == '__main__':
# the following line can be commented, if the model has been trained before
train_classification()
train_cheat_net()