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

Update MXNet example in README.md #872

Merged
merged 1 commit into from Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions README.md
Expand Up @@ -258,15 +258,12 @@ loss_fn = ...
for epoch in range(num_epoch):
train_data.reset()
for nbatch, batch in enumerate(train_data, start=1):
data = gluon.utils.split_and_load(batch.data[0], ctx_list=[context],
batch_axis=0)
label = gluon.utils.split_and_load(batch.label[0], ctx_list=[context],
batch_axis=0)
data = batch.data[0].as_in_context(context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please apply the same change to the imagenet example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do

label = batch.label[0].as_in_context(context)
with autograd.record():
outputs = [model(x.astype(dtype, copy=False)) for x in data]
loss = [loss_fn(yhat, y) for yhat, y in zip(outputs, label)]
for l in loss:
l.backward()
output = model(data.astype(dtype, copy=False))
loss = loss_fn(output, label)
loss.backward()
trainer.step(batch_size)
```

Expand Down
25 changes: 11 additions & 14 deletions examples/mxnet_imagenet_resnet50.py
Expand Up @@ -150,10 +150,8 @@ def get_data_rec(rec_train, rec_train_idx, rec_val, rec_val_idx, batch_size,
mean_rgb = [123.68, 116.779, 103.939]

def batch_fn(batch, ctx):
data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx,
batch_axis=0)
label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx,
batch_axis=0)
data = batch.data[0].as_in_context(ctx)
label = batch.label[0].as_in_context(ctx)
return data, label

train_data = mx.io.ImageRecordIter(
Expand Down Expand Up @@ -303,10 +301,10 @@ def evaluate(epoch):
acc_top1 = mx.metric.Accuracy()
acc_top5 = mx.metric.TopKAccuracy(5)
for _, batch in enumerate(val_data):
data, label = batch_fn(batch, [context])
outputs = [net(x.astype(args.dtype, copy=False)) for x in data]
acc_top1.update(label, outputs)
acc_top5.update(label, outputs)
data, label = batch_fn(batch, context)
output = net(data.astype(args.dtype, copy=False))
acc_top1.update([label], [output])
acc_top5.update([label], [output])

top1_name, top1_acc = acc_top1.get()
top5_name, top5_acc = acc_top5.get()
Expand Down Expand Up @@ -336,15 +334,14 @@ def evaluate(epoch):

btic = time.time()
for nbatch, batch in enumerate(train_data, start=1):
data, label = batch_fn(batch, [context])
data, label = batch_fn(batch, context)
with autograd.record():
outputs = [net(x.astype(args.dtype, copy=False)) for x in data]
loss = [loss_fn(yhat, y) for yhat, y in zip(outputs, label)]
for l in loss:
l.backward()
output = net(data.astype(args.dtype, copy=False))
loss = loss_fn(output, label)
loss.backward()
trainer.step(batch_size)

metric.update(label, outputs)
metric.update([label], [output])
if args.log_interval and nbatch % args.log_interval == 0:
name, acc = metric.get()
logging.info('Epoch[%d] Rank[%d] Batch[%d]\t%s=%f\tlr=%f',
Expand Down