示例#1
0
def kmeans(net, layers, num_c=16, initials=None, snapshot=False, alpha=0.0):
    codebook = {}
    if type(num_c) == type(1):
        num_c = [num_c] * len(layers)
    else:
        assert len(num_c) == len(layers)

    print "==============Perform K-means============="
    for idx, layer in enumerate(layers):
        print "Eval layer:", layer
        W = net.params[layer][0].data.flatten()
        W = W[np.where(W != 0)]
        if initials is None:  # Default: uniform sample
            min_W = np.min(W)
            max_W = np.max(W)
            initial_uni = np.linspace(min_W, max_W, num_c[idx] - 1)

            codebook[layer], _ = scv.kmeans(W,
                                            initial_uni,
                                            compress=False,
                                            alpha=alpha)

        elif type(initials) == type(np.array([])):
            codebook[layer], _ = scv.kmeans(W, initials)
        elif initials == 'random':
            codebook[layer], _ = scv.kmeans(W, num_c[idx] - 1)

        codebook[layer] = np.append(0, codebook[layer])
        print "codebook size:", len(codebook[layer])

    return codebook
示例#2
0
def kmeans_net(net, layers, num_c=16, initials=None, method='linear', compress=False):
    codebook = {}
    if type(num_c) == type(1):
        num_c = [num_c] * len(layers)
    else:
        assert len(num_c) == len(layers)

    print "==============Perform K-means============="
    for idx, layer in enumerate(layers):
        print "Eval layer:", layer
        W = net.params[layer][0].data.flatten()
        W = W[np.where(W != 0)]
        if initials is None:  # Default: uniform sample
            if method == 'linear':
                min_W = np.min(W)
                max_W = np.max(W)
                initial_uni = np.linspace(min_W, max_W, num_c[idx] - 1)
                codebook[layer], _ = scv.kmeans(W, initial_uni, compress=compress)
            elif method == 'random':
                codebook[layer], _ = scv.kmeans(W, num_c[idx] - 1, compress=compress)
            else:
                raise Exception

        else:
            codebook[layer], _ = scv.kmeans(W, initials)
        codebook[layer] = np.append(0.0, codebook[layer])
        print "codebook size:", len(codebook[layer])
    return codebook
示例#3
0
def kmeans_net(net, layers, num_c=16, initials=None, snapshot=False, alpha=0.0):
    codebook = {}
    if type(num_c) == type(1):
        num_c = [num_c] * len(layers)
    else:
        assert len(num_c) == len(layers)

    print "==============Perform K-means============="
    for idx, layer in enumerate(layers):
        print "Eval layer:", layer
        W = net.params[layer][0].data.flatten()
        W = W[np.where(W != 0)]
        if initials is None:  # Default: uniform sample
            min_W = np.min(W)
            max_W = np.max(W)
            initial_uni = np.linspace(min_W, max_W, num_c[idx] - 1)
            ''' # Legacy
            std = np.std(W)
            initial_uni = np.linspace(std * -4, std * 4, num_c[idx] - 1)
            '''

            if snapshot and layer=='fc6':
                codebook[layer], _, codebook_history = scv.kmeans(W, initial_uni, compress=False, snapshot=True, alpha=alpha)
            else:
                codebook[layer], _= scv.kmeans(W, initial_uni, compress=False, alpha=alpha)

            '''
            codebook[layer],_= scv.kmeans(W, num_c[idx] - 1)
            '''
        elif type(initials) == type(np.array([])):
            codebook[layer], _ = scv.kmeans(W, initials)
        elif initials == 'random':
            codebook[layer], _ = scv.kmeans(W, num_c[idx]-1)
            
        codebook[layer] = np.append(0.0, codebook[layer])
        print "codebook size:", len(codebook[layer])

    if snapshot:
        return codebook, codebook_history

    return codebook
示例#4
0
def kmeans_net(net,
               layers,
               num_c=16,
               initials=None,
               method='linear',
               compress=False):
    codebook = {}
    if type(num_c) == type(1):
        num_c = [num_c] * len(layers)
    else:
        assert len(num_c) == len(layers)

    print "==============Perform K-means============="
    for idx, layer in enumerate(layers):
        print "Eval layer:", layer
        W = net.params[layer][0].data.flatten()
        W = W[np.where(W != 0)]
        if initials is None:  # Default: uniform sample
            if method == 'linear':
                min_W = np.min(W)
                max_W = np.max(W)
                initial_uni = np.linspace(min_W, max_W, num_c[idx] - 1)
                codebook[layer], _ = scv.kmeans(W,
                                                initial_uni,
                                                compress=compress)
            elif method == 'random':
                codebook[layer], _ = scv.kmeans(W,
                                                num_c[idx] - 1,
                                                compress=compress)
            else:
                raise Exception

        else:
            codebook[layer], _ = scv.kmeans(W, initials)
        codebook[layer] = np.append(0.0, codebook[layer])
        print "codebook size:", len(codebook[layer])
    return codebook