szha commented on a change in pull request #9633: Gluon image-classification
example improvement
URL: https://github.com/apache/incubator-mxnet/pull/9633#discussion_r164932765
##########
File path: example/gluon/image_classification.py
##########
@@ -160,23 +207,34 @@ def train(epochs, ctx):
# on all GPUs for better speed on multiple GPUs.
Ls.append(L)
outputs.append(z)
- for L in Ls:
- L.backward()
+ ag.backward(Ls)
trainer.step(batch.data[0].shape[0])
metric.update(label, outputs)
if opt.log_interval and not (i+1)%opt.log_interval:
name, acc = metric.get()
- logging.info('Epoch[%d] Batch [%d]\tSpeed: %f
samples/sec\t%s=%f'%(
- epoch, i, batch_size/(time.time()-btic), name,
acc))
+ msg = ','.join(['%s=%f'%(n, a) for n, a in zip(as_list(name),
as_list(acc))])
+ logger.info('Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s'%(
+ epoch, i, batch_size/(time.time()-btic), msg))
btic = time.time()
name, acc = metric.get()
- logging.info('[Epoch %d] training: %s=%f'%(epoch, name, acc))
- logging.info('[Epoch %d] time cost: %f'%(epoch, time.time()-tic))
- name, val_acc = test(ctx, val_data)
- logging.info('[Epoch %d] validation: %s=%f'%(epoch, name, val_acc))
+ msg = ','.join(['%s=%f'%(n, a) for n, a in zip(as_list(name),
as_list(acc))])
+ logger.info('[Epoch %d] training: %s'%(epoch, msg))
+ logger.info('[Epoch %d] time cost: %f'%(epoch, time.time()-tic))
+ name, val_acc = test(ctx, metric, val_data)
+ val_msg = ','.join(['%s=%f'%(n, a) for n, a in zip(as_list(name),
as_list(val_acc))])
+ logger.info('[Epoch %d] validation: %s'%(epoch, val_msg))
+ top1 = val_acc[0] if isinstance(val_acc, list) else val_acc
- net.save_params('image-classifier-%s-%d.params'%(opt.model, epochs))
+ if opt.save_frequency and (epoch + 1) % opt.save_frequency == 0:
+ fname = os.path.join(opt.prefix, '%s_%d_acc_%.4f.params' %
(opt.model, epoch, top1))
+ net.save_params(fname)
+ logger.info('[Epoch %d] Saving checkpoint to %s with Accuracy:
%.4f', epoch, fname, top1)
+ if top1 > best_acc:
+ best_acc = top1
+ fname = os.path.join(opt.prefix, '%s_best.params' % (opt.model))
+ net.save_params(fname)
+ logger.info('[Epoch %d] Saving checkpoint to %s with Accuracy:
%.4f', epoch, fname, top1)
Review comment:
consider extracting these into a checkpoint function, so that `train`
function contains only the high-level, readable code.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services