Skip to content
Open
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
64 changes: 33 additions & 31 deletions fluid/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,37 +141,39 @@ def run_benchmark(model, args):
train_reader = paddle.batch(
paddle.dataset.mnist.train(), batch_size=args.batch_size)

for pass_id in range(args.pass_num):
accuracy.reset(exe)
pass_start = time.time()
for batch_id, data in enumerate(train_reader()):
img_data = np.array(
map(lambda x: x[0].reshape([1, 28, 28]), data)).astype(DTYPE)
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
y_data = y_data.reshape([len(y_data), 1])

start = time.time()
outs = exe.run(
fluid.default_main_program(),
feed={"pixel": img_data,
"label": y_data},
fetch_list=[avg_cost] + accuracy.metrics
) # The accuracy is the accumulation of batches, but not the current batch.

end = time.time()
loss = np.array(outs[0])
acc = np.array(outs[1])
print("pass=%d, batch=%d, loss=%f, error=%f, elapse=%f" %
(pass_id, batch_id, loss, 1 - acc, (end - start) / 1000))

pass_end = time.time()

train_avg_acc = accuracy.eval(exe)
test_avg_acc = eval_test(exe, accuracy, inference_program)

print("pass=%d, train_avg_acc=%f, test_avg_acc=%f, elapse=%f" %
(pass_id, train_avg_acc, test_avg_acc,
(pass_end - pass_start) / 1000))
with profiler.profiler(args.device, 'total') as prof:
for pass_id in range(args.pass_num):
accuracy.reset(exe)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fluid.evaluator 下无 Accuracy,请针对本文件所有相应地方做相应修改。
以及前面的 paddle.v2.fluid -> paddle.fluid

pass_start = time.time()
for batch_id, data in enumerate(train_reader()):
img_data = np.array(
map(lambda x: x[0].reshape([1, 28, 28]), data)).astype(
DTYPE)
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
y_data = y_data.reshape([len(y_data), 1])

start = time.time()
outs = exe.run(
fluid.default_main_program(),
feed={"pixel": img_data,
"label": y_data},
fetch_list=[avg_cost] + accuracy.metrics
) # The accuracy is the accumulation of batches, but not the current batch.

end = time.time()
loss = np.array(outs[0])
acc = np.array(outs[1])
print("pass=%d, batch=%d, loss=%f, error=%f, elapse=%f" %
(pass_id, batch_id, loss, 1 - acc, (end - start) / 1000))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

计时建议带上单位,以及 这里是要以 ks 为单位吗?
(下同)


pass_end = time.time()

train_avg_acc = accuracy.eval(exe)
test_avg_acc = eval_test(exe, accuracy, inference_program)

print("pass=%d, train_avg_acc=%f, test_avg_acc=%f, elapse=%f" %
(pass_id, train_avg_acc, test_avg_acc,
(pass_end - pass_start) / 1000))


if __name__ == '__main__':
Expand Down
11 changes: 6 additions & 5 deletions fluid/resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def run_benchmark(model, args):
opts = optimizer.minimize(avg_cost)
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fluid.evaluator 下无 Accuracy,请针对本文件所有相应地方做相应修改。
以及前面的 paddle.v2.fluid -> paddle.fluid


fluid.memory_optimize(fluid.default_main_program())
# fluid.memory_optimize(fluid.default_main_program())

train_reader = paddle.batch(
paddle.reader.shuffle(
Expand Down Expand Up @@ -230,10 +230,11 @@ def run_benchmark(model, args):
data)).astype('float32')
label = np.array(map(lambda x: x[1], data)).astype('int64')
label = label.reshape([-1, 1])
loss, acc = exe.run(fluid.default_main_program(),
feed={'data': image,
'label': label},
fetch_list=[avg_cost] + accuracy.metrics)
with profiler.profiler(args.device, 'total') as prof:
loss, acc = exe.run(fluid.default_main_program(),
feed={'data': image,
'label': label},
fetch_list=[avg_cost] + accuracy.metrics)
every_pass_acc.append(acc)
every_pass_loss.append(loss)
pass_acc = accuracy.eval(exe)
Expand Down
39 changes: 22 additions & 17 deletions fluid/vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import argparse
import functools

import paddle.v2.fluid.profiler as profiler
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

paddle.fluid ,前几行也须进行相应修改。


parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--batch_size', type=int, default=128, help="Batch size for training.")
Expand All @@ -18,7 +20,7 @@
type=float,
default=1e-3,
help="Learning rate for training.")
parser.add_argument('--num_passes', type=int, default=50, help="No. of passes.")
parser.add_argument('--pass_num', type=int, default=50, help="No. of passes.")
parser.add_argument(
'--device',
type=str,
Expand Down Expand Up @@ -52,6 +54,7 @@ def conv_block(input, num_filter, groups, dropouts):
conv_with_batchnorm=True,
conv_batchnorm_drop_rate=dropouts,
pool_type='max')
# use_cudnn=False)

conv1 = conv_block(input, 64, 2, [0.3, 0])
conv2 = conv_block(conv1, 128, 2, [0.4, 0])
Expand Down Expand Up @@ -94,23 +97,24 @@ def main():
# Evaluator
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fluid.evaluator 下无 Accuracy,请针对本文件所有相应地方做相应修改。


# inference program
inference_program = fluid.default_main_program().clone()
with fluid.program_guard(inference_program):
test_target = accuracy.metrics + accuracy.states
inference_program = fluid.io.get_inference_program(test_target)
# # inference program
# inference_program = fluid.default_main_program().clone()
# with fluid.program_guard(inference_program):
# test_target = accuracy.metrics + accuracy.states
# inference_program = fluid.io.get_inference_program(test_target)

# Optimization
optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate)
opts = optimizer.minimize(avg_cost)

fluid.memory_optimize(fluid.default_main_program())
# fluid.memory_optimize(fluid.default_main_program())

# Initialize executor
place = core.CPUPlace() if args.device == 'CPU' else core.CUDAPlace(0)
exe = fluid.Executor(place)

# Parameter initialization
# with profiler.profiler(args.device, 'total') as prof:
exe.run(fluid.default_startup_program())

# data reader
Expand Down Expand Up @@ -141,7 +145,7 @@ def test(exe):
return accuracy.eval(exe)

iters = 0
for pass_id in range(args.num_passes):
for pass_id in range(args.pass_num):
# train
start_time = time.time()
num_samples = 0
Expand All @@ -152,10 +156,11 @@ def test(exe):
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
y_data = y_data.reshape([-1, 1])

loss, acc = exe.run(fluid.default_main_program(),
feed={"pixel": img_data,
"label": y_data},
fetch_list=[avg_cost] + accuracy.metrics)
with profiler.profiler(args.device, 'total') as prof:
loss, acc = exe.run(fluid.default_main_program(),
feed={"pixel": img_data,
"label": y_data},
fetch_list=[avg_cost] + accuracy.metrics)
iters += 1
num_samples += len(data)
print(
Expand All @@ -165,11 +170,11 @@ def test(exe):

pass_elapsed = time.time() - start_time
pass_train_acc = accuracy.eval(exe)
pass_test_acc = test(exe)
print(
"Pass = %d, Training performance = %f imgs/s, Train accuracy = %f, Test accuracy = %f\n"
% (pass_id, num_samples / pass_elapsed, pass_train_acc,
pass_test_acc))
# pass_test_acc = test(exe)
# print(
# "Pass = %d, Training performance = %f imgs/s, Train accuracy = %f, Test accuracy = %f\n"
# % (pass_id, num_samples / pass_elapsed, pass_train_acc,
# pass_test_acc))


def print_arguments():
Expand Down