Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNet-1334][Fit API]base class for estimator and eventhandler #14346

Merged
merged 7 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
67 changes: 67 additions & 0 deletions example/gluon/estimator_example/mnist_cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# coding: utf-8
# pylint: disable=wildcard-import
"""Gluon Estimator example on MNIST dataset with simple CNN"""

import os
import sys

from mxnet import metric
from mxnet import gluon
from mxnet.gluon import nn, data
from mxnet.gluon.estimator import estimator

net = nn.Sequential()

net.add(nn.Conv2D(32, kernel_size=3, activation='relu'),
nn.Conv2D(64, kernel_size=3, activation='relu'),
nn.MaxPool2D(pool_size=2),
nn.Dropout(0.25),
nn.Flatten(),
nn.Dense(128, activation="relu"), nn.Dropout(0.5),
nn.Dropout(0.5),
nn.Dense(10))


def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(
'~', '.mxnet', 'datasets', 'fashion-mnist')):
root = os.path.expanduser(root) # Expand the user path '~'.
transformer = []
if resize:
transformer += [data.vision.transforms.Resize(resize)]
transformer += [data.vision.transforms.ToTensor()]
transformer = data.vision.transforms.Compose(transformer)
mnist_train = data.vision.MNIST(root=root, train=True)
roywei marked this conversation as resolved.
Show resolved Hide resolved
mnist_test = data.vision.MNIST(root=root, train=False)
num_workers = 0 if sys.platform.startswith('win32') else 4
train_iter = data.DataLoader(
roywei marked this conversation as resolved.
Show resolved Hide resolved
mnist_train.transform_first(transformer), batch_size, shuffle=True,
num_workers=num_workers)
test_iter = data.DataLoader(
roywei marked this conversation as resolved.
Show resolved Hide resolved
mnist_test.transform_first(transformer), batch_size, shuffle=False,
num_workers=num_workers)
return train_iter, test_iter


batch_size = 128
train_data, test_data = load_data_fashion_mnist(batch_size, resize=28)
loss = gluon.loss.SoftmaxCrossEntropyLoss()
acc = metric.Accuracy()
est = estimator.Estimator(net=net, loss=loss, metrics=acc)
est.fit(train_data=train_data, epochs=5)
21 changes: 21 additions & 0 deletions python/mxnet/gluon/estimator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# pylint: disable=wildcard-import
"""Gluon Estimator Module"""
from .estimator import *
from .event_handler import *
203 changes: 203 additions & 0 deletions python/mxnet/gluon/estimator/estimator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# coding: utf-8
# pylint: disable=wildcard-import
"""Gluon Estimator"""


import warnings

from .event_handler import LoggingHandler
from ... import *
from ... import gluon, autograd
from ...context import cpu, gpu, num_gpus
from ...metric import EvalMetric, Loss

__all__ = ['Estimator']


class Estimator(object):
"""
Estimator Class for easy model training
TODO: update doc
"""

def __init__(self, net,
loss=None,
metrics=None,
initializer=None,
trainers=None,
context=None):

self.net = net
if isinstance(loss, gluon.loss.Loss):
self.loss = [loss]
else:
self.loss = loss or []
if isinstance(metrics, EvalMetric):
Copy link
Member

Choose a reason for hiding this comment

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

this does not work for a list of metrics?

>>> class A:
...     def __init(self):
...             pass
...
>>> A
<class __main__.A at 0x721d0fe88>
>>> x = A
>>> x = [A(), A()]
>>> isinstance(x, A)
False

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it accepts single metric object or list of metrics(in the else part)

Copy link
Member

Choose a reason for hiding this comment

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

regardless of whether a single metric or a list of them are passed, we should have this validation

Copy link
Member Author

Choose a reason for hiding this comment

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

@nswamy updated

self.metrics = [metrics]
else:
self.metrics = metrics or []
Copy link
Member

Choose a reason for hiding this comment

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

do we allow anything but EvalMetric, if not please validate each metric in the list is a EvalMetric.


self.initializer = initializer
# store training statistics
self.train_stats = {}
Copy link
Member

Choose a reason for hiding this comment

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

can you want to pull initializing stats into a small helper method ? _init_stats or something

Copy link
Member Author

Choose a reason for hiding this comment

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

going to put train_stats as an object and add related methods in a follow up PR.

self.train_stats['epochs'] = []
self.train_stats['learning_rate'] = []
# time used for each epoch
self.train_stats['step'] = ''
roywei marked this conversation as resolved.
Show resolved Hide resolved
for metric in self.metrics:
# record a history of metrics over each epoch
self.train_stats['train_' + metric.name] = []
# only record the latest metric numbers after each batch
self.train_stats['batch_' + metric.name] = 0.
self.loss_metrics = []
# using the metric wrapper for loss to record loss value
for loss in self.loss:
self.loss_metrics.append(Loss(loss.name))
self.train_stats['train_' + loss.name] = []
# only record the latest loss numbers after each batch
self.train_stats['batch_' + loss.name] = 0.

