def load_caffemodel(path, session, prefix='', ignore=set(),
                    conv_fc_transitionals=None, renamed_layers=DummyDict(),
                    color_layer='', verbose=False, pre_adjust_batch_norm=False):
    import tensorflow as tf
    def find_weights(name, which='weights'):
        for tw in tf.trainable_variables():
            if tw.name.split(':')[0] == name + '/' + which:
                return tw
        return None

    """
    def find_batch_norm(name, which='mean'):
        for tw in tf.all_variables():
            if tw.name.endswith(name + '/bn_' + which + ':0'):
                return tw
        return None
    """

    data = dd.io.load(path, '/data')

    assigns = []
    loaded = []
    info = {}
    for key in data:
        local_key = prefix + renamed_layers.get(key, key)
        if key not in ignore:
            bn_name = 'batch_' + key
            if '0' in data[key]:
                weights = find_weights(local_key, 'weights')

                if weights is not None:
                    W = from_caffe(data[key]['0'], name=key, info=info,
                                   conv_fc_transitionals=conv_fc_transitionals,
                                   color_layer=color_layer)
                    if W.ndim != weights.get_shape().as_list():
                        W = W.reshape(weights.get_shape().as_list())

                    init_str = ''
                    if pre_adjust_batch_norm and bn_name in data:
                        bn_data = data[bn_name]
                        sigma = np.sqrt(1e-5 + bn_data['1'] / bn_data['2'])
                        W /= sigma
                        init_str += ' batch-adjusted'

                    assigns.append(weights.assign(W))
                    loaded.append('{}:0 -> {}:weights{} {}'.format(key, local_key, init_str, info.get(key, '')))

            if '1' in data[key]:
                biases = find_weights(local_key, 'biases')
                if biases is not None:
                    bias = data[key]['1']

                    init_str = ''
                    if pre_adjust_batch_norm and bn_name in data:
                        bn_data = data[bn_name]
                        sigma = np.sqrt(1e-5 + bn_data['1'] / bn_data['2'])
                        mu = bn_data['0'] / bn_data['2']
                        bias = (bias - mu) / sigma
                        init_str += ' batch-adjusted'

                    assigns.append(biases.assign(bias))
                    loaded.append('{}:1 -> {}:biases{}'.format(key, local_key, init_str))

            # Check batch norm and load them (unless they have been folded into)
            #if not pre_adjust_batch_norm:

    session.run(assigns)
    if verbose:
        tprint('Loaded model from', path)
        for l in loaded:
            tprint('-', l)
    return loaded