Beispiel #1
0
def upgrade_multipath_vggnet(old_model):
    # Make sure we have the right model
    node_names = {
        'rgb_conv', 'flow_conv', 'shared_layers', 'fc_regr_left',
        'fc_regr_right', 'fc_regr_head', 'fc_clas'
    }
    assert set(old_model.nodes.keys()) == node_names

    rv = Graph()

    # Upgrade RGB path first (weights handled by upgrade_sequential)
    print('Upgrading RGB path')
    rgb_shape = old_model.inputs['images'].input_shape[1:]
    rv.add_input(
        input_shape=rgb_shape, name='images'
    )
    upgraded_rgb_conv = upgrade_sequential(old_model.nodes['rgb_conv'])
    rv.add_node(upgraded_rgb_conv, input='images', name='rgb_conv')

    # Upgrade flow path
    print('Upgrading flow path')
    flow_shape = old_model.inputs['flow'].input_shape[1:]
    rv.add_input(
        input_shape=flow_shape, name='flow'
    )
    upgraded_flow_conv = upgrade_sequential(old_model.nodes['flow_conv'])
    rv.add_node(upgraded_flow_conv, input='flow', name='flow_conv')

    # Upgrade shared path
    print('Upgrading shared path')
    upgraded_share = upgrade_sequential(old_model.nodes['shared_layers'])
    rv.add_node(
        upgraded_share, inputs=['rgb_conv', 'flow_conv'], merge_mode='concat',
        concat_axis=1, name='shared_layers'
    )

    # Upgrade dense outputs (weights handled by dense_to_conv)
    print('Upgrading regression outputs')
    for out_name in {'left', 'right', 'head'}:
        dense_name = 'fc_regr_' + out_name
        old_dense = old_model.nodes[dense_name]
        new_dense = dense_to_conv(old_dense, (old_dense.input_shape[1], 1, 1))
        rv.add_node(new_dense, input='shared_layers', name=dense_name)
        rv.add_output(input=dense_name, name=out_name)

    print('Upgrading classification output')
    old_clas_dense = old_model.nodes['fc_clas']
    # We're using a custom activation, so we have to register it
    register_activation(convolution_softmax, 'convolution_softmax')
    new_clas_dense = dense_to_conv(
        old_clas_dense, (old_clas_dense.input_shape[1], 1, 1),
        activation='convolution_softmax'
    )
    rv.add_node(new_clas_dense, input='shared_layers', name='fc_clas')
    rv.add_output(input='fc_clas', name='class')

    # Plan is to save weights to HDF5 and architecture to JSON, then a small
    # wrapper can load the data for use from Matlab
    print('All done!')
    return rv
Beispiel #2
0
def upgrade_multipath_vggnet(old_model):
    # Make sure we have the right model
    node_names = {
        'rgb_conv', 'flow_conv', 'shared_layers', 'fc_regr_left',
        'fc_regr_right', 'fc_regr_head', 'fc_clas'
    }
    assert set(old_model.nodes.keys()) == node_names

    rv = Graph()

    # Upgrade RGB path first (weights handled by upgrade_sequential)
    print('Upgrading RGB path')
    rgb_shape = old_model.inputs['images'].input_shape[1:]
    rv.add_input(input_shape=rgb_shape, name='images')
    upgraded_rgb_conv = upgrade_sequential(old_model.nodes['rgb_conv'])
    rv.add_node(upgraded_rgb_conv, input='images', name='rgb_conv')

    # Upgrade flow path
    print('Upgrading flow path')
    flow_shape = old_model.inputs['flow'].input_shape[1:]
    rv.add_input(input_shape=flow_shape, name='flow')
    upgraded_flow_conv = upgrade_sequential(old_model.nodes['flow_conv'])
    rv.add_node(upgraded_flow_conv, input='flow', name='flow_conv')

    # Upgrade shared path
    print('Upgrading shared path')
    upgraded_share = upgrade_sequential(old_model.nodes['shared_layers'])
    rv.add_node(upgraded_share,
                inputs=['rgb_conv', 'flow_conv'],
                merge_mode='concat',
                concat_axis=1,
                name='shared_layers')

    # Upgrade dense outputs (weights handled by dense_to_conv)
    print('Upgrading regression outputs')
    for out_name in {'left', 'right', 'head'}:
        dense_name = 'fc_regr_' + out_name
        old_dense = old_model.nodes[dense_name]
        new_dense = dense_to_conv(old_dense, (old_dense.input_shape[1], 1, 1))
        rv.add_node(new_dense, input='shared_layers', name=dense_name)
        rv.add_output(input=dense_name, name=out_name)

    print('Upgrading classification output')
    old_clas_dense = old_model.nodes['fc_clas']
    # We're using a custom activation, so we have to register it
    register_activation(convolution_softmax, 'convolution_softmax')
    new_clas_dense = dense_to_conv(old_clas_dense,
                                   (old_clas_dense.input_shape[1], 1, 1),
                                   activation='convolution_softmax')
    rv.add_node(new_clas_dense, input='shared_layers', name='fc_clas')
    rv.add_output(input='fc_clas', name='class')

    # Plan is to save weights to HDF5 and architecture to JSON, then a small
    # wrapper can load the data for use from Matlab
    print('All done!')
    return rv