# handle context
if isinstance(context, Context):
self.context = [context]
if not context:
if num_gpus() > 0:
# only use 1 GPU by default
if num_gpus() > 1:
warnings.warn("You have multiple GPUs, gpu(0) will be used by default."
Copy link
Member

Choose a reason for hiding this comment

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

we should use all the GPUs on the system?

Copy link
Member Author

Choose a reason for hiding this comment

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

This behavior is borrowed from keras, if no context specified, use 1 gpu by default. Since for beginner use cases, running small dataset over multiple GPU will cause communication overhead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can using the number of gpus be a parameter that the user can pass to Estimator ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@piyushghai Agree that using gpus will be simpler. But the context arg is exposed many places in MXNet/Gluon, from inference to traninig. right now there is no good way to only allow gpu as there are too many api accepting context argument. User may get confused and error if we have both gpus and context.
e.g.

net.initialize(..., ctx=[mx.gpu(0)])
est = Estimator( net, ..., gpus=2)

"To utilize all your GPUs, specify context as a list of gpus, e.g. context=[mx.gpu(0), mx.gpu(2)] ")
self.context = [gpu(0)]
else:
self.context = [cpu()]

# initialize the network
if self.initializer:
Copy link
Member

Choose a reason for hiding this comment

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

suggest to move the initializing and checking of initializing into one method.

Copy link
Member

Choose a reason for hiding this comment

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

?

if self._is_initialized():
# if already initialized, re-init with user specified initializer
warnings.warn("You have already initialized your net, it will be forced re-initialized "
roywei marked this conversation as resolved.
Show resolved Hide resolved
"with the initializer you speficied. You don't need to pass initializer if you alraedy initialized your net.")
self.net.initialize(init=self.initializer, ctx=self.context, force_reinit=True)
else:
# initialize with user specified initializer
self.net.initialize(init=self.initializer, ctx=self.context, force_reinit=False)
else:
if not self._is_initialized():
self.net.initialize(ctx=self.context)

# handle trainers
if isinstance(trainers, gluon.Trainer):
self.trainers = [trainers]
else:
self.trainers = trainers or []
if not self.trainers:
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we dealing with multiple trainer case over here (e.g. Multi task classification)

Copy link
Member Author

Choose a reason for hiding this comment

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

will be addressed in another PR (TODO item 4)

warnings.warn("No trainer specified, default SGD optimizer with learning rate 0.001 is used.")
self.trainers = [gluon.Trainer(self.net.collect_params(), 'sgd', {'learning_rate': 0.001})]

def _is_initialized(self):
param_dict = self.net.collect_params()
for param in param_dict:
try:
param_dict[param].list_ctx()
except RuntimeError:
return False
return True

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

def fit(self, train_data,
Copy link
Member

Choose a reason for hiding this comment

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

nit: train_data-> train_dataloader, ..
if valid_data is not passed are you going to split the train data with some percentage. if not i think we should

Copy link
Member Author

Choose a reason for hiding this comment

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

currently dataloader does not support split into train and val after it's created. User can create that split outside estimator using a random sampler . Same behavior with pytorch . Keras allows this but only because the fit method is accepting numpy arrays directly and it can be slow.

Copy link
Member

Choose a reason for hiding this comment

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

val_data is missing in the signature? Also it should not be optional if you expect user to pass?

Copy link
Member

Choose a reason for hiding this comment

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

please rename to train_dataloader

val_data=None,
epochs=1,
batch_size=None,
event_handlers=None):

if not batch_size:
Copy link
Member

Choose a reason for hiding this comment

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

log.info( with the batch size you are using).

Copy link
Member Author

Choose a reason for hiding this comment

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

will be added in train_begin in loggin handler with the change on train_stats in a follow up PR

batch_size = 32 * len(self.context)

event_handlers = event_handlers or []
# provide default logging handler
if not event_handlers or not any(isinstance(handler, LoggingHandler) for handler in event_handlers):
event_handlers.append(LoggingHandler(self))

# TODO: handle validation logic and update train stats
do_validation = False
if val_data:
do_validation = True

# training begin
for handler in event_handlers:
handler.train_begin()

for epoch in range(epochs):
# epoch begin
self.train_stats["epochs"].append(epoch)
self.train_stats["learning_rate"].append(self.trainers[0].learning_rate)

for handler in event_handlers:
handler.epoch_begin()

for metric in self.metrics + self.loss_metrics:
metric.reset()

