コード例 #1
0
def regression_embedding():
    import regression_based
    class_labels = unpickle('cifar-10-batches-py/batches.meta')['label_names']
    # print class_labels
    Y_8_train = np.array(Y_train)
    X_8_train = np.array(X_train)

    removed_indices = np.where(Y_8_train != 1)
    Y_8_train = Y_8_train[removed_indices]
    X_8_train = X_8_train[removed_indices]
    removed_indices = np.where(Y_8_train != 4)
    Y_8_train = Y_8_train[removed_indices]
    X_8_train = X_8_train[removed_indices]

    Y_8_train = [embeddings[class_labels[i]] for i in Y_8_train]

    Y_8_validation = np.array(Y_validation)
    X_8_validation = np.array(X_validation)

    regression_based.train(X_8_train, Y_8_train)

    Y_2_validation = np.array(Y_validation)
    X_2_validation = np.array(X_validation)

    indices = np.where(np.logical_or(Y_2_validation == 1, Y_2_validation == 4))
    Y_2_validation = Y_2_validation[indices]
    X_2_validation = X_2_validation[indices]

    validaiton_embeddings = regression_based.predict(X_2_validation)

    targets_embeddings = []
    # for i in class_labels:
    for i in [
            'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
            'horse', 'ship', 'truck'
    ]:
        targets_embeddings.append(embeddings[i])
    targets_embeddings = np.array(targets_embeddings, dtype=np.float32)

    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.metrics import accuracy_score
    from scipy.spatial.distance import cosine
    # Y_pred_validation = []
    # for i in validaiton_embeddings:
    #     cos = []
    #     for j in targets_embeddings:
    #         val = cosine(i,j)
    #         cos.append(val)
    #     Y_pred_validation.append(np.argmax(cos))
    #
    neigh = KNeighborsClassifier(n_neighbors=1)
    neigh.fit(targets_embeddings, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    Y_pred_validation = neigh.predict(validaiton_embeddings)
    print(Y_2_validation)
    print(Y_pred_validation)
    print(accuracy_score(Y_2_validation, Y_pred_validation))
コード例 #2
0
def regression_embedding():
    import regression_based
    class_labels = unpickle('cifar-10-batches-py/batches.meta')['label_names']
    #Y_8_train = np.array(Y_train)
    #X_8_train = np.array(X_train)
    #removed_indices = np.where(Y_8_train!=1)
    #Y_8_train = Y_8_train[removed_indices]
    #X_8_train = X_8_train[removed_indices]
    #removed_indices = np.where(Y_8_train!=4)
    #Y_8_train = Y_8_train[removed_indices]
    #X_8_train = X_8_train[removed_indices]
    Y_GO_train = np.array(Y_train)
    X_GO_train = np.array(X_train)
    Y_GO_test = np.array(Y_validation)
    X_GO_test = np.array(X_validation)
    #Y_8_train = [embeddings[class_labels[i]] for i in Y_8_train]
    Y_GO_train = [embeddings[class_labels[i]] for i in np.array(Y_GO_train)]

    #Y_8_validation = np.array(Y_validation)
    #X_8_validation = np.array(X_validation)

    regression_based.train(X_GO_train, Y_GO_train)

    #Y_2_validation = np.array(Y_validation)
    #X_2_validation = np.array(X_validation)

    #indices = np.where(np.logical_or(Y_2_validation==1 ,Y_2_validation==4))
    #Y_2_validation = Y_2_validation[indices]
    #X_2_validation = X_2_validation[indices]

    validaiton_embeddings = regression_based.predict(X_GO_test)

    targets_embeddings_words = []
    targets_embeddings = []
    for i in class_labels:
        targets_embeddings.append(embeddings[i])
        targets_embeddings_words.append(i)

    for k in embeddings:
        if k not in class_labels:
            targets_embeddings.append(embeddings[k])
            targets_embeddings_words.append(k)
    targets_embeddings = np.array(targets_embeddings, dtype=np.float32)

    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.metrics import accuracy_score
    from scipy.spatial.distance import cosine
    # Y_pred_validation = []
    # for i in validaiton_embeddings:
    #     cos = []
    #     for j in targets_embeddings:
    #         val = cosine(i,j)
    #         cos.append(val)
    #     Y_pred_validation.append(np.argmax(cos))
    #
    neigh = KNeighborsClassifier(n_neighbors=1)
    print len(targets_embeddings)
    neigh.fit(targets_embeddings, range(0, len(targets_embeddings)))
    Y_pred_validation = neigh.predict([validaiton_embeddings[0]])
    print Y_pred_validation
    neighbors = neigh.kneighbors([validaiton_embeddings[0]],
                                 20,
                                 return_distance=False)
    print neighbors
    for n in neighbors[0]:
        print targets_embeddings_words[n]
コード例 #3
0
removed_indices = np.where(Y_8_train != 1)
Y_8_train = Y_8_train[removed_indices]
X_8_train = X_8_train[removed_indices]
removed_indices = np.where(Y_8_train != 4)
Y_8_train = Y_8_train[removed_indices]
X_8_train = X_8_train[removed_indices]

Y_8_train = [
    embeddings[str(class_labels[i], encoding='ascii')] for i in Y_8_train
]

Y_8_validation = np.array(Y_validation)
X_8_validation = np.array(X_validation)

# train with L2 loss regression
regression_based.train(X_8_train, Y_8_train)

# validation using 1, 4 data
Y_2_validation = np.array(Y_validation)
X_2_validation = np.array(X_validation)
indices = np.where(np.logical_or(Y_2_validation == 1, Y_2_validation == 4))
Y_2_validation = Y_2_validation[indices]
X_2_validation = X_2_validation[indices]

# predict image data of label 1, 4
validaiton_embeddings = regression_based.predict(X_2_validation)

# embedding of label 1, 4
targets_embeddings = []
for i in ['automobile', 'deer']:
    targets_embeddings.append(embeddings[i])