CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Strange behavior in C.contrib.netopt.convert_to_binary_convolution

Open igormq opened this issue 8 years ago • 6 comments

Hi,

I have been testing the quantization from netopt module and I found a strange behavior when using a pre-trained resnet model. By doing

import cntk as C
C.try_set_default_device(C.device.cpu())
model = C.load_model(model_path)
binz = convert_to_binary_convolution(model)

def visitor(x):
    if type(x) == C.Function and 'Convol' in x.op_name and x.is_block:
        print('isFunction: {}, {}, {}, isFunction: {}, {}, {}'.format(type(x) == C.Function, x.op_name, x.is_block, 
                                                                      type(x.block_root) == C.Function, 
                                                                      x.block_root.op_name, x.block_root.is_block))
        return True

X = C.logging.depth_first_search(binz, visitor, depth=0)

I get that only one convolutional layer is transformed in a binary convolution layer. What am I doing wrong?

Cheers, IMQ

igormq avatar Feb 05 '18 19:02 igormq

Can you try change your search depth to -1 for a full search?

ke1337 avatar Feb 07 '18 03:02 ke1337

Yes, I get the following output with depth=-1:

[BinaryConvolution: Output('stage3_layer1_relu1', [#], [512 x 7 x 7]) -> Output('Block37656_Output_0', [#], [512 x 7 x 7]),
 Convolution: Output('stage3_downsample_relu1', [#], [512 x 7 x 7]) -> Output('stage3_downsample_conv2', [#], [512 x 7 x 7]),
 Convolution: Output('stage2_layer1_relu2', [#], [256 x 14 x 14]) -> Output('stage3_projection_conv', [#], [512 x 7 x 7]),
 Convolution: Output('stage3_downsample_relu2', [#], [512 x 7 x 7]) -> Output('stage3_layer1_conv1', [#], [512 x 7 x 7]),
 Convolution: Output('stage2_layer1_relu2', [#], [256 x 14 x 14]) -> Output('stage3_downsample_conv1', [#], [512 x 7 x 7]),
 Convolution: Output('stage2_layer1_relu1', [#], [256 x 14 x 14]) -> Output('stage2_layer1_conv2', [#], [256 x 14 x 14]),
 Convolution: Output('stage2_downsample_relu1', [#], [256 x 14 x 14]) -> Output('stage2_downsample_conv2', [#], [256 x 14 x 14]),
 Convolution: Output('stage1_layer1_relu2', [#], [128 x 28 x 28]) -> Output('stage2_projection_conv', [#], [256 x 14 x 14]),
 Convolution: Output('stage2_downsample_relu2', [#], [256 x 14 x 14]) -> Output('stage2_layer1_conv1', [#], [256 x 14 x 14]),
 Convolution: Output('stage1_layer1_relu2', [#], [128 x 28 x 28]) -> Output('stage2_downsample_conv1', [#], [256 x 14 x 14]),
 Convolution: Output('stage1_layer1_relu1', [#], [128 x 28 x 28]) -> Output('stage1_layer1_conv2', [#], [128 x 28 x 28]),
 Convolution: Output('stage1_downsample_relu1', [#], [128 x 28 x 28]) -> Output('stage1_downsample_conv2', [#], [128 x 28 x 28]),
 Convolution: Output('stage0_layer1_relu2', [#], [64 x 56 x 56]) -> Output('stage1_projection_conv', [#], [128 x 28 x 28]),
 Convolution: Output('stage1_downsample_relu2', [#], [128 x 28 x 28]) -> Output('stage1_layer1_conv1', [#], [128 x 28 x 28]),
 Convolution: Output('stage0_layer1_relu2', [#], [64 x 56 x 56]) -> Output('stage1_downsample_conv1', [#], [128 x 28 x 28]),
 Convolution: Output('stage0_layer1_relu1', [#], [64 x 56 x 56]) -> Output('stage0_layer1_conv2', [#], [64 x 56 x 56]),
 Convolution: Output('stage0_layer0_relu1', [#], [64 x 56 x 56]) -> Output('stage0_layer0_conv2', [#], [64 x 56 x 56]),
 Convolution: Output('ElementTimes36912_Output_0', [#], [3 x 224 x 224]) -> Output('initial_conv', [#], [64 x 112 x 112]),
 Convolution: Output('stage0_layer0_relu2', [#], [64 x 56 x 56]) -> Output('stage0_layer1_conv1', [#], [64 x 56 x 56]),
 Convolution: Output('max_pool', [#], [64 x 56 x 56]) -> Output('stage0_layer0_conv1', [#], [64 x 56 x 56])]

As you can see, only one convolution was transformed.

igormq avatar Feb 07 '18 17:02 igormq

Hey @jaliyae and @KeDengMS , do you have any update on this?

igormq avatar Feb 15 '18 17:02 igormq

I will look into this and get back to you.

jaliyae avatar Feb 22 '18 22:02 jaliyae

This was due to a bug in the misc.converter.py. It is fixed with this change bd746943fb1afb6af95e593829ab8b73711b0927

jaliyae avatar Feb 28 '18 01:02 jaliyae

Thank you @jaliyae . I will close this issue.

igormq avatar Mar 01 '18 22:03 igormq