How to train variable input shape CNN?
I define a CNN network that can process variable input images, but can't find an example to learn how to train it. All I found is a MutableModule find in example/rcnn,it rebind the module when the shape changed.But after I implement the DataIter of my dataset, it seems that it failed to update the params, the accuracy didn't increase after several epochs. Any one can give an example?
When rebind the module, make sure arg_params and aux_params are passed to new module.
@solin319 I add some code, but nothing change.
if shape_changed:
#logging.info('shape chanege')
cur_arg_params, cur_aux_params = self._curr_module.get_params()
module = Module(self._symbol, self._data_names, self._label_names,
logger=self.logger, context=self._context,
work_load_list=self._work_load_list,
fixed_param_names=self._fixed_param_names)
module.bind(data_batch.provide_data, data_batch.provide_label, self._curr_module.for_training,
self._curr_module.inputs_need_grad, force_rebind=False,
shared_module=self._curr_module)
module.set_params(cur_arg_params, cur_aux_params)
self._curr_module = module
you can find module.py here
@apache/mxnet-committers: This issue has been inactive for the past 90 days. It has no label and needs triage.
For general "how-to" questions, our user forum (and Chinese version) is a good place to get help.
Hi @chenbiaolong are you still facing the same issue? Currently we have a tutorial using CNN for text classification.
@lanking520 the main framework we use to train models is Tensorflow now.In last year we use tensorflow train variable shape images and inference using MXNet. Thank you for your tutorial~ we'll learn from it when we need.
Thanks you for your feedback, we will keep the community a better place as best as we can do. Please let us know if you or your team would like to use MXNet anytime in the future. @nswamy can you add this with label Question, Example and possibly close this?
This issue is not solved, so it won't be closed. We can take this as a feature request for a tutorial.
@lanking520 @marcoabreu We found MXNet is convenient in c++ deployment,especially when we want to improve our throughputs. The C++ API is much friendly than tensorflow(collect the tensorflow c++ header and build the libtensorflow_cc.so is not an easy job ...). So we are glad to see more best practices provided by the community.
Assuming you're asking about variable batch sizes for the input, you should take a look at Module.reshape for this. Some related discussion can be found at https://github.com/apache/incubator-mxnet/issues/2294. And based on the unit-test, an example of its usage would be:
x = mx.sym.Variable('x')
y = mx.sym.FullyConnected(x, num_hidden=4)
exe = y.simple_bind(mx.cpu(), x=(5,4))
exe.arg_arrays[0][:] = 1
exe.arg_arrays[1][:] = mx.nd.ones((4,4))
exe.arg_arrays[2][:] = 0
new_exe = exe.reshape(x=(3,4))
new_exe.forward(is_train=False)
new_exe.outputs[0].asnumpy()
Also note that with Gluon, you can call Blocks with different sized batches without having to manage reshapes, etc.
Module API had been updated to support variable shape training, c.f. https://github.com/apache/incubator-mxnet/pull/6805 before this issue.
MutableModule from rcnn example was introduced before the previous PR to avoid breaking module API. It was unfortunate that the example was not updated timely since the PR. But if still interested, check out https://github.com/apache/incubator-mxnet/blob/master/example/rcnn/train.py#L117.
As for a special tutorial on variable shape training, you don't need one. Use module.fit or Gluon just as if it was fixed shape will work.
@ijkguo could we close out this issue?
I think the feature request for a tutorial (to train variable input shape CNN) mentioned by @marcoabreu is no longer necessary.
Training a variable input shape CNN is no different from a fixed shape one - use module.fit or Gluon as if input shape is fixed.
Okay, thanks everybody for your feedback and guidance!
If there's still something unclear @chenbiaolong , feel free to re-open.