sony/nnabla

Dimensions problem in saving network .nnp with save.py

lpiccinelli opened this issue · 2 comments

I am working with Sony board Spresense and the problem is : when I try to save my model to .nnp format (training all with nnabla), I encounter a problem if I use a Convolution layer. Even in your simple example about saving the MLP, the error occurs when adding a convolutional layer.

In my network, the batch_size is 64 and the number of kernels in the first convolutional layer is 32, but when I try to save it throws me the error (see at the end), it seems that the code takes the number of layer instead of batch size.

I cannot find a well documentation on how the "contents" filed in save function must be, and it seems quite different from .pbtxt style. Please help

Net:

c1 = PF.fixed_point_quantized_convolution(x, 32, (3, 3), name='c1')
c1 = PF.batch_normalization(c1, name='bn1')
r1 = F.relu(c1)

c2 = PF.fixed_point_quantized_convolution(r1, 28, (3, 3), name='c2')
c2 = PF.batch_normalization(c2, name='bn2')
r2 = F.relu(c2)
r2 = F.average_pooling(r2, (2, 2))

c3 = PF.fixed_point_quantized_convolution(r2, 24, (3, 3), name='c3')
c3 = PF.batch_normalization(c3, name='bn3')
r3 = F.relu(c3)

c4 = PF.fixed_point_quantized_convolution(c3, 24, (3, 3), name='c4')
c4 = PF.batch_normalization(c4, name='bn4')
r4 = F.relu(c4)
r4 = F.average_pooling(r4, (2, 2))

fc5 = F.relu(PF.fixed_point_quantized_affine(r4, 32, name='fc5'))

fc6 = F.relu(PF.fixed_point_quantized_affine(fc5, 16, name='fc6'))

y = PF.fixed_point_quantized_affine(fc6, 2, name='fc7')

Code that gives error:

contents = {
    'networks': [
        {'name': 'net1',
         'batch_size': batch_size,
         'outputs': {'y': y},
         'names': {'x':x}}],
    'executors': [
        {'name': 'runtime',
         'network': 'net1',
         'data': ['x'],
         'output': ['y']}]}

save('net1.nnp', contents)

Error:

ValueError Traceback (most recent call last)
in
15 'data': ['x'],
16 'output': ['y']}]}
---> 17 save('net1.nnp', contents)

~/anaconda3/envs/py36/lib/python3.6/site-packages/nnabla/utils/save.py in save(filename, contents, include_params, variable_batch_size, extension)
650 nntxt = io.StringIO()
651 save(nntxt, contents, include_params=False,
--> 652 variable_batch_size=variable_batch_size, extension='.nntxt')
653 nntxt.seek(0)
654

~/anaconda3/envs/py36/lib/python3.6/site-packages/nnabla/utils/save.py in save(filename, contents, include_params, variable_batch_size, extension)
637 if ext == '.nntxt' or ext == '.prototxt':
638 logger.info("Saving {} as prototxt".format(filename))
--> 639 proto = create_proto(contents, include_params, variable_batch_size)
640 with get_file_handle_save(filename, ext) as file:
641 text_format.PrintMessage(proto, file)

~/anaconda3/envs/py36/lib/python3.6/site-packages/nnabla/utils/save.py in create_proto(contents, include_params, variable_batch_size)
458 proto_nets = []
459 for net in contents['networks']:
--> 460 networks[net['name']] = _create_network(net, variable_batch_size)
461 proto_nets.append(networks[net['name']])
462 proto.network.extend(proto_nets)

~/anaconda3/envs/py36/lib/python3.6/site-packages/nnabla/utils/save.py in _create_network(net, variable_batch_size)
229 if b != expect_batch_size:
230 raise ValueError('Variable "{}" has different batch size {} (expected {})'.format(
--> 231 v.name, b, expect_batch_size))
232 shape[0] = -1
233

ValueError: Variable "Convolution_Input" has different batch size 64 (expected 32)

We're currently figuring out what the cause is. Please wait.

Probably it is a matter of mismatch in dimensionality between affine layer (ie array) and convolutional layer (ie: tensor), need to force to see the affine output dimension to be type of (... , ) and not just (...). I solved the problem using: PF.fixed_point_quantized_affine(.., (..,), name=..)
instead of PF.fixed_point_quantized_affine(.., .., name=..) in output's dimension argument.