Beispiel #3
0
def upgrade_multipath_poselet_vggnet(old_model):
    # TODO: This is mostly the same as the above code (cut and pasted). This is
    # non-ideal, so the above code should be deleted or somehow merged with
    # this code.
    node_names = {
        'rgb_conv', 'flow_conv', 'shared_layers', 'fc_pslt'
    }
    assert set(old_model.nodes.keys()) == node_names

    rv = Graph()

    print('Upgrading RGB path')
    rgb_shape = old_model.inputs['images'].input_shape[1:]
    rv.add_input(
        input_shape=rgb_shape, name='images'
    )
    upgraded_rgb_conv = upgrade_sequential(old_model.nodes['rgb_conv'])
    rv.add_node(upgraded_rgb_conv, input='images', name='rgb_conv')
    print('Upgrading flow path')
    flow_shape = old_model.inputs['flow'].input_shape[1:]
    rv.add_input(
        input_shape=flow_shape, name='flow'
    )
    upgraded_flow_conv = upgrade_sequential(old_model.nodes['flow_conv'])
    rv.add_node(upgraded_flow_conv, input='flow', name='flow_conv')
    print('Upgrading shared path')
    upgraded_share = upgrade_sequential(old_model.nodes['shared_layers'])
    rv.add_node(
        upgraded_share, inputs=['rgb_conv', 'flow_conv'], merge_mode='concat',
        concat_axis=1, name='shared_layers'
    )

    print('Upgrading classification output')
    old_clas_dense = old_model.nodes['fc_pslt']
    register_activation(convolution_softmax, 'convolution_softmax')
    new_clas_dense = dense_to_conv(
        old_clas_dense, (old_clas_dense.input_shape[1], 1, 1),
        activation='convolution_softmax'
    )
    rv.add_node(new_clas_dense, input='shared_layers', name='fc_pslt')
    rv.add_output(input='fc_pslt', name='poselet')

    return rv
Beispiel #4
0
def upgrade_multipath_poselet_vggnet(old_model):
    # TODO: This is mostly the same as the above code (cut and pasted). This is
    # non-ideal, so the above code should be deleted or somehow merged with
    # this code.
    node_names = {'rgb_conv', 'flow_conv', 'shared_layers', 'fc_pslt'}
    assert set(old_model.nodes.keys()) == node_names

    rv = Graph()

    print('Upgrading RGB path')
    rgb_shape = old_model.inputs['images'].input_shape[1:]
    rv.add_input(input_shape=rgb_shape, name='images')
    upgraded_rgb_conv = upgrade_sequential(old_model.nodes['rgb_conv'])
    rv.add_node(upgraded_rgb_conv, input='images', name='rgb_conv')
    print('Upgrading flow path')
    flow_shape = old_model.inputs['flow'].input_shape[1:]
    rv.add_input(input_shape=flow_shape, name='flow')
    upgraded_flow_conv = upgrade_sequential(old_model.nodes['flow_conv'])
    rv.add_node(upgraded_flow_conv, input='flow', name='flow_conv')
    print('Upgrading shared path')
    upgraded_share = upgrade_sequential(old_model.nodes['shared_layers'])
    rv.add_node(upgraded_share,
                inputs=['rgb_conv', 'flow_conv'],
                merge_mode='concat',
                concat_axis=1,
                name='shared_layers')

    print('Upgrading classification output')
    old_clas_dense = old_model.nodes['fc_pslt']
    register_activation(convolution_softmax, 'convolution_softmax')
    new_clas_dense = dense_to_conv(old_clas_dense,
                                   (old_clas_dense.input_shape[1], 1, 1),
                                   activation='convolution_softmax')
    rv.add_node(new_clas_dense, input='shared_layers', name='fc_pslt')
    rv.add_output(input='fc_pslt', name='poselet')

    return rv