for i, batch in enumerate(train_data):
data, label = self._batch_fn(batch, self.context)
Copy link
Member

Choose a reason for hiding this comment

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

Hi @roywei, in NVIDIA DALI we return already sliced data, could you make _batch_fn optional?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @ptrendx sounds good!any examples on multi-gpu? would love to check it out.


# batch begin
for handler in event_handlers:
handler.batch_begin()

with autograd.record():
pred = [self.net(x) for x in data]
losses = []
for loss in self.loss:
losses.append([loss(y_hat, y) for y_hat, y in zip(pred, label)])

for loss in losses:
for l in loss:
l.backward()

# update metrics
for metric in self.metrics:
metric.update(label, pred)
self.train_stats['batch_' + metric.name] = metric.get()[1]
for loss, loss_metric, in zip(losses, self.loss_metrics):
loss_metric.update(0, [l for l in loss])
self.train_stats['batch_' + loss_metric.name] = loss_metric.get()[1]

self.train_stats['step'] = str(batch_size * (i + 1)) + '/' + str(len(train_data._dataset))

for trainer in self.trainers:
trainer.step(batch_size)

# batch end
for handler in event_handlers:
handler.batch_end()

for metric in self.metrics + self.loss_metrics:
self.train_stats['train_' + metric.name].append(metric.get()[1])
Copy link
Contributor

Choose a reason for hiding this comment

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

Validation metrics would also be needed in train_stats. How are we dealing with it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's tracked here, will be address in follow up PR

# epoch end
for handler in event_handlers:
handler.epoch_end()

# train end
for handler in event_handlers:
handler.train_end()
100 changes: 100 additions & 0 deletions python/mxnet/gluon/estimator/event_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# coding: utf-8
# pylint: disable=wildcard-import
"""Gluon EventHandlers for Estimators"""

__all__ = ['EventHandler', 'LoggingHandler']
import logging
import os
import time


class EventHandler(object):
def __init__(self, estimator):
self._estimator = estimator

def train_begin(self):
pass

def train_end(self):
pass

def batch_begin(self):
pass

def batch_end(self):
pass

def epoch_begin(self):
pass

def epoch_end(self):
pass


class LoggingHandler(EventHandler):
"""Basic Logging Handler that applies to every Gluon estimator by default.
TODO: add doc
"""

def __init__(self, estimator, log_name=None, file_name=None, file_location=None, ):
super(LoggingHandler, self).__init__(estimator)
log_name = log_name or 'Gluon Estimator'
self.logger = logging.getLogger(log_name)
self.logger.setLevel(logging.INFO)
streamhandler = logging.StreamHandler()
self.logger.addHandler(streamhandler)
# save logger to file only if file name or location is specified
if file_name or file_location:
file_name = file_name or log_name or 'estimator_log'
file_location = file_location or './'
filehandler = logging.FileHandler(os.path.join(file_location, file_name))
self.logger.addHandler(filehandler)

def train_begin(self):
pass
Copy link
Member

Choose a reason for hiding this comment

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

might be a good idea to log all the hyper params and defaults used here

Copy link
Member Author

Choose a reason for hiding this comment

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

will be added with the change on train_stats in a follow up PR

# logger.info(opt)

def train_end(self):
Copy link
Member

Choose a reason for hiding this comment

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

output informational message like number of epochs run, train_loss and valid_loss , other metrics at the end, etc.,

Copy link
Member Author

Choose a reason for hiding this comment

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

will be added with the change on train_stats in a follow up PR

pass

def batch_begin(self):
self.batch_start = time.time()

def batch_end(self):
batch_time = time.time() - self.batch_start
epoch = self._estimator.train_stats['epochs'][-1]
step = self._estimator.train_stats['step']
msg = '[Epoch %d] [Step %s] time/step: %.3fs ' % (epoch, step, batch_time)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we use the term step, lets not introduce new terms lets continue to use batch, look at existing logging

for key in self._estimator.train_stats.keys():
if key.startswith('batch_'):
msg += key[6:] + ': ' + '%.4f ' % self._estimator.train_stats[key]
self.logger.info(msg)

def epoch_begin(self):
self.epoch_start = time.time()

def epoch_end(self):
epoch_time = time.time() - self.epoch_start
epoch = self._estimator.train_stats['epochs'][-1]
msg = 'Epoch %d finished in %.3fs: ' % (epoch, epoch_time)
for key in self._estimator.train_stats.keys():
if key.startswith('train_') or key.startswith('test_'):
Copy link
Contributor

Choose a reason for hiding this comment

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

key should be val_ ?

msg += key + ': ' + '%.4f ' % self._estimator.train_stats[key][epoch]
self.logger.info(msg)